Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

A first EM algorithm

When EM applies

EM applies when two easy problems depend on each other.

  1. If the hidden variable ZZ were known, estimating θ\theta would be easy.

  2. If θ\theta were known, calculating p(Z∣X,θ)p(Z\mid X,\theta) would be easy.

EM solves them in turn.

  1. E step. Use the current θ\theta to calculate probabilities for ZZ.

  2. M step. Use those probabilities to update θ\theta.

Then repeat. Each round raises or preserves the observed-data likelihood. The iteration converges to a stationary point, although the point can depend on the starting values.

This requires a joint model p(X,Z∣θ)p(X,Z\mid\theta) and E and M steps that we can compute.

A two-group model

We observe XiX_i, but not its group ZiZ_i. If the group labels were known, the group proportions and means would be simple counts and averages. EM calculates a probability for each label and uses those probabilities as weights.

Let P(Zi=1)=πP(Z_i=1)=\pi and

Xi∣Zi=k∼N(μk,1),k∈{0,1}.X_i\mid Z_i=k\sim N(\mu_k,1),\qquad k\in\{0,1\}.

The unknown parameters are θ=(π,μ0,μ1)\theta=(\pi,\mu_0,\mu_1).

One fixed sample

We simulate one observed value from one of two hidden groups. The hidden label is shown because this is a teaching example. In real data, it is unknown. The observed value could, for example, be an apple weight and the hidden label its variety.

Loading...

E step: infer the hidden group

Given the current parameters, calculate the probability that observation ii came from group 1.

ri(t)=P(Zi=1∣Xi=xi,θ(t))=π(t)ϕ(xi;μ1(t),1)(1−π(t))ϕ(xi;μ0(t),1)+π(t)ϕ(xi;μ1(t),1).r_i^{(t)} =P(Z_i=1\mid X_i=x_i,\theta^{(t)}) =\frac{\pi^{(t)}\phi(x_i;\mu_1^{(t)},1)} {(1-\pi^{(t)})\phi(x_i;\mu_0^{(t)},1) +\pi^{(t)}\phi(x_i;\mu_1^{(t)},1)}.

ri(t)r_i^{(t)} is also E[Zi∣Xi,θ(t)]E[Z_i\mid X_i,\theta^{(t)}]. It is the expected group-1 count contributed by observation ii.

plot without title

A responsibility is not a hard group label. It measures uncertainty about the hidden group. Values near 1/21/2 mean that the two groups remain difficult to distinguish.

M step: update the parameters

Use the responsibilities as fractional group labels.

π(t+1)=1n∑iri(t),\pi^{(t+1)}=\frac{1}{n}\sum_i r_i^{(t)},
μ0(t+1)=∑i(1−ri(t))xi∑i(1−ri(t)),μ1(t+1)=∑iri(t)xi∑iri(t).\mu_0^{(t+1)}=\frac{\sum_i(1-r_i^{(t)})x_i}{\sum_i(1-r_i^{(t)})},\qquad \mu_1^{(t+1)}=\frac{\sum_i r_i^{(t)}x_i}{\sum_i r_i^{(t)}}.

In general, the M step maximizes the expected complete-data log likelihood.

θ(t+1)=arg⁡max⁡θEZ∣X,θ(t)[ℓc(θ;X,Z)].\theta^{(t+1)}=\arg\max_{\theta} E_{Z\mid X,\theta^{(t)}}\left[\ell_c(\theta;X,Z)\right].

Why the iteration works

The observed-data log likelihood contains a sum over the hidden variable.

ℓ(θ;X)=log⁡∑Zp(X,Z∣θ).\ell(\theta;X)=\log\sum_Z p(X,Z\mid\theta).

After the E step, let qt(Z)=p(Z∣X,θ(t))q_t(Z)=p(Z\mid X,\theta^{(t)}). This distribution gives every possible hidden assignment a weight. With these weights fixed, EM uses the bound

F(qt,θ)=Eqt[log⁡p(X,Z∣θ)]+H(qt)≤ℓ(θ;X).\mathcal F(q_t,\theta) =E_{q_t}[\log p(X,Z\mid\theta)]+H(q_t) \leq \ell(\theta;X).

H(qt)=−Eqt[log⁡qt(Z)]H(q_t)=-E_{q_t}[\log q_t(Z)] measures the remaining uncertainty about the hidden assignment. It is constant during the M step. The bound touches the observed-data log likelihood at the current parameters. The M step raises the bound, and the new likelihood lies above the new bound.

ℓ(θ(t+1);X)≥F(qt,θ(t+1))≥F(qt,θ(t))=ℓ(θ(t);X).\ell(\theta^{(t+1)};X) \geq \mathcal F(q_t,\theta^{(t+1)}) \geq \mathcal F(q_t,\theta^{(t)}) =\ell(\theta^{(t)};X).

Any valid initial value can start the iteration. Under the usual regularity conditions, the likelihood converges and the parameter estimates approach a stationary point. Different initial values can reach different stationary points, so a useful starting value can still matter.

Loading...
plot without title

What to remember

  1. EM applies when hidden data would make estimation simple and both the E and M steps can be computed.

  2. The E step turns each hidden value into a probability. The M step uses those probabilities as fractional observations.

  3. Each iteration raises or preserves the observed-data likelihood, but different starting values can lead to different stationary points.

The genotyping-error model uses the same structure. The latent variable is the true genotype, and the observed data are uncertain genotype measurements.