Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 49

Deriving EM for Gaussian Mixture Models

Recommended resources

Hello! Welcome back to our course on the foundations of data science.

In our last lesson, we explored the geometry of K-means clustering, understanding it as a process that partitions the feature space into a Voronoi tessellation. We saw that this results in "hard" assignments, where each data point belongs to exactly one convex cluster. This works well for cleanly separated, spherical data, but what about more complex, overlapping clusters?

Today, we transition to a more flexible, probabilistic approach: the Gaussian Mixture Model (GMM). Instead of assigning points to clusters definitively, a GMM provides a "soft" assignment, calculating the probability that a data point belongs to each of several Gaussian distributions. The engine that powers the fitting of these models is the Expectation-Maximization (EM) algorithm.

Our learning objective for this lesson aligns perfectly with your interest in mathematical rigor: we will derive the Expectation (E) and Maximization (M) steps for a Gaussian Mixture Model.

1. The Gaussian Mixture Model and the Challenge of MLE

A Gaussian Mixture Model assumes that the data is generated from a weighted sum of different Gaussian distributions. The probability density function of a data point is given by:

Here, the parameters are:

  • : The mixing coefficients, which are the prior probabilities of a data point coming from the -th Gaussian. They must satisfy and .
  • : The mean vector of the -th Gaussian.
  • : The covariance matrix of the -th Gaussian.

Our goal is to find the parameters that best fit the observed data . The natural way to do this is through Maximum Likelihood Estimation (MLE). The log-likelihood function is:

If you were to try and maximize this by taking derivatives with respect to the parameters and setting them to zero, you would quickly run into a problem. The presence of the sum inside the logarithm prevents a closed-form solution. This is the central challenge that the EM algorithm is designed to solve.


2. The EM Algorithm: A Principled Framework for Latent Variables

The key insight of the EM algorithm is to reframe the problem by introducing latent variables. For each data point , we introduce a binary latent variable of length . If the data point was generated by the -th Gaussian, then the -th element of , denoted , is 1, and all other elements are 0.

The problem is that we don't observe these values. EM is a powerful iterative algorithm that allows us to find MLE solutions even in the presence of such hidden variables. It does so by repeatedly constructing a tight lower bound on the log-likelihood and then maximizing that bound. Your request for formal proofs makes the theoretical underpinnings of EM particularly relevant.

To understand why this iterative process is guaranteed to improve the likelihood at each step, we can turn to a rigorous derivation using Jensen's inequality.

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

Before we derive the specific equations for GMM, it's crucial to understand why the EM algorithm works in the first place. This lecture from Stanford's CS229 course by Andrew Ng provides a rigorous derivation of the general EM algorithm. It shows how Jensen's inequality is used to iteratively construct and maximize a lower bound on the log-likelihood function, which directly addresses your interest in formal proofs.

Please watch the segment from 47:05 to 1:08:40. The main goal is to grasp the core strategy: using Jensen's inequality to create a lower bound on the log-likelihood, and understanding how the E and M steps correspond to making this bound tight and then maximizing it.

As the video demonstrates, the EM algorithm consists of two repeating steps:

  1. E-Step (Expectation): We don't know the true values of the latent variables . Instead, we use our current parameter estimates, , to compute the posterior distribution of these latent variables, . We then use this to calculate the expectation of the complete-data log-likelihood. This expected value is called the Q-function, .

  2. M-Step (Maximization): We find the new parameter estimate, , by maximizing the Q-function with respect to :

By iterating these two steps, we are guaranteed to find a local maximum of the original log-likelihood.


3. Deriving the E and M Steps for GMM

Now, let's apply this general framework to our GMM problem. This involves deriving the specific update equations for , , and .

The E-Step: Calculating Responsibilities

In the context of GMM, the posterior probability of the latent variable, , has a very intuitive name: the responsibility that component takes for data point . We'll denote it as . Using Bayes' theorem, we can express it as:

Substituting the GMM definitions ( and ), we get the E-step update rule:

This is the "soft assignment" step. Instead of assigning to a single cluster, we calculate its probability of belonging to each one. The following video segment walks through this derivation clearly.

Deriving the EM Algorithm for the Multivariate Gaussian Mixture Model

Now let's apply the general EM framework to our specific problem of Gaussian Mixture Models. This video provides a very clear, step-by-step derivation of each component. First, we'll focus on the E-step.

Watch from 08:35 to 13:13. This part details the derivation of the 'responsibilities', which are the posterior probabilities we just discussed. Pay close attention to how the joint distribution is used with Bayes' rule to find the unnormalized and then the normalized responsibilities.

The M-Step: Updating the Parameters

With the responsibilities calculated, we can now perform the M-step. We need to maximize the Q-function, which for GMM is:

We can maximize this with respect to , , and separately. The following video provides a detailed walkthrough of the necessary calculus. Since you're interested in the derivations, this is the core of the lesson.

Deriving the EM Algorithm for the Multivariate Gaussian Mixture Model

With the responsibilities calculated, we can proceed to the M-step: maximizing the expected complete-data log-likelihood (the Q-function) to find our updated parameters. The following segments from the video walk through the calculus for each parameter. This is mathematically dense, but it is the core of this lesson's objective.

This is a key part of the lesson. Please watch from 13:13 to 1:08:42. You don't need to follow every single algebraic step in real-time, but focus on the overall strategy for each parameter: Mixing Coefficients (\pi_k) (13:13 - 38:08): Notice the use of a Lagrange multiplier to handle the constraint that the \pi_k must sum to 1. Means (oldsymbol{\mu}_k) (38:08 - 48:03): See how standard vector calculus is used to find the maximum. Covariances (oldsymbol{\Sigma}_k) (48:03 - 1:08:42): This part involves matrix calculus. Pay attention to the final result and the intuition behind it.

Summary of M-Step Derivations

After performing the maximization as shown in the video, we arrive at the following update equations. Let's define the total responsibility for a cluster as .

  1. Update for the Means :
    Taking gives:

    Intuition: The new mean for a cluster is a weighted average of all data points, where the weights are the responsibilities. Points that are highly likely to belong to the cluster contribute more to its new center.

  2. Update for the Covariances :
    Taking gives:

    Intuition: The new covariance is a weighted covariance calculation. Each data point contributes to the covariance matrix of a cluster based on its responsibility for that cluster. Note that we use the newly computed mean here.

  3. Update for the Mixing Coefficients :
    Maximizing with respect to subject to the constraint (using a Lagrange multiplier) gives:

    Intuition: The new mixing coefficient for a cluster is simply the average responsibility that the cluster takes for all the data points.

For a consolidated written reference of these derivations, you can consult the following tutorial.

EM Demystified: An Expectation-Maximization Tutorial

This PDF tutorial, 'EM Demystified', provides an excellent, concise summary of the derivations we've just covered. It's a great resource to have open to follow along with the mathematical steps.

Please review Section 6.2, 'Derivation of EM for GMM Fitting' (pages 10-11). This section lays out the Q-function and the partial derivatives for the means, covariances, and weights, mirroring the steps in the video.


4. The Complete Algorithm

We can now summarize the entire EM algorithm for GMMs:

  1. Initialize the means , covariances , and mixing coefficients . This can be done randomly or by using the output of a simpler algorithm like K-means.

  2. Iterate until convergence:
    a. E-Step: Evaluate the responsibilities using the current parameters .

    b. M-Step: Re-estimate the parameters using the responsibilities calculated in the E-step.
    \begin{align*}
    N_k &= \sum_{n=1}^N \gamma_{nk} \
    \boldsymbol{\mu}k^{(t+1)} &= \frac{1}{N_k} \sum{n=1}^N \gamma_{nk} \mathbf{x}n \
    \boldsymbol{\Sigma}k^{(t+1)} &= \frac{1}{N_k} \sum{n=1}^N \gamma
    {nk} (\mathbf{x}_n - \boldsymbol{\mu}_k^{(t+1)})(\mathbf{x}_n - \boldsymbol{\mu}_k^{(t+1)})^T \

    \pi_k^{(t+1)} &= \frac{N_k}{N}
    \end{align*}
    c. Check for convergence: Evaluate the log-likelihood and stop if the change between iterations is below a certain threshold.


Conclusion

In this lesson, we tackled the core mathematics behind Gaussian Mixture Models. We saw that direct maximization of the likelihood is intractable due to the sum inside the logarithm. This motivated the use of the Expectation-Maximization algorithm, a powerful general-purpose tool for MLE with latent variables.

Key Takeaways:

  • The EM algorithm for GMMs is an iterative procedure that finds a local maximum of the data log-likelihood.
  • The E-step is a "soft assignment" step where we compute the responsibilities , which represent the posterior probability that data point belongs to cluster .
  • The M-step is a maximization step where we update the model parameters () by maximizing the expected complete-data log-likelihood.
  • The update rules have clear intuitive interpretations: the new means and covariances are responsibility-weighted averages, and the new mixing coefficients are the average responsibilities per cluster.

Preview of the Next Lesson

We have now derived the update rules for both K-means (hard clustering) and GMMs (soft clustering). In the next lesson, we will step back from the derivations and focus on the practical implications. We will compare and contrast the use cases for K-means vs. GMM, discussing their respective strengths, weaknesses, and underlying assumptions, so you can make informed decisions about which clustering algorithm to apply to a given problem.