33

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.

gung - Reinstate Monica
  • 132,789
  • 81
  • 357
  • 650
user1315305
  • 1,199
  • 4
  • 14
  • 15

2 Answers2

35

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.

Glen_b
  • 257,508
  • 32
  • 553
  • 939
  • 3
    Implementation of function for random generation from Dirichlet can be fund also in https://cran.r-project.org/web/packages/extraDistr/ – Tim Jun 25 '16 at 18:21
5

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.

Arnaud Mégret
  • 546
  • 4
  • 13
  • 1
    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. – Eli Korvigo Jul 11 '19 at 22:41