# EM, Mixture of Gaussians and K-means

This post ties together EM (Expectation Maximization), GMM (Gaussian Mixture Models), K means and variational inference. If you have taken an introductory machine learning course and have learned these algorithms, but the connections between them are not yet clear, this is the post for you. If you have not heard about two of the above terms, I suggest you read the wikipedia page for each before reading this post.

## TL;DR:

• EM is a variational inference algorithm to optimize the lower bound of the log likelihood.
• GMM is a specific example of EM where the base distribution is MVN (multivariate normal).
• K means is GMM where variance and cluster assignment probabilities are fixed and cluster assignments are hard.

This post is rather theoretic compared to the other ones but I assure you that you’ll have a very deep understanding of the above algorithms by the end of this post.

## Theory of Variational Inference

Let me first introduce the EM algorithm in a rather theoretical way. I found this theoretical introduction more “intuitive” than other attempts. I hope you will, too. In short, the goal of EM is to increase the likelihood, and it does so by giving a lower bound of the likelihood. Let $X=\{X_1,X_2,...,X_N\}$ denote the data we have at hand. Then, the likelihood of a model parameterized by $\theta$ is

Now let me introduce variables for cluster membership $Z=\{Z_1,Z_2,...,Z_N\}$. Here, each $Z_i$ is a $K$ dimensional vector where each vector denotes which cluster each data point belongs to. Then, using the law of total probability,

The learning objective, as usual, is to maximize the log likelihood:

This would have been easier if the log likelihood was of form $\sum_Z log(p(X,Z\lvert \theta))$ as the sum is outside the log and hence we can take the derivative easily. Life is not so easy here since the sum is inside the log. How can we take the log outside? This question motivates us to introduce Jensen’s Inequality:

For any concave function $\phi$, $\phi(EX) \geq E\phi(X)$ Jensen’s Inequality

Using Jensen’s Inequality, we can take the sum outside:

Jensen’s Inequality is used from line 4 to 5 to provide $L(q,\theta)$ as the lower bound for $log \ \sum_Z p(X,Z\lvert \theta)$. You should be able to see that $L(q,\theta)$ only has the sum outside the log.

I abruptly introduced $q(Z)$ without really explaining it. What is $q(Z)$ here? We can think of $q(Z)$ as a probability distribution we model that we wish to be as close to the true data distribution $p(Z\lvert X,\theta)$ as possible. To see why this is the case, consider the difference between the true model log likelihood and our lower bound approximation $L(q,\theta)$:

Note that from line 2 to 3, we are using the fact that $\sum_Z q(Z)=1$. To summarize, we have so far:

This equation explains why we want $q(Z)$ to be as close to $p(Z\lvert X,\theta)$ as possible. It’s because we want to have $KL(q(Z)\lvert \lvert p(Z\lvert X,\theta))$ smaller so as to make $L(q,\theta)$ a tighter lower bound for $log \: l(\theta)$. To sum up, we have:

• Original goal: $argmax_{\theta} \ log \: l(\theta)$
• New goal: $argmax_{q,\theta} \ L(q,\theta)$

## From Variational Inference to EM

Now that we introduced the new optimization problem we care about, let’s actually solve it! This will yield the formula for the EM algorithm in the most general way possible. Since $L(q,\theta)$ has two parameters $q$ and $\theta$, let’s optimize the function w.r.t. one parameter at a time.

### E Step: $argmax_{q} \ L(q,\theta)$

Let’s maximize $L(q,\theta)$ w.r.t. $q$. Since we know that $log \: l(\theta)=L(q,\theta)+KL(q(Z)\lvert \lvert p(Z\lvert X,\theta))$ and also that $log \: l(\theta)$ is fixed and doesn’t depend on $q$, this is equivalent to $argmin_{q} \ KL(q(Z)\lvert \lvert p(Z\lvert X,\theta))$. Since we know that the minimum value of Kullback–Leibler divergence is $0$, we want to set $q(Z)$ such that:

Thus, we have the update formula for the E step:

Because we are minimizing the KL divergence but the likelihood itself doesn’t change, E step is equivalent to making the lower bound tighter.

### M Step: $argmax_{\theta} \ L(q,\theta)$

It is not so hard to maximize $L(q,\theta)$ w.r.t. $\theta$. But the intuition behind M step is trickier. This is because $log \: l(\theta)$ depends on $\theta$ and thus we first need to understand why $\theta$ that maximizes $L(q,\theta)$ also increases $log \: l(\theta)$. To see this, recall

In E step, we minimized the second term on RHS (KL divergence) to be its smallest possible value $0$. Hence, this term can only increase as we change $\theta$ in M step. The first term on RHS will obviously increase because that is what we are aiming for in M step. Hence, as a whole, we know that $log \: l(\theta)$ must also increase.

Now, since we are updating $\theta$, let’s rewrite $q(Z)$ after the E step as $q(Z\lvert X,\theta^{old})$ so as to distinguish with the new $q(Z)$ derived from the M step. Using this new terminology:

where $Q(\theta,\theta^{old})\equiv \sum_Z q(Z\lvert X,\theta^{old}) log \ p(X,Z\lvert \theta)$. Hence,

$Q(\theta,\theta^{old})$ depends on how we parametrize the model. One case is GMM, and is exemplified below. We now have the update rule of the M step. In contrast to the E step, M step is equivalent to raising the log likelihood higher.

## GMM as EM

Now, we will show that GMM is a specific example of EM. To see this, let

• $X=\{X_1,X_2,...,X_N\}$ be the data.
• $Z=\{Z_1,Z_2,...,Z_N\}$ be the cluster membership.
• $\theta=\{\mu_1,...,\mu_K,\Sigma_1,...,\Sigma_K,\pi_1,...,\pi_K\}$ be the parameters. $\mu,\Sigma$ are parameters of the Gaussian, and $\pi$ are prior mixture probability.

The whole model is a mixture of MVN, as follows: $p(X,Z\lvert \theta)=\prod_{n=1}^N \prod_{k=1}^K \pi_k^{Z_{nk}} N(X_n\lvert \mu_k, \Sigma_k)^{Z_{nk}}$

%

### M Step: $argmax_{\theta} \ Q(\theta,\theta^{old})$

% Let $E_{Z\sim q(\cdot \lvert X,\theta^{old})}Z_{nk}=\gamma(Z_{nk})$. Then, %

Since we have the constraint that $\sum_k \pi_k =1$ (the cluster membership probability sums to 1), the ultimate optimization problem becomes:

Since $\theta=\{\mu_1,...,\mu_K,\Sigma_1,...,\Sigma_K,\pi_1,...,\pi_K\}$, we can solve this for $\pi_k,\mu_k,\Sigma_k$ one at a time:

## K means as GMM

It is straightforward to see that K means is a specific instance of GMM. In GMM, assume the following:

• Constant variance: $\Sigma_k = \sigma^2 I_D$
• Constant membership probability: $\pi_k=\frac{1}{k}$
• Hard membership assignment: $X(m,n)= \begin{cases} 1, if \quad k=argmax_k \ N(X_n\lvert \mu_k,\Sigma_k)\\ 0, otherwise \end{cases}$

With these three additional assumptions, we have the K means! To see this, let’s take a look at the E step and the M step.

### E Step: $q(Z)=p(Z\lvert X,\theta)$

Since the membership assignment is hard, for each point, we want to find a class that maximizes its likelihood: %

Hence we see that E step is equivalent to assigning points to the nearest centroid.

### M Step: $argmax_{\theta} \ Q(\theta,\theta^{old})$

We only need to update $\mu_k$ according to the update rule derived above:

This is equivalent to taking the mean of data points in each cluster.

## Summary

I hope you were able to see the theoretical derivation of EM and that GMM and K means are both specific instances of EM. To read more about this material, I suggest you refer to the following:

• Information Theory, Inference, and Learning Algorithms (MacKay) Chapter 20, 22, 33.7
• Machine Learning: A Probabilistic Perspective (Murphy) Chapter 11.4
• Pattern Recognition and Machine Learning (Bishop) Chapter 9

Categories:

Updated: