<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>Ahmed Elhossiny</title>
<link>https://aelhossiny.github.io/articles/</link>
<atom:link href="https://aelhossiny.github.io/articles/index.xml" rel="self" type="application/rss+xml"/>
<description>Postdoctoral Fellow in Computational Oncology at Memorial Sloan Kettering Cancer Center</description>
<generator>quarto-1.9.38</generator>
<lastBuildDate>Sun, 15 Jun 2025 04:00:00 GMT</lastBuildDate>
<item>
  <title>The EM Algorithm, Part 3: The Rigorous Treatment</title>
  <link>https://aelhossiny.github.io/articles/em-algorithm/03-rigorous.html</link>
  <description><![CDATA[ 




<blockquote class="blockquote">
<p><strong>Three-part series.</strong> 1. <a href="../../articles/em-algorithm/01-intuition.html">The Intuition</a> — the idea, no heavy math. 2. <a href="../../articles/em-algorithm/02-math.html">Math with Intuition</a> — every equation, explained in plain words. 3. <strong>The Rigorous Treatment</strong> <em>(you are here)</em> — proofs, calculus, and the formal guarantees.</p>
</blockquote>
<p>This part proves what the earlier parts asserted: the ELBO/KL decomposition, the optimality of the E-step, the closed-form M-step, the monotonicity theorem, and convergence to a stationary point. The two-coin model is the running concrete instance.</p>
<hr>
<section id="the-latent-variable-model" class="level2">
<h2 class="anchored" data-anchor-id="the-latent-variable-model">1. The latent-variable model</h2>
<p>Observed data <img src="https://latex.codecogs.com/png.latex?X%20=%20(x_1,%5Cdots,x_n)">, latent variables <img src="https://latex.codecogs.com/png.latex?Z%20=%20(z_1,%5Cdots,z_n)">, parameters <img src="https://latex.codecogs.com/png.latex?%5Ctheta">. The model specifies a joint <img src="https://latex.codecogs.com/png.latex?p(x_i,%20z_i%5Cmid%5Ctheta)">, and the observed-data likelihood marginalizes the latents:</p>
<p><img src="https://latex.codecogs.com/png.latex?p(x_i%5Cmid%5Ctheta)%20=%20%5Csum_%7Bz_i%7D%20p(x_i,%20z_i%5Cmid%5Ctheta),%20%5Cqquad%20%5Cell(%5Ctheta)%20=%20%5Csum_%7Bi=1%7D%5En%20%5Clog%20p(x_i%5Cmid%5Ctheta)."></p>
<p><strong>Coin instance.</strong> <img src="https://latex.codecogs.com/png.latex?z_i%5Cin%5C%7BA,B%5C%7D">, <img src="https://latex.codecogs.com/png.latex?p(z_i%7B=%7DA)=%5Cpi_A">, <img src="https://latex.codecogs.com/png.latex?p(x_i%5Cmid%20z_i%7B=%7DA,%5Ctheta)=b_%7BiA%7D:=%5Cbinom%7Bm%7D%7Bx_i%7D%5Ctheta_A%5E%7Bx_i%7D(1-%5Ctheta_A)%5E%7Bm-x_i%7D">, so <img src="https://latex.codecogs.com/png.latex?p(x_i%5Cmid%5Ctheta)=%5Cpi_A%20b_%7BiA%7D+%5Cpi_B%20b_%7BiB%7D"> with <img src="https://latex.codecogs.com/png.latex?%5Ctheta=(%5Ctheta_A,%5Ctheta_B)">, <img src="https://latex.codecogs.com/png.latex?%5Cpi_A=%5Cpi_B=%5Ctfrac12">, <img src="https://latex.codecogs.com/png.latex?m=10">.</p>
<p>The difficulty is structural: <img src="https://latex.codecogs.com/png.latex?%5Cell"> contains <img src="https://latex.codecogs.com/png.latex?%5Clog%5Csum_%7Bz_i%7D(%5Ccdot)">, a log-of-a-sum, which is not separable in <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> and admits no closed-form maximizer.</p>
</section>
<section id="jensens-inequality-and-a-corollary" class="level2">
<h2 class="anchored" data-anchor-id="jensens-inequality-and-a-corollary">2. Jensen’s inequality and a corollary</h2>
<p><strong>Theorem (Jensen).</strong> If <img src="https://latex.codecogs.com/png.latex?%5Cvarphi:%5Cmathbb%7BR%7D%5Cto%5Cmathbb%7BR%7D"> is concave and <img src="https://latex.codecogs.com/png.latex?X"> a random variable with finite mean, then <img src="https://latex.codecogs.com/png.latex?%5Cvarphi(%5Cmathbb%7BE%7D%5BX%5D)%20%5Cge%20%5Cmathbb%7BE%7D%5B%5Cvarphi(X)%5D">, with equality iff <img src="https://latex.codecogs.com/png.latex?X"> is almost surely constant or <img src="https://latex.codecogs.com/png.latex?%5Cvarphi"> is affine on the support of <img src="https://latex.codecogs.com/png.latex?X">. For a finite convex combination, <img src="https://latex.codecogs.com/png.latex?%5Cvarphi%5Cbig(%5Csum_k%20%5Clambda_k%20x_k%5Cbig)%20%5Cge%20%5Csum_k%20%5Clambda_k%20%5Cvarphi(x_k)"> with <img src="https://latex.codecogs.com/png.latex?%5Clambda_k%5Cge%200">, <img src="https://latex.codecogs.com/png.latex?%5Csum_k%5Clambda_k%20=%201">.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://aelhossiny.github.io/articles/em-algorithm/fig_jensen.png" class="img-fluid figure-img"></p>
<figcaption>Jensen’s inequality: a concave curve lies above its chords</figcaption>
</figure>
</div>
<p><strong>Corollary (Gibbs’ inequality; non-negativity of KL).</strong> For distributions <img src="https://latex.codecogs.com/png.latex?q,%20p"> over the same finite set,</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7BKL%7D(q%5C,%5C%7C%5C,p)%20:=%20%5Csum_z%20q(z)%5Clog%5Cfrac%7Bq(z)%7D%7Bp(z)%7D%20%5Cge%200,%5Cqquad%20%5Ctext%7Bequality%20iff%20%7D%20q%5Cequiv%20p."></p>
<p><em>Proof.</em> Apply Jensen to the concave <img src="https://latex.codecogs.com/png.latex?%5Clog">:</p>
<p><img src="https://latex.codecogs.com/png.latex?-%5Cmathrm%7BKL%7D(q%5C%7Cp)%20=%20%5Csum_z%20q(z)%5Clog%5Cfrac%7Bp(z)%7D%7Bq(z)%7D%20%5Cle%20%5Clog%5Csum_z%20q(z)%5Cfrac%7Bp(z)%7D%7Bq(z)%7D%20=%20%5Clog%5Csum_z%20p(z)%20=%20%5Clog%201%20=%200."></p>
<p>Equality in Jensen requires <img src="https://latex.codecogs.com/png.latex?p(z)/q(z)"> constant on the support, i.e.&nbsp;<img src="https://latex.codecogs.com/png.latex?q%5Cequiv%20p">. <img src="https://latex.codecogs.com/png.latex?%5C;%5Cblacksquare"></p>
<p>Note that this is the <em>same</em> inequality that makes the ELBO a lower bound — seen once as <img src="https://latex.codecogs.com/png.latex?%5Cell%20%5Cge%20%5Cmathcal%7BL%7D"> and once as <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7BKL%7D%5Cge%200">.</p>
</section>
<section id="the-elbo-and-the-exact-decomposition" class="level2">
<h2 class="anchored" data-anchor-id="the-elbo-and-the-exact-decomposition">3. The ELBO and the exact decomposition</h2>
<p>For any distribution <img src="https://latex.codecogs.com/png.latex?q(z_i)"> with the same support as the posterior, define the <strong>evidence lower bound</strong></p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BL%7D(q,%5Ctheta)%20:=%20%5Csum_i%20%5Csum_%7Bz_i%7D%20q(z_i)%5Clog%5Cfrac%7Bp(x_i,z_i%5Cmid%5Ctheta)%7D%7Bq(z_i)%7D."></p>
<p><strong>Theorem (decomposition).</strong> For every <img src="https://latex.codecogs.com/png.latex?q"> and <img src="https://latex.codecogs.com/png.latex?%5Ctheta">,</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cell(%5Ctheta)%20=%20%5Cmathcal%7BL%7D(q,%5Ctheta)%20+%20%5Csum_i%20%5Cmathrm%7BKL%7D%5Cbig(q(z_i)%5C,%5C%7C%5C,p(z_i%5Cmid%20x_i,%5Ctheta)%5Cbig)."></p>
<p><em>Proof (one experiment; sum over <img src="https://latex.codecogs.com/png.latex?i"> at the end).</em> Since <img src="https://latex.codecogs.com/png.latex?%5Csum_%7Bz%7Dq(z)=1"> and using <img src="https://latex.codecogs.com/png.latex?p(x%5Cmid%5Ctheta)=p(x,z%5Cmid%5Ctheta)/p(z%5Cmid%20x,%5Ctheta)">,</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Clog%20p(x%5Cmid%5Ctheta)%20=%20%5Csum_z%20q(z)%5Clog%20p(x%5Cmid%5Ctheta)%20=%20%5Csum_z%20q(z)%5Clog%5Cfrac%7Bp(x,z%5Cmid%5Ctheta)%7D%7Bp(z%5Cmid%20x,%5Ctheta)%7D."></p>
<p>Insert <img src="https://latex.codecogs.com/png.latex?q(z)/q(z)=1"> inside the log and split the resulting product:</p>
<p><img src="https://latex.codecogs.com/png.latex?=%20%5Csum_z%20q(z)%5Clog%5C!%5CBig%5B%5Cfrac%7Bp(x,z%5Cmid%5Ctheta)%7D%7Bq(z)%7D%5Ccdot%5Cfrac%7Bq(z)%7D%7Bp(z%5Cmid%20x,%5Ctheta)%7D%5CBig%5D%20=%20%5Cunderbrace%7B%5Csum_z%20q(z)%5Clog%5Cfrac%7Bp(x,z%5Cmid%5Ctheta)%7D%7Bq(z)%7D%7D_%7B%5Cmathcal%7BL%7D(q,%5Ctheta)%7D%20+%20%5Cunderbrace%7B%5Csum_z%20q(z)%5Clog%5Cfrac%7Bq(z)%7D%7Bp(z%5Cmid%20x,%5Ctheta)%7D%7D_%7B%5Cmathrm%7BKL%7D(q%5C,%5C%7C%5C,p(z%5Cmid%20x,%5Ctheta))%7D.%5C;%5Cblacksquare"></p>
<p>Because <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7BKL%7D%5Cge%200">, we recover <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BL%7D(q,%5Ctheta)%5Cle%5Cell(%5Ctheta)"> for all <img src="https://latex.codecogs.com/png.latex?q">, with equality iff <img src="https://latex.codecogs.com/png.latex?q(z_i)=p(z_i%5Cmid%20x_i,%5Ctheta)"> for all <img src="https://latex.codecogs.com/png.latex?i">.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-1-contents" aria-controls="callout-1" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Aside: the two-term form of the ELBO and the notation “:=”.
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-1" class="callout-1-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Splitting <img src="https://latex.codecogs.com/png.latex?%5Clog%5Cfrac%7Bp%7D%7Bq%7D=%5Clog%20p-%5Clog%20q"> and reading each sum as an expectation gives</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BL%7D(q,%5Ctheta)%20=%20%5Csum_i%20%5Cmathbb%7BE%7D_%7Bq%7D%5Cbig%5B%5Clog%20p(x_i,z_i%5Cmid%5Ctheta)%5Cbig%5D%20-%20%5Csum_i%20%5Cmathbb%7BE%7D_%7Bq%7D%5Cbig%5B%5Clog%20q(z_i)%5Cbig%5D."></p>
<p>The first term is the <img src="https://latex.codecogs.com/png.latex?%5Ctheta">-dependent <strong>expected complete-data log-likelihood</strong> <img src="https://latex.codecogs.com/png.latex?Q(%5Ctheta)"> (Section 5); the second is the negative entropy <img src="https://latex.codecogs.com/png.latex?-H(q)">, independent of <img src="https://latex.codecogs.com/png.latex?%5Ctheta">. The symbol “<img src="https://latex.codecogs.com/png.latex?=:">” (as in <img src="https://latex.codecogs.com/png.latex?(%5Ccdots)%20=:%20%5Cmathcal%7BL%7D">) means “the left side <em>defines</em> the new name on the colon side” — it is an equality by definition, not a derived identity. (<img src="https://latex.codecogs.com/png.latex?:="> is the same with the new name on the left.)</p>
</div>
</div>
</div>
</section>
<section id="the-e-step-optimal-q" class="level2">
<h2 class="anchored" data-anchor-id="the-e-step-optimal-q">4. The E-step: optimal q</h2>
<p><strong>Proposition.</strong> For fixed <img src="https://latex.codecogs.com/png.latex?%5Ctheta">, <img src="https://latex.codecogs.com/png.latex?%5C;%5Carg%5Cmax_%7Bq%7D%5Cmathcal%7BL%7D(q,%5Ctheta)%20=%20%5Carg%5Cmin_%7Bq%7D%5Csum_i%5Cmathrm%7BKL%7D%5Cbig(q(z_i)%5C,%5C%7C%5C,p(z_i%5Cmid%20x_i,%5Ctheta)%5Cbig)%20=%20%5Cbig(p(z_i%5Cmid%20x_i,%5Ctheta)%5Cbig)_i">, and at this optimum <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BL%7D(q,%5Ctheta)=%5Cell(%5Ctheta)">.</p>
<p><em>Proof.</em> In the decomposition <img src="https://latex.codecogs.com/png.latex?%5Cell(%5Ctheta)=%5Cmathcal%7BL%7D(q,%5Ctheta)+%5Csum_i%5Cmathrm%7BKL%7D(%5Ccdots)">, the term <img src="https://latex.codecogs.com/png.latex?%5Cell(%5Ctheta)"> does not depend on <img src="https://latex.codecogs.com/png.latex?q">. Hence maximizing <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BL%7D"> over <img src="https://latex.codecogs.com/png.latex?q"> is equivalent to minimizing the (non-negative) KL sum, which attains its floor of <img src="https://latex.codecogs.com/png.latex?0"> exactly at <img src="https://latex.codecogs.com/png.latex?q(z_i)=p(z_i%5Cmid%20x_i,%5Ctheta)">. <img src="https://latex.codecogs.com/png.latex?%5C;%5Cblacksquare"></p>
<p><strong>Coin instance (responsibilities).</strong> By Bayes’ rule, with the binomial coefficient cancelling,</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cgamma_i%20:=%20p(z_i%7B=%7DA%5Cmid%20x_i,%5Ctheta)%20=%20%5Cfrac%7B%5Cpi_A%5C,%5Ctheta_A%5E%7Bx_i%7D(1-%5Ctheta_A)%5E%7Bm-x_i%7D%7D%7B%5Cpi_A%5C,%5Ctheta_A%5E%7Bx_i%7D(1-%5Ctheta_A)%5E%7Bm-x_i%7D%20+%20%5Cpi_B%5C,%5Ctheta_B%5E%7Bx_i%7D(1-%5Ctheta_B)%5E%7Bm-x_i%7D%7D."></p>
</section>
<section id="the-m-step-maximizing-q" class="level2">
<h2 class="anchored" data-anchor-id="the-m-step-maximizing-q">5. The M-step: maximizing Q</h2>
<p>With <img src="https://latex.codecogs.com/png.latex?q"> fixed (entropy term constant in <img src="https://latex.codecogs.com/png.latex?%5Ctheta">), maximizing <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BL%7D(q,%5Ctheta)"> over <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> is maximizing</p>
<p><img src="https://latex.codecogs.com/png.latex?Q(%5Ctheta)%20=%20%5Csum_i%20%5Cmathbb%7BE%7D_%7Bq%7D%5Cbig%5B%5Clog%20p(x_i,z_i%5Cmid%5Ctheta)%5Cbig%5D%20=%20%5Csum_i%5CBig%5B%5Cgamma_i%5Clog(%5Cpi_A%20b_%7BiA%7D)%20+%20(1-%5Cgamma_i)%5Clog(%5Cpi_B%20b_%7BiB%7D)%5CBig%5D."></p>
<p><strong>Coin instance.</strong> Using <img src="https://latex.codecogs.com/png.latex?%5Clog(%5Cpi_A%20b_%7BiA%7D)%20=%20%5Ctext%7Bconst%7D%20+%20x_i%5Clog%5Ctheta_A%20+%20(m-x_i)%5Clog(1-%5Ctheta_A)">,</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cfrac%7B%5Cpartial%20Q%7D%7B%5Cpartial%5Ctheta_A%7D%20=%20%5Csum_i%20%5Cgamma_i%5CBig%5B%5Cfrac%7Bx_i%7D%7B%5Ctheta_A%7D%20-%20%5Cfrac%7Bm-x_i%7D%7B1-%5Ctheta_A%7D%5CBig%5D%20=%20%5Cfrac%7B1%7D%7B%5Ctheta_A(1-%5Ctheta_A)%7D%5Csum_i%20%5Cgamma_i%5C,(x_i%20-%20m%5Ctheta_A)."></p>
<p>Setting this to zero — and noting <img src="https://latex.codecogs.com/png.latex?%5Cgamma_i"> is a <strong>constant</strong> here (frozen by the E-step) — gives a linear equation with the closed-form root</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cboxed%7B%5C;%5Ctheta_A%5E%7B(t+1)%7D%20=%20%5Cfrac%7B%5Csum_i%20%5Cgamma_i%5C,%20x_i%7D%7Bm%5Csum_i%20%5Cgamma_i%7D,%20%5Cqquad%20%5Ctheta_B%5E%7B(t+1)%7D%20=%20%5Cfrac%7B%5Csum_i%20(1-%5Cgamma_i)%5C,%20x_i%7D%7Bm%5Csum_i%20(1-%5Cgamma_i)%7D%5C;%7D"></p>
<p>The two updates are decoupled because <img src="https://latex.codecogs.com/png.latex?%5Ctheta_B">’s terms in <img src="https://latex.codecogs.com/png.latex?Q"> carry coefficient <img src="https://latex.codecogs.com/png.latex?(1-%5Cgamma_i)"> and vanish under <img src="https://latex.codecogs.com/png.latex?%5Cpartial/%5Cpartial%5Ctheta_A">.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-2-contents" aria-controls="callout-2" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Lemma: the binomial derivative used above.
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-2" class="callout-2-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>For <img src="https://latex.codecogs.com/png.latex?b(%5Ctheta)=%5Cbinom%7Bm%7D%7Bx%7D%5Ctheta%5E%7Bx%7D(1-%5Ctheta)%5E%7Bm-x%7D">,</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cfrac%7Bd%7D%7Bd%5Ctheta%7D%5Clog%20b%20=%20%5Cfrac%7Bx%7D%7B%5Ctheta%7D%20-%20%5Cfrac%7Bm-x%7D%7B1-%5Ctheta%7D%20=%20%5Cfrac%7Bx%20-%20m%5Ctheta%7D%7B%5Ctheta(1-%5Ctheta)%7D,%20%5Cquad%5Ctext%7Bso%7D%5Cquad%20%5Cfrac%7Bdb%7D%7Bd%5Ctheta%7D%20=%20b%5Ccdot%5Cfrac%7Bx%20-%20m%5Ctheta%7D%7B%5Ctheta(1-%5Ctheta)%7D."></p>
<p>This is what makes both the <img src="https://latex.codecogs.com/png.latex?%5Cpartial%20Q/%5Cpartial%5Ctheta_A"> above and the <img src="https://latex.codecogs.com/png.latex?%5Cpartial%5Cell/%5Cpartial%5Ctheta_A"> in Section 6 collapse to factors of <img src="https://latex.codecogs.com/png.latex?(x_i%20-%20m%5Ctheta_A)">.</p>
</div>
</div>
</div>
</section>
<section id="why-no-closed-form-exists-for-ℓ-directly" class="level2">
<h2 class="anchored" data-anchor-id="why-no-closed-form-exists-for-ℓ-directly">6. Why no closed form exists for ℓ directly</h2>
<p>It is instructive to differentiate the <em>intractable</em> <img src="https://latex.codecogs.com/png.latex?%5Cell"> and see precisely where it jams. With <img src="https://latex.codecogs.com/png.latex?p_i%20=%20%5Cpi_A%20b_%7BiA%7D%20+%20%5Cpi_B%20b_%7BiB%7D">,</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cfrac%7B%5Cpartial%5Cell%7D%7B%5Cpartial%5Ctheta_A%7D%20=%20%5Csum_i%20%5Cfrac%7B1%7D%7Bp_i%7D%5C,%5Cfrac%7B%5Cpartial%20p_i%7D%7B%5Cpartial%5Ctheta_A%7D%20=%20%5Csum_i%20%5Cfrac%7B1%7D%7Bp_i%7D%5C,%5Cpi_A%5Cfrac%7B%5Cpartial%20b_%7BiA%7D%7D%7B%5Cpartial%5Ctheta_A%7D%20=%20%5Cfrac%7B1%7D%7B%5Ctheta_A(1-%5Ctheta_A)%7D%5Csum_i%20%5Cunderbrace%7B%5Cfrac%7B%5Cpi_A%20b_%7BiA%7D%7D%7Bp_i%7D%7D_%7B=%5C,%5Cgamma_i%7D%5C,(x_i%20-%20m%5Ctheta_A)."></p>
<p>The chain rule on the outer <img src="https://latex.codecogs.com/png.latex?%5Clog"> drops a <img src="https://latex.codecogs.com/png.latex?1/p_i"> whose denominator <img src="https://latex.codecogs.com/png.latex?p_i"> contains <strong>both</strong> <img src="https://latex.codecogs.com/png.latex?%5Ctheta_A"> and <img src="https://latex.codecogs.com/png.latex?%5Ctheta_B">. Equivalently, the prefactor <img src="https://latex.codecogs.com/png.latex?%5Cgamma_i%20=%20%5Cpi_A%20b_%7BiA%7D/p_i"> is itself a nonlinear function of <img src="https://latex.codecogs.com/png.latex?(%5Ctheta_A,%5Ctheta_B)">. Setting <img src="https://latex.codecogs.com/png.latex?%5Cpartial%5Cell/%5Cpartial%5Ctheta_A=0"> therefore yields</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Ctheta_A%20=%20%5Cfrac%7B%5Csum_i%5Cgamma_i%20x_i%7D%7Bm%5Csum_i%5Cgamma_i%7D%5Cquad%5Ctext%7Bwith%7D%5Cquad%20%5Cgamma_i%20=%20%5Cgamma_i(%5Ctheta_A,%5Ctheta_B),"></p>
<p>an <strong>implicit, transcendental, coupled</strong> system: <img src="https://latex.codecogs.com/png.latex?%5Ctheta_A"> appears on the left and (through <img src="https://latex.codecogs.com/png.latex?%5Cgamma_i">) nonlinearly on the right, and the twin equation for <img src="https://latex.codecogs.com/png.latex?%5Ctheta_B"> mirrors it. No finite algebraic formula solves it.</p>
<p>EM’s resolution is exactly the comparison of Sections 5 and 6: <strong>freezing <img src="https://latex.codecogs.com/png.latex?%5Cgamma_i"> at <img src="https://latex.codecogs.com/png.latex?%5Ctheta%5E%7B(t)%7D"></strong> converts the same stationarity equation into a linear one with a closed-form root. EM is thus a fixed-point iteration <img src="https://latex.codecogs.com/png.latex?%5Ctheta%5E%7B(t+1)%7D%20=%20M(%5Ctheta%5E%7B(t)%7D)"> for the coupled score equations, solved by alternation; a fixed point satisfies the true <img src="https://latex.codecogs.com/png.latex?%5Cnabla%5Cell%20=%200"> (Section 8).</p>
</section>
<section id="the-monotonicity-theorem" class="level2">
<h2 class="anchored" data-anchor-id="the-monotonicity-theorem">7. The monotonicity theorem</h2>
<p><strong>Theorem.</strong> Let <img src="https://latex.codecogs.com/png.latex?q%5E%7B(t)%7D(z_i)%20=%20p(z_i%5Cmid%20x_i,%5Ctheta%5E%7B(t)%7D)"> (E-step) and <img src="https://latex.codecogs.com/png.latex?%5Ctheta%5E%7B(t+1)%7D%20=%20%5Carg%5Cmax_%5Ctheta%20Q(%5Ctheta;%5Ctheta%5E%7B(t)%7D)"> where <img src="https://latex.codecogs.com/png.latex?Q(%5Ctheta;%5Ctheta%5E%7B(t)%7D)%20=%20%5Cmathbb%7BE%7D_%7Bq%5E%7B(t)%7D%7D%5B%5Clog%20p(X,Z%5Cmid%5Ctheta)%5D"> (M-step). Then</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cell(%5Ctheta%5E%7B(t+1)%7D)%20%5Cge%20%5Cell(%5Ctheta%5E%7B(t)%7D)."></p>
<p><em>Proof.</em> Write the decomposition with <img src="https://latex.codecogs.com/png.latex?q%20=%20q%5E%7B(t)%7D"> and abbreviate <img src="https://latex.codecogs.com/png.latex?H%20=%20-%5Csum_i%5Cmathbb%7BE%7D_%7Bq%5E%7B(t)%7D%7D%5B%5Clog%20q%5E%7B(t)%7D%5D"> (the <img src="https://latex.codecogs.com/png.latex?%5Ctheta">-free entropy), so that <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BL%7D(q%5E%7B(t)%7D,%5Ctheta)%20=%20Q(%5Ctheta;%5Ctheta%5E%7B(t)%7D)%20+%20H">. Then</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Baligned%7D%0A%5Cell(%5Ctheta%5E%7B(t+1)%7D)%0A&amp;=%20%5Cmathcal%7BL%7D(q%5E%7B(t)%7D,%5Ctheta%5E%7B(t+1)%7D)%20+%20%5Cunderbrace%7B%5Ctextstyle%5Csum_i%5Cmathrm%7BKL%7D%5Cbig(q%5E%7B(t)%7D%5C,%5C%7C%5C,p(z_i%5Cmid%20x_i,%5Ctheta%5E%7B(t+1)%7D)%5Cbig)%7D_%7B%5Cge%5C,0%7D%20%5C%5C%0A&amp;%5Cge%20%5Cmathcal%7BL%7D(q%5E%7B(t)%7D,%5Ctheta%5E%7B(t+1)%7D)%20=%20Q(%5Ctheta%5E%7B(t+1)%7D;%5Ctheta%5E%7B(t)%7D)%20+%20H%20%5C%5C%0A&amp;%5Cge%20Q(%5Ctheta%5E%7B(t)%7D;%5Ctheta%5E%7B(t)%7D)%20+%20H%20%5Cqquad%20(%5Ctheta%5E%7B(t+1)%7D%5Ctext%7B%20maximizes%20%7D%20Q)%20%5C%5C%0A&amp;=%20%5Cmathcal%7BL%7D(q%5E%7B(t)%7D,%5Ctheta%5E%7B(t)%7D)%20=%20%5Cell(%5Ctheta%5E%7B(t)%7D)%20%5Cqquad%20(%5Cmathrm%7BKL%7D=0%20%5Ctext%7B%20at%20%7D%20%5Ctheta%5E%7B(t)%7D).%5C;%5Cblacksquare%0A%5Cend%7Baligned%7D"></p>
<p>Since <img src="https://latex.codecogs.com/png.latex?%5Cell"> is non-decreasing and (here) bounded above, the sequence <img src="https://latex.codecogs.com/png.latex?%5Cell(%5Ctheta%5E%7B(t)%7D)"> converges.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://aelhossiny.github.io/articles/em-algorithm/fig_staircase.png" class="img-fluid figure-img"></p>
<figcaption>EM as a staircase of lower bounds climbing the log-likelihood</figcaption>
</figure>
</div>
</section>
<section id="convergence-to-a-stationary-point" class="level2">
<h2 class="anchored" data-anchor-id="convergence-to-a-stationary-point">8. Convergence to a stationary point</h2>
<p>Define the gap as a function of <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> with <img src="https://latex.codecogs.com/png.latex?q%20=%20q%5E%7B(t)%7D"> fixed: <img src="https://latex.codecogs.com/png.latex?G(%5Ctheta)%20:=%20%5Csum_i%5Cmathrm%7BKL%7D%5Cbig(q%5E%7B(t)%7D%5C,%5C%7C%5C,p(z_i%5Cmid%20x_i,%5Ctheta)%5Cbig)%20%5Cge%200">. By construction <img src="https://latex.codecogs.com/png.latex?G(%5Ctheta%5E%7B(t)%7D)%20=%200">, so <img src="https://latex.codecogs.com/png.latex?%5Ctheta%5E%7B(t)%7D"> is a global minimizer of <img src="https://latex.codecogs.com/png.latex?G">, whence (assuming differentiability) <img src="https://latex.codecogs.com/png.latex?%5Cnabla%20G(%5Ctheta%5E%7B(t)%7D)%20=%200">. Differentiating the decomposition <img src="https://latex.codecogs.com/png.latex?%5Cell%20=%20%5Cmathcal%7BL%7D(q%5E%7B(t)%7D,%5Ccdot)%20+%20G"> at <img src="https://latex.codecogs.com/png.latex?%5Ctheta%5E%7B(t)%7D"> gives the <strong>tangency identity</strong></p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cnabla%5Cell(%5Ctheta%5E%7B(t)%7D)%20=%20%5Cnabla_%5Ctheta%20%5Cmathcal%7BL%7D(q%5E%7B(t)%7D,%5Ctheta)%5Cbig%7C_%7B%5Ctheta%5E%7B(t)%7D%7D%20=%20%5Cnabla_%5Ctheta%20Q(%5Ctheta;%5Ctheta%5E%7B(t)%7D)%5Cbig%7C_%7B%5Ctheta%5E%7B(t)%7D%7D."></p>
<p>Consequently, if <img src="https://latex.codecogs.com/png.latex?%5Ctheta%5E%5Cstar"> is a fixed point of EM, then <img src="https://latex.codecogs.com/png.latex?%5Ctheta%5E%5Cstar"> maximizes <img src="https://latex.codecogs.com/png.latex?Q(%5Ccdot;%5Ctheta%5E%5Cstar)">, so <img src="https://latex.codecogs.com/png.latex?%5Cnabla_%5Ctheta%20Q(%5Ctheta%5E%5Cstar;%5Ctheta%5E%5Cstar)=0">, and by tangency <img src="https://latex.codecogs.com/png.latex?%5Cnabla%5Cell(%5Ctheta%5E%5Cstar)%20=%200">: <strong>every EM fixed point is a stationary point of the marginal log-likelihood.</strong> (EM may converge to a local maximum or saddle, not necessarily the global maximum; initialization matters.)</p>
<p>The tangency also explains the <em>rate</em>: near <img src="https://latex.codecogs.com/png.latex?%5Ctheta%5E%5Cstar"> each bound peaks almost directly above its contact point, so steps shrink and the tail is linear (slow).</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://aelhossiny.github.io/articles/em-algorithm/fig_elbo_bounds.png" class="img-fluid figure-img"></p>
<figcaption>Real ELBO bounds touching and being climbed (1-D slice, θ_B fixed at 0.52)</figcaption>
</figure>
</div>
<p>The figure (a 1-D slice fixing <img src="https://latex.codecogs.com/png.latex?%5Ctheta_B=0.52">) shows each amber ELBO touching <img src="https://latex.codecogs.com/png.latex?%5Cell"> at a green point where <img src="https://latex.codecogs.com/png.latex?q"> equals the posterior (KL <img src="https://latex.codecogs.com/png.latex?=0">), the M-step reaching its peak (orange diamond), and the reopened KL gap between each diamond and the next contact point above it.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-3-contents" aria-controls="callout-3" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Aside: EM as coordinate ascent on a single objective.
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-3" class="callout-3-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Both steps maximize the <em>same</em> function <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BL%7D(q,%5Ctheta)">: the E-step ascends in <img src="https://latex.codecogs.com/png.latex?q"> (with <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> fixed) to $q = $ posterior; the M-step ascends in <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> (with <img src="https://latex.codecogs.com/png.latex?q"> fixed) to <img src="https://latex.codecogs.com/png.latex?%5Carg%5Cmax_%5Ctheta%20Q">. EM is therefore block-coordinate ascent on <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BL%7D">, which converges to a stationary point of <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BL%7D">; by the decomposition, stationary points of <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BL%7D"> in <img src="https://latex.codecogs.com/png.latex?(q,%5Ctheta)"> project to stationary points of <img src="https://latex.codecogs.com/png.latex?%5Cell"> in <img src="https://latex.codecogs.com/png.latex?%5Ctheta">.</p>
</div>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-4-contents" aria-controls="callout-4" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Aside: generalization beyond two coins.
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-4" class="callout-4-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Nothing above is special to binomials. For any model with complete-data likelihood in an exponential family, the M-step replaces sufficient statistics by their posterior expectations under <img src="https://latex.codecogs.com/png.latex?q%5E%7B(t)%7D"> (expected sufficient statistics), and the E-step computes those expectations. Gaussian mixtures, HMMs (Baum–Welch), and factor analysers are all instances: same decomposition, same monotonicity proof, same tangency-based stationarity.</p>
</div>
</div>
</div>
</section>
<section id="numerical-appendix" class="level2">
<h2 class="anchored" data-anchor-id="numerical-appendix">9. Numerical appendix</h2>
<p>EM on <img src="https://latex.codecogs.com/png.latex?X=(5,9,8,4,7)">, <img src="https://latex.codecogs.com/png.latex?m=10">, <img src="https://latex.codecogs.com/png.latex?%5Cpi_A=%5Cpi_B=%5Ctfrac12">, seed <img src="https://latex.codecogs.com/png.latex?%5Ctheta%5E%7B(0)%7D=(0.55,0.45)">. Responsibilities <img src="https://latex.codecogs.com/png.latex?%5Cgamma%5E%7B(t)%7D"> are computed at <img src="https://latex.codecogs.com/png.latex?%5Ctheta%5E%7B(t)%7D">; the row shows the resulting <img src="https://latex.codecogs.com/png.latex?%5Ctheta%5E%7B(t+1)%7D"> and the new <img src="https://latex.codecogs.com/png.latex?%5Cell">.</p>
<table class="caption-top table">
<colgroup>
<col style="width: 20%">
<col style="width: 20%">
<col style="width: 20%">
<col style="width: 20%">
<col style="width: 20%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?t"></th>
<th style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Cgamma%5E%7B(t)%7D"> (per experiment)</th>
<th style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Ctheta_A%5E%7B(t+1)%7D"></th>
<th style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Ctheta_B%5E%7B(t+1)%7D"></th>
<th style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Cell(%5Ctheta%5E%7B(t+1)%7D)"></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: center;">0</td>
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5B0.500,%5C,0.833,%5C,0.769,%5C,0.401,%5C,0.691%5D"></td>
<td style="text-align: center;">0.7073</td>
<td style="text-align: center;">0.5765</td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?-10.1061"></td>
</tr>
<tr class="even">
<td style="text-align: center;">1</td>
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5B0.305,%5C,0.813,%5C,0.710,%5C,0.198,%5C,0.580%5D"></td>
<td style="text-align: center;">0.7435</td>
<td style="text-align: center;">0.5691</td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?-9.9552"></td>
</tr>
<tr class="odd">
<td style="text-align: center;">2</td>
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5B0.222,%5C,0.868,%5C,0.750,%5C,0.115,%5C,0.578%5D"></td>
<td style="text-align: center;">0.7671</td>
<td style="text-align: center;">0.5500</td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?-9.8574"></td>
</tr>
<tr class="even">
<td style="text-align: center;">3</td>
<td style="text-align: left;">—</td>
<td style="text-align: center;">0.7826</td>
<td style="text-align: center;">0.5350</td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?-9.8126"></td>
</tr>
<tr class="odd">
<td style="text-align: center;">4</td>
<td style="text-align: left;">—</td>
<td style="text-align: center;">0.7908</td>
<td style="text-align: center;">0.5265</td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?-9.8000"></td>
</tr>
<tr class="even">
<td style="text-align: center;">5</td>
<td style="text-align: left;">—</td>
<td style="text-align: center;">0.7944</td>
<td style="text-align: center;">0.5225</td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?-9.7974"></td>
</tr>
<tr class="odd">
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Cvdots"></td>
<td style="text-align: left;"></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Cvdots"></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Cvdots"></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Cvdots"></td>
</tr>
<tr class="even">
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Cto"></td>
<td style="text-align: left;">converged</td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?0.7968"></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?0.5196"></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?-9.7969"></td>
</tr>
</tbody>
</table>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://aelhossiny.github.io/articles/em-algorithm/fig_convergence.png" class="img-fluid figure-img"></p>
<figcaption>EM convergence from an off-target seed</figcaption>
</figure>
</div>
<p><strong>Worked first update (verification):</strong> <img src="https://latex.codecogs.com/png.latex?%5Ctheta_A%5E%7B(1)%7D%20=%20%5Cfrac%7B0.500(5)+0.833(9)+0.769(8)+0.401(4)+0.691(7)%7D%7B10%5C,(0.500+0.833+0.769+0.401+0.691)%7D%20=%20%5Cfrac%7B22.59%7D%7B31.94%7D%20=%200.7073."></p>
<p>The log-likelihood column is monotone non-decreasing (Section 7); <img src="https://latex.codecogs.com/png.latex?%5Ctheta_B"> is non-monotone (it overshoots to <img src="https://latex.codecogs.com/png.latex?0.5765"> then relaxes), consistent with the theorem guaranteeing ascent only in <img src="https://latex.codecogs.com/png.latex?%5Cell">, not in individual coordinates.</p>
<hr>
<p>← <a href="../../articles/em-algorithm/02-math.html">Part 2 — Math with Intuition</a> | <a href="../../articles/em-algorithm/01-intuition.html">Back to Part 1 — The Intuition</a></p>


</section>

 ]]></description>
  <category>statistics</category>
  <category>machine-learning</category>
  <category>algorithms</category>
  <category>EM-algorithm</category>
  <guid>https://aelhossiny.github.io/articles/em-algorithm/03-rigorous.html</guid>
  <pubDate>Sun, 15 Jun 2025 04:00:00 GMT</pubDate>
</item>
<item>
  <title>The EM Algorithm, Part 2: Math with Intuition</title>
  <link>https://aelhossiny.github.io/articles/em-algorithm/02-math.html</link>
  <description><![CDATA[ 




<blockquote class="blockquote">
<p><strong>Three-part series.</strong> 1. <a href="../../articles/em-algorithm/01-intuition.html">The Intuition</a> — the idea, no heavy math. 2. <strong>Math with Intuition</strong> <em>(you are here)</em> — every equation, explained in plain words. 3. <a href="../../articles/em-algorithm/03-rigorous.html">The Rigorous Treatment</a> — proofs, calculus, and the formal guarantees.</p>
</blockquote>
<p>This part keeps the two-coin example from Part 1 and builds the full machinery: the marginal likelihood, the obstacle that makes it hard, Jensen’s inequality, the ELBO, the KL gap, and the geometry that guarantees EM works.</p>
<hr>
<section id="notation" class="level2">
<h2 class="anchored" data-anchor-id="notation">Notation</h2>
<p>Per experiment <img src="https://latex.codecogs.com/png.latex?i">: <img src="https://latex.codecogs.com/png.latex?x_i"> = heads observed (data), <img src="https://latex.codecogs.com/png.latex?z_i%20%5Cin%20%5C%7BA,B%5C%7D"> = which coin was used (latent). Parameters <img src="https://latex.codecogs.com/png.latex?%5Ctheta%20=%20(%5Ctheta_A,%20%5Ctheta_B)">; mixing priors <img src="https://latex.codecogs.com/png.latex?%5Cpi_A%20=%20%5Cpi_B%20=%20%5Ctfrac12">; <img src="https://latex.codecogs.com/png.latex?m%20=%2010"> flips. One coin gives a binomial:</p>
<p><img src="https://latex.codecogs.com/png.latex?p(x_i%20%5Cmid%20z_i%20=%20A,%20%5Ctheta)%20=%20%5Cbinom%7Bm%7D%7Bx_i%7D%5C,%5Ctheta_A%5E%7Bx_i%7D(1-%5Ctheta_A)%5E%7Bm-x_i%7D."></p>
<p>For brevity write <img src="https://latex.codecogs.com/png.latex?b_%7BiA%7D%20=%20%5Cbinom%7Bm%7D%7Bx_i%7D%5Ctheta_A%5E%7Bx_i%7D(1-%5Ctheta_A)%5E%7Bm-x_i%7D"> (and similarly <img src="https://latex.codecogs.com/png.latex?b_%7BiB%7D">).</p>
</section>
<section id="the-thing-we-actually-want-to-maximize" class="level2">
<h2 class="anchored" data-anchor-id="the-thing-we-actually-want-to-maximize">The thing we actually want to maximize</h2>
<p>We only see the <img src="https://latex.codecogs.com/png.latex?x_i">, so the quantity to maximize is the <strong>marginal log-likelihood</strong>, summing the latent coin out:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cell(%5Ctheta)%20=%20%5Csum_i%20%5Clog%20p(x_i%20%5Cmid%20%5Ctheta)%20=%20%5Csum_i%20%5Clog%20%5Csum_%7Bz_i%20%5Cin%20%5C%7BA,B%5C%7D%7D%20p(x_i,%20z_i%20%5Cmid%20%5Ctheta)."></p>
<p>Reading it from the inside out:</p>
<ul>
<li><strong>The joint</strong> <img src="https://latex.codecogs.com/png.latex?p(x_i,%20z_i%20%5Cmid%20%5Ctheta)%20=%20p(z_i)%5C,p(x_i%5Cmid%20z_i,%5Ctheta)"> — “pick a coin, then flip it.” For the two coins: <img src="https://latex.codecogs.com/png.latex?%5Cpi_A%20b_%7BiA%7D"> and <img src="https://latex.codecogs.com/png.latex?%5Cpi_B%20b_%7BiB%7D">.</li>
<li><strong>The inner sum <img src="https://latex.codecogs.com/png.latex?%5Csum_%7Bz_i%7D"></strong> is <em>marginalization</em>: you can’t see the coin, so you add up over every value it could take. The result is the per-experiment marginal, a <strong>mixture of two binomials</strong>:</li>
</ul>
<p><img src="https://latex.codecogs.com/png.latex?p(x_i%20%5Cmid%20%5Ctheta)%20=%20%5Cpi_A%5C,b_%7BiA%7D%20+%20%5Cpi_B%5C,b_%7BiB%7D."></p>
<ul>
<li><strong>The <img src="https://latex.codecogs.com/png.latex?%5Clog"></strong> puts it on log scale; <strong>the outer <img src="https://latex.codecogs.com/png.latex?%5Csum_i"></strong> adds across the (independent) experiments.</li>
</ul>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-1-contents" aria-controls="callout-1" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Do we get the marginal first then take the log, or take the log of the sum then add up?
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-1" class="callout-1-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Both — they’re the same number, via one log identity. There are <strong>two</strong> sums: an inner sum over the <em>coin</em> (builds each marginal) and an outer combination over <em>experiments</em>. Because experiments are independent, the whole-dataset likelihood is a <strong>product</strong> of the marginals:</p>
<p><img src="https://latex.codecogs.com/png.latex?L(%5Ctheta)%20=%20%5Cprod_i%20p(x_i%5Cmid%5Ctheta)%20=%20%5Cprod_i%5CBig%5B%5Csum_%7Bz_i%7D%20p(x_i,z_i%5Cmid%5Ctheta)%5CBig%5D."></p>
<p>Take one log of that product, and <img src="https://latex.codecogs.com/png.latex?%5Clog(ab)%20=%20%5Clog%20a%20+%20%5Clog%20b"> turns it into a sum of logs:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cell(%5Ctheta)%20=%20%5Csum_i%20%5Clog%20p(x_i%5Cmid%5Ctheta)%20=%20%5Csum_i%20%5Clog%20%5Csum_%7Bz_i%7D%20p(x_i,z_i%5Cmid%5Ctheta)."></p>
<p>So “multiply the marginals, then log” and “log each marginal, then add” land on the identical <img src="https://latex.codecogs.com/png.latex?%5Cell(%5Ctheta)">. In practice we always use the sum-of-logs form (adding beats multiplying tiny probabilities that underflow).</p>
</div>
</div>
</div>
</section>
<section id="the-obstacle-log-of-a-sum" class="level2">
<h2 class="anchored" data-anchor-id="the-obstacle-log-of-a-sum">The obstacle: log of a sum</h2>
<p>Look at the shape of the right-hand side: a <img src="https://latex.codecogs.com/png.latex?%5Clog"> wrapped around a <strong>sum</strong>.</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Clog%20p(x_i%5Cmid%5Ctheta)%20=%20%5Clog%5Cbig(%5Cpi_A%20b_%7BiA%7D%20+%20%5Cpi_B%20b_%7BiB%7D%5Cbig)."></p>
<p>You can’t distribute it — <img src="https://latex.codecogs.com/png.latex?%5Clog(a+b)%20%5Cneq%20%5Clog%20a%20+%20%5Clog%20b"> — so the two unknowns <img src="https://latex.codecogs.com/png.latex?%5Ctheta_A">, <img src="https://latex.codecogs.com/png.latex?%5Ctheta_B"> stay tangled inside that log, with no clean derivative and no closed-form maximizer. <strong>This single obstacle is the entire reason EM exists.</strong></p>
<p>Contrast the easy world: if the coin labels were <em>known</em>, the log would sit on a single binomial, and <img src="https://latex.codecogs.com/png.latex?%5Clog%20b_%7BiA%7D%20=%20%5Ctext%7Bconst%7D%20+%20x_i%5Clog%5Ctheta_A%20+%20(m-x_i)%5Clog(1-%5Ctheta_A)"> — a tidy, separable expression you can maximize in closed form. The sum is what ruins this.</p>
</section>
<section id="slipping-in-a-helper-distribution-q" class="level2">
<h2 class="anchored" data-anchor-id="slipping-in-a-helper-distribution-q">Slipping in a helper distribution q</h2>
<p>Introduce a <strong>free distribution</strong> <img src="https://latex.codecogs.com/png.latex?q(z_i)"> over the latent coin — just two numbers <img src="https://latex.codecogs.com/png.latex?q(z_i%7B=%7DA)"> and <img src="https://latex.codecogs.com/png.latex?q(z_i%7B=%7DB)"> summing to 1. “Free” means <em>we</em> choose it, and the algebra below holds for <em>any</em> choice.</p>
<p>Multiply and divide inside the sum by <img src="https://latex.codecogs.com/png.latex?q"> (which equals multiplying by 1):</p>
<p><img src="https://latex.codecogs.com/png.latex?p(x_i%20%5Cmid%20%5Ctheta)%20=%20%5Csum_%7Bz_i%7D%20q(z_i)%5C,%5Cfrac%7Bp(x_i,%20z_i%5Cmid%5Ctheta)%7D%7Bq(z_i)%7D%20=%20%5Cmathbb%7BE%7D_%7Bq%7D%5C!%5Cleft%5B%5Cfrac%7Bp(x_i,%20z_i%5Cmid%5Ctheta)%7D%7Bq(z_i)%7D%5Cright%5D."></p>
<p>That last equality is just the <strong>definition of expectation</strong>, <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D_q%5Bg(z)%5D%20=%20%5Csum_z%20q(z)%5C,g(z)">. So the marginal is now “log of an expectation,” <img src="https://latex.codecogs.com/png.latex?%5Clog%5Cmathbb%7BE%7D_q%5B%5Ccdot%5D">.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-2-contents" aria-controls="callout-2" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Why does dividing by q turn the sum into an expectation, and what does “free” mean?
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-2" class="callout-2-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>An expectation <em>is</em> the pattern “sum over outcomes of (probability) × (value).” By writing <img src="https://latex.codecogs.com/png.latex?%5Csum_%7Bz_i%7D%20q(z_i)%5Ccdot%5Cfrac%7Bp%7D%7Bq(z_i)%7D"> we forced the marginal into exactly that shape, with <img src="https://latex.codecogs.com/png.latex?q"> as the weighting and <img src="https://latex.codecogs.com/png.latex?%5Cfrac%7Bp%7D%7Bq%7D"> as the function being averaged. The two copies of <img src="https://latex.codecogs.com/png.latex?q"> don’t cancel <em>conceptually</em>: the front one is a probability weight, the bottom one is part of the function — and that separation is what sets up the next step.</p>
<p>“Free” means <img src="https://latex.codecogs.com/png.latex?q"> is a variable we control, constrained only by being a distribution (<img src="https://latex.codecogs.com/png.latex?q(z_i)%5Cge%200">, sums to 1, and nonzero wherever the joint is). The whole point of slipping it in is to reshape the stubborn <img src="https://latex.codecogs.com/png.latex?%5Clog%5Csum"> into a <img src="https://latex.codecogs.com/png.latex?%5Clog%5Cmathbb%7BE%7D"> that Jensen’s inequality can act on.</p>
</div>
</div>
</div>
</section>
<section id="jensens-inequality" class="level2">
<h2 class="anchored" data-anchor-id="jensens-inequality">Jensen’s inequality</h2>
<p>For a <strong>concave</strong> function <img src="https://latex.codecogs.com/png.latex?%5Cvarphi"> (like <img src="https://latex.codecogs.com/png.latex?%5Clog">), the function of an average is at least the average of the function:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cvarphi%5Cbig(%5Cmathbb%7BE%7D%5BX%5D%5Cbig)%20%5Cge%20%5Cmathbb%7BE%7D%5Cbig%5B%5Cvarphi(X)%5Cbig%5D."></p>
<p>Geometrically: a concave curve bulges <em>above</em> its chords, so evaluating the curve at the average (a point on the curve) sits at or above averaging two curve-heights (a point on the chord).</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://aelhossiny.github.io/articles/em-algorithm/fig_jensen.png" class="img-fluid figure-img"></p>
<figcaption>Jensen’s inequality: chord below curve, with the Jensen gap</figcaption>
</figure>
</div>
<p>The green dot is “function of the average” (on the curve); the orange dot is “average of the function” (on the chord); the vertical distance is the <strong>Jensen gap</strong>, which is zero only when the two inputs coincide.</p>
<p>Applying this to our <img src="https://latex.codecogs.com/png.latex?%5Clog%5Cmathbb%7BE%7D_q%5B%5Ccdot%5D"> — concave <img src="https://latex.codecogs.com/png.latex?%5Clog">, so push it inside at the cost of an inequality:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Clog%20p(x_i%5Cmid%5Ctheta)%20=%20%5Clog%5Cmathbb%7BE%7D_q%5C!%5Cleft%5B%5Cfrac%7Bp(x_i,z_i%5Cmid%5Ctheta)%7D%7Bq(z_i)%7D%5Cright%5D%20%5C;%5Cge%5C;%20%5Cmathbb%7BE%7D_q%5C!%5Cleft%5B%5Clog%5Cfrac%7Bp(x_i,z_i%5Cmid%5Ctheta)%7D%7Bq(z_i)%7D%5Cright%5D%20=%20%5Csum_%7Bz_i%7D%20q(z_i)%5Clog%5Cfrac%7Bp(x_i,z_i%5Cmid%5Ctheta)%7D%7Bq(z_i)%7D."></p>
<p>That right-hand side is the <strong>ELBO</strong>, written <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BL%7D(q,%5Ctheta)">. The <img src="https://latex.codecogs.com/png.latex?%5Clog"> now sits <strong>outside</strong> the sum, on single product terms — exactly the splittable, differentiable form we wanted.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-3-contents" aria-controls="callout-3" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Doesn’t splitting that log violate log(a+b) ≠ log(a)+log(b)?
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-3" class="callout-3-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>No — the thing inside this log is a <strong>quotient</strong>, not a sum. <img src="https://latex.codecogs.com/png.latex?%5Clog"> splits over <img src="https://latex.codecogs.com/png.latex?%5Ctimes"> and <img src="https://latex.codecogs.com/png.latex?%5Cdiv"> freely (<img src="https://latex.codecogs.com/png.latex?%5Clog%5Cfrac%7Ba%7D%7Bb%7D%20=%20%5Clog%20a%20-%20%5Clog%20b">); it’s only <img src="https://latex.codecogs.com/png.latex?%5Clog(a+b)"> that’s forbidden. The sum over <img src="https://latex.codecogs.com/png.latex?z_i"> is now <strong>outside</strong> the log (it’s the expectation’s weighting), while <strong>inside</strong> each log there’s just one fraction <img src="https://latex.codecogs.com/png.latex?%5Cfrac%7Bp(x_i,z_i%5Cmid%5Ctheta)%7D%7Bq(z_i)%7D">. Jensen’s whole job was to relocate the sum from inside the log (forbidden) to outside it (fine) — which is precisely what makes this split legal.</p>
</div>
</div>
</div>
</section>
<section id="the-exact-gap-is-a-kl-divergence" class="level2">
<h2 class="anchored" data-anchor-id="the-exact-gap-is-a-kl-divergence">The exact gap is a KL divergence</h2>
<p>The inequality has an exact bookkeeping form. For <em>any</em> <img src="https://latex.codecogs.com/png.latex?q">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cboxed%7B%5C;%5Clog%20p(x_i%20%5Cmid%20%5Ctheta)%20=%20%5Cunderbrace%7B%5Cmathcal%7BL%7D(q,%5Ctheta)%7D_%7B%5Ctext%7BELBO%7D%7D%20+%20%5Cunderbrace%7B%5Cmathrm%7BKL%7D%5Cbig(q(z_i)%5C,%5C%7C%5C,p(z_i%5Cmid%20x_i,%5Ctheta)%5Cbig)%7D_%7B%5Cge%5C,0%7D%5C;%7D"></p>
<p>The gap between the true log-likelihood and the ELBO is exactly the KL divergence between your chosen <img src="https://latex.codecogs.com/png.latex?q"> and the <strong>posterior</strong> <img src="https://latex.codecogs.com/png.latex?p(z_i%5Cmid%20x_i,%5Ctheta)">. The KL has two properties that drive everything:</p>
<ul>
<li><img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7BKL%7D%20%5Cge%200"> always — so the ELBO is always a <em>lower</em> bound;</li>
<li><img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7BKL%7D%20=%200"> <strong>iff</strong> <img src="https://latex.codecogs.com/png.latex?q(z_i)%20=%20p(z_i%5Cmid%20x_i,%5Ctheta)"> — so the bound is <em>tight</em> exactly when <img src="https://latex.codecogs.com/png.latex?q"> is the posterior.</li>
</ul>
<p>On the coins, with <img src="https://latex.codecogs.com/png.latex?q(z%7B=%7DA)%20=%20w"> and posterior <img src="https://latex.codecogs.com/png.latex?p(z%7B=%7DA%5Cmid%20x,%5Ctheta)%20=%20%5Cgamma">, the KL is just</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7BKL%7D%20=%20w%5Clog%5Cfrac%7Bw%7D%7B%5Cgamma%7D%20+%20(1-w)%5Clog%5Cfrac%7B1-w%7D%7B1-%5Cgamma%7D,"></p>
<p>a concrete number measuring how far your working belief <img src="https://latex.codecogs.com/png.latex?w"> is from the row’s actual posterior <img src="https://latex.codecogs.com/png.latex?%5Cgamma"> — zero when they match.</p>
</section>
<section id="the-two-steps-and-the-geometry" class="level2">
<h2 class="anchored" data-anchor-id="the-two-steps-and-the-geometry">The two steps, and the geometry</h2>
<p>Rearranging the boxed identity, <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BL%7D(q,%5Ctheta)%20=%20%5Cell(%5Ctheta)%20-%20%5Cmathrm%7BKL%7D">. Hold <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> fixed: <img src="https://latex.codecogs.com/png.latex?%5Cell(%5Ctheta)"> is a constant, so <strong>maximizing the ELBO over <img src="https://latex.codecogs.com/png.latex?q"> = minimizing the KL over <img src="https://latex.codecogs.com/png.latex?q"></strong> — a seesaw. Both bottom out at $q = $ posterior.</p>
<p><strong>E-step (touch).</strong> Set <img src="https://latex.codecogs.com/png.latex?q%20=%20p(z_i%5Cmid%20x_i,%5Ctheta_%7B%5Ctext%7Bold%7D%7D)">, the posterior at your current spot. This zeroes the KL <em>at <img src="https://latex.codecogs.com/png.latex?%5Ctheta_%7B%5Ctext%7Bold%7D%7D"></em>, so the ELBO rises up to <strong>touch</strong> the true curve right where you stand.</p>
<p><strong>M-step (climb).</strong> Freeze that <img src="https://latex.codecogs.com/png.latex?q"> and maximize the ELBO over <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> — <img src="https://latex.codecogs.com/png.latex?%5Ctheta_%7B%5Ctext%7Bnew%7D%7D">. Since the bound is easy and decoupled, this is a closed-form update.</p>
<p><strong>Why climbing the bound raises the truth</strong> — chain three facts:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cell(%5Ctheta_%7B%5Ctext%7Bnew%7D%7D)%20%5Cunderbrace%7B%5Cge%7D_%7B%5Ctext%7Bbound%20below%20truth%7D%7D%20%5Cmathcal%7BL%7D(q,%5Ctheta_%7B%5Ctext%7Bnew%7D%7D)%20%5Cunderbrace%7B%5Cge%7D_%7B%5Ctheta_%7B%5Ctext%7Bnew%7D%7D%5Ctext%7B%20is%20the%20peak%7D%7D%20%5Cmathcal%7BL%7D(q,%5Ctheta_%7B%5Ctext%7Bold%7D%7D)%20%5Cunderbrace%7B=%7D_%7B%5Ctext%7Btouch%7D%7D%20%5Cell(%5Ctheta_%7B%5Ctext%7Bold%7D%7D)."></p>
<p>End to end: <img src="https://latex.codecogs.com/png.latex?%5Cell(%5Ctheta_%7B%5Ctext%7Bnew%7D%7D)%20%5Cge%20%5Cell(%5Ctheta_%7B%5Ctext%7Bold%7D%7D)">. The true log-likelihood never decreases.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://aelhossiny.github.io/articles/em-algorithm/fig_minorize_maximize.png" class="img-fluid figure-img"></p>
<figcaption>One EM step: the bound touches the true curve, then is climbed</figcaption>
</figure>
</div>
<p>Because the dome reopens a gap once you step off <img src="https://latex.codecogs.com/png.latex?%5Ctheta_%7B%5Ctext%7Bold%7D%7D">, you rebuild it each round — a staircase of bounds walking up the true curve:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://aelhossiny.github.io/articles/em-algorithm/fig_staircase.png" class="img-fluid figure-img"></p>
<figcaption>EM as a staircase of lower bounds climbing the log-likelihood</figcaption>
</figure>
</div>
<p>The steps shrink near the top: as you approach the optimum, each bound’s peak sits almost directly above its touch point (the bound is <em>tangent</em> to <img src="https://latex.codecogs.com/png.latex?%5Cell"> there), so there’s less left to climb — which is both why EM converges and why its tail is slow.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-4-contents" aria-controls="callout-4" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Why does the bound curve down instead of following the true curve?
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-4" class="callout-4-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Because the <img src="https://latex.codecogs.com/png.latex?q"> in the bound is <strong>frozen</strong> at its <img src="https://latex.codecogs.com/png.latex?%5Ctheta_%7B%5Ctext%7Bold%7D%7D"> value during the climb. The gap is <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7BKL%7D(q%5C,%5C%7C%5C,p(z%5Cmid%20x,%5Ctheta))">, which compares your frozen <img src="https://latex.codecogs.com/png.latex?q"> to the posterior <em>at the current <img src="https://latex.codecogs.com/png.latex?%5Ctheta"></em>. At <img src="https://latex.codecogs.com/png.latex?%5Ctheta_%7B%5Ctext%7Bold%7D%7D"> they match (KL = 0, touch). Step away and the posterior shifts while <img src="https://latex.codecogs.com/png.latex?q"> does not, so KL grows and the dome sags below the true curve by exactly that amount. If you let <img src="https://latex.codecogs.com/png.latex?q"> track the posterior at every <img src="https://latex.codecogs.com/png.latex?%5Ctheta">, KL would be zero everywhere and the dome would <em>be</em> the true log-likelihood — with all its intractability back. The sag is the price of making the bound easy; re-touching each round is how EM keeps paying it.</p>
</div>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-5-contents" aria-controls="callout-5" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>How can q be the “true posterior” if I don’t know the right θ yet?
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-5" class="callout-5-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p><img src="https://latex.codecogs.com/png.latex?q"> is the true posterior <strong>given your current <img src="https://latex.codecogs.com/png.latex?%5Ctheta"></strong>, not given the correct <img src="https://latex.codecogs.com/png.latex?%5Ctheta%5E%5Cstar">. The posterior under the right parameters, <img src="https://latex.codecogs.com/png.latex?p(z%5Cmid%20x,%5Ctheta%5E%5Cstar)">, is indeed uncomputable — but EM never needs it. It only ever computes <img src="https://latex.codecogs.com/png.latex?p(z%5Cmid%20x,%5Ctheta_%7B%5Ctext%7Bold%7D%7D)">, which uses the concrete numbers you’re holding. Within one iteration EM treats <img src="https://latex.codecogs.com/png.latex?%5Ctheta_%7B%5Ctext%7Bold%7D%7D"> <em>as if</em> it were correct: “suppose the biases are <img src="https://latex.codecogs.com/png.latex?(0.6,%200.5)">; then the mathematically correct belief about the coins is exactly this posterior.” That conditional is exactly true regardless of whether <img src="https://latex.codecogs.com/png.latex?(0.6,0.5)"> is right. Feeding this self-consistent-but-slightly-wrong posterior into the M-step yields a <em>less</em> wrong <img src="https://latex.codecogs.com/png.latex?%5Ctheta">; at convergence, current <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> has become <img src="https://latex.codecogs.com/png.latex?%5Ctheta%5E%5Cstar"> and the two notions of “true posterior” finally coincide.</p>
</div>
</div>
</div>
</section>
<section id="the-e-step-and-m-step-on-the-coins" class="level2">
<h2 class="anchored" data-anchor-id="the-e-step-and-m-step-on-the-coins">The E-step and M-step on the coins</h2>
<p><strong>E-step (responsibilities).</strong> Setting <img src="https://latex.codecogs.com/png.latex?q"> to the posterior gives, by Bayes’ rule,</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cgamma_i%20=%20p(z_i%20=%20A%5Cmid%20x_i,%5Ctheta)%20=%20%5Cfrac%7B%5Cpi_A%5C,%5Ctheta_A%5E%7Bx_i%7D(1-%5Ctheta_A)%5E%7Bm-x_i%7D%7D%7B%5Cpi_A%5C,%5Ctheta_A%5E%7Bx_i%7D(1-%5Ctheta_A)%5E%7Bm-x_i%7D%20+%20%5Cpi_B%5C,%5Ctheta_B%5E%7Bx_i%7D(1-%5Ctheta_B)%5E%7Bm-x_i%7D%7D."></p>
<p><strong>M-step (weighted MLE).</strong> Maximizing the ELBO over <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> reduces to maximizing the <em>expected complete-data log-likelihood</em>,</p>
<p><img src="https://latex.codecogs.com/png.latex?Q(%5Ctheta)%20=%20%5Csum_i%5CBig%5B%5Cgamma_i%5Clog%5Cbig(%5Cpi_A%20b_%7BiA%7D%5Cbig)%20+%20(1-%5Cgamma_i)%5Clog%5Cbig(%5Cpi_B%20b_%7BiB%7D%5Cbig)%5CBig%5D,"></p>
<p>whose maximizers are soft heads-over-flips counts:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cboxed%7B%5C;%5Ctheta_A%20=%20%5Cfrac%7B%5Csum_i%20%5Cgamma_i%5C,%20x_i%7D%7Bm%5Csum_i%20%5Cgamma_i%7D,%20%5Cqquad%20%5Ctheta_B%20=%20%5Cfrac%7B%5Csum_i%20(1-%5Cgamma_i)%5C,%20x_i%7D%7Bm%5Csum_i%20(1-%5Cgamma_i)%7D%5C;%7D"></p>
<p>Each row contributes to each coin in proportion to its responsibility — the “soft counting” from Part 1, now derived.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-6-contents" aria-controls="callout-6" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Is q(z=A) the same kind of thing as the prior π_A?
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-6" class="callout-6-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Same <em>type</em> (both probabilities about which coin), different <em>role</em>. <img src="https://latex.codecogs.com/png.latex?%5Cpi_A"> is the <strong>prior</strong> — the chance coin A is picked <em>before</em> seeing the flips (fixed, same for every row, here <img src="https://latex.codecogs.com/png.latex?0.5">). <img src="https://latex.codecogs.com/png.latex?q(z_i%7B=%7DA)"> is meant to be a <strong>posterior</strong> — the belief about <em>this row’s</em> coin <em>after</em> folding in its flips (per-row, carries the subscript <img src="https://latex.codecogs.com/png.latex?i">). They’re linked by Bayes’ rule, with <img src="https://latex.codecogs.com/png.latex?%5Cpi_A"> sitting <em>inside</em> the formula for the best <img src="https://latex.codecogs.com/png.latex?q">: start at the prior <img src="https://latex.codecogs.com/png.latex?0.5">, update on 9 heads, land at <img src="https://latex.codecogs.com/png.latex?%5Capprox%200.80">. The data is exactly what separates the two.</p>
</div>
</div>
</div>
</section>
<section id="watching-the-bounds-touch-and-climb-for-real" class="level2">
<h2 class="anchored" data-anchor-id="watching-the-bounds-touch-and-climb-for-real">Watching the bounds touch and climb, for real</h2>
<p>Running EM on the five experiments from a seed of <img src="https://latex.codecogs.com/png.latex?%5Ctheta_A%20=%200.55">, <img src="https://latex.codecogs.com/png.latex?%5Ctheta_B%20=%200.45"> produces this trajectory:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://aelhossiny.github.io/articles/em-algorithm/fig_convergence.png" class="img-fluid figure-img"></p>
<figcaption>EM convergence from an off-target seed</figcaption>
</figure>
</div>
<p>And here is the actual ELBO geometry (1-D slice fixing <img src="https://latex.codecogs.com/png.latex?%5Ctheta_B%20=%200.52">, varying <img src="https://latex.codecogs.com/png.latex?%5Ctheta_A">). The blue curve is the true log-likelihood; each amber dome is a real ELBO built at one iterate:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://aelhossiny.github.io/articles/em-algorithm/fig_elbo_bounds.png" class="img-fluid figure-img"></p>
<figcaption>Real ELBO bounds touching the log-likelihood and being climbed</figcaption>
</figure>
</div>
<p>Read it left to right:</p>
<ul>
<li>Each amber dome <strong>touches</strong> the blue curve at a green dot (where <img src="https://latex.codecogs.com/png.latex?q"> was set to the posterior, so KL = 0).</li>
<li>The M-step <strong>climbs</strong> the dome to its peak — the orange diamond.</li>
<li>Look directly <em>above</em> each diamond: the blue curve is higher. That vertical gap is the <strong>KL reopening</strong>, and it’s why the next green dot (the next E-step) sits up on blue, not down at the diamond. You gained <em>more</em> true likelihood than the bound alone promised.</li>
<li>The green dots march monotonically up blue toward <img src="https://latex.codecogs.com/png.latex?%5Ctheta_A%5E%5Cstar%20%5Capprox%200.80">.</li>
</ul>
</section>
<section id="the-loop-compactly" class="level2">
<h2 class="anchored" data-anchor-id="the-loop-compactly">The loop, compactly</h2>
<p><img src="https://latex.codecogs.com/png.latex?%5Ctheta%5E%7B(t)%7D%20%5C;%5Cxrightarrow%7B%5C;%5Ctext%7BE:%20%7D%20%5Cgamma_i%5E%7B(t)%7D%20=%20p(z_i%5Cmid%20x_i,%5Ctheta%5E%7B(t)%7D)%5C;%7D%5C;%20%5Cgamma%5E%7B(t)%7D%20%5C;%5Cxrightarrow%7B%5C;%5Ctext%7BM:%20%7D%20%5Ctheta_A=%5Cfrac%7B%5Csum%5Cgamma_i%20x_i%7D%7Bm%5Csum%5Cgamma_i%7D,%5C%20%5Ctheta_B=%5Cfrac%7B%5Csum(1-%5Cgamma_i)x_i%7D%7Bm%5Csum(1-%5Cgamma_i)%7D%5C;%7D%5C;%20%5Ctheta%5E%7B(t+1)%7D."></p>
<p>Repeat until <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> stops moving.</p>
<hr>
<p>← <a href="../../articles/em-algorithm/01-intuition.html">Part 1 — The Intuition</a> | Continue to <strong><a href="../../articles/em-algorithm/03-rigorous.html">Part 3 — The Rigorous Treatment →</a></strong></p>


</section>

 ]]></description>
  <category>statistics</category>
  <category>machine-learning</category>
  <category>algorithms</category>
  <category>EM-algorithm</category>
  <guid>https://aelhossiny.github.io/articles/em-algorithm/02-math.html</guid>
  <pubDate>Sun, 08 Jun 2025 04:00:00 GMT</pubDate>
</item>
<item>
  <title>The EM Algorithm, Part 1: The Intuition</title>
  <link>https://aelhossiny.github.io/articles/em-algorithm/01-intuition.html</link>
  <description><![CDATA[ 




<blockquote class="blockquote">
<p><strong>This is a three-part series.</strong> 1. <strong>The Intuition</strong> <em>(you are here)</em> — the idea, no heavy math. 2. <a href="../../articles/em-algorithm/02-math.html">Math with Intuition</a> — every equation, explained in plain words. 3. <a href="../../articles/em-algorithm/03-rigorous.html">The Rigorous Treatment</a> — proofs, calculus, and the formal guarantees.</p>
</blockquote>
<hr>
<section id="the-problem-a-chicken-and-egg-puzzle" class="level2">
<h2 class="anchored" data-anchor-id="the-problem-a-chicken-and-egg-puzzle">The problem: a chicken-and-egg puzzle</h2>
<p>The Expectation–Maximization (EM) algorithm exists to solve one frustrating situation: <strong>you want to estimate some parameters, but to estimate them you’d need to know a hidden label, and to know the hidden label you’d need the parameters.</strong> Each one would unlock the other, and you have neither.</p>
<p>EM breaks the loop by <em>guessing</em>, then refining.</p>
</section>
<section id="the-running-example-two-coins" class="level2">
<h2 class="anchored" data-anchor-id="the-running-example-two-coins">The running example: two coins</h2>
<p>Imagine two biased coins, <strong>A</strong> and <strong>B</strong>, with unknown head-probabilities <img src="https://latex.codecogs.com/png.latex?%5Ctheta_A"> and <img src="https://latex.codecogs.com/png.latex?%5Ctheta_B">. Someone runs five experiments. In each one they secretly grab one of the two coins, flip it 10 times, and write down <strong>only the number of heads — not which coin they used</strong>:</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th style="text-align: center;">Experiment</th>
<th style="text-align: center;">Heads (out of 10)</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: center;">1</td>
<td style="text-align: center;">5</td>
</tr>
<tr class="even">
<td style="text-align: center;">2</td>
<td style="text-align: center;">9</td>
</tr>
<tr class="odd">
<td style="text-align: center;">3</td>
<td style="text-align: center;">8</td>
</tr>
<tr class="even">
<td style="text-align: center;">4</td>
<td style="text-align: center;">4</td>
</tr>
<tr class="odd">
<td style="text-align: center;">5</td>
<td style="text-align: center;">7</td>
</tr>
</tbody>
</table>
<p>Your goal is to recover <img src="https://latex.codecogs.com/png.latex?%5Ctheta_A"> and <img src="https://latex.codecogs.com/png.latex?%5Ctheta_B">. The annoying part is the <strong>missing label</strong> — which coin produced each row. That hidden label is the <em>latent variable</em>.</p>
<section id="why-its-circular" class="level3">
<h3 class="anchored" data-anchor-id="why-its-circular">Why it’s circular</h3>
<ul>
<li>If a kind soul told you the coin for each row, this would be trivial: pool all the A-rows, count heads over flips, done.</li>
<li>Conversely, if someone handed you <img src="https://latex.codecogs.com/png.latex?%5Ctheta_A"> and <img src="https://latex.codecogs.com/png.latex?%5Ctheta_B">, you could guess which coin produced each row (a row with 9 heads probably came from the more heads-happy coin).</li>
</ul>
<p>But you have <strong>neither</strong>.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-1-contents" aria-controls="callout-1" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>What’s the difference between θ, θ_A, and θ_B?
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-1" class="callout-1-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p><img src="https://latex.codecogs.com/png.latex?%5Ctheta_A"> is the bias of coin A — the probability a single flip of coin A lands heads (one number between 0 and 1). <img src="https://latex.codecogs.com/png.latex?%5Ctheta_B"> is the same for coin B. <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> (no subscript) is just shorthand for <strong>both of them bundled together</strong>, the full parameter set:</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Ctheta%20=%20(%5Ctheta_A,%20%5Ctheta_B)."></p>
<p>So <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> isn’t a third quantity with its own value — it’s the container. Writing <img src="https://latex.codecogs.com/png.latex?p(x_i%20%5Cmid%20%5Ctheta)"> means “given both biases at once.” (If we also wanted to learn how often each coin gets <em>picked</em>, we’d fold those mixing probabilities in too, but here we fix them at 50/50.)</p>
</div>
</div>
</div>
</section>
</section>
<section id="the-fix-guess-soften-repeat" class="level2">
<h2 class="anchored" data-anchor-id="the-fix-guess-soften-repeat">The fix: guess, soften, repeat</h2>
<p>Start with a guess, say <img src="https://latex.codecogs.com/png.latex?%5Ctheta_A%20=%200.6">, <img src="https://latex.codecogs.com/png.latex?%5Ctheta_B%20=%200.5">. Now instead of forcing a hard “this row is A” decision, EM does something gentler, alternating two steps:</p>
<p><strong>E-step (Expectation).</strong> For each row, use the current guesses to compute the <strong>probability</strong> it came from each coin. The 9-heads row comes out to roughly <strong>80% A / 20% B</strong>; the 4-heads row to roughly <strong>35% A / 65% B</strong>. These soft probabilities are called <em>responsibilities</em> — coin A is “responsible” for 80% of the 9-heads row.</p>
<p><strong>M-step (Maximization).</strong> Re-estimate the coins, but let every row contribute to <em>both</em> coins in proportion to those responsibilities. The 9-heads row dumps 80% of its 9 heads into A’s tally and 20% into B’s. It’s a weighted version of the trivial counting you’d do if labels were known — using <strong>soft counts</strong> instead of hard ones.</p>
<p>Then take the new <img src="https://latex.codecogs.com/png.latex?%5Ctheta">’s back to the E-step and go around again. Each loop the responsibilities sharpen and the estimates improve. The hidden labels and the parameters <strong>bootstrap each other into existence</strong>.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-2-contents" aria-controls="callout-2" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>How is the 80/20 split actually computed?
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-2" class="callout-2-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>It compares how well each coin explains that row’s outcome. For a row with <img src="https://latex.codecogs.com/png.latex?x"> heads out of <img src="https://latex.codecogs.com/png.latex?m=10"> flips, the responsibility of coin A is</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cgamma%20=%20%5Cfrac%7B%5Ctheta_A%5E%7Bx%7D(1-%5Ctheta_A)%5E%7Bm-x%7D%7D%7B%5Ctheta_A%5E%7Bx%7D(1-%5Ctheta_A)%5E%7Bm-x%7D%20+%20%5Ctheta_B%5E%7Bx%7D(1-%5Ctheta_B)%5E%7Bm-x%7D%7D."></p>
<p>(The prior 50/50 and the binomial coefficient cancel between top and bottom.) For the <strong>9-heads</strong> row with <img src="https://latex.codecogs.com/png.latex?%5Ctheta_A%20=%200.6">, <img src="https://latex.codecogs.com/png.latex?%5Ctheta_B%20=%200.5">:</p>
<p><img src="https://latex.codecogs.com/png.latex?L_A%20=%200.6%5E%7B9%7D%5Ccdot%200.4%5E%7B1%7D%20=%200.004031,%5Cqquad%20L_B%20=%200.5%5E%7B10%7D%20=%200.000977,"> <img src="https://latex.codecogs.com/png.latex?%5Cgamma%20=%20%5Cfrac%7B0.004031%7D%7B0.004031%20+%200.000977%7D%20%5Capprox%200.805."></p>
<p>For the <strong>4-heads</strong> row: <img src="https://latex.codecogs.com/png.latex?L_A%20=%200.6%5E4%5Ccdot%200.4%5E6%20=%200.000531">, <img src="https://latex.codecogs.com/png.latex?L_B%20=%200.5%5E%7B10%7D%20=%200.000977">, giving <img src="https://latex.codecogs.com/png.latex?%5Cgamma%20%5Capprox%200.352">. More heads tilts the ratio toward the heads-leaning coin; fewer heads tilts it toward the fair one.</p>
</div>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-3-contents" aria-controls="callout-3" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Are we solving for θ or for the hidden labels z?
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-3" class="callout-3-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>We are solving for <strong>θ</strong> (the coin biases). That’s the answer you report: “coin A lands heads ~80% of the time.” The hidden labels <img src="https://latex.codecogs.com/png.latex?z_i"> (which coin made each row) are a <strong>nuisance</strong> — they only appear because they were never recorded. EM computes <em>beliefs</em> about them (the responsibilities) purely as an intermediate device to make the <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> update possible. Think of the responsibilities as <strong>scaffolding</strong> and <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> as the <strong>building</strong>: you keep rebuilding the scaffolding only because it lets you raise the building higher each round, and you throw it away at the end.</p>
</div>
</div>
</div>
</section>
<section id="why-it-keeps-getting-better-the-surrogate-dome-picture" class="level2">
<h2 class="anchored" data-anchor-id="why-it-keeps-getting-better-the-surrogate-dome-picture">Why it keeps getting better: the surrogate-dome picture</h2>
<p>Here’s the mental model that makes EM click. Picture the quality of a parameter guess as a height on a hill — the <strong>true likelihood</strong> of the data. You want the summit, but the hill is shrouded in fog: you can’t survey it or optimize it directly.</p>
<p>So at your feet you lay down a <strong>smooth dome</strong> — a simpler surrogate — engineered with two properties:</p>
<ol type="1">
<li>it <strong>touches</strong> the hill exactly where you’re standing, and</li>
<li>it <strong>never pokes above</strong> the hill anywhere.</li>
</ol>
<p>You can easily find the top of the dome (that’s the M-step). Walk there. Because the dome never exceeded the hill, the hill at your new spot is <strong>at least as high</strong> as the dome’s peak — which was at least as high as where you started. So you’ve climbed the real hill without ever seeing it. Then you lay a fresh dome at the new spot and repeat.</p>
<p>That dome is the <strong>ELBO</strong> (Evidence Lower BOund), and the “touch then climb” rhythm is exactly the E-step and M-step. The fact that the dome stays <em>below</em> the hill is what makes climbing it safe — you can never be tricked into walking somewhere that looks good on the surrogate but is bad in reality.</p>
<blockquote class="blockquote">
<p>The deeper “why” — why the dome touches where it does, why it stays below, and the inequality that guarantees the climb — is the heart of <a href="../../articles/em-algorithm/02-math.html">Part 2</a>.</p>
</blockquote>
</section>
<section id="watching-it-actually-converge" class="level2">
<h2 class="anchored" data-anchor-id="watching-it-actually-converge">Watching it actually converge</h2>
<p>Here’s EM run for real on the five experiments above, deliberately seeded <strong>off-target</strong> at <img src="https://latex.codecogs.com/png.latex?%5Ctheta_A%20=%200.55">, <img src="https://latex.codecogs.com/png.latex?%5Ctheta_B%20=%200.45">:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://aelhossiny.github.io/articles/em-algorithm/fig_convergence.png" class="img-fluid figure-img"></p>
<figcaption>EM convergence: parameters and log-likelihood vs.&nbsp;iteration</figcaption>
</figure>
</div>
<p>Two things worth noticing, both of which the theory predicts:</p>
<ul>
<li><strong>The log-likelihood (bottom panel) never decreases.</strong> That’s EM’s core guarantee — every round either improves the fit of the data or leaves it unchanged.</li>
<li><strong><img src="https://latex.codecogs.com/png.latex?%5Ctheta_B"> overshoots.</strong> It jumps up to <img src="https://latex.codecogs.com/png.latex?0.577"> on the first step, then drifts back down to <img src="https://latex.codecogs.com/png.latex?0.520">. That’s allowed: EM only promises the <em>overall likelihood</em> climbs monotonically, <strong>not</strong> that each individual parameter marches straight to its target. From a bad seed the first responsibilities are misjudged, so the first update over-corrects, and later rounds walk it back.</li>
</ul>
<p>The run settles at <img src="https://latex.codecogs.com/png.latex?%5Ctheta_A%5E%5Cstar%20%5Capprox%200.80">, <img src="https://latex.codecogs.com/png.latex?%5Ctheta_B%5E%5Cstar%20%5Capprox%200.52"> — the biases that best explain the five observed heads-counts.</p>
</section>
<section id="the-whole-algorithm-in-one-breath" class="level2">
<h2 class="anchored" data-anchor-id="the-whole-algorithm-in-one-breath">The whole algorithm in one breath</h2>
<blockquote class="blockquote">
<p><strong>Fill in, then re-fit.</strong> The E-step fills in the missing labels with their expected (soft) values given where you currently stand; the M-step re-fits the parameters as if those soft labels were the data. Because the fill-in depended on the old parameters, you fill in again at the new spot — and around you go, until nothing moves.</p>
</blockquote>
<p>Continue to <strong><a href="../../articles/em-algorithm/02-math.html">Part 2 — Math with Intuition →</a></strong></p>


</section>

 ]]></description>
  <category>statistics</category>
  <category>machine-learning</category>
  <category>algorithms</category>
  <category>EM-algorithm</category>
  <guid>https://aelhossiny.github.io/articles/em-algorithm/01-intuition.html</guid>
  <pubDate>Sun, 01 Jun 2025 04:00:00 GMT</pubDate>
</item>
</channel>
</rss>
