MIT 18.650 — Lecture 12

The EM Algorithm

§ Computing the MLE

In this lecture, we'll turn our attention to actually computing the MLE. For very simple models, computing the MLE as a function of the sample data {Xi}i=1n\{X_i\}_{i = 1}^n is very easy; recall Lecture 9.

More generally, the MLE is the solution to an optimization problem: for what value θ\theta is n(θ):=i=1nlogfθ(Xi)\ell_n(\theta) := \sum_{i = 1}^n \log f_{\theta}(X_i) maximized?

There are two natural approaches to solving optimization problems.

Gradient Ascent. Assuming n(θ)\ell_n'(\theta) is computable, we can start at some guess θ1\theta_1 and search for the maximum by picking some constant η(0,1)\eta \in (0, 1) of our choice and updating our guesses {θi}i=1\{\theta_i\}_{i = 1}^{\infty} like so:

θi+1=θi+ηn(θi).\theta_{i + 1} = \theta_i + \eta \cdot \ell_n'(\theta_i).

Newton-Raphson (Root Finding). The idea is to find a root of n(θ)\ell_n'(\theta) and hope the critical point we find is a global maximum. The Newton-Raphson algorithm updates guesses {θi}i=1\{\theta_i\}_{i = 1}^{\infty} for the root like so:

θi+1=θin(θi)n(θi).\theta_{i + 1} = \theta_i - \frac{\ell'_n(\theta_i)}{\ell''_n(\theta_i)}.

Fortunately, when our model is something like {N(θ,1)θR}\{\mathcal{N}(\theta, 1) \mid \theta \in \mathbb{R}\}, the log likelihood n(θ)\ell_n(\theta) is not very hard to optimize, because its derivative is simple. (This depends on normal distributions nicely having exponential PDFs!)

But there's a special kind of model that is particularly hard to work with: mixture models.

§ Mixture Models

If we measure the heights of a random group of people, a model like {N(θ,1)θR}\{\mathcal{N}(\theta, 1) \mid \theta \in \mathbb{R}\} won't work perfectly anymore. One reason is that our sample is a mixture of men and women. We might (simplistically) assume the following:

Our samples thus follow the PDF f=(1p)fμ0+pfμ1f = (1 - p)f_{\mu_0} + pf_{\mu_1}. This is a mixture model, with parameter (μ0,μ1,p)(\mu_0, \mu_1, p). More generally…

Definition. In a mixture model, there are two models with parameters θ0\theta_0 and θ1\theta_1, along with corresponding PDFs fθ0f_{\theta_0} and gθ1g_{\theta_1}. Every sample ii yields some data (Yi,Zi)(Y_i, Z_i), where:

ZiBer(p)    and    Yi{Pθ0 if Zi=0.Pθ1 if Zi=1.Z_i \sim \mathrm{Ber}(p) ~~~ \text{ and } ~~~ Y_i \sim \begin{cases} \mathbb{P}_{\theta_0} & \text{ if } Z_i = 0. \\ \mathbb{P}_{\theta_1} & \text{ if } Z_i = 1. \end{cases}

Note that we may only receive the data {Yi}i=1n\{Y_i\}_{i = 1}^n and don't even know the value of pp.

There are two challenges with finding the MLE for mixture models.

Challenge #1. We don't know the value of pp, so the log likelihood n(μ0,μ1,p)\ell_n(\mu_0, \mu_1, p) has a floating pp. This means we have a third parameter to worry about now.

Challenge #2. Even if we did assume, say, p=12p = \frac{1}{2}, the log likelihood still looks awful.

n(μ0,μ1)=i=1nlog(12[12πexp(12(Yiμ0)2)]+12[12πexp(12(Yiμ1)2)])\ell_n(\mu_0, \mu_1) = \sum_{i = 1}^n \log\left( \frac{1}{2} \left[\frac{1}{\sqrt{2\pi}}\exp\left( -\frac{1}{2}(Y_i - \mu_0)^2 \right)\right] + \frac{1}{2} \left[\frac{1}{\sqrt{2\pi}}\exp\left( -\frac{1}{2}(Y_i - \mu_1)^2 \right)\right] \right)

The difficulty is that the logarithm of a sum does not simplify well at all.

§ The EM Algorithm

It turns out there is a strategy for computing the MLE of a mixture model: the EM Algorithm.

Idea #1. If we know all of the ZiZ_i, then estimating (p,θ0,θ1)(p, \theta_0, \theta_1) is doable.

Reasoning: Of course, if we know all the ZiZ_i, we can just estimate pp directly.

p^=1ni=1nZi.\hat{p} = \frac{1}{n} \sum_{i = 1}^n Z_i.

As for (θ0,θ1)(\theta_0, \theta_1), the rough idea is just to split the samples {Yi}i=1n\{Y_i\}_{i = 1}^n into two halves based on their value of ZiZ_i. Then just use one half to estimate θ0\theta_0, and the other half to estimate θ1\theta_1. Formally, this looks like:

θ^0=arg maxθ0i=1n(1Zi)logfθ0(Yi)    and    θ^1=arg maxθ1i=1nZiloggθ1(Yi).\hat{\theta}_0 = \argmax_{\theta_0} \sum_{i = 1}^n (1 - Z_i) \log f_{\theta_0}(Y_i) ~~~ \text{ and } ~~~ \hat{\theta}_1 = \argmax_{\theta_1} \sum_{i = 1}^n Z_i \log g_{\theta_1}(Y_i).

And these are a lot easier to compute.

Of course, we don't know all of the ZiZ_i. But maybe we can at least estimate the ZiZ_i

Idea #2. If we have some prior estimate for (p,θ0,θ1)(p, \theta_0, \theta_1), then we can estimate ZiZ_i reasonably well.

Reasoning: Well, if we know (p,θ0,θ1)(p, \theta_0, \theta_1), then ZiZ_i can be estimated via Bayes' Rule.

Z^i=gθ1(Yi)pgθ1(Yi)p+fθ0(Yi)(1p).\hat{Z}_i = \frac{g_{\theta_1}(Y_i) \cdot p}{ g_{\theta_1}(Y_i) \cdot p + f_{\theta_0}(Y_i) \cdot (1 - p) }.

Here, Z^i\hat{Z}_i is not a discrete value in {0,1}\{0, 1\}, but rather a continuous value in [0,1][0, 1]. Still, it's a good enough estimate that the reasoning in Idea #1 still holds.

We have a problem though: Idea #1 relies on Idea #2, and vice versa! Here's the trick, though: we don't care.

EM Algorithm. Start with some prior estimate (θ0(0),θ1(0),p(0))(\theta_0^{(0)}, \theta_1^{(0)}, p^{(0)}). At time step tt, make the following updates:

Remark. Suppose that instead of a mixture of just 22 distributions, we had a mixture of kk distributions.

There's an easy fix: instead of saying the ithi^{\text{th}} sample only has the data (Yi,Zi)(Y_i, Z_i), say it has the data (Yi,Zi,1,Zi,2,,Zi,k)(Y_i, Z_{i, 1}, Z_{i, 2}, \dots, Z_{i, k}), where (Zi,1,Zi,2,,Zi,k)Categorical(p1,p2,,pk)(Z_{i, 1}, Z_{i, 2}, \dots, Z_{i, k}) \sim \mathrm{Categorical}(p_1, p_2, \dots, p_k) for all ii, and p1+p2++pk=1p_1 + p_2 + \dots + p_k = 1. Then:

It turns out the following is true.

Theorem. (EM Algorithm Efficacy) The EM algorithm never decreases the log likelihood; that is,

n(θ0(t+1),θ1(t+1),p(t+1))n(θ0(t),θ1(t),p(t))  for all t.\ell_n(\theta_0^{(t + 1)}, \theta_1^{(t + 1)}, p^{(t + 1)}) \geq \ell_n(\theta_0^{(t)}, \theta_1^{(t)}, p^{(t)}) ~ \text{ for all } t.

Proof omitted—it's fairly involved.