Skip to content
EM lab, home

Maths

The derivations, written out

The maths from the original explainer, rendered properly. Equations are as Rin wrote them and the prose is lightly condensed. Where a hand-worked number did not survive recomputation, an amber box gives the exact value; the original is preserved unchanged in original/em-explainer.md.

On this page

Likelihood and log-likelihood

Probability distributions describe which values are likely and which are not. For a normal distribution with mean μ\mu and standard deviation σ\sigma the density is

f(x)=12πσ2exp⁡(−(x−μ)22σ2)f(x) = \frac{1}{\sqrt{2\pi\sigma^2}} \exp\left(-\frac{(x - \mu)^2}{2\sigma^2}\right)

If xx is close to μ\mu the density is high, if it is far away it is low, and σ\sigma controls how spread out it is. The likelihood asks how plausible the data are under given parameters. Multiplying many small densities underflows, so we add logarithms instead:

ℓ(θ)=∑i=1nlog⁡f(xi∣θ)\ell(\theta) = \sum_{i=1}^{n} \log f(x_i \mid \theta)

EM is a method for maximising this log-likelihood when part of the data is hidden.

The mixture set-up

Separate users into two groups (“sci-fi lovers” and “romance lovers”) without knowing who is in which.

  • Observed data: X={x1,x2,…,xn}\mathbf{X} = \{x_1, x_2, \ldots, x_n\}, the ratings.
  • Latent variables: Z={zik}\mathbf{Z} = \{z_{ik}\} with zik=1z_{ik} = 1 if user ii belongs to group kk and 0 otherwise; exactly one zikz_{ik} is 1 for each user.
  • Parameters: θ={π1,π2,μ1,μ2,σ12,σ22}\theta = \{\pi_1, \pi_2, \mu_1, \mu_2, \sigma_1^2, \sigma_2^2\}, the mixing proportions, means and variances.

If we knew which group each user belonged to, the complete-data log-likelihood would be

log⁡L(θ;X,Z)=∑i=1n∑k=12ziklog⁡[πkf(xi∣μk,σk2)]\log L(\theta; \mathbf{X}, \mathbf{Z}) = \sum_{i=1}^{n} \sum_{k=1}^{2} z_{ik} \log\big[\pi_k f(x_i \mid \mu_k, \sigma_k^2)\big]
log⁡L(θ;X,Z)=∑i=1n∑k=12ziklog⁡[πkf(xi∣μk,σk2)]\begin{aligned} &\log L(\theta; \mathbf{X}, \mathbf{Z}) \\ &\quad = \sum_{i=1}^{n} \sum_{k=1}^{2} z_{ik} \log\big[\pi_k f(x_i \mid \mu_k, \sigma_k^2)\big] \end{aligned}

E-step: “what's the best guess?”

The E-step replaces each unknown zikz_{ik} by its expected value given the data and the current parameters θ(t)\theta^{(t)}:

γik=E[zik∣xi,θ(t)]=P(zik=1∣xi,θ(t))\gamma_{ik} = E[z_{ik} \mid x_i, \theta^{(t)}] = P(z_{ik} = 1 \mid x_i, \theta^{(t)})
γik=E[zik∣xi,θ(t)]=P(zik=1∣xi,θ(t))\begin{aligned} \gamma_{ik} &= E[z_{ik} \mid x_i, \theta^{(t)}] \\ &= P(z_{ik} = 1 \mid x_i, \theta^{(t)}) \end{aligned}

This is about group membership, not activity: every user is in exactly one group, and γik\gamma_{ik} is how sure we are which. By Bayes' theorem,

P(zik=1∣xi,θ(t))=P(xi∣zik=1,θ(t)) P(zik=1∣θ(t))P(xi∣θ(t))P(z_{ik} = 1 \mid x_i, \theta^{(t)}) = \frac{P(x_i \mid z_{ik} = 1, \theta^{(t)}) \, P(z_{ik} = 1 \mid \theta^{(t)})}{P(x_i \mid \theta^{(t)})}
P(zik=1∣xi,θ(t))=P(xi∣zik=1,θ(t))×P(zik=1∣θ(t))P(xi∣θ(t))\begin{aligned} &P(z_{ik} = 1 \mid x_i, \theta^{(t)}) \\ &\quad = \frac{\begin{gathered} P(x_i \mid z_{ik} = 1, \theta^{(t)}) \\ {}\times P(z_{ik} = 1 \mid \theta^{(t)}) \end{gathered}}{P(x_i \mid \theta^{(t)})} \end{aligned}

The three pieces are the group's density, its prior share, and the marginal density of the rating:

P(xi∣zik=1,θ(t))=f(xi∣μk(t),σk2(t))P(zik=1∣θ(t))=πk(t)P(xi∣θ(t))=∑j=12πj(t)f(xi∣μj(t),σj2(t))\begin{aligned} P(x_i \mid z_{ik} = 1, \theta^{(t)}) &= f(x_i \mid \mu_k^{(t)}, \sigma_k^{2(t)}) \\ P(z_{ik} = 1 \mid \theta^{(t)}) &= \pi_k^{(t)} \\ P(x_i \mid \theta^{(t)}) &= \textstyle\sum_{j=1}^{2} \pi_j^{(t)} f(x_i \mid \mu_j^{(t)}, \sigma_j^{2(t)}) \end{aligned}
P(xi∣zik=1,θ(t))=f(xi∣μk(t),σk2(t))P(zik=1∣θ(t))=πk(t)P(xi∣θ(t))=∑j=12πj(t)f(xi∣μj(t),σj2(t))\begin{aligned} &P(x_i \mid z_{ik} = 1, \theta^{(t)}) \\ &\qquad = f(x_i \mid \mu_k^{(t)}, \sigma_k^{2(t)}) \\[0.4em] &P(z_{ik} = 1 \mid \theta^{(t)}) = \pi_k^{(t)} \\[0.4em] &P(x_i \mid \theta^{(t)}) \\ &\qquad = \textstyle\sum_{j=1}^{2} \pi_j^{(t)} f(x_i \mid \mu_j^{(t)}, \sigma_j^{2(t)}) \end{aligned}

Putting them together:

γik=πk(t)f(xi∣μk(t),σk2(t))∑j=12πj(t)f(xi∣μj(t),σj2(t))\gamma_{ik} = \frac{\pi_k^{(t)} f(x_i \mid \mu_k^{(t)}, \sigma_k^{2(t)})}{\sum_{j=1}^{2} \pi_j^{(t)} f(x_i \mid \mu_j^{(t)}, \sigma_j^{2(t)})}

The numerator is the joint probability P(xi,zik=1∣θ(t))P(x_i, z_{ik} = 1 \mid \theta^{(t)}), the denominator the marginal, and their ratio the conditional; so γi1+γi2=1\gamma_{i1} + \gamma_{i2} = 1.

Worked example

A user rates “The Matrix” 5 stars, with current estimates μ1=4.5, σ1=0.8, π1=0.6\mu_1 = 4.5,\ \sigma_1 = 0.8,\ \pi_1 = 0.6 (sci-fi) and μ2=3.0, σ2=1.2, π2=0.4\mu_2 = 3.0,\ \sigma_2 = 1.2,\ \pi_2 = 0.4 (romance). The explainer computes 0.6×f(5∣4.5,0.82)=0.6×0.47=0.280.6 \times f(5 \mid 4.5, 0.8^2) = 0.6 \times 0.47 = 0.28 and 0.4×f(5∣3.0,1.22)=0.4×0.12=0.050.4 \times f(5 \mid 3.0, 1.2^2) = 0.4 \times 0.12 = 0.05, so γi1=0.28/(0.28+0.05)=0.85\gamma_{i1} = 0.28 / (0.28 + 0.05) = 0.85.

The hand-worked four-rating example in the explainer has the same kind of slip; the stepper replays it with both sets of numbers side by side.

M-step: “update your model”

With responsibilities in hand, each group is re-estimated by weighted averages, the weights being how strongly each user belongs to the group:

πk(t+1)=1n∑i=1nγik,μk(t+1)=∑i=1nγikxi∑i=1nγik\pi_k^{(t+1)} = \frac{1}{n}\sum_{i=1}^{n} \gamma_{ik}, \qquad \mu_k^{(t+1)} = \frac{\sum_{i=1}^{n} \gamma_{ik} x_i}{\sum_{i=1}^{n} \gamma_{ik}}
πk(t+1)=1n∑i=1nγikμk(t+1)=∑i=1nγikxi∑i=1nγik\begin{gathered} \pi_k^{(t+1)} = \frac{1}{n}\sum_{i=1}^{n} \gamma_{ik} \\[0.3em] \mu_k^{(t+1)} = \frac{\sum_{i=1}^{n} \gamma_{ik} x_i}{\sum_{i=1}^{n} \gamma_{ik}} \end{gathered}
σk2(t+1)=∑i=1nγik(xi−μk(t+1))2∑i=1nγik\sigma_k^{2(t+1)} = \frac{\sum_{i=1}^{n} \gamma_{ik} (x_i - \mu_k^{(t+1)})^2}{\sum_{i=1}^{n} \gamma_{ik}}

The mixing proportion is the average membership; the mean is a weighted average in which users more likely to be in group kk count for more; the variance is the weighted spread around the new mean. The explainer's example: users with sci-fi probabilities 0.85, 0.20 and 0.90 rated 5, 4 and 5, so

μsci-fi=0.85×5+0.20×4+0.90×50.85+0.20+0.90=9.551.95≈4.9\mu_{\text{sci-fi}} = \frac{0.85 \times 5 + 0.20 \times 4 + 0.90 \times 5}{0.85 + 0.20 + 0.90} = \frac{9.55}{1.95} \approx 4.9
μsci-fi=0.85×5+0.20×4+0.90×50.85+0.20+0.90=9.551.95≈4.9\begin{aligned} \mu_{\text{sci-fi}} &= \frac{0.85 \times 5 + 0.20 \times 4 + 0.90 \times 5}{0.85 + 0.20 + 0.90} \\ &= \frac{9.55}{1.95} \approx 4.9 \end{aligned}

(That one checks out.) Better groups give better memberships, which give better groups: a feedback loop.

Deriving the mean update

Start from the expected complete-data log-likelihood:

Q(θ∣θ(t))=∑i=1n∑k=12γiklog⁡[πkf(xi∣μk,σk2)]=∑i=1n∑k=12γik[log⁡πk+log⁡f(xi∣μk,σk2)]\begin{aligned} Q(\theta \mid \theta^{(t)}) &= \sum_{i=1}^{n} \sum_{k=1}^{2} \gamma_{ik} \log\big[\pi_k f(x_i \mid \mu_k, \sigma_k^2)\big] \\ &= \sum_{i=1}^{n} \sum_{k=1}^{2} \gamma_{ik} \big[\log \pi_k + \log f(x_i \mid \mu_k, \sigma_k^2)\big] \end{aligned}
Q(θ∣θ(t))    =∑i=1n∑k=12γiklog⁡[πkf(xi∣μk,σk2)]    =∑i=1n∑k=12γik[log⁡πk+log⁡f(xi∣μk,σk2)]\begin{aligned} &Q(\theta \mid \theta^{(t)}) \\ &\;\; = \sum_{i=1}^{n} \sum_{k=1}^{2} \gamma_{ik} \log\big[\pi_k f(x_i \mid \mu_k, \sigma_k^2)\big] \\ &\;\; = \sum_{i=1}^{n} \sum_{k=1}^{2} \gamma_{ik} \big[\log \pi_k \\ &\qquad\qquad {} + \log f(x_i \mid \mu_k, \sigma_k^2)\big] \end{aligned}

Differentiate with respect to μk\mu_k:

∂Q∂μk=∑i=1nγik∂∂μk[−12log⁡(2πσk2)−(xi−μk)22σk2]=∑i=1nγikxi−μkσk2\begin{aligned} \frac{\partial Q}{\partial \mu_k} &= \sum_{i=1}^{n} \gamma_{ik} \frac{\partial}{\partial \mu_k} \left[-\tfrac{1}{2}\log(2\pi\sigma_k^2) - \frac{(x_i - \mu_k)^2}{2\sigma_k^2}\right] \\ &= \sum_{i=1}^{n} \gamma_{ik} \frac{x_i - \mu_k}{\sigma_k^2} \end{aligned}
∂Q∂μk=∑i=1nγik∂∂μk[−12log⁡(2πσk2)−(xi−μk)22σk2]=∑i=1nγikxi−μkσk2\begin{aligned} \frac{\partial Q}{\partial \mu_k} &= \sum_{i=1}^{n} \gamma_{ik} \frac{\partial}{\partial \mu_k} \bigg[-\tfrac{1}{2}\log(2\pi\sigma_k^2) \\ &\hspace{6em} {} - \frac{(x_i - \mu_k)^2}{2\sigma_k^2}\bigg] \\ &= \sum_{i=1}^{n} \gamma_{ik} \frac{x_i - \mu_k}{\sigma_k^2} \end{aligned}

Set it to zero and solve:

∑i=1nγik(xi−μk)=0    ⟹    μk=∑i=1nγikxi∑i=1nγik\sum_{i=1}^{n} \gamma_{ik} (x_i - \mu_k) = 0 \;\;\Longrightarrow\;\; \mu_k = \frac{\sum_{i=1}^{n} \gamma_{ik} x_i}{\sum_{i=1}^{n} \gamma_{ik}}
∑i=1nγik(xi−μk)=0⟹    μk=∑i=1nγikxi∑i=1nγik\begin{gathered} \sum_{i=1}^{n} \gamma_{ik} (x_i - \mu_k) = 0 \\ \Longrightarrow\;\; \mu_k = \frac{\sum_{i=1}^{n} \gamma_{ik} x_i}{\sum_{i=1}^{n} \gamma_{ik}} \end{gathered}

A Normal + Beta mixture

EM does not need the components to be the same family. Suppose tech-savvy users rate on a continuous 0 to 10 scale (normal) while casual users give a thumbs up or down, recorded on [0, 1] (beta):

f1(x∣μ,σ2)=12πσ2exp⁡(−(x−μ)22σ2)f_1(x \mid \mu, \sigma^2) = \frac{1}{\sqrt{2\pi\sigma^2}} \exp\left(-\frac{(x - \mu)^2}{2\sigma^2}\right)
f1(x∣μ,σ2)=12πσ2exp⁡(−(x−μ)22σ2)\begin{aligned} &f_1(x \mid \mu, \sigma^2) \\ &\quad = \frac{1}{\sqrt{2\pi\sigma^2}} \exp\left(-\frac{(x - \mu)^2}{2\sigma^2}\right) \end{aligned}
f2(x∣α,β)=xα−1(1−x)β−1B(α,β),B(α,β)=Γ(α)Γ(β)Γ(α+β)f_2(x \mid \alpha, \beta) = \frac{x^{\alpha-1}(1-x)^{\beta-1}}{B(\alpha, \beta)}, \qquad B(\alpha, \beta) = \frac{\Gamma(\alpha)\Gamma(\beta)}{\Gamma(\alpha+\beta)}
f2(x∣α,β)=xα−1(1−x)β−1B(α,β)B(α,β)=Γ(α)Γ(β)Γ(α+β)\begin{gathered} f_2(x \mid \alpha, \beta) = \frac{x^{\alpha-1}(1-x)^{\beta-1}}{B(\alpha, \beta)} \\[0.3em] B(\alpha, \beta) = \frac{\Gamma(\alpha)\Gamma(\beta)}{\Gamma(\alpha+\beta)} \end{gathered}

The E-step is the same Bayes calculation with two different densities in the numerator:

γi1=π1(t)f1(xi∣μ(t),σ2(t))π1(t)f1(xi∣μ(t),σ2(t))+π2(t)f2(xi∣α(t),β(t)),γi2=1−γi1\gamma_{i1} = \frac{\pi_1^{(t)} f_1(x_i \mid \mu^{(t)}, \sigma^{2(t)})}{\pi_1^{(t)} f_1(x_i \mid \mu^{(t)}, \sigma^{2(t)}) + \pi_2^{(t)} f_2(x_i \mid \alpha^{(t)}, \beta^{(t)})}, \quad \gamma_{i2} = 1 - \gamma_{i1}
γi1=π1(t)f1(xi∣μ(t),σ2(t))π1(t)f1(xi∣μ(t),σ2(t))+π2(t)f2(xi∣α(t),β(t))γi2=1−γi1\begin{gathered} \gamma_{i1} = \frac{\pi_1^{(t)} f_1(x_i \mid \mu^{(t)}, \sigma^{2(t)})}{\begin{gathered} \pi_1^{(t)} f_1(x_i \mid \mu^{(t)}, \sigma^{2(t)}) \\ {}+ \pi_2^{(t)} f_2(x_i \mid \alpha^{(t)}, \beta^{(t)}) \end{gathered}} \\[0.3em] \gamma_{i2} = 1 - \gamma_{i1} \end{gathered}

The mixing proportions and the normal's μ\mu, σ2\sigma^2 update exactly as before (using γi1\gamma_{i1}). The beta parameters have no closed form; they solve

∂Q∂α=∑i=1nγi2[log⁡xi−ψ(α)+ψ(α+β)]=0∂Q∂β=∑i=1nγi2[log⁡(1−xi)−ψ(β)+ψ(α+β)]=0\begin{aligned} \frac{\partial Q}{\partial \alpha} &= \sum_{i=1}^{n} \gamma_{i2} \big[\log x_i - \psi(\alpha) + \psi(\alpha + \beta)\big] = 0 \\ \frac{\partial Q}{\partial \beta} &= \sum_{i=1}^{n} \gamma_{i2} \big[\log(1-x_i) - \psi(\beta) + \psi(\alpha + \beta)\big] = 0 \end{aligned}
∂Q∂α=∑i=1nγi2[log⁡xi−ψ(α)+ψ(α+β)]=0∂Q∂β=∑i=1nγi2[log⁡(1−xi)−ψ(β)+ψ(α+β)]=0\begin{aligned} \frac{\partial Q}{\partial \alpha} &= \sum_{i=1}^{n} \gamma_{i2} \big[\log x_i - \psi(\alpha) \\ &\qquad {} + \psi(\alpha + \beta)\big] = 0 \\[0.4em] \frac{\partial Q}{\partial \beta} &= \sum_{i=1}^{n} \gamma_{i2} \big[\log(1-x_i) - \psi(\beta) \\ &\qquad {} + \psi(\alpha + \beta)\big] = 0 \end{aligned}

where ψ(x)=ddxlog⁡Γ(x)\psi(x) = \frac{d}{dx}\log\Gamma(x) is the digamma function. In the explainer's example, starting from μ=7, σ=1\mu = 7,\ \sigma = 1 and α=β=2\alpha = \beta = 2 with equal shares, a rating of 0.2 is almost certainly beta and a rating of 7.5 is certainly normal, since the beta density is zero outside [0, 1].

The algorithm and convergence

  1. Initialise θ(0)\theta^{(0)}.
  2. E-step: compute γik=E[zik∣xi,θ(t)]\gamma_{ik} = E[z_{ik} \mid x_i, \theta^{(t)}].
  3. M-step: θ(t+1)=arg⁡max⁡θQ(θ∣θ(t))\theta^{(t+1)} = \arg\max_{\theta} Q(\theta \mid \theta^{(t)}).
  4. Stop if ∣ℓ(θ(t+1))−ℓ(θ(t))∣<ϵ|\ell(\theta^{(t+1)}) - \ell(\theta^{(t)})| < \epsilon; otherwise repeat.

EM guarantees the log-likelihood never decreases,

ℓ(θ(t+1))≥ℓ(θ(t))\ell(\theta^{(t+1)}) \ge \ell(\theta^{(t)})

so it converges to a local maximum (or a saddle point) of the likelihood. Local is the important word: see the pitfalls page.

Revival addendum

Mixing proportions (a Lagrange multiplier)

Maximise ∑i∑kγiklog⁡πk\sum_i \sum_k \gamma_{ik} \log \pi_k subject to π1+π2=1\pi_1 + \pi_2 = 1:

∂∂πk[∑i,kγiklog⁡πk+λ(1−∑kπk)]=∑iγikπk−λ=0\frac{\partial}{\partial \pi_k}\Big[\sum_{i,k} \gamma_{ik} \log \pi_k + \lambda\big(1 - \textstyle\sum_k \pi_k\big)\Big] = \frac{\sum_i \gamma_{ik}}{\pi_k} - \lambda = 0
∂∂πk[∑i,kγiklog⁡πk+λ(1−∑kπk)]=∑iγikπk−λ=0\begin{aligned} &\frac{\partial}{\partial \pi_k}\Big[\sum_{i,k} \gamma_{ik} \log \pi_k + \lambda\big(1 - \textstyle\sum_k \pi_k\big)\Big] \\ &\quad = \frac{\sum_i \gamma_{ik}}{\pi_k} - \lambda = 0 \end{aligned}
⟹    πk=1n∑i=1nγik\Longrightarrow\;\; \pi_k = \frac{1}{n}\sum_{i=1}^{n} \gamma_{ik}

since summing over kk gives λ=∑i,kγik=n\lambda = \sum_{i,k}\gamma_{ik} = n.

Variances

∂Q∂σk2=∑i=1nγik[−12σk2+(xi−μk)22σk4]=0    ⟹    σk2=∑iγik(xi−μk)2∑iγik\frac{\partial Q}{\partial \sigma_k^2} = \sum_{i=1}^{n} \gamma_{ik} \left[-\frac{1}{2\sigma_k^2} + \frac{(x_i - \mu_k)^2}{2\sigma_k^4}\right] = 0 \;\;\Longrightarrow\;\; \sigma_k^2 = \frac{\sum_i \gamma_{ik} (x_i - \mu_k)^2}{\sum_i \gamma_{ik}}
∂Q∂σk2=∑i=1nγik[−12σk2+(xi−μk)22σk4]=0⟹    σk2=∑iγik(xi−μk)2∑iγik\begin{aligned} \frac{\partial Q}{\partial \sigma_k^2} &= \sum_{i=1}^{n} \gamma_{ik} \bigg[-\frac{1}{2\sigma_k^2} \\ &\qquad\quad {} + \frac{(x_i - \mu_k)^2}{2\sigma_k^4}\bigg] = 0 \\[0.3em] \Longrightarrow\;\; \sigma_k^2 &= \frac{\sum_i \gamma_{ik} (x_i - \mu_k)^2}{\sum_i \gamma_{ik}} \end{aligned}

The notebook updates σk\sigma_k as the square root of this, which is the same update. If one component's weight concentrates on a single point, the numerator goes to zero: that is the variance collapse pitfall.

Why the log-likelihood never decreases

For any distribution qiq_i over the group of user ii, Jensen's inequality gives a lower bound on the log-likelihood:

ℓ(θ)=∑ilog⁡∑kqikπkf(xi∣μk,σk)qik  ≥  ∑i∑kqiklog⁡πkf(xi∣μk,σk)qik\ell(\theta) = \sum_i \log \sum_k q_{ik} \frac{\pi_k f(x_i \mid \mu_k, \sigma_k)}{q_{ik}} \;\ge\; \sum_i \sum_k q_{ik} \log \frac{\pi_k f(x_i \mid \mu_k, \sigma_k)}{q_{ik}}
ℓ(θ)=∑ilog⁡∑kqikπkf(xi∣μk,σk)qik≥∑i∑kqiklog⁡πkf(xi∣μk,σk)qik\begin{aligned} \ell(\theta) &= \sum_i \log \sum_k q_{ik} \frac{\pi_k f(x_i \mid \mu_k, \sigma_k)}{q_{ik}} \\ &\ge \sum_i \sum_k q_{ik} \log \frac{\pi_k f(x_i \mid \mu_k, \sigma_k)}{q_{ik}} \end{aligned}

The E-step chooses qik=γikq_{ik} = \gamma_{ik}, which makes the bound touch ℓ(θ(t))\ell(\theta^{(t)}); the M-step maximises the bound over θ\theta. So ℓ(θ(t+1))≥bound(θ(t+1))≥bound(θ(t))=ℓ(θ(t))\ell(\theta^{(t+1)}) \ge \text{bound}(\theta^{(t+1)}) \ge \text{bound}(\theta^{(t)}) = \ell(\theta^{(t)}). The playground checks this on every trace it draws.