1
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ This is probably better suited for StackOverflow $\endgroup$ – Spencer Bryngelson Oct 30 '20 at 15:53
  • $\begingroup$ 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 $\endgroup$ – Yann Oct 30 '20 at 21:16
  • $\begingroup$ Your question has nothing to do with statistics and everything to do with how Python loads and overwrites modules $\endgroup$ – Spencer Bryngelson Oct 30 '20 at 21:27
  • $\begingroup$ I don't even know if I am on the right track here using rv_continuous, so any other approach is warmly welcome, too $\endgroup$ – Yann Oct 30 '20 at 23:58
  • $\begingroup$ This question was answered on SO: stackoverflow.com/q/64615973 $\endgroup$ – Spencer Bryngelson Nov 4 '20 at 15:08
0
$\begingroup$

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?

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.