# [SciPy-User] Request for usage examples on scipy.stats.rv_continuous and scipy.ndimage.grey_dilate(structure)

Mon Mar 22 11:58:47 CDT 2010

```Dear Robert,

thanks for the tip. I tried understanding the examples in scipy/stats/distributions.py, but being a python / scipy newbie I find the mechanism hard to understand and couldn't implement the simple examples I suggested below.

Maybe it would be possible to add an example to the tutorial? At http://docs.scipy.org/scipy/docs/scipy-docs/tutorial/stats.rst/#stats there is an example on how to use rv_discrete, but none on how to use rv_continuous.

Would it be possible to add a convenience function to scipy.stats that makes it easy to construct a distribution from a function:
>>> p = lambda x: x**2
>>> pdist = scipy.stats.rv_continuous_from_function(pdf=p, lim=[0,2]) # a suggestion, doesn't exist at the moment
>>> samples = pdist.rvs(size=10)

I would guess that getting random numbers from a user defined distribution function is such a common usage that it would be nice (at least for newbies like me :-) to being able to do it from the command line, without having to derive a class.

Cheers, --Christoph

On Mar 22, 2010, at 3:50 PM, Robert Kern wrote:

> On Mon, Mar 22, 2010 at 09:44, Christoph Deil
>> Hi,
>>
>> I am having some trouble figuring out how to use the following two aspects of scipy:
>>
>> 1) How can I  create a random distribution given by some python formula, e.g. pdf(x) = x**2 in the range x = 1 to 2.
>>
>> The example given in the rv_continuous docstring doesn't work for me:
>> In [1]: import matplotlib.pyplot as plt
>> In [2]: numargs = generic.numargs
>> AttributeError: type object 'numpy.generic' has no attribute 'numargs'
>>
>> Is it also possible to use rv_continuous for multidimensional distributions, say pdf(x,y)=x*y in the range x = 1 to 2 and y = 0 to 3?
>> Or for pdfs that are just given numerically on a grid (say as the result of a gaussian_kde() ), not by a python function?
>>
>> It would be nice if someone could add a few examples that show rv_continuous usage to the docstring or tutorial.
>
> rv_continuous is a base class. You do not use it directly. The reason
> the docstring doesn't work is because it is used as a template to
> build the docstrings for the subclasses. If you want to subclass from
> it to make your own distribution class, read the source of the other
> distribution classes for examples.
>
> --
> Robert Kern
>
> "I have come to believe that the whole world is an enigma, a harmless
> enigma that is made terrible by our own mad attempt to interpret it as
> though it had an underlying truth."
>  -- Umberto Eco
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user

```