Draw samples from the Dirichlet distribution.
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.
New code should use the
dirichletmethod of a
default_rng()instance instead; see random-quick-start.
- alphasequence of floats, length k
Parameter of the distribution (length
kfor sample of length
- sizeint or tuple of ints, optional
Output shape. If the given shape is, e.g.,
(m, n), then
m * n * ksamples are drawn. Default is None, in which case a vector of length
The drawn samples, of shape
If any value in
alphais less than or equal to zero
which should be used for new code.
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
David McKay, “Information Theory, Inference and Learning Algorithms,” chapter 23, http://www.inference.org.uk/mackay/itila/
Wikipedia, “Dirichlet distribution”, https://en.wikipedia.org/wiki/Dirichlet_distribution
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) >>> plt.barh(range(20), s, left=s, color='g') >>> plt.barh(range(20), s, left=s+s, color='r') >>> plt.title("Lengths of Strings")