Expectation Maximization - An explanation of statistical inference using the example of Gaussian Mixture Models

Christoph Winkler
M. Sc. Business Information Systems, Data Scientist.
Generative Modeling is simply about modeling "How the world could be" and not necessarily "How the world actually is".

Clustering forms a group of unsupervised learning algorithms that are designed to find unknown patterns in data. It is one of the fundamental methods for many researches and practitioners working with data. The k-means clustering algorithm is one of the best known and simpliest clustering methods used today. The algorithm assigns each data point to exactly one cluster. This is called hard assignment. However, the lack of in-between assignment often leads to issues regarding overlapping clusters. Additionally, k-means ignores the variance of the clusters and therefore it can happen that the algorithm does not work well with different cluster sizes.

In this article the Expectation Maximization (EM) algorithm is explained and discussed in simple words as a fundamental principal of statistical inference. Afterwards an implementation of the concept is presented in Python using the example of univariate Gaussian Mixture Models (GMMs). The article is written for researchers and practitioners that have a basic understanding of statistics and machine learning.

Model

EM clustering is a method to adress the issue of hard assignment and data generated by different cluster sizes. It adds the statistical assumption that every data point xi is randomly drawn from a statistical distribution k. In Gaussian Mixture Models the underlying distribution is a normal distribution. Therefore, every cluster k ∈ {1,…,K} equals a normal distribution with mean μk and standard deviation σk:

K is a hyperparameter of the model and determines the number of clusters which is fixed. A hyperparameter is a constant that has to be estimated before inferring the model parameters. Usually a hyperparameter does not change during training. However, a model parameter is not known before. It has to be estimated during inference. In many cases model parameters are randomly initialized.
Another relevant variable is x. It represents the observed data which depends on the cluster assignment z, the mean μ and the standard deviation σ. Φ is a K dimensional vector of a categorial distribution. It encodes the prior probability assumption that a data point xi was generated from a certain cluster zi. However, we do not have an inital assumption. Therefore, we set Φk = 1/K for k ∈ {1,…,K}.

EM Clustering

The goal of the algorithm is to maximize the overall likelihood p(x|z,μ,σ) that the data x is observed given the final cluster assignments zi and its parameters (μ and σ). A requirement of the EM algorithm is that the probability density function (pdf) of the a posteriori distribution is known and available in closed form. The probability density function of the posterior distribution in univariate GMMs is the probability density function of the univariate normal distribution:

The EM algorithm computes a point estimate of the parameters of the actual posterior distribution. However, the function that is optimized during inference is non-convex. The properties of a non-convex function let us conclude that a found optimum is not guaranteed to be the global optimum. Therefore, it only finds a local optimal solution for the latent variables (z, μ; and σ) by using the observed variables x.

E-Step

In the first step the probability for each data point xi and every possible cluster assignment is computed.

In the numerator the prior expectation of the cluster assignment is multiplied by the density of the currently selected cluster. The denominator is the normalization factor that simply computes the sum of densities over all possible cluster assignments k ∈ {1,…,K}. Therefore, the outcome of the normalized densities is a probability value between 0 and 1. It represents the probability that a data point xi was generated by cluster k and its estimated parameters. These probabilites are computed for each data point and each possible cluster assignment which forces a runtime complexity of at least O(n * k).

M-Step

In the next step the model parameters are updated. First the prior expectation of the cluster assignment:

Next the cluster parameters μ and σ are updated:

The updated value for μ is the weighted average of all data points xi that are assigned to cluster k. Similar the updated value for σ is also computed by using the probabilities as weights.

Example

Let’s assume we observe the data points x. We set K=2 with a prior cluster assignment Φ. The initial values are set as follows:

The density for data point 1 (assuming cluster 1 generated it):

The density for data point 1 (assuming cluster 2 generated it):

Next step is to normalize the densities to compute the probability values (E-Step):

The probability that data point 1 was generated by cluster 1 is 83 percent whereas the probability that it was generated by cluster 2 is 17 percent. We also compute the probability values for data point 2:

The probability that data point 2 was generated by cluster 1 is 5 percent whereas the probability that it was generated by cluster 2 is 95 percent.

Last step is to update the model parameters (M-Step). Here you can find the new model parameter estimates after the first iteration:

In practice both steps are repeated several times. It is guaranteed that the model parameters converge to a stationary point. Therefore, let’s evaluate the convergence of the model. We compute the overall log likelihood before and after the first iteration:

It turns out that the log likelihood is increasing which lets us conclude it is more likely that the estimated model after the first iteration has generated the observed data. Therefore, the algorithm works as expected and the model is getting better.

I hope you enjoyed reading this article and got a better understanding of the Expectation Maximization algorithm. As a next step, please go on to read Expectation Maximization - A Python implementation.