A generic discrete random variable class meant for subclassing.
rv_discrete is a base class to construct specific distribution classes and instances from for discrete random variables. rv_discrete can be used to construct an arbitrary distribution with defined by a list of support points and the corresponding probabilities.
Parameters : | a : float, optional
b : float, optional
moment_tol : float, optional
values : tuple of two array_like
inc : integer
badvalue : object, optional
name : str, optional
longname : str, optional
shapes : str, optional
extradoc : str, optional
|
---|
Notes
Alternatively, the object may be called (as a function) to fix the shape and location parameters returning a “frozen” discrete RV object:
You can construct an aribtrary discrete rv where P{X=xk} = pk by passing to the rv_discrete initialization method (through the values=keyword) a tuple of sequences (xk, pk) which describes only those values of X (xk) that occur with nonzero probability (pk).
To create a new discrete distribution, we would do the following:
class poisson_gen(rv_continuous):
#"Poisson distribution"
def _pmf(self, k, mu):
...
and create an instance
poisson = poisson_gen(name=”poisson”, shapes=”mu”, longname=’A Poisson’)
The docstring can be created from a template.
Examples
>>> import matplotlib.pyplot as plt
>>> numargs = generic.numargs
>>> [ <shape(s)> ] = ['Replace with resonable value', ]*numargs
Display frozen pmf:
>>> rv = generic(<shape(s)>)
>>> x = np.arange(0, np.min(rv.dist.b, 3)+1)
>>> h = plt.plot(x, rv.pmf(x))
Check accuracy of cdf and ppf:
>>> prb = generic.cdf(x, <shape(s)>)
>>> h = plt.semilogy(np.abs(x-generic.ppf(prb, <shape(s)>))+1e-20)
Random number generation:
>>> R = generic.rvs(<shape(s)>, size=100)
Custom made discrete distribution:
>>> vals = [arange(7), (0.1, 0.2, 0.3, 0.1, 0.1, 0.1, 0.1)]
>>> custm = rv_discrete(name='custm', values=vals)
>>> h = plt.plot(vals[0], custm.pmf(vals[0]))
Methods
generic.rvs(<shape(s)>, loc=0, size=1) | random variates |
generic.pmf(x, <shape(s)>, loc=0) | probability mass function |
generic.cdf(x, <shape(s)>, loc=0) | cumulative density function |
generic.sf(x, <shape(s)>, loc=0) | survival function (1-cdf — sometimes more accurate) |
generic.ppf(q, <shape(s)>, loc=0) | percent point function (inverse of cdf — percentiles) |
generic.isf(q, <shape(s)>, loc=0) | inverse survival function (inverse of sf) |
generic.stats(<shape(s)>, loc=0, moments=’mv’) | mean(‘m’, axis=0), variance(‘v’), skew(‘s’), and/or kurtosis(‘k’) |
generic.entropy(<shape(s)>, loc=0) | entropy of the RV |
generic(<shape(s)>, loc=0) | calling a distribution instance returns a frozen distribution |