[SciPy-User] scipy.stats: Sampling from an arbitrary probability distribution
Sun Jun 3 12:51:49 CDT 2012
On Sun, Jun 3, 2012 at 11:21 AM, <email@example.com> wrote:
> On Sun, Jun 3, 2012 at 10:07 AM, <firstname.lastname@example.org> wrote:
>> On Sun, Jun 3, 2012 at 7:20 AM, Daniel Sabinasz
>> <email@example.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')
> I didn't look at the example before
> x**2 makes a proper pdf only with a bounded support, so, when creating
> the instance, then the bounds .a and .b need to be given as keyword
> arguments. (or set as attributes)
> It also looks like ppf should have a closed form expression.
> (my algebra isn't good enough today to figure out bounds)
I picked [-1, 1] for the bounds of the distribution, example with
explicit ppf is much faster
>>> 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
>> 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.)
>>> Thanks in advance!
>>> SciPy-User mailing list
More information about the SciPy-User