A generic continuous random variable class meant for subclassing.
rv_continuous is a base class to construct specific distribution classes and instances from for continuous random variables. It cannot be used directly as a distribution.
Parameters: | momtype : int, optional
a : float, optional
b : float, optional
xtol : float, optional
badvalue : object, optional
name : str, optional
longname : str, optional
shapes : str, optional
extradoc : str, optional, deprecated
|
---|
Notes
Methods that can be overwritten by subclasses
_rvs
_pdf
_cdf
_sf
_ppf
_isf
_stats
_munp
_entropy
_argcheck
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.
Frozen Distribution
Alternatively, the object may be called (as a function) to fix the shape, location, and scale parameters returning a “frozen” continuous RV object:
Subclassing
New random variables can be defined by subclassing rv_continuous class and re-defining at least the _pdf or the _cdf method (normalized to location 0 and scale 1) which will be given clean arguments (in between a and b) and passing the argument check method.
If positive argument checking is not correct for your RV then you will also need to re-define the _argcheck method.
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
Rarely would you override _isf, _sf or _logsf, but you could.
Statistics are computed using numerical integration by default. For speed you can redefine this using _stats:
- 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=<str>, where <str> 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 n and shape parameters and returns the nth non-central moment of the distribution.
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
To create a new Gaussian distribution, we would do the following:
class gaussian_gen(rv_continuous):
"Gaussian distribution"
def _pdf(self, x):
...
...
Methods
rvs(<shape(s)>, loc=0, scale=1, size=1) | random variates |
pdf(x, <shape(s)>, loc=0, scale=1) | probability density function |
logpdf(x, <shape(s)>, loc=0, scale=1) | log of the probability density function |
cdf(x, <shape(s)>, loc=0, scale=1) | cumulative density function |
logcdf(x, <shape(s)>, loc=0, scale=1) | log of the cumulative density function |
sf(x, <shape(s)>, loc=0, scale=1) | survival function (1-cdf — sometimes more accurate) |
logsf(x, <shape(s)>, loc=0, scale=1) | log of the survival function |
ppf(q, <shape(s)>, loc=0, scale=1) | percent point function (inverse of cdf — quantiles) |
isf(q, <shape(s)>, loc=0, scale=1) | inverse survival function (inverse of sf) |
moment(n, <shape(s)>, loc=0, scale=1) | non-central n-th moment of the distribution. May not work for array arguments. |
stats(<shape(s)>, loc=0, scale=1, moments=’mv’) | mean(‘m’), variance(‘v’), skew(‘s’), and/or kurtosis(‘k’) |
entropy(<shape(s)>, loc=0, scale=1) | (differential) entropy of the RV. |
fit(data, <shape(s)>, loc=0, scale=1) | Parameter estimates for generic data |
expect(func=None, args=(), loc=0, scale=1, lb=None, ub=None, | conditional=False, **kwds) Expected value of a function with respect to the distribution. Additional kwd arguments passed to integrate.quad |
median(<shape(s)>, loc=0, scale=1) | Median of the distribution. |
mean(<shape(s)>, loc=0, scale=1) | Mean of the distribution. |
std(<shape(s)>, loc=0, scale=1) | Standard deviation of the distribution. |
var(<shape(s)>, loc=0, scale=1) | Variance of the distribution. |
interval(alpha, <shape(s)>, loc=0, scale=1) | Interval that with alpha percent probability contains a random realization of this distribution. |
__call__(<shape(s)>, loc=0, scale=1) | Calling a distribution instance creates a frozen RV object with the same methods but holding the given shape, location, and scale fixed. See Notes section. |
Parameters for Methods | |
x | (array_like) quantiles |
q | (array_like) lower or upper tail probability |
<shape(s)> | (array_like) shape parameters |
loc | (array_like, optional) location parameter (default=0) |
scale | (array_like, optional) scale parameter (default=1) |
size | (int or tuple of ints, optional) shape of random variates (default computed from input arguments ) |
moments | (string, optional) composed of letters [‘mvsk’] specifying which moments to compute where ‘m’ = mean, ‘v’ = variance, ‘s’ = (Fisher’s) skew and ‘k’ = (Fisher’s) kurtosis. (default=’mv’) |
n | (int) order of moment to calculate in method moments |