Post · 11 min read· pp. 24–30

Expectation-maximization: theory and intuition

Expectation-maximization (EM) is a popular algorithm for performing maximum-likelihood estimation of the parameters in a latent variable model. In this post, I discuss the theory behind, and intuition into this algorithm.

Introduction

Expectation-maximization (EM) is a popular algorithm for performing maximum-likelihood estimation of the parameters in a latent variable model. Introductory machine learning courses often teach the variants of EM used for estimating parameters in important models such as Guassian Mixture Models and Hidden Markov Models. EM also features heavily in bioinformatics where, for example, it is used for quantifying gene expression from RNA-sequencing data and for finding sequence-motifs in genomes.

After learning about this algorithm in introductory graduate school courses, the intuition was clear, but I still had several remaining questions. When is EM preferred over some other method for estimating parameters? What is EM really doing? Why is EM guaranteed to converge?

In this blog post, I’ll describe my current understanding of the theory behind the EM algorithm, which helped me to answer the aformentioned questions. I’ll also offer a few perspectives for intuiting this algorithm.

The Setting

As mentioned previously, EM is used for performing maximum likelihood estimation on the parameters of a latent variable model. To review this task, let’s say we have two sets of random variables XX and ZZ and some parameterized joint distribution over these random variables:

p(x,z;θ)p(x, z; \theta)

where pp is the probability mass/density function, xx and zz are values for XX and ZZ, and θ\theta parameterizes the distribution.

Further, we assume that we have observed xx, the value of XX, but have not observed zz, the value of ZZ. Despite not observing zz, we wish to find the value for θ\theta that makes xx most likely under our model. Since we have not observed z, our likelihood function must marginalize over z:

l(θ):=log⁡p(x;θ)=log⁡∫p(x,z;θ) dzl(\theta) := \log p(x ; \theta) = \log \int p(x, z ; \theta) \ dz

In practice, maximizing this function over θ may be difficult to do analytically due to this integral. For such situations, the EM algorithm may provide a method for computing a local maximum of this function with respect to θ.

Description of EM

The EM algorithm alternates between two steps: an expectation-step (E-step) and a maximization-step (M-step). Throughout execution, the algorithm maintains an estimate of the parameters θ\theta that is updated on each iteration. As the algorithm progresses, this estimate of θ\theta converges to a local maximum of l(θ)l(\theta). Let tt denote a given step of the algorithm. On the ttth step, we’ll denote θt\theta_t as the ttth estimate of θ\theta.

Each step works as follows: On the ttth E-step, the algorithm formulates a function of θ\theta called the Q-function, denoted Qt(θ)Q_t(\theta). Then, on the next M-step, the algorithm assigns θt+1\theta_{t+1} to be the value that maximizes Qt(θ)Q_t(\theta).

This alternation between formulating a Q-function and maximizing that Q-function are repeated until the estimate of θ\theta converges. As we will prove later, not only is this process guaranteed to converge, but it will converge to a local maximum of l(θ)l(\theta).

More specifically, the E-Step and M-Step work as follows:

E-Step

Compute the conditional probability p(z∣x;θt)p(z \mid x ; \theta_t). From this calculation, formulate the Q-function:

Qt(θ):=EZ∣x,θt[log⁡p(x,z;θ)]=∫p(z∣x;θt)log⁡p(x,z;θ) dz\begin{align*}Q_t(\theta) &:= E_{Z\mid x, \theta_t}\left[ \log p(x, z ; \theta) \right] \\ &= \int p(z \mid x ; \theta_t) \log p(x,z ; \theta) \ dz \end{align*}

The “core” of formulating an EM algorithm for a particular model is calculating p(z∣x;θt)p(z \mid x ; \theta_t). If the model admits an easy calculation of this function, then the EM algorithm may be a good a choice!

M-Step

Assign θt+1\theta_{t+1} to be the value that maximizes the Q-function that was formulated on the previous E-step:

θt+1:=arg maxθQt(θ)\theta_{t+1} := \text{arg max}_\theta Q_t(\theta)

We repeat the E-Step and M-Step until the difference between θt\theta_t and θt+1\theta_{t+1} is small, indicating convergence. We also note that θ0\theta_0 can be set to an arbitrary value – usually set randomly. Since EM finds a local maximum of l(θ)l(\theta), it is often wise to try many different θ0\theta_{0}’s!

EM is coordinate ascent on the evidence lower bound

Now, let’s dig into what the EM algorithm is really doing. Specifically, we will show that EM is a coordinate ascent algorithm on a quantity called the evidence lower bound (ELBO).

Coordinate ascent? ELBO? Before talking about EM, let’s first dig into the these concepts:

The evidence lower bound

For a thorough discussion regarding the evidence lower bound, see my blog post. To review this discussion, if we happen to know (or posit) that ZZ follows some distribution q(z)q(z) (and that p(x,z;θ):=p(x∣z;θ)q(z)p(x, z; \theta) := p(x \mid z ; \theta)q(z)), then the evidence lower bound (ELBO) is a lower bound on the likelihood function:

log⁡p(x;θ)≥ELBO:=EZ∼q[log⁡p(x,Z;θ)q(Z)]\log p(x ; \theta) \geq \text{ELBO} := E_{Z \sim q}\left[\log \frac{p(x,Z; \theta)}{q(Z)} \right]

Coordinate ascent

Coordinate ascent is a relatively simple and iterative strategy for maximizing a function. Given a function f(a,b)f(a, b) that takes two arguments aa and bb, the idea of coordinate ascent is that we will iteratively fix aa to some value a^\hat{a} and then choose for bb the value b^\hat{b} that maximizes f(a^,b)f(\hat{a}, b) . Given b^\hat{b}, we then re-assign to aa the value that maximizes f(a,b^)f(a, \hat{b}). This procedure is repeated until convergence.

Coordinate ascent on the evidence lower bound

In our setting, we don’t yet know which value to use for θ\theta, nor do we know the distribution qq for ZZ. Thus, the ELBO becomes a function of both qq and θ\theta:

F(q,θ):=EZ∼q[log⁡p(x,Z;θ)q(Z)]F(q, \theta) := E_{Z \sim q}\left[\log \frac{p(x,Z; \theta)}{q(Z)} \right]

EM is just coordinate ascent on this function. In the E-Step, we fix θ\theta and solve for qq. In the M-Step, we fix qq and solve for θ\theta.

First, let’s fix θ\theta to θt\theta_t (our current value for θ\theta) and solve for qq:

argmaxq F(q,θt)=argmaxq  Ez∼q[log⁡p(Z,x;θ)q(Z)]=argminq KL(q(z) ∣∣ p(z∣x;θt))=p(z∣x;θt)\begin{align*}\text{argmax}_q \ F(q, \theta_t) &= \text{argmax}_q \ \ E_{z \sim q}\left[ \log \frac{p(Z, x ; \theta)}{q(Z)} \right] \\ &= \text{argmin}_q \ \text{KL}( q(z) \ || \ p(z \mid x ; \theta_t)) \\ &= p(z \mid x ; \theta_t)\end{align*}

I skipped over several steps in the derivation, but the idea is that maximizing the ELBO with respect to qq is equivalent to finding a qq that minimizes the KL-divergence (a measure of dissimilarity between two distributions, but not discussed here) between q(z)q(z) and p(z∣x;θt)p(z \mid x ; \theta_t). The qq to do this, is simply p(z∣x;θt)p(z \mid x ; \theta_t) itself. Notably, computing p(z∣x;θt)p(z \mid x ; \theta_t) is exactly what is needed to formulate the Q-function in the E-Step!

Next, if we hold qq fixed to qt:=p(z∣x;θt)q_t := p(z \mid x ; \theta_t), we see that maximizing F(qt,θ)F(q_t, \theta) with respect to θ\theta is equivalent to maximizing the Q-function:

argmaxθ F(qt,θ)=argmaxθ  Ez∼q[log⁡p(Z,x;θ)p(z∣x;θt)]=argmaxθ  EZ∣x,θt[log⁡p(x,z;θ)]\begin{align*}\text{argmax}_{\theta} \ F(q_t, \theta) &= \text{argmax}_{\theta} \ \ E_{z \sim q}\left[ \log \frac{p(Z, x ; \theta)}{p(z \mid x ; \theta_t)} \right] \\ &= \text{argmax}_{\theta} \ \ E_{Z\mid x, \theta_t}\left[ \log p(x, z ; \theta) \right]\end{align*}

The last line in the above derivation is simply maximzing the Q-function – exactly the M-Step!

Visualizing EM

To build up a visual depiction of how the EM algorithm works, we’ll first lay out some relationships between FF and ll:

First, though not proven here, at iteration tt, the function FF touches the likelihood function ll at precisely θt\theta_t. That is,

F(qt,θt)=l(θt)F(q_t, \theta_t) = l(\theta_t)

Second, with qq fixed at qtq_t, the function FF at θt+1\theta_{t+1} will be greater than at θt\theta_t. That is,

F(qt,θt+1)≥F(qt,θt)F(q_t, \theta_{t+1}) \geq F(q_t, \theta_t)

This follows from the fact that θt+1\theta_{t+1} maximizes F(qt,θ)F(q_t, \theta), by design of the algorithm.

With qq fixed at qtq_t, FF is bounded from above by ll. This follows from the fact that FF is the the evidence lower bound – that is, it is a lower bound for ll:

F(qt,θ)≤l(θ)F(q_t, \theta) \leq l(\theta)

These properties are depicted below:

drawing

Given these properties, we can reason that EM converges on a local maximum of l(θ)l(\theta) by “crawling” up the likelihood surface. That is, on each iteration tt, F(qt,θ)F(q_t,\theta) lies at or below the likelihood surface, but touches it at θt\theta_t. That is, F(qt,θt)=l(θt)F(q_t, \theta_t) = l(\theta_t). Then, since F(qt,θt+1)>F(qt,θt)F(q_t, \theta_{t+1}) > F(q_t, \theta_t), it follows that θt+1\theta_{t+1} increases the likelihood function. Furthermore, on the next iteration, we can evaluate the likelihood function at θt+1\theta_{t+1} via F(qt+1,θt+1)F(q_{t+1}, \theta_{t+1}). Thus, each proceeding iteration’s value of F(qt+1,θt+1)F(q_{t+1}, \theta_{t+1}) is a new, higher value on the likelihood surface than the current value. This process is depicted below:

drawing

When to use the EM algorithm

The EM algorithm is just one choice of many for tackling the optimization problem:

argmaxθlog⁡p(x;θ)\text{argmax}_\theta \log p(x; \theta)

When would EM be preferred over other optimization algorithms out there?

First, the EM algorithm might be a good choice when l(θ)l(\theta) is challenging to optimize, but the Q-function is easier. The likelihood function is often difficult to optimize due to the presence of the log of an integral:

l(θ):=log⁡∫p(x,z;θ) dzl(\theta) := \log \int p(x, z ; \theta) \ dz

Taking the derivative/gradient of this function with respect to θ\theta, setting it to zero, and then solving for θ\theta is often very difficult to do analytically.

On the otherhand, if the integral and logarithm were flipped, then function might be much easier to optimize analytically. The Q-function is just such a function:

Qt(θ):=∫f(z)log⁡p(x,z;θ) dzQ_t(\theta) := \int f(z) \log p(x,z ; \theta) \ dz

where f(z):=p(z∣x;θt)f(z) := p(z \mid x ; \theta_t).

Thus, we see that EM converts a difficult optimiziation problem into a series of easier optimization problems where each sub-problem is made easier by flipping the integral and log within the objective function!

Intuition behind the Q-function: a generalization of the likelihood function

In the case in which ZZ is a discrete random variable, the Q-function can be interpreted as a “generalized” version of the complete data likelihood function. First, let’s say that both XX and ZZ are observed where X=xX = x and Z=zZ = z. Then, we could write the complete data likelihood as

log⁡p(x,z;θ):=∑z′∈ZI(z=z′)log⁡p(x,z′;θ)\log p(x, z; \theta) := \sum_{z' \in \mathcal{Z}} \mathbb{I}(z=z') \log p(x, z' ; \theta)

where Z\mathcal{Z} is the support of ZZ’s probability mass function and I(z=z’)\mathbb{I}(z=z’) is an indicator function that equals one when z’=zz’ = z and equals zero otherwise.

Now, recall the Q-function:

Qt(θ):=∑z′p(z′∣x;θ)log⁡p(x,z′;θ)Q_t(\theta) := \sum_{z'} p(z' \mid x ; \theta) \log p(x, z' ; \theta)

We note that the difference between these two functions is that the indicator variable in the complete data likelihood I(z=z’)\mathbb{I}(z=z’) is replaced by the probability p(z∣x;θ)p(z \mid x ;\theta). This leads us to viewing the Q-function as a sort of generalization of the complete data likelihood. That is, p(z∣x;θ)p(z \mid x ; \theta) acts as a weight for its corresponding term in the summation, and measures our current certainty that the hidden data is equal to z′z′. When we know Z=zZ = z, all of the weight is assigned to the term in which z′=zz′ = z and no weight is assigned to the terms in which z′≠zz′ \neq z – that is, p(z∣x;θ)p(z \mid x ;\theta) reduces to the indicator function.

Intuition behind the Q-function: a likelihood function over a hypothetical dataset

Another way to view the Q-function is as a complete data likelihood function over a hypothetical dataset where this hypothetical dataset is generated from a distribution that depends on our current best guess of θ\theta (i.e. θt\theta_t at the ttth iteration).

That is, let us assume that we generate a very large set of values for ZZ drawn i.i.d. from the distribution specified by p(z∣x;θt)p(z \mid x ; \theta_t):

z1′,z2′,…,zn′∼p(z∣x;θt)z'_1, z'_2, \dots, z'_n \sim p(z \mid x ; \theta_t)

For each z’iz’_i, we create a new “sub”-dataset (x,z’i)(x, z’_i). Note that the observed data xx is duplicated in each of these sub-datasets. We then merge all of the datasets (x,z’1),...,(x,z’n)(x, z’_1), . . . , (x, z’_n) to form our new hypothetical dataset. Then, the likelihood over these data is

l′(θ):=∏i=1np(x,zi′;θ)l'(\theta) := \prod_{i=1}^n p(x, z'_i ; \theta)

Now we will show that maximizing the Q-function is equivalent to maximizing this likelihood function over the hypothetical data. Let

Z′:={z1′,z2′,…,zn′}Z' := \{z'_1, z'_2, \dots, z'_n\}

Now let c(z)c(z) be the cardinality of

{z′∈Z′∣z′=z}\{ z' \in Z' \mid z' = z \}

That is, c(z)c(z) is the number of items in Z’Z’ that equal zz. As nn approaches infinity then

c(z)∑z∗c(z∗)=p(z∣x;θt)\frac{c(z)}{\sum_{z^*}c(z^*)} = p(z \mid x ; \theta_t)

Finally we can formulate our maximum likelihood problem on this hypothetical data:

argmaxθ l′(θ):=argmaxθ ∏i=1np(x,zi′;θ)=argmaxθ ∏z∈Zp(x,z;θ)c(z)=argmaxθ ∑z∈Zc(z)∑z∗c(z∗)log⁡p(x,z;θ)=argmaxθ ∑z∈Zp(z∣x;θt)log⁡p(x,z;θ)\begin{align*}\text{argmax}_\theta \ l'(\theta) &:= \text{argmax}_\theta \ \prod_{i=1}^n p(x, z'_i ; \theta) \\ &= \text{argmax}_\theta \ \prod_{z \in \mathcal{Z}} p(x, z ; \theta)^{c(z)} \\ &= \text{argmax}_\theta \ \sum_{z \in \mathcal{Z}} \frac{c(z)}{\sum_{z^*}c(z^*)} \log p(x, z ; \theta) \\ &= \text{argmax}_\theta \ \sum_{z \in \mathcal{Z}} p(z \mid x ; \theta_t) \log p(x, z ; \theta)\end{align*}

The final line in the above calculation is the Q-function!

In this light, we can view the EM algorithm as an iterative process, in which we posit a distribution over the hidden data p(z∣x;θt)p(z \mid x ; \theta_t), then generate a hypothetical data set on which we perform maximum likelihood estimation. This new estimate of the parameters, as estimated from the hypothetical dataset, is then used in the next iteration to generate another data set. This process is repeated until the estimates of the parameters converge.