The EM algorithm is a general technique for maximum likelihood estimation in latent variable models, where the key difficulty is the sum-inside-log form of the marginal likelihood.
In the Gaussian Mixture model, the latent variable zi is a one-hot cluster assignment, and the posterior p(zik=1∣xi) acts as the responsibility, enabling soft assignment.
MLE for GMM yields fixed-point equations with no closed-form solution, so we iterate between an E-step that computes the responsibilities and an M-step that updates μk,Σk,πk.
By Jensen’s inequality, the ELBO L(q,θ) lower-bounds logp(X∣θ): the M-step maximizes it with respect to θ, while the E-step tightens it by minimizing DKL(q(Z)∥p(Z∣X,θ)).
When the exact posterior is intractable, we restrict q(Z) to a tractable family. The optimization leading to Variational Inference.
The Expectation-maximization algorithm is a general technique for finding maximum likelihood solutions for latent variable models. Here I use the discrete latent-variable case, the Gaussian mixture model, to introduce the idea of EM.
GMM is one of the discrete latent variable models, and also one of Mixture model that can be used for clustering and density estimation. It is similar to K-means, but uses Gaussian distributions and “responsibilities” to support more flexible modeling (from sphere to ellipse) and soft-assignment.
For a GMM, the latent variable zi is defined as the assignment of a single data xi to a cluster k out of K clusters, which normally uses one-hot coding: zi=(zi1,⋯,ziK)∈{0,1}K, where
zik={1,0,if xi belongs to cluster kotherwise
The relevant probabilities for a single data point xi are shown below, with parameters θ={π,μ,Σ} to be learned.
Prior: the probability of a random data belonging to cluster k
P(zik=1):=πk,p(zi)=j∏Kπjzij
with ∑kKπk=1.
Likelihood: the probability of the data given the cluster k
Note that the fixed-point equations above do not provide a closed-form solution for the parameters, because the responsibilities rik which appear in the right-hand side depend on those parameters.
However, it does suggest an iterative scheme to find a solution, which turns out to be an instance of the Expectation-maximization algorithm:
E-step: Assign responsibilities by computing the posterior.
The initialization matters for a good and fast convergence. In practice, the K-means algorithm is used to find a suitable initialization for it, which takes much less iterations than GMM to converge. But note that it does not guarantee a global maximum, as the log-likelihood logp(X∣θ) can be multimodal.
Why the EM-algorithm for GMM introduced earlier actually works to increase logp(X∣θ) was poorly shown. Here let’s dive into the algorithm deeper with abstract settings.
For an arbitrary latent variables model, the marginal log-likelihood has to be obtained by summing over all possible values of the latent variables Z, which results in a sum-inside-log form:1
logp(X∣θ)=log[Z∑p(X,Z∣θ)]
The sum-inside-log form makes the optimization intractable. It “prevents the log from acting directly on the joint distribution, resulting in complicated expressions for the maximum likelihood”.2
How can we deal with it? Since the logarithm function log(⋅) is concave, then Jensen’s inequality apply:
log(E[X])≥E[log(X)]
That is, the log of the mean is bigger than the mean of the logs.
Moreover, we can turn the summations into an expectation with a proposal distribution q(Z):
The lower bound L(q,θ) gets rid of the annoying sum-inside-log form, and thus is much easier for optimization.
With L(q,θ), also called evidence lower bound (ELBO) or negative free energy, one may naturally think of maximizing it with respect to θ to increase the targeted logp(X∣θ). That is exactly the underlying idea of the general EM-algorithm! The only with additional step is to also update the ELBO so that it can ‘catch up with’ logp(X∣θ). These two steps actually correspond to the M-step and E-step, respectively. Next let’s see how it works.
For the M-step, given a fixed proposal q(Z), we can maximize the ELBO with respect to the parameters:
The optimization objective is then changed to maximize Eq(Z)[logp(X,Z∣θ)], which is the expected complete-data log-likelihood.
It is called ‘complete-data’ in the sense that it contains both the observed and latent variables. Because we cannot collect the complete dataset {X,Z} but only the incomplete one {X}, we consider the expected values over q(Z), or the posterior p(Z∣X,θ) ideally.
For the E-step, we want to update the ELBO with θnew, so that it is as close to logp(X∣θ) as possible. To do so, we need to formalize the ‘distance’ between them, which turns out to be the KL-divergence between the proposal q(Z) and the posterior p(Z∣X,θ):
Therefore, with a new parameter θnew, the E-step aims to find a new p(Z) that minimizes the KL-divergence, and an ideal one is to just use the posterior, so that DKL[q(Z)∥p(Z∣X,θ)]=0, and the ELBO can ‘tight’ the marginal log-likelihood.
By iterating the two steps, the EM algorithm guarantees a monotonic increase of the log-likelihood logp(X∣θ), but again not a global maximum.
In many cases, it is pretty hard to actually find the posterior to make DKL[q(Z)∥p(Z∣X,θ)]=0 perfectly. And we have to use q(Z) in a convenient family to make life easier, for example, Gaussian distributions or exponential family. The optimization over function q(Z) towards this objective leads to calculus of variations, and the idea of Variational Inference.