[SciPy-User] defining new distributions with, rv_continuous

Francesco De Gasperin fdg at voo.it
Mon Apr 14 07:15:05 EDT 2014


Many thanks for the quick help. I see that the overhead is quite large 
but I don't look for high performances now, I need to generate only 1000 
numbers. For interested people, I attach here a more flexible code which 
should work as it numerically normalizes the pdf given "a" and "b".

#####################################################
from scipy.stats import distributions
from scipy import integrate

class test(distributions.rv_continuous):

     def __init__(self,a=-np.inf,b=np.inf,name=''):
         super().__init__(a=a,b=b,name=name)
         self.integral = integrate.quad(self.f, self.a, self.b)[0]

     def f(self, x):
	# in this case is a gaussian, but it could be a more complex pdf
         sigma = 1.
         c = 0.
         return np.exp( -(x-c)**2 / (2*sigma**2) )

     def _pdf(self, x):
         return self.f(x)/self.integral

t_obj = test(a=0,name="test")
######################################################

thanks,
Francesco



More information about the SciPy-User mailing list