Likelihood and log-likelihood
Probability distributions describe which values are likely and which are not. For a normal distribution with mean and standard deviation the density is
If is close to the density is high, if it is far away it is low, and 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:
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: , the ratings.
- Latent variables: with if user belongs to group and 0 otherwise; exactly one is 1 for each user.
- Parameters: , the mixing proportions, means and variances.
If we knew which group each user belonged to, the complete-data log-likelihood would be
E-step: “what's the best guess?”
The E-step replaces each unknown by its expected value given the data and the current parameters :
This is about group membership, not activity: every user is in exactly one group, and is how sure we are which. By Bayes' theorem,
The three pieces are the group's density, its prior share, and the marginal density of the rating:
Putting them together:
The numerator is the joint probability , the denominator the marginal, and their ratio the conditional; so .
Worked example
A user rates “The Matrix” 5 stars, with current estimates (sci-fi) and (romance). The explainer computes and , so .
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:
The mixing proportion is the average membership; the mean is a weighted average in which users more likely to be in group 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
(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:
Differentiate with respect to :
Set it to zero and solve:
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):
The E-step is the same Bayes calculation with two different densities in the numerator:
The mixing proportions and the normal's , update exactly as before (using ). The beta parameters have no closed form; they solve
where is the digamma function. In the explainer's example, starting from and 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
- Initialise .
- E-step: compute .
- M-step: .
- Stop if ; otherwise repeat.
EM guarantees the log-likelihood never decreases,
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 subject to :
since summing over gives .
Variances
The notebook updates 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 over the group of user , Jensen's inequality gives a lower bound on the log-likelihood:
The E-step chooses , which makes the bound touch ; the M-step maximises the bound over . So . The playground checks this on every trace it draws.