Dirichlet Distribution

The Dirichlet distribution is a generalization of the beta distribution to the multivariate case. The Dirichlet distribution is often used in Bayesian analysis.

A Dirichlet distribution of order K is defined on the K-dimensional hyperplane with $\sum x_k = 1$. It has K shape parameters, usually denoted by the Greek letter α. Its probability density function (PDF) is:

$$\frac{1}{B(\alpha) \prod_{i=1}^K x_i^{\alpha_i-1}$$

where

$$B(\alpha) = \frac{\prod_{i=1}^K\Gamma(\alpha_i)}{\Gamma(\alpha_0)}$$

and

$$\alpha_0 = \sum_{i=1}^K \alpha_i$$

When K = 2, the Dirichlet distribution reduces to the Beta distribution. The Dirichlet distribution is implemented by the DirichletDistribution class. It has three constructors. The first constructor takes a Vector<T> that contains the shape parameters.

C#
var alpha = Vector.Create(3.0, 7.0, 5.0);
var dirichlet1 = new DirichletDistribution(alpha);

The two remaining constructors estimate the distribution parameters from a set of variables. One constructor takes a Vector<T> array. The other takes a Matrix<T> whose columns contain the variables.

The GetParameters method returns a copy of the parameters α. The GetMeans method returns a Vector<T> that contains the mean of the distribution. The GetVarianceCovarianceMatrix method returns a SymmetricMatrix<T> that contains the variance-covariance matrix of the distribution.

There are two options to generate random samples from the distribution. One is to use the Sample method. Sample takes a System.Random and returns a vector with a single sample from the distribution. There are also FillSample and FillSamples methods, which copy the sample(s) into the vector or matrix that is supplied as an additional parameter. The example below shows how to generate random samples using each of these techniques:

C#
Pcg32 random = new Pcg32();
var sample = dirichlet1.Sample(random);
var samples = dirichlet1.Sample(random, 10000);
dirichlet1.FillSample(random, sample);
dirichlet1.FillSamples(random, samples);