Gaussian Mixture Models
1 Introduction
In this section, we introduce Gaussian Mixture Models, or GMMs. Like K-means, a GMM is an unsupervised method that can be used to find clusters in unlabelled data. Unlike K-means, which makes implicit assumptions about the data but does not explicitly model the likelihood of data or its method of generation, a GMM is a generative model that assumes data was generated from a collection of Gaussians in the data space.
2 Soft K-Means
The regular K-means algorithm makes hard assignments, which means that each point belongs to exactly one cluster. However, real data may not form clean clusters. Two clusters may overlap, or a point may partially belong to multiple clusters. This occurs in real life. For example, if we cluster people by ethnicity, a person may feel that they belong to multiple clusters.
One way to address this limitation is to modify the K-means algorithm to make soft assignments, which allow each point to belong to multiple clusters, each to some degree. Let’s look at one example of such an algorithm, called Soft K-means.
In Soft K-means, each cluster has a responsibility for each point, which is a number between 0 and 1. We write \(r_k^{(i)}\) for the responsibility of cluster \(k\) for point \(i\). The higher the responsibility, the more strongly the point belongs to that cluster. For each point \(i\), the responsibilities of the \(K\) clusters form a vector \({\bf r}^{(i)}\) whose entries sum to one.
For example, suppose that \(K = 3\) and point \(i\) lies between clusters 1 and 2, far from cluster 3. K-means must assign the point to exactly one cluster, so its assignment vector is one-hot.
\[ \begin{aligned} & {\bf r}^{(i)} = \begin{bmatrix} 1 & 0 & 0 \end{bmatrix}^\top \end{aligned} \]
Soft K-means can instead share the point among the clusters, giving most of it to the two nearby clusters.
\[ \begin{aligned} & {\bf r}^{(i)} = \begin{bmatrix} 0.5 & 0.3 & 0.2 \end{bmatrix}^\top \end{aligned} \]
Here, the point belongs to cluster 1 with weight 0.5, to cluster 2 with weight 0.3, and to cluster 3 with weight 0.2.
The Soft K-means algorithm has the same structure as K-means. It alternates between an assignment step, which now computes soft assignments, and a refitting step.
Definition: The Soft K-means algorithm runs the following steps on a dataset of \(N\) points with \(K\) clusters.
- Initialize the \(K\) centroids \(\mathbf{m}_1, \ldots, \mathbf{m}_K\) randomly.
- Repeat until convergence. We measure convergence by how much the value of the objective function changes from one iteration to the next.
Assignment step: Update the responsibilities for each point. For each point \(i\) and each cluster \(k\), we set the responsibility as follows.
\[ \begin{aligned} & r_k^{(i)} = \frac{\exp\left(-\beta \| \mathbf{m}_k - \mathbf{x}^{(i)} \|^2\right)}{\sum_{j=1}^K \exp\left(-\beta \| \mathbf{m}_j - \mathbf{x}^{(i)} \|^2\right)} \end{aligned} \]
Refitting step: Update each centroid to be the weighted average of all the points, where each point is weighted by the cluster’s responsibility for it. For each cluster \(k\), we set the centroid as follows.
\[ \begin{aligned} & \mathbf{m}_k = \frac{\sum_{i=1}^N r_k^{(i)} \mathbf{x}^{(i)}}{\sum_{i=1}^N r_k^{(i)}} \end{aligned} \]
Each step of Soft K-means is a softer version of the corresponding step in K-means.
In the assignment step, K-means gives the whole point to the closest centroid. The closest cluster gets a responsibility of 1, and every other cluster gets 0. Soft K-means replaces this all-or-nothing choice with the softmax function (introduced in Chapter 4). Softmax starts with a score for each cluster. Here, the score is the negative squared distance from the point to the centroid, scaled by \(\beta\). Softmax exponentiates each score, which makes it positive. It then divides by the sum over all clusters, which makes the responsibilities sum to one. A closer centroid has a higher score, so it gets a larger responsibility. Still, every cluster gets some share of the point.
The hyperparameter \(\beta\) controls how soft the assignments are. When \(\beta\) is small, the scores are close together, so the responsibilities are spread out across the clusters. As \(\beta\) grows, the closest centroid’s score dominates the others. In the limit as \(\beta \to \infty\), each \({\bf r}^{(i)}\) becomes one-hot, and we recover K-means.
In the refitting step, both algorithms compute the same weighted average of the points. The only difference is that the weights can now be any value between 0 and 1, not just 0 or 1.
Now that we have two versions of K-means, we want to take care to tell them apart. We will follow the convention of referring to the original K-means algorithm as plain “K-means” or “Hard K-means” and the version above as Soft K-means.
Soft K-means still leaves some questions open. First, it is unclear how to choose \(\beta\). We cannot tune it by checking predictions against labels, because the data has no labels.
Second, what if the clusters contain very different numbers of points, or have different shapes? Suppose one cluster has 900 points and another has 100. A point halfway between their centroids is much more likely to come from the larger cluster. However, Soft K-means looks only at distances, so it gives the point the same responsibility for both clusters. Similarly, if a cluster is stretched out in one direction, Soft K-means still measures distance the same way in every direction. So it is unclear how to use Soft K-means to model such clusters.
These questions are not easy to answer within Soft K-means. Instead, we will reformulate Soft K-means as a generative model, called a GMM.
3 Gaussian Mixture Models
3.1 The GMM as a Generative Model
Let’s look at the leaf data again and think about what process could have generated it. Figure 1 below shows the unlabelled leaf data from the previous section. We can roughly see three modes in the data, which are the regions where the points are most concentrated. Around each mode, the points are packed densely near the centre and spread out more thinly farther away.
Unlabelled Leaf Data
The points around each mode form a cluster. It is natural to model each cluster with a multivariate Gaussian, introduced in the GDA chapter. A Gaussian follows the same pattern, since points are most likely to appear near its centre and less likely the farther out we go. Modelling the data points using multiple Gaussians, one for each cluster, is called a Gaussian Mixture Model (GMM).
A GMM describes how the data points could have been generated. Let \(z^{(i)}\) denote the cluster chosen for point \(i\). We generate each point in two steps.
Choose a cluster \(z^{(i)}\), where cluster \(k\) is chosen with probability \(\pi_k\). \[ \begin{aligned} & z^{(i)} \sim \textrm{Categorical}(\boldsymbol\pi) \\ & \sum_{k=1}^K \pi_k = 1\quad \text{ and }\quad \pi_k\geq 0 \quad \forall k \end{aligned} \]
Sample the point \({\bf x}^{(i)}\) from the Gaussian of the chosen cluster. \[ \begin{aligned} & {\bf x}^{(i)} \sim \mathcal{N}(\boldsymbol\mu_{z^{(i)}}, \boldsymbol\Sigma_{z^{(i)}}) \end{aligned} \]
Each cluster \(k\) has three parameters, and each one has a clear role. The mixing coefficient \(\pi_k\) is the prior probability that a point comes from cluster \(k\), before we look at the point itself. Since they are probabilities, the mixing coefficients are non-negative and sum to one. A cluster with more points has a larger mixing coefficient, so a GMM can handle clusters with very different numbers of points. The mean \(\boldsymbol\mu_k\) is the centre of the cluster, just like a centroid in K-means. The covariance matrix \(\boldsymbol\Sigma_k\) describes the shape of the cluster. It allows a cluster to be stretched out in some directions and narrow in others, so a GMM can handle clusters with different shapes. The number of clusters \(K\) is a hyperparameter, just like in K-means.
The two steps above also tell us the density of the data points, which is called the mixture distribution. To compute it, we start from the probability of choosing each cluster and the distribution of a point given its cluster.
\[ \begin{aligned} & P(z = k) = \pi_k \\ & p({\bf x} \mid z = k) = \mathcal{N}({\bf x} \,;\, \boldsymbol\mu_k, \boldsymbol\Sigma_k) \end{aligned} \]
In practice, we see only the point \({\bf x}\), not the cluster it came from. To get the mixture distribution, we sum over all \(K\) clusters.
\[ \begin{aligned} p({\bf x}) & = \sum_{k=1}^K P(z = k) \, p({\bf x} \mid z = k) \\ & = \sum_{k=1}^K \pi_k \, \mathcal{N}({\bf x} \,;\, \boldsymbol\mu_k, \boldsymbol\Sigma_k) \end{aligned} \]
The Gaussian Mixture Model gets its name because this density is a weighted sum of Gaussians.
Definition: A Gaussian Mixture Model (GMM) with \(K\) components models the density of a point \({\bf x}\) as a weighted sum of \(K\) Gaussians.
\[ \begin{aligned} & p({\bf x}) = \sum_{k=1}^K \pi_k \, \mathcal{N}({\bf x} \,;\, \boldsymbol\mu_k, \boldsymbol\Sigma_k) \end{aligned} \]
The mixing coefficients \(\pi_k\) are non-negative and sum to one, and \(\boldsymbol\mu_k\) and \(\boldsymbol\Sigma_k\) are the mean and covariance of Gaussian \(k\).
3.2 GMMs on the Leaf Data
Now that we have defined a GMM, let’s see how it clusters the leaf datasets from the previous section, compared with K-means. We will see how to fit a GMM later on this page. In the figures below, the GMM is on the left and K-means is on the right. Each Gaussian is shown by its centre (outlined in black) and coloured contours at 0.5 and 1 standard deviations.
Figure 2 shows the two methods on the full leaf dataset. The two clusterings mostly agree, but they assign a few points differently. The green triangle cluster along the bottom is stretched out horizontally. The GMM captures this shape with a wide, flat Gaussian, so it keeps the points at the left end of this cluster in the green cluster. K-means measures distance the same way in every direction, so it assigns these points to the brown circle cluster, whose centroid is closer.
Leaf Data - 3 Cluster GMM (left) and K-means (right)
The difference is even clearer on the troublesome dataset for K-means from the previous section, shown in Figure 3. This dataset has a round cluster and a long, flat cluster below it. K-means assigns the four points at the left end of the flat cluster to the round cluster, because they are closer to its centroid. The GMM fits a round Gaussian to the round cluster and a wide, flat Gaussian to the flat cluster, so it captures both clusters correctly.
A Troublesome Dataset for K-means, but not for a GMM
In general, a GMM is better able to fit non-circular clusters than K-means. In fact, GMMs are universal approximators of density, which is to say that a GMM can approximate any data-generating function to an arbitrary level of precision.
3.3 Generating New Data from a GMM
Since a GMM describes how data is generated, we can use a GMM with known parameters to generate new data. For each new point, we follow the two steps above. We choose a cluster with probability given by the mixing coefficients, and then sample the point from that cluster’s Gaussian. The point is most likely to land near the Gaussian’s centre, and less likely the farther it is from the centre. Generating data this way is useful for creating synthetic datasets. For example, since we know which Gaussian generated each point, we can use the synthetic data to test how well a clustering algorithm recovers the true clusters. It also helps us build intuition about how a GMM works.
In the figure below, we have copied the GMM from Figure 2 above. Try generating some new data, and observe how it has the same shape as the data in the original figure, but is not actually made up of the same individual data points.
Generating Data from a GMM
3.4 The GMM Objective
In the figure above, you may have noticed that if you generated enough points, the clusters started to overlap. Since we generated the data ourselves, we knew which Gaussian each point came from. But if you removed the colours and shapes, would you be able to tell? Can you say which Gaussian generated each of the following points?
3 Clusters - Clustering A
Point A was most likely generated by the brown circle Gaussian, since it is close to that Gaussian’s centre and far from the others. On the other hand, Point B seems about equally likely to have come from the brown circle or the orange square Gaussian. If these points were part of an unlabelled dataset, we could never know for certain which Gaussian generated them. Strictly speaking, real data comes from some natural process that is probably not Gaussian, and we only approximate it with Gaussians. So, we say that the cluster assignments \(z^{(i)}\) are latent variables, meaning that we cannot observe them.
Definition: A latent variable is a variable in a model that is never observed in the data. In a GMM, the cluster assignment \(z^{(i)}\) of each point is a latent variable, since we see only the point \({\bf x}^{(i)}\) and not the Gaussian that generated it.
So far, we have assumed that we know the parameters of the GMM. In practice, we are given only an unlabelled dataset of \(N\) points, and we need to learn the parameters from it.
\[ \begin{aligned} & {\bf X}= \{{\bf x}^{(i)}\}_{i=1}^{N} \end{aligned} \]
If we knew the cluster assignment of each data point, then we could fit each Gaussian separately to the points in its cluster. We discussed how to find the parameters of a Gaussian in the GDA chapter. However, because the cluster assignments are latent, we are forced to consider the contribution of each Gaussian to the probability of every data point. This is why the mixture distribution sums over all Gaussians:
\[ \begin{aligned} & p({\bf x}) = \sum_{k=1}^K \pi_k \, \mathcal{N}({\bf x} \,;\, \boldsymbol\mu_k,\boldsymbol\Sigma_k) \end{aligned} \]
We will learn the parameters using maximum likelihood. The likelihood of the dataset is the joint probability of all \(N\) points. Assuming the points are generated independently, this is the product of the likelihoods of the individual points:
\[ \begin{aligned} p({\bf X}) & = p({\bf x}^{(1)}, {\bf x}^{(2)}, \dots, {\bf x}^{(N)}) \\ & = \prod_{i=1}^{N} p({\bf x}^{(i)}) \\ & = \prod_{i=1}^{N}\sum_{k=1}^K \pi_k \, \mathcal{N}({\bf x}^{(i)} \,;\, \boldsymbol\mu_k,\boldsymbol\Sigma_k) \end{aligned} \]
To find parameters that fit the data well, we want this likelihood to be as large as possible. That is, we want to find the mixing coefficients \(\pi_k\), means \(\boldsymbol\mu_k\), and covariance matrices \(\boldsymbol\Sigma_k\) that maximize \(p({\bf X})\). To simplify this problem, we use our trick of taking the log-likelihood:
\[ \begin{aligned} & \log p({\bf X}) = \sum_{i=1}^N \log \left( \sum_{k=1}^K \pi_k \, \mathcal{N}({\bf x}^{(i)} \,;\, \boldsymbol\mu_k,\boldsymbol\Sigma_k)\right) \end{aligned} \]
…and we are stuck. When we take partial derivatives of this function, instead of getting neatly separated equations for each parameter, we find that the parameters all depend on each other. In general, there are no closed-form solutions to this problem.
While gradient descent is technically possible, it has a number of issues that we state here without proof. The objective is non-convex (due to permutation symmetry). We need to enforce a non-negativity constraint on \(\pi_k\), and we need the covariance matrices \(\boldsymbol\Sigma_k\) to be positive semi-definite. Finally, derivatives with respect to \(\boldsymbol\Sigma_k\) are expensive and complicated.
Fortunately, as with K-means, there is an algorithm we can use that alternates between two simpler problems.
4 Expectation-Maximization
As we saw above, if we knew the latent cluster assignments, we could fit the parameters \(\pi_k\), \(\boldsymbol\mu_k\), and \(\boldsymbol\Sigma_k\) using maximum likelihood, as in the GDA chapter. We refer to these parameters collectively as \(\theta\). Conversely, if we knew \(\theta\), we could compute the probability that each point came from each cluster using Bayes’ Rule, as we did for inference in GDA.
\[ \begin{aligned} & P(z=k \mid {\bf x}) = \frac{P(z=k) \, p({\bf x} \mid z=k)}{\sum_{\ell=1}^K P(z=\ell) \, p({\bf x} \mid z=\ell)} \quad \text{for } k = 1, \dots, K \end{aligned} \]
Instead of picking the most likely cluster for each point, we use these probabilities directly as soft responsibilities, just as we did for Soft K-means.
We now face the same situation as in K-means, and a similar solution works. If we fix \(\theta\), we can compute the responsibilities for each data point. If we then fix the responsibilities, we can compute better values for \(\theta\). Alternating between these two steps gradually improves our clustering via block coordinate descent. This algorithm is called Expectation-Maximization (EM).
4.1 The Expectation Step
The Expectation Step, so named because we are taking the expectation of the latent cluster assignments, fixes the parameter values \(\theta\) and produces new responsibilities \(r_k^{(i)}\) for each data point:
\[ \begin{aligned} r_k^{(i)} & = P(z^{(i)}=k \mid {\bf x}^{(i)} \,;\, \theta) \quad \text{for } k = 1, \dots, K \\[0.5em] & = \frac{P(z^{(i)}=k) \, p({\bf x}^{(i)} \mid z^{(i)}=k)}{\sum_{\ell=1}^K P(z^{(i)}=\ell) \, p({\bf x}^{(i)} \mid z^{(i)}=\ell)} \\[0.5em] & = \frac{\pi_k \, \mathcal{N}({\bf x}^{(i)} \,;\, \boldsymbol\mu_k,\boldsymbol\Sigma_k)}{\sum_{\ell=1}^K \pi_\ell \, \mathcal{N}({\bf x}^{(i)} \,;\, \boldsymbol\mu_\ell,\boldsymbol\Sigma_\ell)} \end{aligned} \]
In Soft K-means, a responsibility was a weight that measured how strongly a point belongs to a cluster. In a GMM, the responsibility \(r_k^{(i)}\) is a probability. It is the probability that point \(i\) was generated by Gaussian \(k\), given the point and the current parameters.
Question: For a practice problem that is easy to do by hand, let’s consider some one-dimensional data. Suppose we have the following measurements for the temperature in Toronto this month: \[ \begin{aligned} & x^{(1)} = 10,\ x^{(2)} = 12,\ x^{(3)} = 5,\ x^{(4)} = 4,\ x^{(5)} = 9 \end{aligned} \] We wish to fit a mixture of \(K=2\) Gaussians to this dataset. Suppose we initialize \(\mu_1 = 2\), \(\mu_2 = 7\), \(\sigma_1 = \sigma_2 = 1\), and \(\pi_1 = \pi_2 = 0.5\). What is the value of the responsibility vector \({\bf r}^{(1)}\) for the first example? Recall that the probability density function of the univariate Gaussian is \[ \begin{aligned} & p(x) = \mathcal{N}(x \,;\, \mu, \sigma) = \frac{1}{\sigma \sqrt{2 \pi}} e^{-\frac{1}{2} \left( \frac{x - \mu}{\sigma} \right)^2} \end{aligned} \]
Answer:
To get started, use Bayes’ Rule: \[ \begin{aligned} & r_k^{(1)} = P(z^{(1)}=k \mid x^{(1)} \,;\, \theta) \\ & = \frac{p(x^{(1)} \mid z^{(1)}=k)\, \pi_k}{\sum_{\ell=1}^2 p(x^{(1)} \mid z^{(1)}=\ell)\, \pi_\ell} \end{aligned} \]
Now evaluate the numerator for each of the two Gaussians: \[ \begin{aligned} & p(x^{(1)}=10 \mid z^{(1)}=1)\, \pi_1 = \mathcal{N}(10 \,;\, \mu=2, \sigma=1)\, \pi_1 \approx 2.53 \times 10^{-15} \\ & p(x^{(1)}=10 \mid z^{(1)}=2)\, \pi_2 = \mathcal{N}(10 \,;\, \mu=7, \sigma=1)\, \pi_2 \approx 0.00222 \end{aligned} \]
Since the second term is much, much larger than the first, \(r_1^{(1)} \approx 0\) and \(r_2^{(1)} \approx 1\). So the responsibility vector is \[ \begin{aligned} & {\bf r}^{(1)} \approx \begin{bmatrix}0 & 1\end{bmatrix}^\top \end{aligned} \]
4.2 The Maximization Step
In the Maximization Step, we fix the responsibilities for each data point and optimize the parameters \(\theta\) to maximize the data likelihood. To do this, we carry over the equations for GDA found by maximum likelihood in the last chapter, and modify them slightly to account for soft responsibilities. (Instead of each point counting as “1 point” in exactly one class, each point counts as a fraction of a point for each cluster, given by that cluster’s responsibility for it.)
\[ \begin{aligned} \pi_k & = \frac{1}{N} \sum_{i=1}^N r_k^{(i)} \\[0.5em] \boldsymbol\mu_k & = \frac{\sum_{i=1}^N r_k^{(i)}\cdot {\bf x}^{(i)}}{\sum_{i=1}^N r_k^{(i)}} \\[0.5em] \boldsymbol\Sigma_k & = \frac{1}{\sum_{i=1}^N r_k^{(i)}} \sum_{i=1}^N r_k^{(i)} ({\bf x}^{(i)} -\boldsymbol\mu_k)({\bf x}^{(i)} -\boldsymbol\mu_k)^\top \end{aligned} \]
Question: Continuing the temperature example above, suppose that after an E-step we have \[ \begin{aligned} {\bf r}^{(1)} & \approx \begin{bmatrix}0 & 1\end{bmatrix}^\top \\[0.5em] {\bf r}^{(2)} & \approx \begin{bmatrix}0 & 1\end{bmatrix}^\top \\[0.5em] {\bf r}^{(3)} & \approx \begin{bmatrix}0.08 & 0.92\end{bmatrix}^\top \\[0.5em] {\bf r}^{(4)} & \approx \begin{bmatrix}0.92 & 0.08\end{bmatrix}^\top \\[0.5em] {\bf r}^{(5)} & \approx \begin{bmatrix}0 & 1\end{bmatrix}^\top \end{aligned} \] Re-estimate \(\mu_1\), \(\mu_2\), \(\sigma_1\), \(\sigma_2\), \(\pi_1\), and \(\pi_2\).
Answer:
Apply the M-step updates with \(N=5\): \[ \begin{aligned} \pi_1 & = \frac{1}{5} \sum_{i=1}^5 r_1^{(i)} = \frac{1}{5}(0 + 0 + 0.08 + 0.92 + 0) = 0.2 \\[0.5em] \pi_2 & = \frac{1}{5} \sum_{i=1}^5 r_2^{(i)} = 1 - \pi_1 = 0.8 \\[0.5em] \mu_1 & = \frac{\sum_{i=1}^5 r_1^{(i)}\cdot x^{(i)}}{\sum_{i=1}^5 r_1^{(i)}} = \frac{0.08 \cdot 5 + 0.92 \cdot 4}{0.08 + 0.92} = 4.08 \\[0.5em] \mu_2 & = \frac{\sum_{i=1}^5 r_2^{(i)}\cdot x^{(i)}}{\sum_{i=1}^5 r_2^{(i)}} = \frac{10 + 12 + 0.92 \cdot 5 + 0.08 \cdot 4 + 9}{4} = 8.98 \\[0.5em] \sigma_1^2 & = \frac{1}{\sum_{i=1}^5 r_1^{(i)}} \sum_{i=1}^5 r_1^{(i)} (x^{(i)} - \mu_1)^2 \approx 0.074 \implies \sigma_1 \approx 0.27 \\[0.5em] \sigma_2^2 & = \frac{1}{\sum_{i=1}^5 r_2^{(i)}} \sum_{i=1}^5 r_2^{(i)} (x^{(i)} - \mu_2)^2 \approx 6.68 \implies \sigma_2 \approx 2.58 \end{aligned} \]
4.3 Expectation-Maximization - Putting It All Together
Let’s put together the details of the EM algorithm into the definition below.
Definition: The Expectation-Maximization (EM) algorithm for a GMM is as follows:
Initialize the values of \(\theta\).
Perform the Expectation Step, treating the parameters \(\theta\) as fixed. For each data point \(i\) and each Gaussian \(k\), compute the responsibility \[ \begin{aligned} r_k^{(i)} = \frac{\pi_k \, \mathcal{N}({\bf x}^{(i)} \,;\, \boldsymbol\mu_k,\boldsymbol\Sigma_k)}{\sum_{\ell=1}^K \pi_\ell \, \mathcal{N}({\bf x}^{(i)} \,;\, \boldsymbol\mu_\ell,\boldsymbol\Sigma_\ell)} \end{aligned} \]
Perform the Maximization Step, treating the responsibilities as fixed. For each Gaussian \(k\), update the parameters \[ \begin{aligned} \pi_k & = \frac{1}{N} \sum_{i=1}^N r_k^{(i)} \\[0.5em] \boldsymbol\mu_k & = \frac{\sum_{i=1}^N r_k^{(i)}\cdot {\bf x}^{(i)}}{\sum_{i=1}^N r_k^{(i)}} \\[0.5em] \boldsymbol\Sigma_k & = \frac{1}{\sum_{i=1}^N r_k^{(i)}} \sum_{i=1}^N r_k^{(i)} ({\bf x}^{(i)} -\boldsymbol\mu_k)({\bf x}^{(i)} -\boldsymbol\mu_k)^\top \end{aligned} \]
Repeat steps 2 and 3 until convergence. For a tolerance value \(\epsilon\), stop when \[ \begin{aligned} \left| r_k^{(i)} - \tilde{r}_k^{(i)} \right| \le \epsilon \quad \text{for all } i \text{ and } k \end{aligned} \] where \(\tilde{r}_k^{(i)}\) is the responsibility from the previous iteration.
There are two points worth discussing. First, the algorithm can be initialized in multiple ways. We can initialize the model parameters randomly or by spreading the initial clusters evenly throughout the data space. We could also initialize the responsibilities instead, which swaps the order of the E and M steps.
Second, the definition above uses a modified convergence criterion because the responsibilities are soft. In K-means, we stop when no assignment changes. In EM, the soft responsibilities may keep changing by tiny amounts indefinitely, so we stop when no responsibility changes by more than \(\epsilon\). There are other ways to decide when to stop, such as setting a limit on the number of iterations.
You will probably have noticed that the EM algorithm has many parallels with K-means. They are very closely related algorithms, where a GMM has the ability to fit more varied distributions at the cost of more complexity and computation.
Using the figures below, you can step through the EM algorithm with up to \(K=5\) for each of the two datasets we began this section with. You should see that, compared to K-means, the GMM can much more easily capture clusters that are not circular. And, because each Gaussian has its own covariance parameter, a GMM is also better able to capture clusters of different sizes. Note that, because we are maximizing log-likelihood rather than minimizing cost, we expect the line graphs to increase in value with each iteration, rather than decrease.
GMM, One Step at a Time
GMM, One Step at a Time, on a Tricky Dataset
5 Summary
In this section, we have introduced the GMM and the Expectation-Maximization algorithm for optimizing its parameters. More complex and more powerful than K-means, a GMM is better suited to capturing clusters of varying sizes and irregular (non-spherical) shapes.
Although clustering is a different kind of machine learning from the supervised learning topics we saw in previous chapters, we can make several connections back to the Fundamental Ideas of machine learning. We have seen how, via block coordinate descent, clustering tasks can be framed as optimization problems (Idea #1: Learning is Optimization). We have also seen that both K-means and GMMs are explicitly geometrical in nature, dealing with distances in the data space (Idea #4: ML Describes Geometric Processes), and that a GMM is a generative, probabilistic model (Idea #5: ML Demands a Probabilistic Lens).
In the next chapter, we will discuss a completely different kind of unsupervised learning: Principal Component Analysis (PCA). Like clustering, PCA gives us insight into the structure of unlabelled data, and in addition, it finally gives us a tool to fight back against the curse of dimensionality.