![](https://secure.gravatar.com/avatar/ad13088a623822caf74e635a68a55eae.jpg?s=120&d=mm&r=g)
On Sun, Jun 3, 2012 at 11:21 AM, <josef.pktd@gmail.com> wrote:
On Sun, Jun 3, 2012 at 10:07 AM, <josef.pktd@gmail.com> wrote:
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')
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 https://gist.github.com/2864348 Josef
Josef
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-sc...
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