bundles / scipy 1.17.1 / scipy / stats / _distn_infrastructure / rv_discrete
class
scipy.stats._distn_infrastructure:rv_discrete
Signature
class rv_discrete ( a = 0 , b = inf , name = None , badvalue = None , moment_tol = 1e-08 , values = None , inc = 1 , longname = None , shapes = None , seed = None ) Members
-
__getstate__ -
__init__ -
_attach_methods -
_cdf -
_cdf_single -
_construct_docstrings -
_entropy -
_logpmf -
_logpxf -
_nonzero -
_param_info -
_pmf -
_unpack_loc_scale -
_updated_ctor_param -
cdf -
expect -
isf -
logcdf -
logpmf -
logsf -
pmf -
ppf -
rvs -
sf
Summary
A generic discrete random variable class meant for subclassing.
Extended Summary
rv_discrete is a base class to construct specific distribution classes and instances for discrete random variables. It can also be used to construct an arbitrary distribution defined by a list of support points and corresponding probabilities.
Parameters
a: float, optionalLower bound of the support of the distribution, default: 0
b: float, optionalUpper bound of the support of the distribution, default: plus infinity
moment_tol: float, optionalThe tolerance for the generic calculation of moments.
values: tuple of two array_like, optional(xk, pk)wherexkare integers andpkare the non-zero probabilities between 0 and 1 withsum(pk) = 1.xkandpkmust have the same shape, andxkmust be unique.inc: integer, optionalIncrement for the support of the distribution. Default is 1. (other values have not been tested)
badvalue: float, optionalThe value in a result arrays that indicates a value that for which some argument restriction is violated, default is np.nan.
name: str, optionalThe name of the instance. This string is used to construct the default example for distributions.
longname: str, optionalThis string is used as part of the first line of the docstring returned when a subclass has no docstring of its own. Note:
longnameexists for backwards compatibility, do not use for new subclasses.shapes: str, optionalThe shape of the distribution. For example "m, n" for a distribution that takes two integers as the two shape arguments for all its methods If not provided, shape parameters will be inferred from the signatures of the private methods,
_pmfand_cdfof the instance.seed: {None, int, `numpy.random.Generator`, `numpy.random.RandomState`}, optionalIf
seedis None (ornp.random), the numpy.random.RandomState singleton is used. Ifseedis an int, a newRandomStateinstance is used, seeded withseed. Ifseedis already aGeneratororRandomStateinstance then that instance is used.
Attributes
a, b: float, optionalLower/upper bound of the support of the unshifted/unscaled distribution. This value is unaffected by the
locand scale parameters. To calculate the support of the shifted/scaled distribution, use the support method.
Methods
rvspmflogpmfcdflogcdfsflogsfppfisfmomentstatsentropyexpectmedianmeanstdvarinterval__call__support
Notes
This class is similar to rv_continuous. Whether a shape parameter is valid is decided by an _argcheck method (which defaults to checking that its arguments are strictly positive.) The main differences are as follows.
The support of the distribution is a set of integers.
Instead of the probability density function,
pdf(and the corresponding private_pdf), this class defines the probability mass function, pmf (and the corresponding private_pmf.)There is no
scaleparameter.The default implementations of methods (e.g.
_cdf) are not designed for distributions with support that is unbounded below (i.e.a=-np.inf), so they must be overridden.
To create a new discrete distribution, we would do the following:
>>> from scipy.stats import rv_discrete >>> class poisson_gen(rv_discrete): ... "Poisson distribution" ... def _pmf(self, k, mu): ... return exp(-mu) * mu**k / factorial(k)
and create an instance
>>> poisson = poisson_gen(name="poisson")Note that above we defined the Poisson distribution in the standard form. Shifting the distribution can be done by providing the loc parameter to the methods of the instance. For example, poisson.pmf(x, mu, loc) delegates the work to poisson._pmf(x-loc, mu).
Discrete distributions from a list of probabilities
Alternatively, you can construct an arbitrary discrete rv defined on a finite set of values xk with Prob{X=xk} = pk by using the values keyword argument to the rv_discrete constructor.
Deepcopying / Pickling
If a distribution or frozen distribution is deepcopied (pickled/unpickled, etc.), any underlying random number generator is deepcopied with it. An implication is that if a distribution relies on the singleton RandomState before copying, it will rely on a copy of that random state after copying, and np.random.seed will no longer control the state.
Examples
Custom made discrete distribution:import numpy as np 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)) import matplotlib.pyplot as plt fig, ax = plt.subplots(1, 1)✓
ax.plot(xk, custm.pmf(xk), 'ro', ms=12, mec='r') ax.vlines(xk, 0, custm.pmf(xk), colors='r', lw=4)✗
plt.show()
✓
R = custm.rvs(size=100)
✓Aliases
-
scipy.stats.rv_discrete