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: |
|
---|
random variates
probability mass function
log of the probability density function
cumulative density function
log of the cumulative density function
survival function (1-cdf --- sometimes more accurate)
log of the survival function
percent point function (inverse of cdf --- percentiles)
inverse survival function (inverse of sf)
non-central n-th moment of the distribution. May not work for array arguments.
mean('m', axis=0), variance('v'), skew('s'), and/or kurtosis('k')
entropy of the RV
conditional=False)
Expected value of a function with respect to the distribution. Additional kwd arguments passed to integrate.quad
Median of the distribution.
Mean of the distribution.
Standard deviation of the distribution.
Variance of the distribution.
Interval that with alpha percent probability contains a random realization of this distribution.
calling a distribution instance returns a frozen distribution
Notes
You can construct an arbitrary 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_discrete):
#"Poisson distribution"
def _pmf(self, k, mu):
...
and create an instance:
poisson = poisson_gen(name="poisson",
longname='A Poisson')
The docstring can be created from a template.
Alternatively, the object may be called (as a function) to fix the shape and location parameters returning a "frozen" discrete RV object:
myrv = generic(<shape(s)>, loc=0)
- frozen RV object with the same methods but holding the given
shape and location fixed.
A note on shapes: subclasses need not specify them explicitly. In this case, the shapes will be automatically deduced from the signatures of the overridden methods. If, for some reason, you prefer to avoid relying on introspection, you can specify shapes explicitly as an argument to the instance constructor.
Examples
Custom made discrete distribution:
>>> import matplotlib.pyplot as plt
>>> from scipy import stats
>>> xk = np.arange(7)
>>> pk = (0.1, 0.2, 0.3, 0.1, 0.1, 0.1, 0.1)
>>> custm = stats.rv_discrete(name='custm', values=(xk, pk))
>>> h = plt.plot(xk, custm.pmf(xk))
Random number generation:
>>> R = custm.rvs(size=100)
Display frozen pmf:
>>> numargs = generic.numargs
>>> [ <shape(s)> ] = ['Replace with resonable value', ]*numargs
>>> rv = generic(<shape(s)>)
>>> x = np.arange(0, np.min(rv.dist.b, 3)+1)
>>> h = plt.plot(x, rv.pmf(x))
Here, rv.dist.b is the right endpoint of the support of rv.dist.
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)
Methods
__init__([a, b, name, badvalue, moment_tol, ...]) | |
cdf(k, *args, **kwds) | Cumulative distribution function of the given RV. |
entropy(*args, **kwds) | Differential entropy of the RV. |
expect([func, args, loc, lb, ub, conditional]) | Calculate expected value of a function with respect to the distribution |
freeze(*args, **kwds) | Freeze the distribution for the given arguments. |
interval(alpha, *args, **kwds) | Confidence interval with equal areas around the median. |
isf(q, *args, **kwds) | Inverse survival function (1-sf) at q of the given RV. |
logcdf(k, *args, **kwds) | Log of the cumulative distribution function at k of the given RV |
logpmf(k, *args, **kwds) | Log of the probability mass function at k of the given RV. |
logsf(k, *args, **kwds) | Log of the survival function of the given RV. |
mean(*args, **kwds) | Mean of the distribution |
median(*args, **kwds) | Median of the distribution. |
moment(n, *args, **kwds) | n'th order non-central moment of distribution. |
pmf(k, *args, **kwds) | Probability mass function at k of the given RV. |
ppf(q, *args, **kwds) | Percent point function (inverse of cdf) at q of the given RV |
rvs(*args, **kwargs) | Random variates of given type. |
sf(k, *args, **kwds) | Survival function (1-cdf) at k of the given RV. |
stats(*args, **kwds) | Some statistics of the given RV |
std(*args, **kwds) | Standard deviation of the distribution. |
var(*args, **kwds) | Variance of the distribution |