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$
6
  • 1
    $\begingroup$ This is probably better suited for StackOverflow $\endgroup$
    – user20857
    Oct 30, 2020 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, 2020 at 21:16
  • $\begingroup$ Your question has nothing to do with statistics and everything to do with how Python loads and overwrites modules $\endgroup$
    – user20857
    Oct 30, 2020 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, 2020 at 23:58
  • $\begingroup$ This question was answered on SO: stackoverflow.com/q/64615973 $\endgroup$
    – user20857
    Nov 4, 2020 at 15:08

1 Answer 1

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 and acknowledge that you have read and understand our privacy policy and code of conduct.

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