MoonSay /Tutorials/Machine Learning Interview #3
Contents
  1. 01Introduction
  2. 02What clustering is
  3. 03K-means
  4. 04Choosing K, and where K-means fails
  5. 05Gaussian mixture models and the EM algorithm
  6. 06Dirichlet process mixtures
  7. 07DBSCAN
  8. 08Hierarchical clustering
  9. 09Spectral clustering
  10. 10Putting it all together
  11. 11Interview questions and model answers
  12. 12Summary
  13. References
Clustering Algorithms/Chapter 05 of 12

05Gaussian mixture models and the EM algorithm

5.1A cluster is a Gaussian blob

A Gaussian mixture model (GMM) answers the question differently: a cluster is a bell-shaped blob of probability, with its own centre \mu_k, shape \Sigma_k (a covariance matrix, drawn as an ellipse) and weight \pi_k. The data density is a weighted sum of K Gaussians,

p(x)=\sum_{k=1}^{K}\pi_k\,\mathcal{N}(x\mid\mu_k,\Sigma_k),\qquad \pi_k\ge0,\quad\sum_k\pi_k=1 . \tag{6}

It helps to read (6) as a recipe for generating data: first pick a cluster z\in\{1,\dots,K\} with probabilities \pi_1,\dots,\pi_K, then draw x from the Gaussian of that cluster. The cluster label z is a latent (hidden) variable: it was used to generate x but we do not observe it. Clustering means inferring it. By Bayes’ rule, the probability that point x_i came from cluster k is

\gamma_{ik}=P(z_i=k\mid x_i)=\frac{\pi_k\,\mathcal{N}(x_i\mid\mu_k,\Sigma_k)}{\sum_{j=1}^{K}\pi_j\,\mathcal{N}(x_i\mid\mu_j,\Sigma_j)} , \tag{7}

called the responsibility of cluster k for point i. Membership is soft: a customer can be 70\% saver and 30\% traveller. Think of a detective who splits the blame for each clue among the suspects in proportion to how well each one explains it, weighted by how likely each suspect was to begin with.

5.2The likelihood, and why it has no closed form

We fit the parameters \theta=\{\pi_k,\mu_k,\Sigma_k\} by maximum likelihood. The log-likelihood of n independent points is

\ln L(\theta)=\sum_{i=1}^{n}\ln\Big(\sum_{k=1}^{K}\pi_k\,\mathcal{N}(x_i\mid\mu_k,\Sigma_k)\Big). \tag{8}

The sum inside the logarithm is what makes this hard: for a single Gaussian the logarithm would cancel the exponential and give a quadratic with a closed-form maximum. Still, the conditions for a maximum have a telling form. Using \partial\ln\mathcal{N}(x\mid\mu,\Sigma)/\partial\mu=\Sigma^{-1}(x-\mu), the derivative of (8) with respect to \mu_k is

\sum_{i=1}^{n}\frac{\pi_k\mathcal{N}(x_i\mid\mu_k,\Sigma_k)}{\sum_j\pi_j\mathcal{N}(x_i\mid\mu_j,\Sigma_j)}\,\Sigma_k^{-1}(x_i-\mu_k)=\Sigma_k^{-1}\sum_{i=1}^{n}\gamma_{ik}(x_i-\mu_k),

so the responsibilities appear by themselves. Setting it to zero, and similarly for \Sigma_k and for \pi_k (with a Lagrange multiplier \lambda for \sum_k\pi_k=1, which turns out to be \lambda=-n), gives, with the effective cluster sizes N_k=\sum_i\gamma_{ik},

\mu_k=\frac{1}{N_k}\sum_{i=1}^{n}\gamma_{ik}x_i,\qquad \Sigma_k=\frac{1}{N_k}\sum_{i=1}^{n}\gamma_{ik}(x_i-\mu_k)(x_i-\mu_k)^\top,\qquad \pi_k=\frac{N_k}{n}. \tag{9}

These are the formulas for the mean, covariance and share of a cluster, with every point counted with weight \gamma_{ik} (Bishop, 2006, Section 9.2.2). If the labels were known (\gamma_{ik} equal to 0 or 1), they would be exactly the class means, covariances and priors of quadratic discriminant analysis, the supervised counterpart of a GMM (linear discriminant analysis if all classes share one covariance) (Hastie et al., 2009, Section 4.3). They are not a closed-form solution, because the \gamma_{ik} on the right depend on the parameters through (7). But they suggest an iteration.

5.3The EM algorithm

Start with some parameters and alternate:

  1. E step (expectation): compute the responsibilities (7) with the current parameters;
  2. M step (maximization): re-estimate the parameters with (9), holding the responsibilities fixed.

EM is soft K-means: the E step is a soft assignment, the M step a weighted update, and in addition every cluster learns its own shape and weight. Figure 4 shows the video’s run on three elongated clusters (standardized). The start has three round blobs of equal weight; after one iteration the ellipses already stretch along the data; after 20 iterations the three clusters are recovered exactly, and the log-likelihood has climbed from -1133.1 to -409.6, after which it changes by less than 0.01. The climb is not steady: from iteration 6 to 11 it creeps from -556 to -548 while one component still covers one left-hand cluster and part of the other, and only then does the configuration reorganize and \ln L rise quickly. Such slow stretches are typical of EM. After the first iteration many points are genuinely shared: one point has responsibilities (0.70,0.30,0), the video’s “seventy percent saver, thirty percent traveller”. At convergence the clusters are well separated, and even the most ambiguous point is 98\% in one cluster.

EM for a three-component Gaussian mixture on 330 standardized points. Ellipses show one and two standard deviations of each component; each point’s colour mixes the three cluster colours in proportion to its responsibilities. (a) Start: round components of equal weight. (b) After one iteration. (c) After ten. (d) Converged (20 iterations). The log-likelihood \ln L never decreases. Data: computed for this tutorial, with the same data and starting values as the companion video.
Figure 4. EM for a three-component Gaussian mixture on 330 standardized points. Ellipses show one and two standard deviations of each component; each point’s colour mixes the three cluster colours in proportion to its responsibilities. (a) Start: round components of equal weight. (b) After one iteration. (c) After ten. (d) Converged (20 iterations). The log-likelihood \ln L never decreases. Data: computed for this tutorial, with the same data and starting values as the companion video.
Try itWatch EM fit a Gaussian mixture

The video's EM run of Figure 4. Move through the iterations: each point's colour mixes the three cluster colours by its responsibilities \gamma_{ik}, and the ellipses show one and two standard deviations of each component. Switch to hard colours to see the K-means-style assignment \arg\max_k\gamma_{ik}.

Worked example 5. One EM iteration by hand in one dimension. Data x=(-2,-1,0,3,4); two components with \mu=(-1,3), variances (1,1) and weights (0.5,0.5). E step. For the point 0 the two densities are \mathcal{N}(0\mid-1,1)=0.24197 and \mathcal{N}(0\mid3,1)=0.00443, in the ratio e^{(9-1)/2}=e^4=54.6, so \gamma=(54.6/55.6,\;1/55.6)=(0.982,\,0.018). The responsibilities of component 1 for the five points are 1.0000, 0.9997, 0.9820, 0.0003 and 0.0000. M step. N_1=2.982 and N_2=2.018, so the weights become (0.596,\,0.404); the means become \mu_1=-1.006 and \mu_2=3.468 (the point 0 pulls component 2 slightly, with weight 0.018); the variances become 0.667 and 0.359. The log-likelihood rises from -9.542 to -8.527.

5.4Why EM never decreases the likelihood

The monotone increase seen in Figure 4 is a theorem, and its proof shows what EM really does. Take any probability distribution q_i(k) over the clusters for each point. Multiplying and dividing by q_i(k) inside (8) and using Jensen’s inequality (\ln is concave, so the log of an average is at least the average of the logs),

\ln L(\theta)=\sum_i\ln\sum_k q_i(k)\frac{\pi_k\mathcal{N}(x_i\mid\mu_k,\Sigma_k)}{q_i(k)}\;\ge\;\sum_i\sum_k q_i(k)\ln\frac{\pi_k\mathcal{N}(x_i\mid\mu_k,\Sigma_k)}{q_i(k)}=:\mathcal{L}(q,\theta). \tag{10}

The gap between the two sides is \sum_i\mathrm{KL}\big(q_i\,\|\,P(z_i\mid x_i,\theta)\big)\ge0, which is zero exactly when q_i(k)=\gamma_{ik} (Bishop, 2006, Section 9.4). So:

Theorem 3 (Dempster et al., 1977). Each EM iteration satisfies \ln L(\theta^{\mathrm{new}})\ge\ln L(\theta^{\mathrm{old}}).

The E step sets q_i(k)=\gamma_{ik} computed at \theta^{\mathrm{old}}, which makes the bound tight, \mathcal{L}(q,\theta^{\mathrm{old}})=\ln L(\theta^{\mathrm{old}}). The M step maximizes \mathcal{L}(q,\theta) over \theta with q fixed; for Gaussians this is a weighted maximum-likelihood problem whose solution is (9). Hence

\ln L(\theta^{\mathrm{new}})\;\ge\;\mathcal{L}(q,\theta^{\mathrm{new}})\;\ge\;\mathcal{L}(q,\theta^{\mathrm{old}})\;=\;\ln L(\theta^{\mathrm{old}}),

where the first inequality is (10).

EM is thus coordinate ascent on the lower bound \mathcal{L}, alternating between q and \theta, just as Lloyd’s algorithm is coordinate descent on J. Like Lloyd’s algorithm it finds a local optimum only, so it is usually started from a K-means solution and run from several starts.

5.5K-means is a special case

Proposition 1 (Bishop, 2006, Section 9.3.2). Fix all covariances to \Sigma_k=\epsilon I and let \epsilon\to0. Then the responsibilities become hard nearest-centre assignments, the M step for the means becomes the K-means update, and maximizing the expected complete-data log-likelihood becomes minimizing J.

With \Sigma_k=\epsilon I the normalizing constants cancel and (7) becomes

\gamma_{ik}=\frac{\pi_k\,e^{-\|x_i-\mu_k\|^2/2\epsilon}}{\sum_j\pi_j\,e^{-\|x_i-\mu_j\|^2/2\epsilon}} .

Divide numerator and denominator by the term of the nearest centre j^\ast. Every other term is multiplied by \exp\!\big(-(\|x_i-\mu_j\|^2-\|x_i-\mu_{j^\ast}\|^2)/2\epsilon\big), which tends to 0, so \gamma_{ij^\ast}\to1 and the others tend to 0, whatever the (nonzero) weights. The mean update (9) then averages the points assigned to each centre. Finally, \sum_{i,k}\gamma_{ik}\ln\mathcal{N}(x_i\mid\mu_k,\epsilon I)=-\frac{1}{2\epsilon}\sum_{i,k}\gamma_{ik}\|x_i-\mu_k\|^2+\text{const}\to-\frac{1}{2\epsilon}J+\text{const}.

For a point at distance 1 from one centre and 1.5 from another, with equal weights, the responsibility of the nearer centre is 0.651 for \epsilon=1, 0.924 for \epsilon=0.25 and more than 0.9999 for \epsilon=0.05: the soft assignment hardens as the blobs shrink. In other words, \gamma_{i\cdot} is a softmax of the scores \ln\pi_k-\|x_i-\mu_k\|^2/2\epsilon, with \epsilon acting as a temperature, and as the temperature goes to zero the softmax becomes an arg max. K-means is therefore a GMM with tiny, equal, round blobs and hard assignments, which is exactly why it prefers round clusters of similar size.

Practical notes. The likelihood (8) is unbounded: a component can shrink onto a single data point, \mu_k=x_i and \Sigma_k\to0, and drive \ln L to +\infty (Bishop, 2006, Section 9.2.1). Implementations add a small constant to the diagonal of every \Sigma_k (the video’s code adds 10^{-4}). Restricting the covariances (spherical, diagonal or shared) reduces the number of parameters, and K is usually chosen with BIC.

Pause and think. A GMM always fits the data at least as well as K-means in likelihood. Does that make it always the better clustering method?

Show the answer

No. It has many more parameters (K\,d(d+3)/2+K-1 against Kd), so it needs more data, is slower and is more prone to poor local optima and degenerate components; and it still assumes elliptical clusters, so it fails on the moons as well. When clusters really are round and similar, K-means is faster and just as good.