Skip to main content
`Ctrl`+`K`

# scipy.stats.dirichlet

scipy.stats.dirichlet = <scipy.stats._multivariate.dirichlet_gen object>

  A Dirichlet random variable.

  The `alpha` keyword specifies the concentration parameters of the distribution.

  Added in version 0.15.0.

  Parameters:
    alpha array_like

      The concentration parameters. The number of entries determines the dimensionality of the distribution.

    seed{None, int, np.random.RandomState, np.random.Generator}, optional

      Used for drawing random variates. If seed is None, the 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

  pdf(x, alpha) | Probability density function.
  logpdf(x, alpha) | Log of the probability density function.
  rvs(alpha, size=1, random_state=None) | Draw random samples from a Dirichlet distribution.
  mean(alpha) | The mean of the Dirichlet distribution
  var(alpha) | The variance of the Dirichlet distribution
  cov(alpha) | The covariance of the Dirichlet distribution
  entropy(alpha) | Compute the differential entropy of the Dirichlet distribution.

  Notes

  Each \(\alpha\) entry must be positive. The distribution has only support on the simplex defined by

  \[\sum_{i=1}^{K} x_i = 1\]

  where \(0 < x_i < 1\).

  If the quantiles don’t lie within the simplex, a ValueError is raised.

  The probability density function for `dirichlet` is

  \[f(x) = \frac{1}{\mathrm{B}(\boldsymbol\alpha)} \prod_{i=1}^K x_i^{\alpha_i - 1}\]

  where

  \[\mathrm{B}(\boldsymbol\alpha) = \frac{\prod_{i=1}^K \Gamma(\alpha_i)} {\Gamma\bigl(\sum_{i=1}^K \alpha_i\bigr)}\]

  and \(\boldsymbol\alpha=(\alpha_1,\ldots,\alpha_K)\), the concentration parameters and \(K\) is the dimension of the space where \(x\) takes values.

  Note that the `dirichlet` interface is somewhat inconsistent. The array returned by the rvs function is transposed with respect to the format expected by the pdf and logpdf.

  Examples

      >>> import numpy as np
      >>> from scipy.stats import dirichlet

  Generate a dirichlet random variable

      >>> quantiles = np.array([0.2, 0.2, 0.6])  # specify quantiles
      >>> alpha = np.array([0.4, 5, 15])  # specify concentration parameters
      >>> dirichlet.pdf(quantiles, alpha)
      0.2843831684937255

  The same PDF but following a log scale

      >>> dirichlet.logpdf(quantiles, alpha)
      -1.2574327653159187

  Once we specify the dirichlet distribution we can then calculate quantities of interest

      >>> dirichlet.mean(alpha)  # get the mean of the distribution
      array([0.01960784, 0.24509804, 0.73529412])
      >>> dirichlet.var(alpha) # get variance
      array([0.00089829, 0.00864603, 0.00909517])
      >>> dirichlet.entropy(alpha)  # calculate the differential entropy
      -4.3280162474082715

  We can also return random samples from the distribution

      >>> dirichlet.rvs(alpha, size=1, random_state=1)
      array([[0.00766178, 0.24670518, 0.74563305]])
      >>> dirichlet.rvs(alpha, size=2, random_state=2)
      array([[0.01639427, 0.1292273 , 0.85437844],
             [0.00156917, 0.19033695, 0.80809388]])

  Alternatively, the object may be called (as a function) to fix concentration parameters, returning a “frozen” Dirichlet random variable:

      >>> rv = dirichlet(alpha)
      >>> # Frozen object with the same methods but holding the given
      >>> # concentration parameters fixed.