Python scipy - specify arbitrary discrete distribution

I use various continuous distributions from scipy.stats (e.g. norms). Therefore, if I want to find P (Z <0.5), I would do:

from scipy.stats import norm norm(0, 1).cdf(0.5) # Z~N(0,1) 

Is there a tool (scipy.stats or statsmodels or else) that I can use to describe the discrete distribution, and then calculate CDF / CMF, etc. On him? I can write the code myself, but I was wondering if something exists, for example:

pdf (x) = 1/3 for x = 1,2,3; else 0

Then I can build 2 vectors x = [1,2,3], p = [1/3, 1/3, 1/3] and introduce them into the library class, which will then provide .cdf (), etc.

+5
source share
1 answer

I think you are looking for scipy.stats.rv_discrete here. From docs :

rv_discrete is the base class for constructing specific distribution classes and instances for discrete random variables. It can also be used to build an arbitrary distribution defined by a list of support points and corresponding probabilities.

Example from the docs:

 from scipy import stats xk = np.arange(7) pk = (0.1, 0.2, 0.3, 0.1, 0.1, 0.0, 0.2) custm = stats.rv_discrete(name='custm', values=(xk, pk)) 

And your example:

 In [1]: import numpy as np In [2]: from scipy import stats In [3]: custm = stats.rv_discrete(name='custm', values=((1, 2, 3), (1./3, 1./3, 1./3))) In [4]: custm.cdf(2.5) Out[4]: 0.66666666666666663 
+4
source

Source: https://habr.com/ru/post/1262844/


All Articles