rv_continuous(momtype=1, a=None, b=None, xtol=1e-14, badvalue=None, name=None, longname=None, shapes=None, extradoc=None, seed=None)¶
A generic continuous random variable class meant for subclassing.
rv_continuousis a base class to construct specific distribution classes and instances for continuous random variables. It cannot be used directly as a distribution.
- momtypeint, optional
The type of generic moment calculation to use: 0 for pdf, 1 (default) for ppf.
- afloat, optional
Lower bound of the support of the distribution, default is minus infinity.
- bfloat, optional
Upper bound of the support of the distribution, default is plus infinity.
- xtolfloat, optional
The tolerance for fixed point calculation for generic ppf.
- badvaluefloat, optional
The value in a result arrays that indicates a value that for which some argument restriction is violated, default is np.nan.
- namestr, optional
The name of the instance. This string is used to construct the default example for distributions.
- longnamestr, optional
This string is used as part of the first line of the docstring returned when a subclass has no docstring of its own. Note: longname exists for backwards compatibility, do not use for new subclasses.
- shapesstr, optional
The 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 signature of the private methods,
_cdfof the instance.
- extradocstr, optional, deprecated
This string is used as the last part of the docstring returned when a subclass has no docstring of its own. Note: extradoc exists for backwards compatibility, do not use for new subclasses.
- seedNone or int or
This parameter defines the RandomState object to use for drawing random variates. If None (or np.random), the global np.random state is used. If integer, it is used to seed the local RandomState instance. Default is None.
Public methods of an instance of a distribution class (e.g.,
cdf) check their arguments and pass valid arguments to private, computational methods (
xis valid if it is within the support of the distribution. Whether a shape parameter is valid is decided by an
_argcheckmethod (which defaults to checking that its arguments are strictly positive.)
New random variables can be defined by subclassing the
rv_continuousclass and re-defining at least the
_cdfmethod (normalized to location 0 and scale 1).
If positive argument checking is not correct for your RV then you will also need to re-define the
For most of the scipy.stats distributions, the support interval doesn’t depend on the shape parameters.
xbeing in the support interval is equivalent to
self.a <= x <= self.b. If either of the endpoints of the support do depend on the shape parameters, then i) the distribution must implement the
_get_supportmethod; and ii) those dependent endpoints must be omitted from the distribution’s call to the
Correct, but potentially slow defaults exist for the remaining methods but for speed and/or accuracy you can over-ride:
_logpdf, _cdf, _logcdf, _ppf, _rvs, _isf, _sf, _logsf
The default method
_rvsrelies on the inverse of the cdf,
_ppf, applied to a uniform random variate. In order to generate random variates efficiently, either the default
_ppfneeds to be overwritten (e.g. if the inverse cdf can expressed in an explicit form) or a sampling method needs to be implemented in a custom
If possible, you should override
_logsf. The main reason would be to improve numerical accuracy: for example, the survival function
_sfis computed as
1 - _cdfwhich can result in loss of precision if
_cdf(x)is close to one.
Methods that can be overwritten by subclasses
_rvs _pdf _cdf _sf _ppf _isf _stats _munp _entropy _argcheck _get_support
There are additional (internal and private) generic methods that can be useful for cross-checking and for debugging, but might work in all cases when directly called.
A note on
shapes: subclasses need not specify them explicitly. In this case, shapes will be automatically deduced from the signatures of the overridden methods (
cdfetc). If, for some reason, you prefer to avoid relying on introspection, you can specify
shapesexplicitly as an argument to the instance constructor.
Normally, you must provide shape parameters (and, optionally, location and scale parameters to each call of a method of a distribution.
Alternatively, the object may be called (as a function) to fix the shape, location, and scale parameters returning a “frozen” continuous RV object:
- rv = generic(<shape(s)>, loc=0, scale=1)
rv_frozen object with the same methods but holding the given shape, location, and scale fixed
Statistics are computed using numerical integration by default. For speed you can redefine this using
take shape parameters and return mu, mu2, g1, g2
If you can’t compute one of these, return it as None
Can also be defined with a keyword argument
moments, which is a string composed of “m”, “v”, “s”, and/or “k”. Only the components appearing in string should be computed and returned in the order “m”, “v”, “s”, or “k” with missing values returned as None.
Alternatively, you can override
_munp, which takes
nand shape parameters and returns the n-th non-central moment of the distribution.
To create a new Gaussian distribution, we would do the following:
>>> from scipy.stats import rv_continuous >>> class gaussian_gen(rv_continuous): ... "Gaussian distribution" ... def _pdf(self, x): ... return np.exp(-x**2 / 2.) / np.sqrt(2.0 * np.pi) >>> gaussian = gaussian_gen(name='gaussian')
scipy.statsdistributions are instances, so here we subclass
rv_continuousand create an instance. With this, we now have a fully functional distribution with all relevant methods automagically generated by the framework.
Note that above we defined a standard normal distribution, with zero mean and unit variance. Shifting and scaling of the distribution can be done by using
gaussian.pdf(x, loc, scale)essentially computes
y = (x - loc) / scaleand
gaussian._pdf(y) / scale.
Get or set the RandomState object for generating random variates.
rvs(self, \*args, \*\*kwds)
Random variates of given type.
Probability density function at x of the given RV.
logpdf(self, x, \*args, \*\*kwds)
Log of the probability density function at x of the given RV.
cdf(self, x, \*args, \*\*kwds)
Cumulative distribution function of the given RV.
logcdf(self, x, \*args, \*\*kwds)
Log of the cumulative distribution function at x of the given RV.
sf(self, x, \*args, \*\*kwds)
Survival function (1 -
cdf) at x of the given RV.
logsf(self, x, \*args, \*\*kwds)
Log of the survival function of the given RV.
ppf(self, q, \*args, \*\*kwds)
Percent point function (inverse of
cdf) at q of the given RV.
isf(self, q, \*args, \*\*kwds)
Inverse survival function (inverse of
sf) at q of the given RV.
moment(self, n, \*args, \*\*kwds)
n-th order non-central moment of distribution.
stats(self, \*args, \*\*kwds)
Some statistics of the given RV.
entropy(self, \*args, \*\*kwds)
Differential entropy of the RV.
expect(self[, func, args, loc, scale, lb, …])
Calculate expected value of a function with respect to the distribution by numerical integration.
median(self, \*args, \*\*kwds)
Median of the distribution.
mean(self, \*args, \*\*kwds)
Mean of the distribution.
std(self, \*args, \*\*kwds)
Standard deviation of the distribution.
var(self, \*args, \*\*kwds)
Variance of the distribution.
interval(self, alpha, \*args, \*\*kwds)
Confidence interval with equal areas around the median.
__call__(self, \*args, \*\*kwds)
Freeze the distribution for the given arguments.
fit(self, data, \*args, \*\*kwds)
Return MLEs for shape (if applicable), location, and scale parameters from data.
fit_loc_scale(self, data, \*args)
Estimate loc and scale parameters from data using 1st and 2nd moments.
nnlf(self, theta, x)
Return negative loglikelihood function.
support(self, \*args, \*\*kwargs)
Return the support of the distribution.