# Define continuous, non-analytical pdfs in python

I am planning to do some basic algebra on continuous, non-analytical random variabels. I want to define their probability density functions as arrays x and f(x).

Yet, I was surprised to find out that there does not seem to be any package that does basic operations like computing sum- or product distributions of two pdfs (please correct me if I'm wrong). To implement those operations by myself, I then planned to create a subclass of scipy.stats rv_continuous, following this thread:

import scipy as sp
import numpy as np

class my_pdf(sp.stats.rv_continuous):
def __init__(self,x,p):
self.x = x
self.p = p

def _pdf(self,x):
return sp.interpolate.interp1d(self.x,self.p)(x)

x = np.linspace(0,1,101)
f = 3*x**2
my_cv = my_pdf(x,f)
my_cv.pdf(0)


However, overwriting the init method is probably not the way to go. Is there a way to add additional parameters to rv_continuous, or another way to approach the problem, other than building everything from scratch?

• This is probably better suited for StackOverflow Oct 30, 2020 at 15:53
• the linked question was well answered in this forum, and I feel like scientist should be much more interested in pdfs than developers. However I'll try it, and post the answer here if I get one. thx
– Yann
Oct 30, 2020 at 21:16
• Your question has nothing to do with statistics and everything to do with how Python loads and overwrites modules Oct 30, 2020 at 21:27
• I don't even know if I am on the right track here using rv_continuous, so any other approach is warmly welcome, too
– Yann
Oct 30, 2020 at 23:58
• This question was answered on SO: stackoverflow.com/q/64615973 Nov 4, 2020 at 15:08

So, if anyone stumbles over this again, it got answered here:

import scipy as sp
import scipy.stats
import numpy as np

class my_pdf(sp.stats.rv_continuous):
def __init__(self,x,p):
super().__init__(a=x.min(), b=x.max())
self.x = x
self.p = p

def _pdf(self,x):
return sp.interpolate.interp1d(self.x,self.p)(x)

x = np.linspace(0,1,101)
f = 3*x**2
my_cv = my_pdf(x,f)
my_cv.pdf(0)
my_cv.cdf(0.5)


Still, I feel like there should be a package for operations on this kind of random variables?