title: Generate random data from arbitrary CDF in R?
body: <p>I have an arbitrary CDF that is applied to a point estimate. I have a number of these point estimates with associated CDFs, that I need to simulate random data for a Monte Carlo simulation.</p>
  
  <p>The CDF I'm generating by doing a spline fit to the arbitrary points provided in a table. For example, the quantile 0.1 is a product of 0.13 * point estimate. The quantile 0.9 is a product of 7.57 * point estimate. It is fairly crude and is based on a large study comparing these models to real world system -- ignore that for now please.</p>
  
  <p>I fit the CDF using a spline fit as shown <a href="https://imgur.com/a/gqapS" rel="nofollow noreferrer">here</a>.</p>
  
  <p>If I take the derivative of this, I get the shape of the pdf (<a href="https://imgur.com/a/idDNY" rel="nofollow noreferrer">image</a>).</p>
  
  <p>I modified the function "samplepdf" found here, <a href="http://blog.quantitations.com/tutorial/2012/11/20/sampling-from-an-arbitrary-density/" rel="nofollow noreferrer">Sampling from an Arbitrary Density</a>, as follows:</p>
  
  <pre><code>samplecdf &lt;- function(n, cdf, spdf.lower = -Inf, spdf.upper=Inf) {
    my_fun &lt;- match.fun(cdf)
    invcdf &lt;- function(u) {
      subcdf &lt;- function(t) my_fun(t) - u
      if (spdf.lower == -Inf) 
        spdf.lower &lt;- endsign(subcdf, -1)
      if (spdf.upper == Inf) 
        spdf.upper &lt;- endsign(subcdf)
      return(uniroot(subcdf, c(spdf.lower, spdf.upper))$root)
    }
   sapply(runif(n), invcdf)
  }
  </code></pre>
  
  <p>This seems to work, OK - when I compare the quantiles I estimate from the randomly generated data they are fairly close to the initial values. However, when I look at the histogram something funny is happening at the tail where it is looks like my function is consistently generating more values than it should according to the pdf. This function consistently does that across all my point-estimates and even though I can look at the individual quantiles and they seem close, I can tell that the overall Monte Carlo simulation is demonstrating higher estimates for the 50% percentile than I expect. Here is <a href="https://imgur.com/a/M8GrT" rel="nofollow noreferrer">a plot of my histogram of the random samples</a>.</p>
  
  <p>Any tips or advice would be very welcome. I think the best route would be to fit an exponential distribution to the CDF, but I'm struggling to do that. All "fitting" assumes that you have data that needs to be fitted -- this is more arbitrary than that.</p>