34
$\begingroup$

Let's say we have a Dirichlet distribution with $K$-dimensional vector parameter $\vec\alpha = [\alpha_1, \alpha_2,...,\alpha_K]$. How can I draw a sample (a $K$-dimensional vector) from this distribution? I need a (possibly) simple explanation.

$\endgroup$

2 Answers 2

36
$\begingroup$

First, draw $K$ independent random samples $y_1, \ldots, y_K$ from Gamma distributions each with density

$$ \textrm{Gamma}(\alpha_i, 1) = \frac{y_i^{\alpha_i-1} \; e^{-y_i}}{\Gamma (\alpha_i)},$$

and then set

$$x_i = \frac{y_i}{\sum_{j=1}^K y_j}. $$

Now, $x_1,...,x_K$ will follow a Dirichlet distribution

The Wikipedia page on the Dirichlet distribution tells you exactly how to sample from the Dirichlet distribution.

Also, in the R library MCMCpack there is a function for sampling random variables from the Dirichlet distribution.

$\endgroup$
1
5
$\begingroup$

A simple method (while not exact) consists in using the fact that drawing a Dirichlet distribution is equivalent to the Polya's urn experiment. (Drawing from a set of colored balls and each time you draw a ball, you put it back in the urn with a second ball of the same color)

Consider your Dirichlet parameters $\alpha_i$ as an unormalized distribution over i.

Then :

repeat N times

--> draw an i using the $\alpha_i$ multinomial distribution

--> add 1 to $\alpha_i$

end repeat

Normalize $\alpha$ to get your distribution

If I am not wrong, that method is asymptotically exact. But since N is finite, you will NEVER draw some distributions with very small prior probabilities (while you should draw them with a very small frequency). I guess it might be satisfying in most cases with N = K.10.

$\endgroup$
1
  • 3
    $\begingroup$ I suspect this is the way np.random.dirichlet is implemented, because it does generate exact zeros in sampled probability vectors, though such vectors do not belong to any Dirichlet support. This is what got me here. $\endgroup$ Jul 11, 2019 at 22:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.