class: middle, center, title-slide
Lecture 7: Expectation-Maximization
Prof. Gilles Louppe
g.louppe@uliege.be
class: middle
The Old Faithful geyser in Yellowstone National Park (USA) is famous for its frequent and predictable eruptions of hot water and steam.
class: middle, black-slide
.center[
<iframe width="640" height="400" src="https://www.youtube.com/embed/Qxf3xzirBrs?cc_load_policy=1&hl=en&version=3" frameborder="0" allowfullscreen></iframe> ]class: middle
class: middle
The observed data
class: middle
The marginal log-likelihood of the observed data is $$\begin{aligned} \log p(\{x_n\}_{n=1}^N | \pi, \mu, \sigma^2) &= \log \prod_{n=1}^N p(x_n | \pi, \mu, \sigma^2) \\ &= \log \prod_{n=1}^N \sum_{z_n=1}^2 p(x_n, z_n | \pi, \mu, \sigma^2) \\ &= \sum_{n=1}^N \log \sum_{z_n=1}^2 p(x_n | z_n, \mu, \sigma^2) p(z_n | \pi), \end{aligned}$$ where
$p(x_n | z_n = k, \mu, \sigma^2) = \mathcal{N}(x_n | \mu_k, \sigma_k^2 I)$ -
$p(z_n = k | \pi) = \pi_k$ for$k = 1, 2$ .
class: middle
Direct maximization of the marginal log-likelihood w.r.t. parameters
- The objective is non-convex.
- No closed-form solution.
- Numerically unstable.
.alert[More generally, latent variable models lead to log-likelihoods involving sums or integrals inside the logarithm. As their domain grows, these sums/integrals .bold[become intractable to even just evaluate], let alone optimize.]
class: middle
If the latent
The evaluation of the log-likelihood becomes tractable, and so does its optimization.
class: middle
For the Gaussian mixture model, differentiating the complete-data log-likelihood with respect to the parameters and setting to zero would even yield closed-form expressions for the maximum likelihood estimates:
$\mu_k = \frac{1}{N_k} \sum_{\{n: z_n = k\}} x_n$ $\sigma_k^2 = \frac{1}{N_k} \sum_{\{n: z_n = k\}} |x_n - \mu_k|^2$ $\pi_k = \frac{N_k}{N}$
where
class: middle
.center[What if we alternate between guessing the latent variables
and optimizing the hyperparameters?]
class: middle
class: middle
Assume a generic latent variable model
class: middle
The marginal log-likelihood for a single observation
class: middle
By Jensen's inequality, the log-likelihood can be lower-bounded as
$$\begin{aligned}
\log p(x | \theta) &\geq \mathbb{E}_{q(z)} \left[\log \frac{p(x, z | \theta)}{q(z)}\right] = \mathcal{L}(q, \theta),
\end{aligned}$$
where
class: middle
The ELBO can first be rewritten as
$$\begin{aligned}
\mathcal{L}(q, \theta) &= \mathbb{E}_{q(z)} \left[\log \frac{p(x, z | \theta)}{q(z)}\right] \\
&= \mathbb{E}_{q(z)} \left[\log \frac{p(x | z, \theta) p(z | \theta)}{q(z)}\right] \\
&= \mathbb{E}_{q(z)} \left[\log p(x | z, \theta)\right] - \text{KL}(q(z) || p(z | \theta)),
\end{aligned}$$
where
This expression highlights the trade-off between fitting the data well (first term) and keeping the variational distribution
class: middle
By factorizing the joint in the other way, the ELBO can also be rewritten as
$$\begin{aligned}
\mathcal{L}(q, \theta) &= \mathbb{E}_{q(z)} \left[\log \frac{p(x, z | \theta)}{q(z)}\right] \\
&= \mathbb{E}_{q(z)} \left[\log \frac{p(z | x, \theta) p(x | \theta)}{q(z)}\right] \\
&= \log p(x | \theta) - \text{KL}(q(z) || p(z | x, \theta)).
\end{aligned}$$
Therefore,
This decomposition reveals that the ELBO is a lower bound on the log-likelihood, with a gap measured by the KL divergence between the variational distribution
class: middle
.center.width-70[
]
The KL gap
class: middle
.center.width-70[
]
The ELBO is tight when
class: middle
The EM algorithm maintains parameter estimates
- E-step: maximize the ELBO
$\mathcal{L}(q, \theta^{(t)})$ w.r.t.$q$ while keeping$\theta^{(t)}$ fixed. - M-step: maximize the ELBO
$\mathcal{L}(q^{(t+1)}, \theta)$ w.r.t.$\theta$ while keeping$q^{(t+1)}(z)$ fixed.
class: middle
The E-step consists in solving
.italic[Proof.] From the decomposition of the log-likelihood, we have
class: middle
The M-step consists in solving
Depending on the model, this maximization can sometimes be done in closed-form. Otherwise, numerical optimization algorithms can be used.
class: middle
Finally, to initialize the algorithm, we need to set initial parameters
class: middle
class: middle count: false
class: middle count: false
class: middle
.italic[Proposition.] The EM algorithm monotonically increases the marginal log-likelihood at each iteration, i.e.,
.italic[Proof.] Assume we have parameter estimates
class: middle
For the Gaussian mixture model introduced earlier, the E-step consists in computing the posterior distribution over the latent variables:
$$\begin{aligned}
q^{(t+1)}(z_n = k) &= p(z_n = k | x_n, \pi^{(t)}, \mu^{(t)}, \sigma^{2(t)}) \\
&= \frac{p(x_n | z_n = k, \mu^{(t)}, \sigma^{2(t)}) p(z_n = k | \pi^{(t)})}{\sum_{j=1}^2 p(x_n | z_n = j, \mu^{(t)}, \sigma^{2(t)}) p(z_n = j | \pi^{(t)})} \\
&= \frac{\pi_k^{(t)} \mathcal{N}(x_n | \mu_k^{(t)}, \sigma_k^{2(t)} I)}{\sum_{j=1}^2 \pi_j^{(t)} \mathcal{N}(x_n | \mu_j^{(t)}, \sigma_j^{2(t)} I)}
\end{aligned}$$
for
class: middle
The M-step consists in updating the parameters as
$$\begin{aligned}
\mu_k^{(t+1)} &= \frac{\sum_{n=1}^N q^{(t+1)}(z_n = k) x_n}{\sum_{n=1}^N q^{(t+1)}(z_n = k)} \\
\sigma_k^{2(t+1)} &= \frac{\sum_{n=1}^N q^{(t+1)}(z_n = k) |x_n - \mu_k^{(t+1)}|^2}{\sum_{n=1}^N q^{(t+1)}(z_n = k)} \\
\pi_k^{(t+1)} &= \frac{1}{N} \sum_{n=1}^N q^{(t+1)}(z_n = k)
\end{aligned}$$
for
class: middle
class: middle count: false
class: middle count: false
class: middle count: false
class: middle count: false
class: middle
In hierarchical Bayesian models, model parameters
A full Bayesian treatment to obtain
class: middle
An alternative is to use an .bold[empirical Bayes] approach, which consists in approximating
.alert[While empirical Bayes is unorthodox from a fully Bayesian perspective, it can lead to good practical results and is used in many applications.]
class: middle
The maximization can be performed using the EM algorithm by treating both the latent variables
- E-step: compute the posterior distribution over both latents and parameters
$$q^{(t+1)}(z, \theta) = p(z, \theta | x, \eta^{(t)}).$$ - M-step: update the hyper-parameters as
$$\eta^{(t+1)} = \arg\max_\eta \mathbb{E}_{q^{(t+1)}(z, \theta)} \left[\log p(x, z | \theta) + \log p(\theta | \eta)\right].$$
class: middle
EM can also be used to learn complex prior distributions parameterized by deep generative models, such as diffusion models, from noisy and incomplete observations only.
.footnote[Credits: Rozet et al (arXiv:2405.13712), 2024.]
class: middle
.footnote[Credits: Rozet et al (arXiv:2405.13712), 2024.]
class: end-slide, center count: false
The end.













