This is a pre-release version (2.5.0.dev0+git20251130.2de293a). Go to latest (2.4.4)
{ } Raw JSON

bundles / numpy 2.5.0.dev0+git20251130.2de293a / numpy / random / RandomState / dirichlet

cython_function_or_method

numpy.random:RandomState.dirichlet

Signature

def   dirichlet ( alpha size = None )

Summary

Draw samples from the Dirichlet distribution.

Extended Summary

Draw size samples of dimension k from a Dirichlet distribution. A Dirichlet-distributed random variable can be seen as a multivariate generalization of a Beta distribution. The Dirichlet distribution is a conjugate prior of a multinomial distribution in Bayesian inference.

Parameters

alpha : sequence of floats, length k

Parameter of the distribution (length k for sample of length k).

size : int or tuple of ints, optional

Output shape. If the given shape is, e.g., (m, n), then m * n * k samples are drawn. Default is None, in which case a vector of length k is returned.

Returns

samples : ndarray,

The drawn samples, of shape (size, k).

Raises

: ValueError

If any value in alpha is less than or equal to zero

Notes

The Dirichlet distribution is a distribution over vectors that fulfil the conditions and .

The probability density function of a Dirichlet-distributed random vector is proportional to

where is a vector containing the positive concentration parameters.

The method uses the following property for computation: let be a random vector which has components that follow a standard gamma distribution, then is Dirichlet-distributed

Examples

Taking an example cited in Wikipedia, this distribution can be used if one wanted to cut strings (each of initial length 1.0) into K pieces with different lengths, where each piece had, on average, a designated average length, but allowing some variation in the relative sizes of the pieces.
s = np.random.dirichlet((10, 5, 3), 20).transpose()
import matplotlib.pyplot as plt
plt.barh(range(20), s[0])
plt.barh(range(20), s[1], left=s[0], color='g')
plt.barh(range(20), s[2], left=s[0]+s[1], color='r')
plt.title("Lengths of Strings")

See also

random.Generator.dirichlet

which should be used for new code.

Aliases

  • numpy.random.dirichlet
  • numpy.random.RandomState.dirichlet