[SciPy-user] Generate random samples from a non-standard distribution

Anne Archibald peridot.faceted at gmail.com
Fri Oct 19 04:02:41 EDT 2007


On 19/10/2007, Manu Hack <manuhack at gmail.com> wrote:

> I've been goolging around but not sure how to generate random samples
> given the density function of a random variable.  I was looking around
> something like scipy.stats.rv_continuous but couldn't find anything
> useful.

There's always the everything-looks-like-a-nail approach: you can
implement the CDF by numerical integration of the PDF. You can
implement the inverse CDF by root-finding on the CDF. You can generate
points by generating uniform random variates and running them through
the inverse CDF.

More reasonably, you can generate a lot of samples and produce a
spline that approximates the inverse CDF, which you can then evaluate
fairly efficiently.

This isn't terribly *good*, mind you, the deviations from your
distribution may be rather peculiar, but it's better than nothing.

Anne



More information about the SciPy-User mailing list