EM for a mixture of Bernoullis, and which log-likelihood to watch
How the EM algorithm fits a mixture of multivariate Bernoulli distributions, and why the quantity you monitor must be the observed-data log-likelihood, not the expected complete-data one.
University lab From my coursework in the MSc in Statistics and Machine Learning at Linköping University, rewritten as a note to review later.
Mixture models say the data come from a few hidden groups, each with its own simple distribution, and we don’t know who belongs where. The EM algorithm fits such a model by guessing soft group memberships, refitting each group, and repeating. I implemented it in a lab for binary vectors (a mixture of multivariate Bernoullis), and the part I got wrong is a good lesson: I was monitoring the wrong log-likelihood.
The model
Each observation is a binary vector , . There are components. Component is chosen with probability (the mixing weights, summing to 1), and inside component each bit is an independent coin flip with probability of being 1:
We want to maximize the observed-data log-likelihood
The log of a sum doesn’t split nicely, so there’s no closed form. EM gets around it.
E-step and M-step
E-step. With the current parameters, compute the responsibility of component for point , which is the posterior probability that came from :
M-step. Refit each component as if the responsibilities were known weights:
So is just a weighted average of the data vectors. Repeat until stops changing. Each iteration is guaranteed not to decrease , which makes it a good convergence check and a good bug detector.
The mistake: monitoring instead of
In my lab code, the “log-likelihood” I printed was
That’s the expected complete-data log-likelihood, the thing the M-step maximizes. It isn’t . The two are linked by
where is the total entropy of the soft assignments. So is always below , and the gap is bigger when memberships are more uncertain.
This explained something odd in my results. My final values were about for , for and for : getting worse as grew. For the true log-likelihood that shouldn’t happen. A model with more components can always copy a smaller one, so at a good optimum goes up (or stays the same) with . What I saw was the entropy term: with more components, points are shared more ambiguously, grows, and falls.
Re-running a corrected version on data generated the same way shows exactly that pattern: goes from about () to () to (), while goes down from to to . (These come from my re-run, so they don’t match the report’s numbers exactly.)
Note also that a higher for larger doesn’t mean larger is better. To choose , use a penalized criterion like BIC, or held-out log-likelihood.
em_bernoulli <- function(x, K, iters = 200, tol = 1e-6) {
pi <- rep(1 / K, K)
mu <- matrix(runif(K * ncol(x), 0.4, 0.6), K) # K x D, random start
ll_old <- -Inf
for (it in 1:iters) {
logp <- x %*% t(log(mu)) + (1 - x) %*% t(log(1 - mu)) # log p(x_n | mu_k), N x K
a <- sweep(logp, 2, log(pi), "+") # log(pi_k p(x_n | mu_k))
m <- apply(a, 1, max)
log_px <- m + log(rowSums(exp(a - m))) # log-sum-exp: log p(x_n)
ll <- sum(log_px) # the observed-data log-likelihood
gamma <- exp(a - log_px) # E-step
if (ll - ll_old < tol) break
ll_old <- ll
pi <- colMeans(gamma) # M-step
mu <- pmin(pmax(t(gamma) %*% x / colSums(gamma), 1e-6), 1 - 1e-6)
}
list(pi = pi, mu = mu, loglik = ll, Q = sum(gamma * a), iters = it)
}
Two practical details in there. Products of many probabilities underflow, so it’s safer to work with logs and use the log-sum-exp trick. And clipping away from exactly 0 and 1 avoids when a component ends up owning only points with the same bit.
What the lab showed about K
With the true , EM recovered the three true profiles quite well. With , one of the components (the one with all ) got split into two. That flat component is hard to pin down: its bit probabilities are exactly the average of the other two components’ (they’re mirror images around 0.5), so on average it looks like a 50/50 blend of them. Only the correlations between bits tell them apart, and EM converges slowly while it sorts that out. EM also only finds a local optimum, so it’s worth running it from several random starts and keeping the best .
The same E-step / M-step pattern shows up elsewhere with a different hidden quantity: missing values in EM for missing data, and a hidden state sequence in EM for state-space models. If you’d rather sample the hidden labels than average over them, that’s Gibbs sampling for mixtures.
Key takeaways
- EM alternates soft assignments (E-step) and weighted refits (M-step); for Bernoulli mixtures, is a responsibility-weighted mean.
- Monitor the observed-data log-likelihood ; it must never decrease.
- is not : entropy, so can fall as grows even when rises.
- Choose with BIC or held-out likelihood, and use several random starts.
Resources
- 732A99 Machine Learning, Linköping University (course lab, autumn 2019)
- Christopher M. Bishop, Pattern Recognition and Machine Learning, Section 9.3 (an alternative view of EM) and Section 9.3.3 (mixtures of Bernoulli distributions), Section 9.4 (the decomposition of into a lower bound and a KL term)
- A. P. Dempster, N. M. Laird, D. B. Rubin, “Maximum likelihood from incomplete data via the EM algorithm”, Journal of the Royal Statistical Society B 39(1) (1977): https://doi.org/10.1111/j.2517-6161.1977.tb01600.x