Skip to main content
Create your own

Implementing GMMs with EM

Hello! Let's dive into our final lesson in the "Unsupervised Learning and Representation" module.

Introduction

In our previous lessons, we explored dimensionality reduction with PCA and visualization with t-SNE. While t-SNE is fantastic for visually identifying potential clusters, it doesn't provide a formal model of the data's probability distribution. Today, we bridge that gap by exploring Gaussian Mixture Models (GMMs), a powerful probabilistic approach to clustering.

A GMM assumes that the data points are generated from a mixture of several Gaussian (normal) distributions, each with its own mean and covariance. This allows GMMs to perform "soft clustering," where each data point has a probability of belonging to each cluster, making it more flexible than "hard clustering" methods like K-Means, which assign each point to a single cluster.

To fit a GMM to the data, we'll use a cornerstone algorithm in machine learning called Expectation-Maximization (EM). The EM algorithm is an elegant iterative method for finding maximum likelihood estimates of parameters in statistical models that have latent, or hidden, variables.

By the end of this lesson, you will be able to implement a Gaussian Mixture Model using the Expectation-Maximization algorithm, solidifying your understanding of probabilistic modeling and latent variable optimization.

1. From Hard to Soft Clustering: The GMM Concept

Let's start by contrasting GMMs with a more familiar clustering algorithm, K-Means.

  • K-Means assigns each data point to the single closest cluster center. This is a hard assignment.
  • GMMs take a probabilistic view. They model the data as if it were generated from a mix of several Gaussian distributions. Instead of a hard assignment, a GMM provides a probability that a data point belongs to each of the component Gaussians. This is a soft assignment.

Imagine data that is not perfectly spherical or has varying densities. K-Means might struggle, but a GMM, by modeling clusters as ellipses (contours of Gaussian distributions) of different sizes and orientations, can capture the structure far more effectively.

Gaussian Mixture Models (GMM) Explained

To get a quick overview of this idea, watch the introductory part of this video from DataMListic. It provides a great high-level contrast between K-Means and GMMs.

Watch from the beginning to 01:56. Focus on the visual difference between the hard clustering of K-Means and the probabilistic approach of GMMs.

The GMM as a Generative Model

Formally, a GMM is a probability density function expressed as a weighted sum of Gaussian components:

Where:

  • is the number of clusters (Gaussian components).
  • are the mixing coefficients (the weights of each Gaussian), with . You can think of as the probability of picking the -th Gaussian.
  • is the probability density of point under the -th Gaussian component, which has its own mean and covariance matrix .

The core challenge is: if we observe a set of data points , how do we find the best parameters ? This is a maximum likelihood estimation problem. However, we have a "chicken-and-egg" problem because we don't know which Gaussian component generated each data point. This unknown component identity is a latent variable.

2. The Expectation-Maximization (EM) Algorithm

The EM algorithm is the iterative procedure we use to solve this problem of latent variables. It breaks the problem down into two repeating steps:

  1. Expectation (E-step): Given our current estimates of the model parameters (), we calculate the probability that each data point belongs to each cluster . This is the "soft assignment" or "responsibility" of each cluster for each data point.
  2. Maximization (M-step): Using these responsibilities as weights, we re-estimate the model parameters. We update the mean, covariance, and mixing coefficient for each cluster to better fit the data points that have been assigned to it.

We then repeat these E and M steps until the parameters converge.

Gaussian Mixture Model Flowchart
A flowchart illustrating the iterative process of the GMM algorithm using Expectation-Maximization.

Let's watch a more detailed explanation of how these steps work intuitively.

Lecture 14 - Expectation-Maximization Algorithms | Stanford CS229: Machine Learning (Autumn 2018)

In this segment of his Stanford lecture, Andrew Ng explains the EM algorithm for GMMs, framing it as a 'soft' version of K-Means. This analogy is incredibly helpful for building intuition.

Watch from 27:35 to 40:48. Focus on: The concept of w_ij (the responsibility) being a 'soft guess' for the latent variable z_i. How the E-step calculates these responsibilities using Bayes' rule. How the M-step uses these responsibilities as weights to update the parameters, mirroring the update steps in K-Means.

The iterative refinement process is beautifully illustrated by how the Gaussian components gradually move and reshape themselves to fit the data clusters.

Gaussian Mixture Model (GMM) Expectation-Maximization (EM) Algorithm Iterations
This visualization shows the EM algorithm in action. The three Gaussian components (ellipses) start at random initializations (Step 0) and iteratively adjust their position, shape, and orientation (Steps 1, 10) until they converge to fit the three distinct data clusters (Step 20).

3. The Mathematics of EM for GMMs

Now let's formalize the E and M steps with their update equations. Given your CS background and the course's goal of in-depth understanding, we'll first look at the what (the equations) and then the why (the derivation).

The Update Equations

Let's define as the "responsibility" that component takes for explaining data point .

E-Step: We calculate the responsibility for each point-cluster pair using our current parameters . This is essentially applying Bayes' theorem:

The numerator is the "likelihood" of the point under Gaussian scaled by the "prior" probability of that Gaussian. The denominator is the total probability of the point under the whole mixture model, which serves as a normalization constant.

M-Step: We use these responsibilities to calculate the new parameters . Let be the effective number of points assigned to cluster .

  1. Update mixing coefficients: The new weight for a cluster is the average responsibility it takes for all data points.

    where is the total number of data points.

  2. Update means: The new mean of a cluster is a weighted average of all data points, where the weights are the responsibilities.

  3. Update covariances: The new covariance matrix is a weighted average of the outer products of the centered data points.

Why does EM work? (The Derivation)

You might be asking: why does this two-step process guarantee that we are maximizing the log-likelihood of our data? It's a non-obvious and elegant result based on a principle called the Evidence Lower Bound (ELBO).

The derivation is mathematically involved, but the core idea is exactly what's needed for a deep understanding of many advanced AI models (like Variational Autoencoders, which we'll see later). Given your goals, this is a crucial concept.

Lecture 14 - Expectation-Maximization Algorithms | Stanford CS229: Machine Learning (Autumn 2018)

Let's return to Andrew Ng's lecture for the formal derivation. He explains how EM can be viewed as an algorithm that iteratively maximizes a lower bound of the log-likelihood function.

This is a challenging but rewarding section. Watch from 40:48 to 1:07:35 (the rest of the video covers related topics). Don't worry about memorizing every step, but focus on the narrative: The Goal: Maximize the log-likelihood, which is difficult because of the log of a sum. Jensen's Inequality: This mathematical tool is used to introduce a lower bound on the log-likelihood. The log function's concavity is key here. Constructing the Lower Bound (ELBO): By introducing an arbitrary distribution Q(z) over the latent variables, we create a tractable lower bound on our objective. E-Step (Tightening the Bound): We make the lower bound touch the actual log-likelihood at the current parameter values. This happens when we choose Q(z) to be the posterior distribution P(z|x, θ)—exactly what we calculate in the E-step! M-Step (Maximizing the Bound): We maximize this lower bound with respect to the parameters θ. Because the bound is tight, maximizing it guarantees that the true log-likelihood also increases.

This process of coordinate ascent—first fixing parameters to optimize the bound's shape (E-step), then fixing the bound's shape to optimize the parameters (M-step)—guarantees convergence to a local maximum of the log-likelihood.

Test your understanding!

In the derivation of the EM algorithm, Jensen's inequality is used to transform the tricky into a more manageable by establishing a lower bound. What specific condition must be met for this lower bound to be "tight," meaning it is exactly equal to the true log-likelihood at the current parameter settings? How does the E-step ensure this condition is met?

Show answer

For the lower bound to be tight, the inequality in Jensen's theorem ( for a concave function ) must become an equality. This happens only if the random variable inside the expectation is a constant.

In the context of the EM derivation, the random variable is . For this to be constant for all values of the latent variable , the distribution must be proportional to .

The E-step ensures this by setting to be exactly the posterior probability , which is defined as . Since is constant with respect to , this choice makes proportional to , satisfying the condition for a tight bound.

4. Implementation from Scratch

With a solid grasp of the theory, we can now translate the EM algorithm for GMMs into code. Given your proficiency in Python and interest in fundamentals, implementing this from scratch is the best way to solidify your knowledge.

We will use a clear, object-oriented approach. The implementation will mirror the update equations we've just discussed.

ML From Scratch, Part 5: Gaussian Mixture Models

The article 'ML From Scratch, Part 5: Gaussian Mixture Models' provides an excellent, clean Python implementation. We will use it as our guide.

Read the section "Implementation". Study the GMM class provided. Notice how the methods map directly to our concepts: initialize(): Sets up the initial random parameters. A good initialization strategy (picking random data points as initial means) is important to avoid poor local optima. e_step(): This calls predict_proba(), which implements the responsibility calculation (Equation 11 in the article). It then updates the mixing coefficients self.phi. m_step(): This implements the weighted updates for the means (self.mu) and covariance matrices (self.sigma) (Equations 13 and 14). fit(): The main loop that alternates between e_step() and m_step() for a fixed number of iterations.

This implementation gives you a fully functional GMM. You can see that once the mathematical steps are clear, the code becomes a direct translation. For a software engineer, it's also interesting to note how these operations can be vectorized using NumPy for efficiency, as shown in the em_gmm_vect function in the Duke University resource (LINK, section 6), which you can explore for further insight.

Conclusion

In this lesson, we have moved from simple clustering to a sophisticated probabilistic model. We dissected Gaussian Mixture Models and implemented the powerful Expectation-Maximization algorithm to fit them.

Key Takeaways:

  • GMMs provide a flexible, probabilistic "soft clustering" by modeling data as a mixture of multiple Gaussian distributions.
  • EM Algorithm is the key to training GMMs. It's an iterative two-step process for finding maximum likelihood estimates in models with latent variables.
  • The E-Step calculates the responsibilities—the probability of each data point belonging to each cluster, given the current model.
  • The M-Step updates the model parameters (means, covariances, mixing weights) using the responsibilities as weights.
  • The magic of EM lies in its guarantee to increase the data log-likelihood at each iteration by maximizing a tight lower bound (ELBO).

This lesson concludes our module on Unsupervised Learning. You have now worked with linear dimensionality reduction (PCA), non-linear visualization (t-SNE), and probabilistic clustering (GMMs), giving you a robust toolkit for exploring and understanding the structure of unlabeled data.

Preview of the next lesson:
We are now ready to pivot to the core of modern AI: Deep Learning. In our next module, "Deep Neural Network Fundamentals," we will start from the ground up by building our first neural network, the Multi-Layer Perceptron (MLP). You will learn how information flows through the network in forward propagation to make a prediction. This will be the first step in our journey toward understanding and building complex deep learning architectures.

Can't find a good explanation? Sign up and we'll make it for you

Sign up