Deterministic Annealing
This demo takes a few seconds to load and runs entirely in your browser (NumPy, SciPy and matplotlib via
Pyodide; no tgmm or PyTorch involved).
EM can get stuck when its components start on top of each other. Deterministic annealing EM (Ueda &
Nakano, 1998) tempers the E-step with an inverse temperature \(\beta\in(0,1]\):
\[
r_{ik}(\beta)=\frac{\big(\pi_k\,\mathcal N(x_i\mid\mu_k,\Sigma_k)\big)^{\beta}}{\sum_j\big(\pi_j\,\mathcal N(x_i\mid\mu_j,\Sigma_j)\big)^{\beta}},
\qquad
\beta_t=\begin{cases}\beta_0^{\,1-t/n_{\text{anneal}}}, & t<n_{\text{anneal}}\\ 1, & t\ge n_{\text{anneal}}.\end{cases}
\]
Small \(\beta\) spreads every point's responsibility over all components (a smooth, nearly unimodal objective); \(\beta\) then
rises geometrically to 1 over \(n_{\text{anneal}}=30\) iterations. Below, three Gaussians are fitted to three well-separated
blobs from a saddle start: all three means are jittered (σ = 0.3) around the same blob, each with covariance \(I\) -- a
classic EM failure mode (a poor local optimum that plain EM never leaves). Pick
\(\beta_0\) and the algorithm, then press the button to restart from a fresh jitter.
import%20marimo%20as%20mo%0Aimport%20numpy%20as%20np%0Aimport%20matplotlib.pyplot%20as%20plt%0Afrom%20matplotlib.patches%20import%20Ellipse%0A%0A_rng%20%3D%20np.random.default_rng(0)%0AX%20%3D%20np.vstack(%5B_rng.normal(c%2C%201.0%2C%20(150%2C%202))%20for%20c%20in%20%5B(0%2C%200)%2C%20(8%2C%200)%2C%20(4%2C%207)%5D%5D)%20%20%23%20fixed%20data%2C%203%20blobs%0AK%2C%20N_ANNEAL%2C%20N_ITER%20%3D%203%2C%2030%2C%20150%0A%0Adef%20log_norm(X%2C%20mu%2C%20S)%3A%0A%20%20%20%20L%20%3D%20np.linalg.cholesky(S)%3B%20z%20%3D%20np.linalg.solve(L%2C%20(X%20-%20mu).T)%0A%20%20%20%20return%20-0.5%20*%20(z%20**%202).sum(0)%20-%20np.log(np.diag(L)).sum()%20-%20np.log(2%20*%20np.pi)%0A%0Adef%20fit(hard%2C%20beta0%2C%20seed)%3A%0A%20%20%20%20%22%22%22EM%20(hard%3DFalse)%20or%20CEM%20(hard%3DTrue)%20with%20the%20DAEM%20schedule%20and%20tgmm's%20stopping%20rule%3B%20collapsed%20init.%20Returns%20means%2C%20covs%2C%20labels%2C%20entropy%20trace%2C%20final%20mean%20log-lik.%22%22%22%0A%20%20%20%20mu%20%3D%200.3%20*%20np.random.default_rng(seed).normal(size%3D(K%2C%202))%20%20%23%20all%20three%20means%20start%20on%20top%20of%20blob%200%0A%20%20%20%20S%2C%20pi%2C%20H%20%3D%20np.stack(%5Bnp.eye(2)%5D%20*%20K)%2C%20np.full(K%2C%201%20%2F%20K)%2C%20%5B%5D%0A%20%20%20%20beta%20%3D%20lambda%20t%3A%201.0%20if%20t%20%3E%3D%20N_ANNEAL%20or%20beta0%20%3E%3D%201%20else%20beta0%20**%20(1%20-%20t%20%2F%20N_ANNEAL)%0A%20%20%20%20def%20e_step(b)%3A%0A%20%20%20%20%20%20%20%20lp%20%3D%20np.stack(%5Bnp.log(pi%5Bk%5D)%20%2B%20log_norm(X%2C%20mu%5Bk%5D%2C%20S%5Bk%5D)%20for%20k%20in%20range(K)%5D%2C%201)%0A%20%20%20%20%20%20%20%20t%20%3D%20b%20*%20lp%3B%20r%20%3D%20np.exp(t%20-%20t.max(1%2C%20keepdims%3DTrue))%0A%20%20%20%20%20%20%20%20return%20r%20%2F%20r.sum(1%2C%20keepdims%3DTrue)%2C%20np.logaddexp.reduce(lp%2C%201).mean()%0A%20%20%20%20r%2C%20ll%20%3D%20e_step(beta(0))%3B%20prev%20%3D%20-np.inf%0A%20%20%20%20for%20t%20in%20range(N_ITER)%3A%0A%20%20%20%20%20%20%20%20H.append(-(r%20*%20np.log(r%20%2B%201e-12)).sum(1).mean())%0A%20%20%20%20%20%20%20%20if%20hard%3A%20r%20%3D%20np.eye(K)%5Br.argmax(1)%5D%20%20%20%20%20%20%20%20%20%20%20%20%23%20C-step%3A%20argmax%20is%20unchanged%20by%20beta%20%3E%200%0A%20%20%20%20%20%20%20%20nk%20%3D%20r.sum(0)%20%2B%201e-9%3B%20pi%2C%20mu%20%3D%20nk%20%2F%20len(X)%2C%20(r.T%20%40%20X)%20%2F%20nk%5B%3A%2C%20None%5D%0A%20%20%20%20%20%20%20%20S%20%3D%20np.stack(%5B(r%5B%3A%2C%20k%2C%20None%5D%20*%20(X%20-%20mu%5Bk%5D)).T%20%40%20(X%20-%20mu%5Bk%5D)%20%2F%20nk%5Bk%5D%20%2B%201e-6%20*%20np.eye(2)%20for%20k%20in%20range(K)%5D)%0A%20%20%20%20%20%20%20%20if%20beta(t)%20%3E%3D%201%20and%20abs(ll%20-%20prev)%20%2F%20(abs(prev)%20%2B%201e-20)%20%3C%201e-4%3A%20break%20%20%20%23%20tgmm%3A%20convergence%20only%20checked%20at%20beta%20%3D%201%0A%20%20%20%20%20%20%20%20prev%20%3D%20ll%3B%20r%2C%20ll%20%3D%20e_step(beta(t%20%2B%201))%0A%20%20%20%20r%2C%20ll%20%3D%20e_step(1.0)%0A%20%20%20%20return%20mu%2C%20S%2C%20r.argmax(1)%2C%20H%2C%20ll
beta0%20%3D%20mo.ui.slider(0.05%2C%201.0%2C%20step%3D0.05%2C%20value%3D0.7%2C%20label%3D%22starting%20temperature%20%CE%B2%E2%82%80%22%2C%20debounce%3DTrue)%0Aalgo%20%3D%20mo.ui.dropdown(%5B%22EM%22%2C%20%22CEM%22%5D%2C%20value%3D%22EM%22%2C%20label%3D%22algorithm%22)%0Arun%20%3D%20mo.ui.button(value%3D0%2C%20on_click%3Dlambda%20v%3A%20v%20%2B%201%2C%20label%3D%22Run%20from%20collapsed%20init%22)%0Amo.hstack(%5Bbeta0%2C%20algo%2C%20run%5D%2C%20justify%3D%22start%22)
res%20%3D%20%7B%22annealed%22%3A%20fit(algo.value%20%3D%3D%20%22CEM%22%2C%20beta0.value%2C%202%20%2B%20run.value)%2C%0A%20%20%20%20%20%20%20%22plain%22%3A%20fit(algo.value%20%3D%3D%20%22CEM%22%2C%201.0%2C%202%20%2B%20run.value)%7D%0Ares%5B%22ll%22%5D%20%3D%20(algo.value%2C%20beta0.value%2C%202%20%2B%20run.value%2C%20res%5B%22annealed%22%5D%5B4%5D%2C%20res%5B%22plain%22%5D%5B4%5D)
def%20draw(ax%2C%20out%2C%20title)%3A%0A%20%20%20%20mu%2C%20S%2C%20lab%2C%20_%2C%20ll%20%3D%20out%0A%20%20%20%20ax.scatter(*X.T%2C%20c%3Dlab%2C%20cmap%3D%22viridis%22%2C%20s%3D8%2C%20alpha%3D0.6)%0A%20%20%20%20for%20m%2C%20C%20in%20zip(mu%2C%20S)%3A%0A%20%20%20%20%20%20%20%20w%2C%20V%20%3D%20np.linalg.eigh(C)%0A%20%20%20%20%20%20%20%20ax.add_patch(Ellipse(m%2C%20*(4%20*%20np.sqrt(w))%2C%20angle%3Dnp.degrees(np.arctan2(V%5B1%2C%200%5D%2C%20V%5B0%2C%200%5D))%2C%20fill%3DFalse%2C%20lw%3D2%2C%20color%3D%22k%22))%0A%20%20%20%20ax.set_title(f%22%7Btitle%7D%5Cnmean%20log-likelihood%20%7Bll%3A.3f%7D%22)%3B%20ax.set_aspect(%22equal%22)%0Afig%2C%20axs%20%3D%20plt.subplots(1%2C%203%2C%20figsize%3D(15%2C%204.4))%0Adraw(axs%5B0%5D%2C%20res%5B%22plain%22%5D%2C%20%22%CE%B2%20%3D%201%20(plain%20%22%20%2B%20algo.value%20%2B%20%22)%22)%0Adraw(axs%5B1%5D%2C%20res%5B%22annealed%22%5D%2C%20f%22annealed%2C%20%CE%B2%E2%82%80%20%3D%20%7Bbeta0.value%3Ag%7D%22)%0Aaxs%5B2%5D.plot(res%5B%22plain%22%5D%5B3%5D%2C%20label%3D%22%CE%B2%20%3D%201%22)%3B%20axs%5B2%5D.plot(res%5B%22annealed%22%5D%5B3%5D%2C%20label%3D%22annealed%22)%0Aaxs%5B2%5D.axvline(N_ANNEAL%2C%20color%3D%220.6%22%2C%20ls%3D%22%3A%22)%3B%20axs%5B2%5D.set_xlabel(%22iteration%22)%3B%20axs%5B2%5D.set_ylabel(%22mean%20responsibility%20entropy%20(nats)%22)%0Aaxs%5B2%5D.set_title(%22how%20diffuse%20the%20assignments%20are%22)%3B%20axs%5B2%5D.legend()%0Afig
get_log%2C%20set_log%20%3D%20mo.state(%5B%5D)
set_log(lambda%20log%3A%20(log%20%2B%20%5Bres%5B%22ll%22%5D%5D)%5B-6%3A%5D)
Final mean log-likelihood: annealed -3.870 vs. β = 1: -4.380 (difference +0.510). Recent runs (algorithm, β₀, jitter seed, annealed LL, β = 1 LL)
_gain%20%3D%20res%5B%22annealed%22%5D%5B4%5D%20-%20res%5B%22plain%22%5D%5B4%5D%0Amo.vstack(%5Bmo.md(f%22**Final%20mean%20log-likelihood%3A**%20annealed%20%7Bres%5B'annealed'%5D%5B4%5D%3A.3f%7D%20vs.%20%CE%B2%20%3D%201%3A%20%7Bres%5B'plain'%5D%5B4%5D%3A.3f%7D%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22(difference%20%7B_gain%3A%2B.3f%7D).%20%22%20%2B%20(%22Hard%20assignments%3A%20the%20argmax%20of%20the%20tempered%20posterior%20equals%20the%20argmax%20of%20the%20plain%20one%2C%20so%20%CE%B2%20changes%20nothing%20for%20CEM.%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20algo.value%20%3D%3D%20%22CEM%22%20else%20%22%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20mo.md(%22**Recent%20runs**%20(algorithm%2C%20%CE%B2%E2%82%80%2C%20jitter%20seed%2C%20annealed%20LL%2C%20%CE%B2%20%3D%201%20LL)%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20mo.ui.table(%5Bdict(zip((%22algo%22%2C%20%22%CE%B2%E2%82%80%22%2C%20%22seed%22%2C%20%22annealed%22%2C%20%22%CE%B2%3D1%22)%2C%20(a%2C%20b%2C%20s%2C%20round(x%2C%203)%2C%20round(y%2C%203))))%20for%20a%2C%20b%2C%20s%2C%20x%2C%20y%20in%20get_log()%5D%2C%20selection%3DNone)%5D)
What to look for
- EM, β₀ ≈ 0.7 (default): the tempered E-step lets the components see more than the blob they started on, and this
jitter ends in the three-blob solution (higher log-likelihood), while plain EM at \(\beta=1\) settles in a poor local
optimum. The right-hand plot shows how the entropy of the assignments evolves as \(\beta\to1\) (dotted line: end of annealing).
- It is a heuristic, not a guarantee. Press the button a few times: with other jitters annealing ties, or even loses
to plain EM. And too hot a start collapses the fit: for \(\beta_0\lesssim0.3\) the means are pulled together until all
three components describe the same blob (log-likelihood far below either alternative). A near-symmetric state
stays symmetric under tempering, so annealing needs some real asymmetry in the start to amplify.
- CEM: assignments are hard, and \(\arg\max_k r_{ik}(\beta)\) does not depend on \(\beta>0\), so annealing has no effect
(this is also what
tgmm does: the C-step follows the tempered E-step).
- This page mirrors
tgmm's loop: the convergence test is only applied once \(\beta=1\), and the reported value is the true
mean log-likelihood of the final fit (while \(\beta<1\), tgmm records the tempered normaliser).
See the Stochastic EM, Annealing & SAEM tutorial for the same idea with tgmm.