Skip to main content
`Ctrl`+`K`
scipy.stats.

# Binomial

class scipy.stats.Binomial(*, n, p, **kwargs)

  Binomial distribution with prescribed success probability and number of trials

  The probability density function of the binomial distribution is:

  \[f(x) = {n \choose x} p^x (1 - p)^{n-x}\]

  for \(x \in \{0, 1, ..., n - 1, n\}\). This class accepts one parameterization: `n` for \(n \in \{0, 1, 2, ...\}\), `p` for \(p \in (0, 1)\).

  Parameters:
    tol positive float, optional

      The desired relative tolerance of calculations. Left unspecified, calculations may be faster; when provided, calculations may be more likely to meet the desired accuracy.

    validation_policy{None, “skip_all”}

      Specifies the level of input validation to perform. Left unspecified, input validation is performed to ensure appropriate behavior in edge case (e.g. parameters out of domain, argument outside of distribution support, etc.) and improve consistency of output dtype, shape, etc. Pass `'skip_all'` to avoid the computational overhead of these checks when rough edges are acceptable.

    cache_policy{None, “no_cache”}

      Specifies the extent to which intermediate results are cached. Left unspecified, intermediate results of some calculations (e.g. distribution support, moments, etc.) are cached to improve performance of future calculations. Pass `'no_cache'` to reduce memory reserved by the class instance.

  Attributes:
    All parameters are available as attributes.

  Methods

  `support`() | Support of the random variable
  `plot`([x, y, t, ax]) | Plot a function of the distribution.
  `sample`([shape, method, rng]) | Random sample from the distribution.
  `moment`([order, kind, method]) | Raw, central, or standard moment of positive integer order.
  `mean`(*[, method]) | Mean (raw first moment about the origin)
  `median`(*[, method]) | Median (50th percentile)
  `mode`(*[, method]) | Mode (most likely value)
  `variance`(*[, method]) | Variance (central second moment)
  `standard_deviation`(*[, method]) | Standard deviation (square root of the second central moment)
  `skewness`(*[, method]) | Skewness (standardized third moment)
  `kurtosis`(*[, method, convention]) | Kurtosis (standardized fourth moment)
  `pdf`(x, /, *[, method]) | Probability density function
  `logpdf`(x, /, *[, method]) | Log of the probability density function
  `cdf`(x[, y, method]) | Cumulative distribution function
  `icdf`(p, /, *[, method]) | Inverse of the cumulative distribution function.
  `ccdf`(x[, y, method]) | Complementary cumulative distribution function
  `iccdf`(p, /, *[, method]) | Inverse complementary cumulative distribution function.
  `logcdf`(x[, y, method]) | Log of the cumulative distribution function
  `ilogcdf`(logp, /, *[, method]) | Inverse of the logarithm of the cumulative distribution function.
  `logccdf`(x[, y, method]) | Log of the complementary cumulative distribution function
  `ilogccdf`(logp, /, *[, method]) | Inverse of the log of the complementary cumulative distribution function.
  `entropy`(*[, method]) | Differential entropy
  `logentropy`(*[, method]) | Logarithm of the differential entropy

  See also

  Random Variable Transition Guide

    Tutorial

  Notes

  The following abbreviations are used throughout the documentation.

  - PDF: probability density function

  - CDF: cumulative distribution function

  - CCDF: complementary CDF

  - entropy: differential entropy

  - log-F: logarithm of F (e.g. log-CDF)

  - inverse F: inverse function of F (e.g. inverse CDF)

  The API documentation is written to describe the API, not to serve as a statistical reference. Effort is made to be correct at the level required to use the functionality, not to be mathematically rigorous. For example, continuity and differentiability may be implicitly assumed. For precise mathematical definitions, consult your preferred mathematical text.

  Examples

  To use the distribution class, it must be instantiated using keyword parameters corresponding with one of the accepted parameterizations.

      >>> import numpy as np
      >>> import matplotlib.pyplot as plt
      >>> from scipy import stats
      >>> from scipy.stats import Binomial
      >>> X = Binomial(n=10.0, p=0.34)

  For convenience, the `plot` method can be used to visualize the density and other functions of the distribution.

      >>> X.plot()
      >>> plt.show()

  The support of the underlying distribution is available using the `support` method.

      >>> X.support()
      (np.float64(0.0), np.float64(10.0))

  The numerical values of parameters associated with all parameterizations are available as attributes.

      >>> X.n, X.p
      (np.float64(10.0), np.float64(0.34))

  To evaluate the probability density/mass function of the underlying distribution at argument `x=3.0`:

      >>> x = 3.0
      >>> X.pdf(x), X.pmf(x)
      (np.float64(inf), np.float64(0.2572915634331659))

  The cumulative distribution function, its complement, and the logarithm of these functions are evaluated similarly.

      >>> np.allclose(np.exp(X.logccdf(x)), 1 - X.cdf(x))
      True

  There are methods for computing measures of central tendency, dispersion, higher moments, and entropy.

      >>> X.mean(), X.median(), X.mode()
      (np.float64(3.4000000000000004), np.float64(3.0), np.float64(3.0))

      >>> X.variance(), X.standard_deviation()
      (np.float64(2.2439999999999998), np.float64(1.4979986648859203))

      >>> X.skewness(), X.kurtosis()
      (np.float64(0.21361834793382106), np.float64(2.845632798573975))

      >>> np.allclose(X.moment(order=6, kind='standardized'),
      ...             X.moment(order=6, kind='central') / X.variance()**3)
      True

      >>> X.entropy()
      1.816303869320522

  Pseudo-random samples can be drawn from the underlying distribution using `sample`.

      >>> X.sample(shape=(4,))
      array([4., 3., 3., 5.])  # may vary