flopscope.

flopscope.numpy.random.Generator.dirichlet

fnp.random.Generator.dirichlet(self, alpha, size=None)

Draw samples from the Dirichlet distribution.

Adapted from NumPy docs np.random.Generator.dirichlet

Arearandom
Typecounted
Cost
numel(output)\text{numel}(\text{output})
Flopscope Context

Dirichlet distribution; cost = numel(output).

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 zero

Notes

The Dirichlet distribution is a distribution over vectors xx that fulfil the conditions xi>0x_i>0 and i=1kxi=1\sum_{i=1}^k x_i = 1.

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

p(x)i=1kxiαi1,p(x) \propto \prod_{i=1}^{k}{x^{\alpha_i-1}_i},

where α\alpha is a vector containing the positive concentration parameters.

The method uses the following property for computation: let YY be a random vector which has components that follow a standard gamma distribution, then X=1i=1kYiYX = \frac{1}{\sum_{i=1}^k{Y_i}} Y is Dirichlet-distributed

References

footnote
1

David McKay, "Information Theory, Inference and Learning
Algorithms," chapter 23,
https://www.inference.org.uk/mackay/itila/
footnote
2

Wikipedia, "Dirichlet distribution",
https://en.wikipedia.org/wiki/Dirichlet_distribution

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.

>>> rng = flops.random.default_rng()
>>> s = rng.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")