[SciPy-User] scipy.stats: Sampling from an arbitrary probability distribution

josef.pktd@gmai... josef.pktd@gmai...
Sun Jun 3 09:07:52 CDT 2012


On Sun, Jun 3, 2012 at 7:20 AM, Daniel Sabinasz
<d.sabinasz@googlemail.com> wrote:
> Hi all,
>
> I need to sample a random number from a distribution whose probability
> density function I specify myself. Is that possible using scipy.stats?
>
> Here is what I have already:
>
> import scipy.stats as st
>
> class my_pdf(st.rv_continuous):
>     def _pdf(self,x):
>         return x*x/10.0
>
> my_cv = my_pdf(name='my_pdf')
>
> Can I now somehow sample a random number from my_cv?

you can define your own distribution with the pdf, then the generic
methods will calculate the rvs

http://stackoverflow.com/questions/10678546/creating-new-distributions-in-scipy

It will require a large number of calls to generate random numbers,
numerically calculating ppf and cdf, so this won't be efficient.
Using a linear interpolated ppf will be faster, and might be accurate enough.

(I still need to write an example for trying out the ppf interpolation version.)

Josef

>
> Thanks in advance!
>
> Daniel
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user


More information about the SciPy-User mailing list