advanced39 min studyLesson 270 in Mathematics

Variational inference and approximation error

Derive the ELBO, analyze a mean-field Gaussian example, and distinguish optimization success from posterior accuracy.

On this page

What you’ll understand

  • Relate the evidence lower bound to reverse KL divergence.
  • Calculate the variance lost by a factorized Gaussian approximation.
  • Separate optimization error, approximation-family error, and Monte Carlo error.

Variational inference turns posterior approximation into optimization over a chosen family of distributions. It can be much faster than difficult sampling, but a rapidly converged objective is not the same as an accurate posterior. The family may exclude important dependence or multiple modes, and the divergence being minimized values errors asymmetrically. An analytically solvable Gaussian example makes these issues concrete: an approximation can get the mean right while understating uncertainty in a scientifically relevant sum of parameters.

The objective includes an unknown constant

Let p(y,θ)p(y,\theta) be a joint density with positive finite evidence p(y)p(y), and let qλ(θ)q_\lambda(\theta) be a normalized approximating density. When the relevant expectations and divergence are well-defined and finite, expanding the reverse Kullback–Leibler divergence gives log evidence minus an evidence lower bound, or ELBO. Since the evidence is independent of variational parameters, maximizing the ELBO minimizes this divergence within the chosen family. The word lower refers to log evidence, not to every posterior probability or predictive interval.

The ELBO contains an expected log joint density and the approximation's entropy. The entropy term prevents the objective from simply being a search for a posterior mode when the family permits nonzero spread. A proper approximation must respect target support; placing positive approximation mass where target density is zero produces infinite reverse KL. Expectations estimated using stochastic gradients introduce numerical noise, while unconstrained parameter transformations still require density Jacobians. Optimizing an incorrectly coded objective does not become valid because its value improves smoothly.

log⁡p(y)=Eqλ[log⁡p(y,θ)−log⁡qλ(θ)]⏟ELBO⁡(λ)+KL⁡ ⁣(qλ ∥ p(θ∣y))\log p(y)=\underbrace{\mathbb E_{q_\lambda}[\log p(y,\theta)-\log q_\lambda(\theta)]}_{\operatorname{ELBO}(\lambda)}+\operatorname{KL}\!\left(q_\lambda\,\|\,p(\theta\mid y)\right)
This finite-expectation decomposition makes ELBO maximization equivalent to reverse-KL minimization within a specified family.↔ Scroll wide equations horizontally.

A factorized family changes uncertainty

Mean-field approximation factorizes the joint approximation into independent coordinate distributions. This is a computational restriction, not a conclusion that the true posterior coordinates are independent. For a correlated Gaussian target, optimizing a fully factorized Gaussian under reverse KL gives coordinate variances determined by diagonal precision entries, rather than the target's marginal variances. These can be much smaller. The missing covariance also affects sums and contrasts, so checking only coordinate means can conceal a serious uncertainty error.

For a bivariate zero-mean Gaussian with unit marginal variances and correlation ρ\rho satisfying ∣ρ∣<1|\rho|<1, its precision diagonal is 1/(1−ρ2)1/(1-\rho^2). A diagonal Gaussian approximation minimizes reverse KL at zero mean and both variances 1−ρ21-\rho^2. Different divergences or richer families yield different solutions. Reverse KL can favor concentrating in one mode when covering low-target-density gaps is expensive, but this is a tendency in particular settings, not a theorem that every variational approximation misses modes or always underestimates every uncertainty.

Σ=(1ρρ1),q∗=N ⁣(0,diag⁡(1−ρ2,1−ρ2))(∣ρ∣<1)\Sigma=\begin{pmatrix}1&\rho\\\rho&1\end{pmatrix},\qquad q^*=N\!\left(0,\operatorname{diag}(1-\rho^2,1-\rho^2)\right)\quad(|\rho|<1)
This is the optimum within factorized Gaussian approximations for reverse KL to the stated Gaussian target, not a universal variance formula.↔ Scroll wide equations horizontally.

Validate the approximation at its intended use

Optimization error means the algorithm has not reached the best available member of its family. Approximation error remains even at that best member, because the family or objective may exclude the target features needed. Monte Carlo error enters when expectations or gradients are estimated from finite random draws. These errors require different responses: more optimization may reduce the first, a richer family may reduce the second, and more or better draws may reduce the third. None of them represents the underlying model's substantive posterior uncertainty.

Compare independent initializations, inspect predictive quantities, and benchmark manageable examples against exact calculations or well-diagnosed sampling. Importance reweighting can sometimes assess or improve an approximation, but unstable weights reveal overlap problems and do not guarantee correction. Simulation-based calibration tests computation under the assumed model; real-data predictive checks test other implications. Report which summaries were checked and whether decisions remain stable under alternative approximations. An ELBO increase is useful optimization evidence, while accurate tail probabilities or calibrated intervals require evidence directed at those particular goals.

Correct means, incorrect uncertainty in a sum

The target posterior is bivariate normal with zero mean, unit marginal variances, and correlation 0.8. Find the reverse-KL optimal factorized Gaussian variances and compare the variance of theta1+theta2 under target and approximation.

  1. The target covariance determinant is 1−0.8²=0.36. Its precision matrix has diagonal entries 1/0.36. For a diagonal Gaussian approximation, differentiating the Gaussian KL with respect to each variance gives variance equal to the reciprocal precision diagonal, or 0.36.

  2. The optimal approximate means are zero because the target precision is positive definite and its mean term is minimized there. The approximating coordinates are independent by the family restriction, so their covariance is zero.

  3. The target sum has variance 1+1+2 times 0.8=3.6. The approximate sum has variance 0.36+0.36=0.72. Thus the approximate variance is only one fifth of the target variance even though both coordinate means are exactly correct.

The result

Factorization loses both marginal spread and positive covariance in this example. Accurate posterior means do not certify accurate uncertainty for a sum.

Explore the model

Interactive lab

Interactive lab · optimization

Optimization convergence does not test the family

Pause and Reset, then set both controls to η=0.50 and x₀=2. For q=N(m,1) approximating N(0,1), reverse KL is m²/2, so identify x with m. Predict m=1 after one Step. Reset and set η=2.50 and x₀=2; predict m=−3 after one Step and subsequent divergence. This exact scalar quadratic illustrates optimization only; it does not represent the correlated target or assess approximation error.

Optimization convergence does not test the familyGradient descent on f(x) = x²/2: xₙ = (1 − η)ⁿx₀. Playback advances two discrete iterations per second. Values outside |x| ≤ 4 are explicitly reported. Current readings: Position x0: 2; Objective x²/2: 2; Iteration behavior: Stable step. All parameters are adjustable with the labeled controls below.−4−202402.174.356.528.7Parameter xf(x) = x²/2n = 0
ObjectiveDiscrete iterate
Position x0
2
Objective x²/2
2
Iteration behavior
Stable step
n = 0 · t = 0 s

Gradient descent on f(x) = x²/2: xₙ = (1 − η)ⁿx₀. Playback advances two discrete iterations per second. Values outside |x| ≤ 4 are explicitly reported. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forAn optimizer can reach its family's best approximation while that family still misrepresents posterior dependence.

Check your understanding

An ELBO optimizer reaches the global best factorized approximation. What can still be wrong?

Try it yourself

Approximate the target N(2,4) using q=N(m,1), where the second normal parameter is variance. Find the reverse-KL optimal m and the minimum KL. Explain why more accurate optimization cannot make this minimum zero.

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

For scalar normals, KL(q||p) is one half of the variance ratio plus the squared mean difference divided by target variance, minus one, plus the log variance ratio in the reverse direction.

Compare with the solution

The KL is one half of [1/4+(m−2)²/4−1+log 4]. Its only mean-dependent term is nonnegative and minimized at m=2. The minimum is (log 4−3/4)/2, approximately 0.3181. It remains positive because the family fixes approximate variance at one while the target variance is four. More iterations can locate the best mean more accurately but cannot change this family restriction; allowing the approximate variance to vary would include the exact target.

Take it with you

  • ELBO maximization minimizes reverse KL within a declared approximation family.
  • Factorization can preserve means while distorting variances and dependent predictions.
  • Optimization, approximation, and Monte Carlo errors need separate checks.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.