Expectation–Maximization

For many latent variable models, maximizing the complete log likelihood is easier than maximizing the log likelihood. The expectation–maximization (EM) algorithm leverages this fact to construct and optimize a tight lower bound. I rederive EM.

Consider a probabilistic model with data x={xn,,xN}\mathbf{x} = \{x_n, \dots, x_N\}, latent variables z={z1,,zN}\mathbf{z} = \{z_1, \dots, z_N\}, and parameters θ\boldsymbol{\theta}. To perform maximum likelihood inference, we need to compute the log likelihood logp(xθ)\log p(\mathbf{x} \mid \boldsymbol{\theta}). However, our modeling assumption is that we have some latent variables z\mathbf{z}. We could handle these latent variables by marginalizing them out,

logp(xθ)=zlogp(x,zθ).(1) \log p(\mathbf{x} \mid \boldsymbol{\theta}) = \sum_{\mathbf{z}} \log p(\mathbf{x}, \mathbf{z} \mid \boldsymbol{\theta}). \tag{1}

However, this may be intractable. For example, a hidden Markov model with NN observations whose latent variables take one of KK states has KNK^N terms in Equation 11.

There is a standard solution to this general problem: Expectation-Maximization or EM (Dempster et al., 1977). It relies on the fact that in many statistical problems, maximizing the complete log likelihood p(x,z,θ)p(\mathbf{x}, \mathbf{z}, \mid \boldsymbol{\theta}) is actually easier than maximizing the log likelihood. Rather than optimizing Equation 11 directly, EM iteratively optimizes a lower bound. As we will see, this lower bound is tight, meaning no greater value is also a lower bound, and maximizing the lower bound guarantees that we maximize the likelihood.

First, let’s derive the lower bound,

logp(xθ)=logzp(x,zθ)=logzq(z)p(x,zθ)q(z)=log(Eq(z)[p(x,zθ)q(z)])Eq(z)[log(p(x,zθ)q(z))]. \begin{aligned} \log p(\mathbf{x} \mid \boldsymbol{\theta}) &= \log \sum_{\mathbf{z}} p(\mathbf{x}, \mathbf{z} \mid \boldsymbol{\theta}) \\ &= \log \sum_{\mathbf{z}} q(\mathbf{z}) \frac{p(\mathbf{x}, \mathbf{z} \mid \boldsymbol{\theta})}{q(\mathbf{z})} \\ &= \log \Big( \mathbb{E}_{q(\mathbf{z})}\Big[\frac{p(\mathbf{x}, \mathbf{z} \mid \boldsymbol{\theta})}{q(\mathbf{z})}\Big] \Big) \\ &\geq \mathbb{E}_{q(\mathbf{z})} \Big[ \log \Big( \frac{p(\mathbf{x}, \mathbf{z} \mid \boldsymbol{\theta})}{q(\mathbf{z})} \Big)\Big]. \end{aligned}

To see why the inequality holds, let f(x)=log(x)f(x) = \log(x). Since ff is a concave function, we can invoke Jensen’s inequality or f(E[a])E[f(a)]f(\mathbb{E}[a]) \geq \mathbb{E}[f(a)]. Please see my previous post for a proof, but note that this inequality is reversed if f()f(\cdot) is convex. We can then use log(a/b)=log(a)log(b)\log(a/b) = \log(a) - \log(b) and the linearity of expectation to write,

logp(xθ)Eq(z)[logp(x,zθ)]Expected complete log likelihoodEq(z)[logq(z)]Entropy of q.(2) \log p(\mathbf{x} \mid \boldsymbol{\theta}) \geq \underbrace{\phantom{\Big|}\mathbb{E}_{q(\mathbf{z})} \Big[ \log p(\mathbf{x}, \mathbf{z} \mid \boldsymbol{\theta})\Big]}_{\text{Expected complete log likelihood}} \underbrace{\phantom{\Big|}- \mathbb{E}_{q(\mathbf{z})} \Big[ \log q(\mathbf{z}) \Big]}_{\text{Entropy of $q$}}. \tag{2}

Since we’re taking the expectation with respect to q(z)q(\mathbf{z}), we know that q(z)q(\mathbf{z}) is a density. However, which density should we choose if we want a tight lower bound? Jensen’s inequality holds with equality if f(a)f(a) is a constant or if

p(x,zθ)q(z)=non-random c. \frac{p(\mathbf{x}, \mathbf{z} \mid \boldsymbol{\theta})}{q(\mathbf{z})} = \text{non-random $c$}.

For the fraction to be a constant, we know it must be true that q(z)p(x,zθ)q(\mathbf{z}) \propto p(\mathbf{x}, \mathbf{z} \mid \boldsymbol{\theta}). And since q(z)q(\mathbf{z}) is a density and must normalize to one, we have

q(z)=p(x,zθ)zp(x,zθ)=p(x,zθ)p(xθ)=p(zx,θ). \begin{aligned} q(\mathbf{z}) &= \frac{p(\mathbf{x}, \mathbf{z} \mid \boldsymbol{\theta})}{\sum_{\mathbf{z}^{\prime}} p(\mathbf{x}, \mathbf{z}^{\prime} \mid \boldsymbol{\theta})} \\ &= \frac{p(\mathbf{x}, \mathbf{z} \mid \boldsymbol{\theta})}{p(\mathbf{x} \mid \boldsymbol{\theta})} \\ &= p(\mathbf{z} \mid \mathbf{x}, \boldsymbol{\theta}). \end{aligned}

Thus, we have found the ideal q(z)q(\mathbf{z}) in our lower-bound approximation of the log likelihood. We can rewrite the inequality in Equation 22 as an equality,

logp(xθ)=Ep(zx,θ)[logp(x,zθ)]Ep(zx,θ)[logp(zx,θ)]. \log p(\mathbf{x} \mid \boldsymbol{\theta}) = \mathbb{E}_{p(\mathbf{z} \mid \mathbf{x}, \boldsymbol{\theta})} \big[ \log p(\mathbf{x}, \mathbf{z} \mid \boldsymbol{\theta})\big] - \mathbb{E}_{p(\mathbf{z} \mid \mathbf{x}, \boldsymbol{\theta})} \big[ \log p(\mathbf{z} \mid \mathbf{x}, \boldsymbol{\theta}) \big].

We are almost ready to construct the EM algorithm. However, since EM is an iterative algorithm, let’s first introduce some notation based on iteration indexes,

logp(xθ)=Ep(zx,θ(t))[logp(x,zθ)]Ep(zx,θ(t))[logp(zx,θ)]=Q(θθ(t))+H(θθ(t)) \begin{aligned} \log p(\mathbf{x} \mid \boldsymbol{\theta}) &= \mathbb{E}_{p(\mathbf{z} \mid \mathbf{x}, \boldsymbol{\theta}^{(t)})} \big[ \log p(\mathbf{x}, \mathbf{z} \mid \boldsymbol{\theta})\big] - \mathbb{E}_{p(\mathbf{z} \mid \mathbf{x}, \boldsymbol{\theta}^{(t)})} \big[ \log p(\mathbf{z} \mid \mathbf{x}, \boldsymbol{\theta}) \big] \\ &= Q(\boldsymbol{\theta} \mid \boldsymbol{\theta}^{(t)}) + H(\boldsymbol{\theta} \mid \boldsymbol{\theta}^{(t)}) \end{aligned}

where Q()Q(\cdot) equal to the first expectation and H()H(\cdot) equal to the negative of the second expectation. This notation is meant to express an expectation of θ\boldsymbol{\theta} with respect to some other parameter value θ(t)\boldsymbol{\theta}^{(t)}.

Now carefully consider the following reasoning. By Gibb’s inequality, we know that

H(θθ(t))H(θ(t)θ(t)). H(\boldsymbol{\theta} \mid \boldsymbol{\theta}^{(t)}) \geq H(\boldsymbol{\theta}^{(t)} \mid \boldsymbol{\theta}^{(t)}).

This is intuitive. The cross entropy (left-hand side) is always greater or equal to the entropy (right-hand side). The measure of “surprise” can only go up. Alternatively—and this is how many other authors explain EM—you can note that H(θθ(t))H(θ(t)θ(t))H(\boldsymbol{\theta} \mid \boldsymbol{\theta}^{(t)}) - H(\boldsymbol{\theta}^{(t)} \mid \boldsymbol{\theta}^{(t)}) is a Kullback–Leibler (KL) divergence, which is nonnegative. Please see my previous post on entropy and the KL divergence if these ideas are not clear.

The above logic implies,

logp(xθ)logp(xθ(t))=Q(θθ(t))Q(θ(t)θ(t))+H(θθ(t))H(θ(t)θ(t))Q(θθ(t))Q(θ(t)θ(t)).(3) \begin{aligned} \log p(\mathbf{x} \mid \boldsymbol{\theta}) - \log p(\mathbf{x} \mid \boldsymbol{\theta}^{(t)}) &= Q(\boldsymbol{\theta} \mid \boldsymbol{\theta}^{(t)}) - Q(\boldsymbol{\theta}^{(t)} \mid \boldsymbol{\theta}^{(t)}) + H(\boldsymbol{\theta} \mid \boldsymbol{\theta}^{(t)}) - H(\boldsymbol{\theta}^{(t)} \mid \boldsymbol{\theta}^{(t)}) \\ &\geq Q(\boldsymbol{\theta} \mid \boldsymbol{\theta}^{(t)}) - Q(\boldsymbol{\theta}^{(t)} \mid \boldsymbol{\theta}^{(t)}). \tag{3} \end{aligned}

The inequality in Equation 33 demonstrates that if we maximize Q(θθ(t))Q(\boldsymbol{\theta} \mid \boldsymbol{\theta}^{(t)}), then we maximize the log likelihood logp(xθ)\log p(\mathbf{x} \mid \boldsymbol{\theta}) by the same amount.

Thus, EM works by iteratively optimizing the expected complete log likelihood Q()Q(\cdot) rather than logp(xθ)\log p(\mathbf{x} \mid \boldsymbol{\theta}). It consists of two eponymous steps:

E-step:Q(θθ(t))=Ep(zx,θ(t))[logp(x,zθ)]M-step:θ(t+1)=arg ⁣maxθQ(θθ(t)). \begin{aligned} \textbf{E-step:} && Q(\boldsymbol{\theta} \mid \boldsymbol{\theta}^{(t)}) &= \mathbb{E}_{p(\mathbf{z} \mid \mathbf{x}, \boldsymbol{\theta}^{(t)})} \big[ \log p(\mathbf{x}, \mathbf{z} \mid \boldsymbol{\theta})\big] \\ \textbf{M-step:} && \boldsymbol{\theta}^{(t+1)} &= \arg\!\max_{\boldsymbol{\theta}} Q(\boldsymbol{\theta} \mid \boldsymbol{\theta}^{(t)}). \end{aligned}

The E-step is so-called because it constructs the expectation of the complete log likelihood. The M-step is so-called because it then maximizes that quantity. Intuitively we can think of the E-step as constructing the desired lower bound, and the M-step as optimizing that bound.

While this post outlines the logic for why optimizing Q()Q(\cdot) optimizes logp(xθ)\log p(\mathbf{x} \mid \boldsymbol{\theta}), it does not prove that this iterative algorithm converges to the desired log likelihood. This was actually proven by (Wu, 1983) six years after Dempster et al’s paper, as the latter had a mistake in their proof due to a misuse of the triangle inequality.

As a final note, it may not be obvious that computing or maximizing the expected complete log likelihood is any easier than maximizing the log likelihood. However, recall that the intractability of the log likelihood arises from marginalizing out z\mathbf{z}, which may induce an exponential number of terms. The complete log likelihood does not suffer from this issue as it just expresses the probability of x\mathbf{x} and z\mathbf{z} conditioned on θ\boldsymbol{\theta}. To see this, it might be useful to work through a complete example of fitting EM to a model. Please see my post on factor analysis for such an example.

  1. Dempster, A. P., Laird, N. M., & Rubin, D. B. (1977). Maximum likelihood from incomplete data via the EM algorithm. Journal of the Royal Statistical Society. Series B (Methodological), 1–38.
  2. Wu, C. F. J. (1983). On the convergence properties of the EM algorithm. The Annals of Statistics, 11(1), 95–103.