package scipy

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type
type tag = [
  1. | `Rv_discrete
]
type t = [ `Object | `Rv_discrete | `Rv_generic ] Obj.t
val of_pyobject : Py.Object.t -> t
val to_pyobject : [> tag ] Obj.t -> Py.Object.t
val as_rv_generic : t -> [ `Rv_generic ] Obj.t
val create : ?a:float -> ?b:float -> ?name:string -> ?badvalue:float -> ?moment_tol:float -> ?values:Py.Object.t -> ?inc:int -> ?longname:string -> ?shapes:string -> ?extradoc:string -> ?seed:[ `I of int | `PyObject of Py.Object.t ] -> unit -> t

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)`` where ``xk`` are integers and ``pk`` are the non-zero probabilities between 0 and 1 with ``sum(pk) = 1``. ``xk`` and ``pk`` 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. extradoc : str, optional 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. seed : None, int, `~np.random.RandomState`, `~np.random.Generator`, optional This parameter defines the object to use for drawing random variates. If `seed` is `None` the `~np.random.RandomState` singleton is used. If `seed` is an int, a new ``RandomState`` instance is used, seeded with seed. If `seed` is already a ``RandomState`` or ``Generator`` instance, then that object is used. Default is None.

Methods ------- rvs pmf logpmf cdf logcdf sf logsf ppf isf moment stats entropy expect 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 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 ``ProbX=xk = pk`` by using the ``values`` keyword argument to the `rv_discrete` constructor.

Examples --------

Custom made discrete distribution:

>>> 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)

val cdf : ?kwds:(string * Py.Object.t) list -> k:[ `Ndarray of [> `Ndarray ] Np.Obj.t | `I of int ] -> Py.Object.t list -> [> tag ] Obj.t -> [ `ArrayLike | `Ndarray | `Object ] Np.Obj.t

Cumulative distribution function of the given RV.

Parameters ---------- k : array_like, int Quantiles. arg1, arg2, arg3,... : 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 : ndarray Cumulative distribution function evaluated at `k`.

val entropy : ?kwds:(string * Py.Object.t) list -> Py.Object.t list -> [> tag ] Obj.t -> Py.Object.t

Differential entropy of the RV.

Parameters ---------- arg1, arg2, arg3,... : 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`:

>>> drv = rv_discrete(values=((0, 1), (0.5, 0.5))) >>> np.allclose(drv.entropy(), np.log(2.0)) True

val expect : ?func:Py.Object.t -> ?args:Py.Object.t -> ?loc:float -> ?lb:Py.Object.t -> ?ub:Py.Object.t -> ?conditional:bool -> ?maxcount:int -> ?tolerance:float -> ?chunksize:int -> [> tag ] Obj.t -> float

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, ub : int, optional Lower and upper bound for the summation, default is set to the support of the distribution, inclusive (``ul <= 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 ``ul <= 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 : float Expected value.

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.

val freeze : ?kwds:(string * Py.Object.t) list -> Py.Object.t list -> [> tag ] Obj.t -> Py.Object.t

Freeze the distribution for the given arguments.

Parameters ---------- arg1, arg2, arg3,... : array_like The shape parameter(s) for the distribution. Should include all the non-optional arguments, may include ``loc`` and ``scale``.

Returns ------- rv_frozen : rv_frozen instance The frozen distribution.

val generic_moment : ?kwargs:(string * Py.Object.t) list -> Py.Object.t list -> [> tag ] Obj.t -> Py.Object.t

Non-central moment of discrete distribution.

val interval : ?kwds:(string * Py.Object.t) list -> alpha:[> `Ndarray ] Np.Obj.t -> Py.Object.t list -> [> tag ] Obj.t -> Py.Object.t

Confidence interval with equal areas around the median.

Parameters ---------- alpha : 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, arg2, ... : 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 : ndarray of float end-points of range that contain ``100 * alpha %`` of the rv's possible values.

val isf : ?kwds:(string * Py.Object.t) list -> q:[> `Ndarray ] Np.Obj.t -> Py.Object.t list -> [> tag ] Obj.t -> [ `ArrayLike | `Ndarray | `Object ] Np.Obj.t

Inverse survival function (inverse of `sf`) at q of the given RV.

Parameters ---------- q : array_like Upper tail probability. arg1, arg2, arg3,... : 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 : ndarray or scalar Quantile corresponding to the upper tail probability, q.

val logcdf : ?kwds:(string * Py.Object.t) list -> k:[ `Ndarray of [> `Ndarray ] Np.Obj.t | `I of int ] -> Py.Object.t list -> [> tag ] Obj.t -> [ `ArrayLike | `Ndarray | `Object ] Np.Obj.t

Log of the cumulative distribution function at k of the given RV.

Parameters ---------- k : array_like, int Quantiles. arg1, arg2, arg3,... : 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 : array_like Log of the cumulative distribution function evaluated at k.

val logpmf : ?kwds:(string * Py.Object.t) list -> k:[> `Ndarray ] Np.Obj.t -> Py.Object.t list -> [> tag ] Obj.t -> [ `ArrayLike | `Ndarray | `Object ] Np.Obj.t

Log of the probability mass function at k of the given RV.

Parameters ---------- k : array_like Quantiles. arg1, arg2, arg3,... : 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 : array_like Log of the probability mass function evaluated at k.

val logsf : ?kwds:(string * Py.Object.t) list -> k:[> `Ndarray ] Np.Obj.t -> Py.Object.t list -> [> tag ] Obj.t -> [ `ArrayLike | `Ndarray | `Object ] Np.Obj.t

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, arg2, arg3,... : 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 : ndarray Log of the survival function evaluated at `k`.

val mean : ?kwds:(string * Py.Object.t) list -> Py.Object.t list -> [> tag ] Obj.t -> float

Mean of the distribution.

Parameters ---------- arg1, arg2, arg3,... : 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 : float the mean of the distribution

val median : ?kwds:(string * Py.Object.t) list -> Py.Object.t list -> [> tag ] Obj.t -> float

Median of the distribution.

Parameters ---------- arg1, arg2, arg3,... : 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 : float The median of the distribution.

See Also -------- rv_discrete.ppf Inverse of the CDF

val moment : ?kwds:(string * Py.Object.t) list -> n:[ `N_1 of Py.Object.t | `I of int ] -> Py.Object.t list -> [> tag ] Obj.t -> Py.Object.t

n-th order non-central moment of distribution.

Parameters ---------- n : int, n >= 1 Order of moment. arg1, arg2, arg3,... : 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)

val pmf : ?kwds:(string * Py.Object.t) list -> k:[> `Ndarray ] Np.Obj.t -> Py.Object.t list -> [> tag ] Obj.t -> [ `ArrayLike | `Ndarray | `Object ] Np.Obj.t

Probability mass function at k of the given RV.

Parameters ---------- k : array_like Quantiles. arg1, arg2, arg3,... : 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 : array_like Probability mass function evaluated at k

val ppf : ?kwds:(string * Py.Object.t) list -> q:[> `Ndarray ] Np.Obj.t -> Py.Object.t list -> [> tag ] Obj.t -> [ `ArrayLike | `Ndarray | `Object ] Np.Obj.t

Percent point function (inverse of `cdf`) at q of the given RV.

Parameters ---------- q : array_like Lower tail probability. arg1, arg2, arg3,... : 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 : array_like Quantile corresponding to the lower tail probability, q.

val rvs : ?kwargs:(string * Py.Object.t) list -> Py.Object.t list -> [> tag ] Obj.t -> [ `ArrayLike | `Ndarray | `Object ] Np.Obj.t

Random variates of given type.

Parameters ---------- arg1, arg2, arg3,... : 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, `~np.random.RandomState`, `~np.random.Generator`, optional This parameter defines the object to use for drawing random variates. If `random_state` is `None` the `~np.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 a ``RandomState`` or ``Generator`` instance, then that object is used. Default is None.

Returns ------- rvs : ndarray or scalar Random variates of given `size`.

val sf : ?kwds:(string * Py.Object.t) list -> k:[> `Ndarray ] Np.Obj.t -> Py.Object.t list -> [> tag ] Obj.t -> [ `ArrayLike | `Ndarray | `Object ] Np.Obj.t

Survival function (1 - `cdf`) at k of the given RV.

Parameters ---------- k : array_like Quantiles. arg1, arg2, arg3,... : 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 : array_like Survival function evaluated at k.

val stats : ?kwds:(string * Py.Object.t) list -> Py.Object.t list -> [> tag ] Obj.t -> Py.Object.t

Some statistics of the given RV.

Parameters ---------- arg1, arg2, arg3,... : 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 : sequence of requested moments.

val std : ?kwds:(string * Py.Object.t) list -> Py.Object.t list -> [> tag ] Obj.t -> float

Standard deviation of the distribution.

Parameters ---------- arg1, arg2, arg3,... : 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 : float standard deviation of the distribution

val support : ?kwargs:(string * Py.Object.t) list -> Py.Object.t list -> [> tag ] Obj.t -> Py.Object.t

Return the support of the distribution.

Parameters ---------- arg1, arg2, ... : 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 : float end-points of the distribution's support.

val var : ?kwds:(string * Py.Object.t) list -> Py.Object.t list -> [> tag ] Obj.t -> float

Variance of the distribution.

Parameters ---------- arg1, arg2, arg3,... : 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 : float the variance of the distribution

val to_string : t -> string

Print the object to a human-readable representation.

val show : t -> string

Print the object to a human-readable representation.

val pp : Stdlib.Format.formatter -> t -> unit

Pretty-print the object to a formatter.