[SciPy-User] defining new distributions with rv_continuous

Francesco De Gasperin fdg at voo.it
Mon Apr 14 06:00:54 EDT 2014


Hi,

I have some problems in defining a new distribution using rv_continuous.
As an example I tried to create a gaussian defined only for positive 
values of the random variable.

This is the code:

###########################################
from scipy.stats import distributions
class gauss_pos(distributions.rv_continuous):
    def _pdf(self, x):
         if x <= 0: return 0
         sigma = 1.
         c = 0.
         return 1/(np.sqrt(2*np.pi)*sigma) * np.exp( -(x-c)**2 / 
(2*sigma**2) )

t_obj = gauss_pos(name="gauss_pos")
print (t_obj.rvs(size=10))
#############################################

but when I run it I always end up with:
OverflowError: (34, 'Numerical result out of range')

Then, I removed the "if x <= 0: return 0" line and I tried to define the 
"a" and "b" parameters when creating the t_obj. Defining only the "a=0" 
produces the same error, while using something as:

t_obj = gauss_pos(a=0,b=2)

instead produces a result of this type:
[ 2.          2.          1.5460072   1.09621782  1.62184086  0.42107306
   2.          1.09281742  0.82957403  2.        ]

which I still do not understand... it seems that "b" is returned every 
time the constraint that the random var must be between a and b is 
violated...

any help is appreciated!
cheers,
Francesco



More information about the SciPy-User mailing list