deephyper.skopt.space.space.rv_discrete#
- class deephyper.skopt.space.space.rv_discrete(a=0, b=inf, name=None, badvalue=None, moment_tol=1e-08, values=None, inc=1, longname=None, shapes=None, seed=None)[source]#
Bases:
rv_generic
A generic discrete random variable class meant for subclassing.
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, optional) – Lower bound of the support of the distribution, default: 0
b (float, optional) – Upper bound of the support of the distribution, default: plus infinity
moment_tol (float, optional) – The tolerance for the generic calculation of moments.
values (tuple of two array_like, optional) –
(xk, pk)
wherexk
are integers andpk
are the non-zero probabilities between 0 and 1 withsum(pk) = 1
.xk
andpk
must have the same shape.inc (integer, optional) – Increment for the support of the distribution. Default is 1. (other values have not been tested)
badvalue (float, optional) – The value in a result arrays that indicates a value that for which some argument restriction is violated, default is np.nan.
name (str, optional) – The name of the instance. This string is used to construct the default example for distributions.
longname (str, 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.
shapes (str, 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 signatures of the private methods,
_pmf
and_cdf
of the instance.seed ({None, int, numpy.random.Generator, numpy.random.RandomState}, optional) – If seed is None (or np.random), the numpy.random.RandomState singleton is used. If seed is an int, a new
RandomState
instance is used, seeded with seed. If seed is already aGenerator
orRandomState
instance then that instance is used.
- moment()#
- stats()#
- entropy()#
- median()#
- mean()#
- std()#
- var()#
- interval()#
- __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: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
.)scale parameter is not defined.
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 topoisson._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
withProb{X=xk} = pk
by using thevalues
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()
Random number generation:
>>> R = custm.rvs(size=100)
Methods
Cumulative distribution function of the given RV.
Differential entropy of the RV.
Calculate expected value of a function with respect to the distribution for discrete distribution by numerical summation.
Freeze the distribution for the given arguments.
Confidence interval with equal areas around the median.
Inverse survival function (inverse of sf) at q of the given RV.
Log of the cumulative distribution function at k of the given RV.
Log of the probability mass function at k of the given RV.
Log of the survival function of the given RV.
Mean of the distribution.
Median of the distribution.
non-central moment of distribution of specified order.
Negative loglikelihood function.
Probability mass function at k of the given RV.
Percent point function (inverse of cdf) at q of the given RV.
Random variates of given type.
Survival function (1 - cdf) at k of the given RV.
Some statistics of the given RV.
Standard deviation of the distribution.
Support of the distribution.
Variance of the distribution.
Attributes
Get or set the generator object for generating random variates.
- __call__(*args, **kwds)#
Freeze the distribution for the given arguments.
- Parameters:
arg1 (array_like) – The shape parameter(s) for the distribution. Should include all the non-optional arguments, may include
loc
andscale
.arg2 (array_like) – The shape parameter(s) for the distribution. Should include all the non-optional arguments, may include
loc
andscale
.arg3 (array_like) – The shape parameter(s) for the distribution. Should include all the non-optional arguments, may include
loc
andscale
.... (array_like) – The shape parameter(s) for the distribution. Should include all the non-optional arguments, may include
loc
andscale
.
- Returns:
rv_frozen – The frozen distribution.
- Return type:
rv_frozen instance
- cdf(k, *args, **kwds)[source]#
Cumulative distribution function of the given RV.
- Parameters:
k (array_like, int) – Quantiles.
arg1 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
arg2 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
arg3 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
... (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
loc (array_like, optional) – Location parameter (default=0).
- Returns:
cdf – Cumulative distribution function evaluated at k.
- Return type:
ndarray
- entropy(*args, **kwds)#
Differential entropy of the RV.
- Parameters:
arg1 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
arg2 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
arg3 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
... (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
loc (array_like, optional) – Location parameter (default=0).
scale (array_like, optional (continuous distributions only).) – Scale parameter (default=1).
Notes
Entropy is defined base e:
>>> import numpy as np >>> drv = rv_discrete(values=((0, 1), (0.5, 0.5))) >>> np.allclose(drv.entropy(), np.log(2.0)) True
- expect(func=None, args=(), loc=0, lb=None, ub=None, conditional=False, maxcount=1000, tolerance=1e-10, chunksize=32)[source]#
Calculate expected value of a function with respect to the distribution for discrete distribution by numerical summation.
- Parameters:
func (callable, optional) – Function for which the expectation value is calculated. Takes only one argument. The default is the identity mapping f(k) = k.
args (tuple, optional) – Shape parameters of the distribution.
loc (float, optional) – Location parameter. Default is 0.
lb (int, optional) – Lower and upper bound for the summation, default is set to the support of the distribution, inclusive (
lb <= k <= ub
).ub (int, optional) – Lower and upper bound for the summation, default is set to the support of the distribution, inclusive (
lb <= k <= ub
).conditional (bool, optional) – If true then the expectation is corrected by the conditional probability of the summation interval. The return value is the expectation of the function, func, conditional on being in the given interval (k such that
lb <= k <= ub
). Default is False.maxcount (int, optional) – Maximal number of terms to evaluate (to avoid an endless loop for an infinite sum). Default is 1000.
tolerance (float, optional) – Absolute tolerance for the summation. Default is 1e-10.
chunksize (int, optional) – Iterate over the support of a distributions in chunks of this size. Default is 32.
- Returns:
expect – Expected value.
- Return type:
Notes
For heavy-tailed distributions, the expected value may or may not exist, depending on the function, func. If it does exist, but the sum converges slowly, the accuracy of the result may be rather low. For instance, for
zipf(4)
, accuracy for mean, variance in example is only 1e-5. increasing maxcount and/or chunksize may improve the result, but may also make zipf very slow.The function is not vectorized.
- freeze(*args, **kwds)#
Freeze the distribution for the given arguments.
- Parameters:
arg1 (array_like) – The shape parameter(s) for the distribution. Should include all the non-optional arguments, may include
loc
andscale
.arg2 (array_like) – The shape parameter(s) for the distribution. Should include all the non-optional arguments, may include
loc
andscale
.arg3 (array_like) – The shape parameter(s) for the distribution. Should include all the non-optional arguments, may include
loc
andscale
.... (array_like) – The shape parameter(s) for the distribution. Should include all the non-optional arguments, may include
loc
andscale
.
- Returns:
rv_frozen – The frozen distribution.
- Return type:
rv_frozen instance
- interval(confidence, *args, **kwds)#
Confidence interval with equal areas around the median.
- Parameters:
confidence (array_like of float) – Probability that an rv will be drawn from the returned range. Each value should be in the range [0, 1].
arg1 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
arg2 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
... (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
loc (array_like, optional) – location parameter, Default is 0.
scale (array_like, optional) – scale parameter, Default is 1.
- Returns:
a, b – end-points of range that contain
100 * alpha %
of the rv’s possible values.- Return type:
ndarray of float
Notes
This is implemented as
ppf([p_tail, 1-p_tail])
, whereppf
is the inverse cumulative distribution function andp_tail = (1-confidence)/2
. Suppose[c, d]
is the support of a discrete distribution; thenppf([0, 1]) == (c-1, d)
. Therefore, whenconfidence=1
and the distribution is discrete, the left end of the interval will be beyond the support of the distribution. For discrete distributions, the interval will limit the probability in each tail to be less than or equal top_tail
(usually strictly less).
- isf(q, *args, **kwds)[source]#
Inverse survival function (inverse of sf) at q of the given RV.
- Parameters:
q (array_like) – Upper tail probability.
arg1 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
arg2 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
arg3 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
... (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
loc (array_like, optional) – Location parameter (default=0).
- Returns:
k – Quantile corresponding to the upper tail probability, q.
- Return type:
ndarray or scalar
- logcdf(k, *args, **kwds)[source]#
Log of the cumulative distribution function at k of the given RV.
- Parameters:
k (array_like, int) – Quantiles.
arg1 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
arg2 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
arg3 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
... (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
loc (array_like, optional) – Location parameter (default=0).
- Returns:
logcdf – Log of the cumulative distribution function evaluated at k.
- Return type:
array_like
- logpmf(k, *args, **kwds)[source]#
Log of the probability mass function at k of the given RV.
- Parameters:
k (array_like) – Quantiles.
arg1 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
arg2 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
arg3 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
... (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
loc (array_like, optional) – Location parameter. Default is 0.
- Returns:
logpmf – Log of the probability mass function evaluated at k.
- Return type:
array_like
- logsf(k, *args, **kwds)[source]#
Log of the survival function of the given RV.
Returns the log of the “survival function,” defined as 1 - cdf, evaluated at k.
- Parameters:
k (array_like) – Quantiles.
arg1 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
arg2 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
arg3 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
... (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
loc (array_like, optional) – Location parameter (default=0).
- Returns:
logsf – Log of the survival function evaluated at k.
- Return type:
ndarray
- mean(*args, **kwds)#
Mean of the distribution.
- Parameters:
arg1 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information)
arg2 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information)
arg3 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information)
... (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information)
loc (array_like, optional) – location parameter (default=0)
scale (array_like, optional) – scale parameter (default=1)
- Returns:
mean – the mean of the distribution
- Return type:
- median(*args, **kwds)#
Median of the distribution.
- Parameters:
arg1 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information)
arg2 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information)
arg3 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information)
... (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information)
loc (array_like, optional) – Location parameter, Default is 0.
scale (array_like, optional) – Scale parameter, Default is 1.
- Returns:
median – The median of the distribution.
- Return type:
See also
rv_discrete.ppf
Inverse of the CDF
- moment(order, *args, **kwds)#
non-central moment of distribution of specified order.
- Parameters:
order (int, order >= 1) – Order of moment.
arg1 (float) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
arg2 (float) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
arg3 (float) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
... (float) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
loc (array_like, optional) – location parameter (default=0)
scale (array_like, optional) – scale parameter (default=1)
- nnlf(theta, x)#
Negative loglikelihood function. .. rubric:: Notes
This is
-sum(log pdf(x, theta), axis=0)
where theta are the parameters (including loc and scale).
- pmf(k, *args, **kwds)[source]#
Probability mass function at k of the given RV.
- Parameters:
k (array_like) – Quantiles.
arg1 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information)
arg2 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information)
arg3 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information)
... (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information)
loc (array_like, optional) – Location parameter (default=0).
- Returns:
pmf – Probability mass function evaluated at k
- Return type:
array_like
- ppf(q, *args, **kwds)[source]#
Percent point function (inverse of cdf) at q of the given RV.
- Parameters:
q (array_like) – Lower tail probability.
arg1 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
arg2 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
arg3 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
... (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
loc (array_like, optional) – Location parameter (default=0).
- Returns:
k – Quantile corresponding to the lower tail probability, q.
- Return type:
array_like
- property random_state#
Get or set the generator object for generating random variates.
If random_state is None (or np.random), the numpy.random.RandomState singleton is used. If random_state is an int, a new
RandomState
instance is used, seeded with random_state. If random_state is already aGenerator
orRandomState
instance, that instance is used.
- rvs(*args, **kwargs)[source]#
Random variates of given type.
- Parameters:
arg1 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
arg2 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
arg3 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
... (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
loc (array_like, optional) – Location parameter (default=0).
size (int or tuple of ints, optional) – Defining number of random variates (Default is 1). Note that size has to be given as keyword, not as positional argument.
random_state ({None, int, numpy.random.Generator,) –
numpy.random.RandomState}, optional
If random_state is None (or np.random), the numpy.random.RandomState singleton is used. If random_state is an int, a new
RandomState
instance is used, seeded with random_state. If random_state is already aGenerator
orRandomState
instance, that instance is used.
- Returns:
rvs – Random variates of given size.
- Return type:
ndarray or scalar
- sf(k, *args, **kwds)[source]#
Survival function (1 - cdf) at k of the given RV.
- Parameters:
k (array_like) – Quantiles.
arg1 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
arg2 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
arg3 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
... (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
loc (array_like, optional) – Location parameter (default=0).
- Returns:
sf – Survival function evaluated at k.
- Return type:
array_like
- stats(*args, **kwds)#
Some statistics of the given RV.
- Parameters:
arg1 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information)
arg2 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information)
arg3 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information)
... (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information)
loc (array_like, optional) – location parameter (default=0)
scale (array_like, optional (continuous RVs only)) – scale parameter (default=1)
moments (str, optional) – composed of letters [‘mvsk’] defining which moments to compute: ‘m’ = mean, ‘v’ = variance, ‘s’ = (Fisher’s) skew, ‘k’ = (Fisher’s) kurtosis. (default is ‘mv’)
- Returns:
stats – of requested moments.
- Return type:
sequence
- std(*args, **kwds)#
Standard deviation of the distribution.
- Parameters:
arg1 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information)
arg2 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information)
arg3 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information)
... (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information)
loc (array_like, optional) – location parameter (default=0)
scale (array_like, optional) – scale parameter (default=1)
- Returns:
std – standard deviation of the distribution
- Return type:
- support(*args, **kwargs)#
Support of the distribution.
- Parameters:
arg1 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
arg2 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
... (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information).
loc (array_like, optional) – location parameter, Default is 0.
scale (array_like, optional) – scale parameter, Default is 1.
- Returns:
a, b – end-points of the distribution’s support.
- Return type:
array_like
- var(*args, **kwds)#
Variance of the distribution.
- Parameters:
arg1 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information)
arg2 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information)
arg3 (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information)
... (array_like) – The shape parameter(s) for the distribution (see docstring of the instance object for more information)
loc (array_like, optional) – location parameter (default=0)
scale (array_like, optional) – scale parameter (default=1)
- Returns:
var – the variance of the distribution
- Return type: