Skip to content
BIASED

Dirichlet processes

A mixture whose number of clusters is inferred from the data rather than fixed in advance: the Dirichlet distribution and the limit $K \to \infty$, the Chinese restaurant process, the expected number of clusters $\approx \alpha \log(1 + n/\alpha)$, stick-breaking, the definition of a Dirichlet process, DP mixtures and collapsed Gibbs sampling.

A mixture with unknown $K$

The topic Gaussian processes put a prior on functions, and the model grew with the data: every new input added a new value to the prior. For clustering we need the same trick one level up, a prior over partitions of the data.

Recall the two-component Gaussian mixture of the topic EM and Bayesian PCA. EM fitted it well, but we had to choose K=2K = 2 before seeing the data. In practice one fits several KK and compares them by BIC or by evidence (the topic Bayesian model selection). Can the number of clusters instead be inferred together with everything else, and grow when more data arrive?

The first tool is the Dirichlet distribution, the multi-outcome version of the Beta. A vector π=(π1,…,πK)\pi = (\pi_1, \dots, \pi_K) on the simplex (πk≥0\pi_k \ge 0, ∑kπk=1\sum_k \pi_k = 1) has the density

Dir⁡(π∣a1,…,aK)=Γ(a0)∏kΓ(ak)∏k=1Kπkak−1,a0=∑kak.\operatorname{Dir}(\pi \mid a_1, \dots, a_K) = \frac{\Gamma(a_0)}{\prod_k \Gamma(a_k)} \prod_{k=1}^K \pi_k^{a_k - 1}, \qquad a_0 = \sum_k a_k .

Three facts are enough for this topic:

  • the mean is E[πk]=ak/a0\mathbb E[\pi_k] = a_k / a_0, and with K=2K = 2 the Dirichlet is exactly Beta⁡(a1,a2)\operatorname{Beta}(a_1, a_2);
  • aggregation: merging components adds their parameters, e.g. (π1+π2,π3)∼Dir⁡(a1+a2,a3)(\pi_1 + \pi_2, \pi_3) \sim \operatorname{Dir}(a_1 + a_2, a_3);
  • conjugacy with categorical labels: after counts n1,…,nKn_1, \dots, n_K the posterior is Dir⁡(a1+n1,…,aK+nK)\operatorname{Dir}(a_1 + n_1, \dots, a_K + n_K).

Now a finite Bayesian mixture with symmetric weights:

π∼Dir⁡(α/K,…,α/K),θk∼H,zi∣π∼Cat⁡(π),xi∣zi∼F(θzi).\pi \sim \operatorname{Dir}(\alpha/K, \dots, \alpha/K), \qquad \theta_k \sim H, \qquad z_i \mid \pi \sim \operatorname{Cat}(\pi), \qquad x_i \mid z_i \sim F(\theta_{z_i}).

Here HH is the base distribution of cluster parameters and FF the likelihood of one cluster; with FF Gaussian this is the mixture from EM with priors added. Integrating π\pi out gives the posterior predictive of the next label, the posterior mean of πk\pi_k:

p(zn+1=k∣z1,…,zn)=nk+α/Kn+α.p(z_{n+1} = k \mid z_1, \dots, z_n) = \frac{n_k + \alpha/K}{n + \alpha}.

With K=2K = 2 and α/K=1\alpha/K = 1 this is the coin from the topic The Bayesian approach: three heads give (3+1)/(3+2)=4/5(3 + 1)/(3 + 2) = 4/5.

Letting $K$ go to infinity

Let the number of components KK go to infinity while α\alpha stays fixed. Look at the predictive rule from the previous block.

An occupied cluster kk with nk>0n_k > 0 points gets

nk+α/Kn+α  ⟶  nkn+α.\frac{n_k + \alpha/K}{n + \alpha} \;\longrightarrow\; \frac{n_k}{n + \alpha}.

Any single empty cluster gets α/Kn+α→0\frac{\alpha/K}{n + \alpha} \to 0. But there are K−KnK - K_n empty clusters, where KnK_n is the number of occupied ones, and together they get

(K−Kn)⋅α/Kn+α  ⟶  αn+α.(K - K_n) \cdot \frac{\alpha/K}{n + \alpha} \;\longrightarrow\; \frac{\alpha}{n + \alpha}.

So in the limit the next point either joins one of the existing clusters or opens a new one, and KK has disappeared from the rule. The labels themselves lose meaning: "cluster number 7" is one of infinitely many interchangeable empty slots. What remains is which points share a cluster, a partition of the data.

The division by KK matters. With weights Dir⁡(1,…,1)\operatorname{Dir}(1, \dots, 1) the total parameter would be KK, the new-cluster probability would tend to 11, and every point would sit alone.

A word on α\alpha. In the topics Bayesian linear regression and The relevance vector machine the letter α\alpha was a prior precision, an inverse variance. Here it is something else: the concentration parameter, the total weight a0a_0 of the Dirichlet. It is not an inverse variance of anything. It works like a prior sample size: with n≪αn \ll \alpha new clusters open often, with n≫αn \gg \alpha rarely.

The Chinese restaurant process

The limiting rule has a name and a story, the Chinese restaurant process (CRP). A restaurant has infinitely many tables. Customers are data points, tables are clusters.

  • The first customer sits at the first table.
  • Customer n+1n + 1 sits at an occupied table kk with probability nkn+α\dfrac{n_k}{n + \alpha}, or at a new table with probability αn+α\dfrac{\alpha}{n + \alpha}.

Popular tables attract more customers: the rich-get-richer effect. A few large clusters and a tail of small ones is the typical picture.

The probability of a whole seating is a product of these choices. Take α=1\alpha = 1 and four customers who sit A, A, B, B:

1⋅11+1⋅12+1⋅13+1=124.1 \cdot \frac{1}{1 + 1} \cdot \frac{1}{2 + 1} \cdot \frac{1}{3 + 1} = \frac{1}{24}.

In general each table contributes α\alpha when it opens and then 1,2,…,nk−11, 2, \dots, n_k - 1 as it fills, while the denominators multiply to α(α+1)⋯(α+n−1)=Γ(α+n)/Γ(α)\alpha(\alpha + 1) \cdots (\alpha + n - 1) = \Gamma(\alpha + n)/\Gamma(\alpha). For KK tables with sizes n1,…,nKn_1, \dots, n_K:

p(z1,…,zn)=αK Γ(α)Γ(α+n)∏k=1K(nk−1)!.p(z_1, \dots, z_n) = \frac{\alpha^{K}\, \Gamma(\alpha)}{\Gamma(\alpha + n)} \prod_{k=1}^{K} (n_k - 1)! .

For A, A, B, B: 12⋅Γ(1)Γ(5)⋅1!⋅1!=124\frac{1^2 \cdot \Gamma(1)}{\Gamma(5)} \cdot 1! \cdot 1! = \frac{1}{24}, as before.

The formula depends only on the table sizes, not on the order of arrival. The CRP is exchangeable: shuffling the customers does not change the probability of a partition. Hence any customer can be treated as the last one to arrive, and that is exactly what a Gibbs sampler will need.

How many clusters

How many tables does the restaurant have after nn customers? Customer ii opens a new table with probability αα+i−1\frac{\alpha}{\alpha + i - 1}, whatever the earlier customers did. So the number of tables KnK_n is a sum of independent indicators, and

E[Kn]=∑i=1nαα+i−1  ≈  αlog⁡ ⁣(1+nα).\mathbb E[K_n] = \sum_{i=1}^{n} \frac{\alpha}{\alpha + i - 1} \;\approx\; \alpha \log\!\left(1 + \frac{n}{\alpha}\right).

The approximation replaces the sum by an integral; the gap stays below one. For α=1\alpha = 1:

nn exact E[Kn]\mathbb E[K_n] αlog⁡(1+n/α)\alpha \log(1 + n/\alpha)
1010 2.932.93 2.402.40
100100 5.195.19 4.624.62
10001000 7.497.49 6.916.91
10 00010\,000 9.799.79 9.219.21

A simulation of 5,000 restaurants with n=100n = 100 gives 5.245.24 tables on average.

The growth is logarithmic: doubling the data adds about αlog⁡2\alpha \log 2 clusters. With α=5\alpha = 5, going from n=1000n = 1000 to n=2000n = 2000 moves E[Kn]\mathbb E[K_n] from 27.0327.03 to 30.4930.49, an increase of 3.45≈5log⁡2=3.473.45 \approx 5 \log 2 = 3.47. Compare the Gaussian process, which keeps one value per data point.

In practice α\alpha is set by inverting this formula from a rough guess of the number of clusters, or given a Gamma prior and sampled together with the labels. It strongly affects the number of clusters and only weakly the predictions.

Stick-breaking

The CRP describes partitions; the mixing weights themselves have been integrated out. Stick-breaking builds them explicitly.

Take a stick of length one. Break off a random fraction v1v_1; that piece is π1\pi_1. From what is left, break off a fraction v2v_2; that is π2\pi_2. Continue forever:

vk∼Beta⁡(1,α),πk=vk∏j<k(1−vj),θk∼H,G=∑k=1∞πk δθk.v_k \sim \operatorname{Beta}(1, \alpha), \qquad \pi_k = v_k \prod_{j < k} (1 - v_j), \qquad \theta_k \sim H, \qquad G = \sum_{k=1}^{\infty} \pi_k\, \delta_{\theta_k}.

Here δθ\delta_\theta is a point mass at θ\theta, so GG is a random discrete distribution: countably many atoms θk\theta_k with weights πk\pi_k.

Why Beta⁡(1,α)\operatorname{Beta}(1, \alpha). Watch the first table in the CRP. After the first customer it has weight 11 and "everything else" has weight α\alpha; each new customer joins the first table with probability n1/(n+α)n_1/(n + \alpha) and otherwise adds to the rest. This is a Pólya urn, and the long-run share of the first table is Beta⁡(1,α)\operatorname{Beta}(1, \alpha). A simulation of 2,000 restaurants with α=2\alpha = 2 and 500 customers gives a mean share of 0.340.34 against 1/31/3.

Since E[vk]=11+α\mathbb E[v_k] = \frac{1}{1 + \alpha}, the first piece has E[π1]=11+α\mathbb E[\pi_1] = \frac{1}{1 + \alpha}. Small α\alpha means large first pieces and few clusters; large α\alpha means many small pieces. The fractions are independent, so the expected mass still unbroken after TT pieces is

E[∏k=1T(1−vk)]=(α1+α)T.\mathbb E\left[\prod_{k=1}^{T} (1 - v_k)\right] = \left(\frac{\alpha}{1 + \alpha}\right)^{T}.

This gives truncation: stop after TT pieces and give the last one the remainder. The error is bounded by the leftover mass. With α=1\alpha = 1 and T=10T = 10 the expected leftover is 2−10≈0.0012^{-10} \approx 0.001; with α=4\alpha = 4 it is still 0.810≈0.1070.8^{10} \approx 0.107. Truncated stick-breaking is what variational inference for DP mixtures uses, for example BayesianGaussianMixture in scikit-learn.

What a Dirichlet process is

The GG built by stick-breaking is a draw from a Dirichlet process. The formal definition says what its masses on sets look like. G∼DP⁡(α,H)G \sim \operatorname{DP}(\alpha, H) if for every partition A1,…,ArA_1, \dots, A_r of the parameter space

(G(A1),…,G(Ar))∼Dir⁡(αH(A1),…,αH(Ar)).\bigl(G(A_1), \dots, G(A_r)\bigr) \sim \operatorname{Dir}\bigl(\alpha H(A_1), \dots, \alpha H(A_r)\bigr).

The definitions for coarse and fine partitions agree thanks to the aggregation property. Taking the partition {A,Ac}\{A, A^c\} gives G(A)∼Beta⁡(αH(A),α(1−H(A)))G(A) \sim \operatorname{Beta}(\alpha H(A), \alpha (1 - H(A))), hence

E[G(A)]=H(A),Var⁡[G(A)]=H(A) (1−H(A))α+1.\mathbb E[G(A)] = H(A), \qquad \operatorname{Var}[G(A)] = \frac{H(A)\,(1 - H(A))}{\alpha + 1}.

So HH is the mean of the random distribution, and α\alpha says how tightly GG concentrates around it. With H(A)=0.3H(A) = 0.3, the standard deviation of G(A)G(A) is 0.2050.205 at α=4\alpha = 4 and 0.0460.046 at α=99\alpha = 99; a stick-breaking simulation agrees. Yet even with a smooth HH, every draw GG is discrete almost surely, so repeated draws from it coincide. Those coincidences are the clusters.

The analogy with the previous topic:

Gaussian process Dirichlet process
indexed by inputs xx sets AA
a draw is a function a distribution
finite marginals Gaussian Dirichlet
specified by mean mm, kernel kk base HH, concentration α\alpha

Posterior. Observe θ1,…,θn∼G\theta_1, \dots, \theta_n \sim G. Every partition is a Dirichlet–categorical update, so the posterior is again a Dirichlet process:

G∣θ1,…,θn  ∼  DP⁡ ⁣(α+n,  αH+∑i=1nδθiα+n).G \mid \theta_1, \dots, \theta_n \;\sim\; \operatorname{DP}\!\left(\alpha + n,\; \frac{\alpha H + \sum_{i=1}^n \delta_{\theta_i}}{\alpha + n}\right).

The new base mixes the prior HH with weight αα+n\frac{\alpha}{\alpha + n} and the observed values with weight nα+n\frac{n}{\alpha + n}. Its mean predicts θn+1\theta_{n+1}: a fresh draw from HH with probability αα+n\frac{\alpha}{\alpha + n}, otherwise a copy of one of the old θi\theta_i. That is the Chinese restaurant once more.

DP mixtures and collapsed Gibbs

A Dirichlet process mixture puts a DP under the cluster parameters:

G∼DP⁡(α,H),θi∣G∼G,xi∣θi∼F(θi).G \sim \operatorname{DP}(\alpha, H), \qquad \theta_i \mid G \sim G, \qquad x_i \mid \theta_i \sim F(\theta_i).

Because GG is discrete, many θi\theta_i coincide, and points with equal θi\theta_i form a cluster. Integrating GG out gives an equivalent model: labels from the CRP, θk∼H\theta_k \sim H for each table, xi∼F(θzi)x_i \sim F(\theta_{z_i}). This is the limit K→∞K \to \infty of the finite mixture from the first block, an infinite Gaussian mixture when FF is Gaussian.

Collapsed Gibbs sampling (see the topic Gibbs sampling and Metropolis–Hastings). With a conjugate HH the cluster parameters can be integrated out, and the sampler works on the labels only. By exchangeability, treat point xix_i as the last customer: remove it, then reseat it with

p(zi=k∣z−i,X)∝n−i,k  p(xi∣X−i,k),p(zi=new∣z−i,X)∝α  p(xi).p(z_i = k \mid z_{-i}, X) \propto n_{-i,k}\; p(x_i \mid X_{-i,k}), \qquad p(z_i = \text{new} \mid z_{-i}, X) \propto \alpha\; p(x_i).

The first factor is the CRP prior; n−i,kn_{-i,k} counts cluster kk without xix_i. The second is a posterior predictive: given the other points of cluster kk, or given nothing for a new cluster. If removing xix_i empties a cluster, that cluster disappears.

For 1-D Gaussian clusters with known variance σ2\sigma^2 and H=N(μ0,τ2)H = \mathcal N(\mu_0, \tau^2), the predictive is the Normal-mean update from the topic Conjugate priors and the exponential family. For a cluster with mm points and sum ss:

λ=1τ2+mσ2,μ^=μ0/τ2+s/σ2λ,p(x∣cluster)=N ⁣(x∣μ^,  1λ+σ2).\lambda = \frac{1}{\tau^2} + \frac{m}{\sigma^2}, \qquad \hat\mu = \frac{\mu_0/\tau^2 + s/\sigma^2}{\lambda}, \qquad p(x \mid \text{cluster}) = \mathcal N\!\left(x \mid \hat\mu,\; \frac{1}{\lambda} + \sigma^2\right).

A new cluster uses N(x∣μ0,τ2+σ2)\mathcal N(x \mid \mu_0, \tau^2 + \sigma^2). Example: σ2=1\sigma^2 = 1, τ2=4\tau^2 = 4, μ0=0\mu_0 = 0, α=1\alpha = 1; cluster A has 4 points with mean 00, cluster B has 2 points with mean 33; reseat x=2x = 2.

option CRP factor predictive density at 22 weight probability
A 44 N(0, 1.235)\mathcal N(0,\ 1.235) 0.07110.0711 0.2840.284 0.2920.292
B 22 N(2.667, 1.444)\mathcal N(2.667,\ 1.444) 0.28460.2846 0.5690.569 0.5850.585
new α=1\alpha = 1 N(0, 5)\mathcal N(0,\ 5) 0.11960.1196 0.1200.120 0.1230.123

B wins although it is half the size of A: the point is close to it. One sweep reseats every point in turn.

Where it breaks and what comes next

Three caveats are worth knowing before using DP mixtures in practice.

  • Slow mixing. Gibbs moves one point at a time. Merging two halves of a true cluster requires passing through improbable intermediate states, so a split cluster can stay split for many sweeps. Split–merge moves, which propose to move whole groups at once, help.
  • A rigid prior. One number α\alpha sets both the expected number of clusters and the rich-get-richer size pattern. Power-law cluster sizes call for the Pitman–Yor process, which adds a discount parameter and grows faster than log⁡n\log n.
  • Inconsistency for the number of clusters. Even when the data come from a finite mixture, the posterior of a DP mixture does not concentrate on the true number of components. Miller and Harrison showed this for data from one standard normal modelled by a DP mixture of unit-variance normals: the posterior probability of one cluster tends to zero. A small extra cluster costs little in the prior and changes the density almost nowhere. So DP mixtures are a good tool for density estimation and a poor one for counting clusters.

So far every data point had one cluster. A text document is different: it mixes several topics, and the same topics recur across documents. That is a mixture of mixtures, with a Dirichlet prior on each document's topic proportions. The next topic, Latent Dirichlet allocation, builds exactly this model with a fixed number of topics; its nonparametric version, the hierarchical Dirichlet process, lets all documents share the atoms of one DP.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.