Search

Search papers and lessons

Variational Lower Bound (ELBO)

Level 4 · 18 min read

The evidence lower bound is a score that never exceeds the log probability of the data; pushing it up makes a simple guess match a hidden posterior that is too hard to compute.

On this page

  • Colour = topic mix
  • Probability
  • Calculus
  • Discrete math
  • Inference
  • Information theory
  • Sign in to see your progress

TL;DR

Fitting a latent variable model means working out the hidden cause behind each observation. That hidden posterior usually has no closed form, because computing it needs an integral over every possible hidden value. The evidence lower bound (ELBO) gets around this. It is a score that is always at most the log probability of the data. You choose a simple distribution for the hidden variable, compute the score, and push it up. The better the score, the closer your simple guess is to the true posterior, and the score can be optimised with ordinary gradient methods.

Where you'll see it: in the training loss of a variational autoencoder, in the variational view of diffusion models, and in approximate Bayesian inference wherever the exact posterior is too hard to compute.

Intuition

A problem with one hard sum

Take a latent variable model. A hidden variable zz generates visible data xx. The model gives you two things cheaply: a prior p(z)p(z) over hidden causes and a likelihood p(x∣z)p(x \mid z) for each cause. What you want is the posterior p(z∣x)p(z \mid x), the hidden cause given what you observed. Bayes' rule says that this equals the joint divided by the evidence p(x)p(x), and the evidence is an integral over every value zz could take.

With two discrete values of zz that integral is a two-term sum. With a twenty-dimensional continuous zz and a neural network inside the likelihood, it has no closed form, and numerical integration is hopeless. You know how each cause would produce the data, but you cannot weigh every cause against the clue at once.

Guess, then score the guess

The trick is to stop computing the true posterior and instead pick a simpler distribution q(z)q(z) from a family you can work with, such as a Gaussian with a mean and a variance you can tune. You then need to know how good the guess is. The gap between your guess and the truth involves the unknown posterior, so you cannot compute it directly. What you can compute is a score that sits below the true answer by exactly that gap. Push the score up, and the gap must shrink.

0.511.522.5−3.5−3−2.5−2variance of the guessscore
  • bound
  • log p(x)
The bound (solid) rises to meet the true log evidence (dashed) at the one guess that equals the posterior. It can never rise above the dashed line.
flowchart LR
  A[Observed data x] --> B[Pick a simple guess q of z]
  B --> C[Compute the ELBO]
  C --> D[Adjust q to raise the ELBO]
  D --> B
  D --> E[q approximates the hidden posterior]

The math

Definition

Definition: Evidence lower bound (ELBO)

Let p(x,z)p(x, z) be a joint model of visible data xx and hidden variable zz, and let q(z)q(z) be any distribution over zz. The ELBO is

L(q)=Eq(z)[log⁡p(x,z)−log⁡q(z)].\mathcal{L}(q) = \mathbb{E}_{q(z)}\left[\log p(x, z) - \log q(z)\right].

Here Eq(z)[⋅]\mathbb{E}_{q(z)}[\cdot] is the average over values of zz drawn from qq, and log⁡\log is the natural logarithm, so all scores are in nats. The ELBO depends on qq and on the fixed model pp, and nothing else.

The bound

Insert qq into the evidence and apply Jensen's inequality:

log⁡p(x)=log⁡∫q(z) p(x,z)q(z) dz=log⁡Eq(z) ⁣[p(x,z)q(z)]≥Eq(z) ⁣[log⁡p(x,z)q(z)]=L(q).\log p(x) = \log \int q(z)\,\frac{p(x,z)}{q(z)}\,dz = \log \mathbb{E}_{q(z)}\!\left[\frac{p(x,z)}{q(z)}\right] \ge \mathbb{E}_{q(z)}\!\left[\log \frac{p(x,z)}{q(z)}\right] = \mathcal{L}(q).

The inequality holds because log⁡\log is concave, and for a concave function the log of an average is at least the average of the logs (see Jensen's inequality).

The exact gap

The bound is tight in a precise way. Use p(x,z)=p(z∣x) p(x)p(x,z) = p(z \mid x)\,p(x) and expand:

log⁡p(x)=L(q)+DKL(q(z) ∥ p(z∣x)).\log p(x) = \mathcal{L}(q) + D_{\mathrm{KL}}\big(q(z)\,\|\,p(z \mid x)\big).

Here DKLD_{\mathrm{KL}} is the KL divergence, which is never negative and is zero only when its two arguments match. So the ELBO is the log evidence minus a KL term. Three consequences follow:

  1. The bound holds, because the KL term is never negative.
  2. The bound is exact when qq equals the true posterior, because then the KL term vanishes.
  3. log⁡p(x)\log p(x) does not depend on qq. Maximising L(q)\mathcal{L}(q) over qq is therefore the same as minimising DKL(q ∥ p(z∣x))D_{\mathrm{KL}}(q \,\|\, p(z \mid x)). The unknown quantity is a constant, so it can be ignored during optimisation.

The form used in practice

Expanding log⁡p(x,z)=log⁡p(x∣z)+log⁡p(z)\log p(x,z) = \log p(x \mid z) + \log p(z) gives a second form:

L(q)=Eq(z)[log⁡p(x∣z)]−DKL(q(z) ∥ p(z)).\mathcal{L}(q) = \mathbb{E}_{q(z)}\big[\log p(x \mid z)\big] - D_{\mathrm{KL}}\big(q(z)\,\|\,p(z)\big).

The first term rewards guesses that explain the data well. The second penalises guesses that stray from the prior. This is the loss of a variational autoencoder: reconstruction error plus a KL penalty.

If the model has parameters θ\theta, the same bound can be maximised over both qq and θ\theta. Because L\mathcal{L} never exceeds log⁡pθ(x)\log p_\theta(x), raising it also pushes up the model's log likelihood. This is the variational version of maximum likelihood.

Worked examples

Example
A tiny discrete model

Let zz take the values 0 and 1, each with prior probability 0.5. For the one observed xx, the likelihoods are p(x∣z=0)=0.2p(x \mid z=0) = 0.2 and p(x∣z=1)=0.6p(x \mid z=1) = 0.6.

  1. Evidence: p(x)=0.5×0.2+0.5×0.6=0.4p(x) = 0.5 \times 0.2 + 0.5 \times 0.6 = 0.4, so log⁡p(x)=ln⁡0.4≈−0.9163\log p(x) = \ln 0.4 \approx -0.9163.
  2. Joint values: p(x,z=0)=0.10p(x, z=0) = 0.10 and p(x,z=1)=0.30p(x, z=1) = 0.30.
  3. True posterior: p(z=0∣x)=0.10/0.40=0.25p(z=0 \mid x) = 0.10 / 0.40 = 0.25 and p(z=1∣x)=0.75p(z=1 \mid x) = 0.75.
  4. Guess a uniform qq: q(0)=q(1)=0.5q(0) = q(1) = 0.5.
  5. Term for z=0z=0: 0.5×[ln⁡0.10−ln⁡0.5]=0.5×(−2.3026+0.6931)=−0.80470.5 \times [\ln 0.10 - \ln 0.5] = 0.5 \times (-2.3026 + 0.6931) = -0.8047.
  6. Term for z=1z=1: 0.5×[ln⁡0.30−ln⁡0.5]=0.5×(−1.2040+0.6931)=−0.25540.5 \times [\ln 0.30 - \ln 0.5] = 0.5 \times (-1.2040 + 0.6931) = -0.2554.
  7. ELBO: −0.8047−0.2554=−1.0601-0.8047 - 0.2554 = -1.0601.
  8. Gap: −0.9163−(−1.0601)=0.1438-0.9163 - (-1.0601) = 0.1438. The KL check gives 0.5ln⁡(0.5/0.25)+0.5ln⁡(0.5/0.75)=0.3466−0.2027=0.14380.5\ln(0.5/0.25) + 0.5\ln(0.5/0.75) = 0.3466 - 0.2027 = 0.1438, as the identity predicts.

Interpretation: the uniform guess is too loose, and the bound sits 0.14 nats below the truth. Now set qq to the posterior, q(0)=0.25q(0) = 0.25 and q(1)=0.75q(1) = 0.75. The ELBO becomes 0.25ln⁡(0.10/0.25)+0.75ln⁡(0.30/0.75)=ln⁡0.40.25\ln(0.10/0.25) + 0.75\ln(0.30/0.75) = \ln 0.4, which equals log⁡p(x)\log p(x) exactly. The gap is zero.

Example
A Gaussian model with a Gaussian guess

The model is z∼N(0,1)z \sim \mathcal{N}(0, 1) and x∣z∼N(z,1)x \mid z \sim \mathcal{N}(z, 1). Observe x=2x = 2. The true posterior is N(1,0.5)\mathcal{N}(1, 0.5): its mean is x/2x/2 and its variance is 1/21/2. The marginal is x∼N(0,2)x \sim \mathcal{N}(0, 2), so log⁡p(2)=−12ln⁡(4π)−1≈−2.2655\log p(2) = -\tfrac12\ln(4\pi) - 1 \approx -2.2655.

Guess q(z)=N(m,s2)q(z) = \mathcal{N}(m, s^2). Both terms of the second form have closed forms. The reconstruction term is Eq[log⁡p(x∣z)]=−12ln⁡(2π)−12[(x−m)2+s2]\mathbb{E}_q[\log p(x \mid z)] = -\tfrac12\ln(2\pi) - \tfrac12\big[(x-m)^2 + s^2\big], and the KL term is DKL(q ∥ p(z))=12(m2+s2−1−ln⁡s2)D_{\mathrm{KL}}(q \,\|\, p(z)) = \tfrac12\big(m^2 + s^2 - 1 - \ln s^2\big). With x=2x = 2 the ELBO is

L(m,s2)=−0.4189−12(2−m)2−12m2−s2+12ln⁡s2.\mathcal{L}(m, s^2) = -0.4189 - \tfrac12(2-m)^2 - \tfrac12 m^2 - s^2 + \tfrac12 \ln s^2.
  1. Exact guess (m,s2)=(1,0.5)(m, s^2) = (1, 0.5): L=−0.4189−0.5−0.5−0.5−0.3466=−2.2655\mathcal{L} = -0.4189 - 0.5 - 0.5 - 0.5 - 0.3466 = -2.2655. This equals log⁡p(2)\log p(2), so the gap is zero.
  2. Lazy guess, the prior (m,s2)=(0,1)(m, s^2) = (0, 1): L=−0.4189−2−0−1+0=−3.4189\mathcal{L} = -0.4189 - 2 - 0 - 1 + 0 = -3.4189. The gap is 1.15341.1534, which matches DKL(N(0,1) ∥ N(1,0.5))=12[ln⁡0.5+(1+1)/0.5−1]=1.1534D_{\mathrm{KL}}(\mathcal{N}(0,1) \,\|\, \mathcal{N}(1, 0.5)) = \tfrac12[\ln 0.5 + (1 + 1)/0.5 - 1] = 1.1534.
  3. Optimise. Setting ∂L/∂m=(2−m)−m=0\partial \mathcal{L}/\partial m = (2 - m) - m = 0 gives m=1m = 1. Setting ∂L/∂s2=−1+1/(2s2)=0\partial \mathcal{L}/\partial s^2 = -1 + 1/(2s^2) = 0 gives s2=0.5s^2 = 0.5. The optimum recovers the posterior exactly.

Interpretation: this model is simple enough that the posterior is known in closed form, so the bound is not needed. The point is to see the recipe work. The plot shows the bound for m=1m = 1 as a function of s2s^2. Its peak touches the true log evidence at s2=0.5s^2 = 0.5.

Tradeoffs and failure modes

Variational inference trades exactness for speed and scale. The table lists the usual choices.

SituationProblemBetter choice
Conjugate model, posterior has a closed formApproximation adds error for no gainCompute the posterior exactly
Large model, many data pointsEach gradient step needs the full dataStochastic or minibatch ELBO
Need exact samples or guaranteed convergence to the posteriorVariational answers are biased[[markov-chain-monte-carlo
Posterior has correlations between hidden variablesA factorised guess cannot represent themRicher families, such as full-covariance Gaussians or normalising flows
Sampling inside the ELBO gives noisy gradientsHigh variance slows trainingThe [[reparameterization-trick

The most common failure is an under-dispersed guess. The reverse KL term DKL(q ∥ p)D_{\mathrm{KL}}(q \,\|\, p) is mode-seeking: it prefers a guess that sits on one peak of a multimodal posterior and ignores the others. The result is a confident answer that is too narrow. Mean-field guesses, which assume hidden variables are independent, make this worse.

A second failure appears in VAEs. If the decoder can model the data well without using zz, the optimiser can drive the KL term to zero, so q(z∣x)q(z \mid x) matches the prior and carries no information. This is called posterior collapse. Common fixes are KL annealing, which weights the KL term down early in training, and a weaker decoder.

History

Variational methods for approximate inference grew out of statistical physics, where a free energy plays the role of a bound. In machine learning, Hinton and van Camp (1993) used a related bound to fit neural network weights under a description-length penalty. Neal and Hinton (1998) recast the EM algorithm as maximising a free energy, which is the same kind of bound, so the ELBO is also called the free energy. Jordan, Ghahramani, Jaakkola and Saul (1999) gathered these ideas into a widely used introduction to variational methods for graphical models.

The name "ELBO" became common in the 2010s with variational autoencoders. Kingma and Welling (2013) and Rezende, Mohamed and Wierstra (2014) showed that the bound could be trained at scale with stochastic gradients and the reparameterisation trick. That made it a practical tool for deep generative models.

Check yourself

QuestionWhat does ELBO stand for, and what does it bound from below?Show answer

Evidence lower bound. It bounds the log evidence log⁡p(x)\log p(x), the log probability of the observed data under the model, from below. The gap between them is the KL divergence between the guess q(z)q(z) and the true posterior p(z∣x)p(z \mid x).

QuestionPrior p(z=0)=p(z=1)=0.5p(z=0) = p(z=1) = 0.5, likelihoods p(x∣z=0)=0.8p(x \mid z=0) = 0.8 and p(x∣z=1)=0.2p(x \mid z=1) = 0.2. With a uniform guess qq, what is the ELBO?Show answer

The evidence is p(x)=0.5×0.8+0.5×0.2=0.5p(x) = 0.5 \times 0.8 + 0.5 \times 0.2 = 0.5. The joint values are p(x,0)=0.4p(x, 0) = 0.4 and p(x,1)=0.1p(x, 1) = 0.1. The ELBO is 0.5ln⁡(0.4/0.5)+0.5ln⁡(0.1/0.5)=−0.1116−0.8047=−0.91630.5\ln(0.4/0.5) + 0.5\ln(0.1/0.5) = -0.1116 - 0.8047 = -0.9163. The true log evidence is ln⁡0.5≈−0.6931\ln 0.5 \approx -0.6931, so the gap is 0.22310.2231, which is the KL divergence from the uniform guess to the posterior (0.8,0.2)(0.8, 0.2).

QuestionWhy can you maximise the ELBO over qq without knowing p(x)p(x) or the true posterior?Show answer

The evidence log⁡p(x)\log p(x) does not depend on qq. It is a constant during optimisation. Maximising the ELBO over qq is therefore the same as minimising the KL divergence to the posterior, even though neither term is computed directly.

QuestionIn a VAE, the KL term has dropped to nearly zero and the reconstructions ignore the latent code. What went wrong, and name one fix?Show answer

This is posterior collapse. The guess q(z∣x)q(z \mid x) has matched the prior, so the latent code carries no information. A strong decoder can model the data without it, so the optimiser has no reason to use zz. One fix is KL annealing, which weights the KL penalty down early in training. Another is a weaker decoder that cannot ignore the latent code.

Sources