Skip to content
BIASED

Variational inference

When the posterior cannot be written down, approximate it: maximize the evidence lower bound over a convenient family. The two KL terms of the ELBO, mean field and the general factor update, coordinate ascent on a Normal–Gamma model checked against the exact answer, underestimated variance and the mode-seeking reverse KL.

From the E-step to optimization

The topic EM and Bayesian PCA split the log-likelihood into a lower bound and a gap, and its E-step closed the gap by setting q(Z)=p(Z∣X,θ)q(Z) = p(Z \mid X, \theta). That step quietly assumed we can write the exact posterior over the latent variables. Often we cannot. A non-conjugate piece or a coupling between latent variables is enough to make the normalizing integral intractable. And once the parameters get priors too, they join the unknowns, and a posterior over everything at once is rarely available in closed form.

Variational inference keeps the decomposition and gives up exactness. Let ZZ now stand for all unknowns: latent variables and parameters alike. For any distribution q(Z)q(Z),

log⁡p(X)=L(q)+KL(q(Z) ∥ p(Z∣X)),L(q)=Eq[log⁡p(X,Z)]−Eq[log⁡q(Z)].\log p(X) = \mathcal L(q) + \mathrm{KL}\big(q(Z) \,\|\, p(Z \mid X)\big), \qquad \mathcal L(q) = \mathbb E_q[\log p(X, Z)] - \mathbb E_q[\log q(Z)].

The left side does not depend on qq. So raising the ELBO L(q)\mathcal L(q) lowers the KL to the posterior by exactly the same amount. We pick a variational family Q\mathcal Q of distributions we can handle and solve

q∗=arg⁡max⁡q∈QL(q).q^* = \arg\max_{q \in \mathcal Q} \mathcal L(q).

An integral we cannot take becomes an optimization we can run. If Q\mathcal Q happens to contain the true posterior, the optimum is the posterior itself, and we are back at the exact E-step of EM.

All of this is useful only if L(q)\mathcal L(q) can be evaluated while the posterior cannot. Read its definition once more with that question in mind; the check below asks exactly this.

If some parameters θ\theta stay point estimates, we can alternate: a variational E-step over q∈Qq \in \mathcal Q, then an M-step over θ\theta. This is variational EM. Each step raises the ELBO, but unlike exact EM the log-likelihood itself is no longer guaranteed to grow.

Two KL terms in one bound

Substitute p(X,Z)=p(X∣Z) p(Z)p(X, Z) = p(X \mid Z)\, p(Z) into the ELBO and regroup:

L(q)=Eq[log⁡p(X∣Z)]−KL(q(Z) ∥ p(Z)).\mathcal L(q) = \mathbb E_q[\log p(X \mid Z)] - \mathrm{KL}\big(q(Z) \,\|\, p(Z)\big).

The first term rewards qq for putting mass where the unknowns explain the data well. The second keeps qq close to the prior. This is the Bayesian trade-off in one line, and it is exactly the VAE loss of the topic Black-box variational inference and the VAE, with the sign flipped.

So this topic now has two KL divergences: one in the decomposition of log⁡p(X)\log p(X) from the previous block, and one inside the bound. Their second arguments differ, and only one of them can be computed. Mixing them up is a classic exam mistake, so keep track of which is which.

A tiny example makes the gap concrete. Take one point xx and the two-component mixture from the topic EM and Bayesian PCA, with label z∈{1,2}z \in \{1, 2\}. Suppose the joint values are p(x,z=1)=0.12p(x, z{=}1) = 0.12 and p(x,z=2)=0.04p(x, z{=}2) = 0.04. Then p(x)=0.16p(x) = 0.16 and the posterior is (0.75,0.25)(0.75, 0.25). For a discrete zz the bound is a sum:

L(q)=∑zq(z)log⁡p(x,z)q(z).\mathcal L(q) = \sum_{z} q(z) \log \frac{p(x, z)}{q(z)}.

With qq equal to the posterior, every ratio equals 0.160.16 and L=log⁡0.16\mathcal L = \log 0.16: no gap. With the hard assignment q=(1,0)q = (1, 0), as in kk-means, L=log⁡0.12\mathcal L = \log 0.12, and the gap is log⁡(0.16/0.12)=log⁡(4/3)≈0.288\log(0.16/0.12) = \log(4/3) \approx 0.288 nats. That number is the KL from (1,0)(1, 0) to (0.75,0.25)(0.75, 0.25).

Mean field and the general update

The choice of Q\mathcal Q is the whole game. A fixed-form family names the shape of qq, for example a Gaussian with free mean and covariance; we return to it in the topic Black-box variational inference and the VAE. A free-form family names only which unknowns are independent and lets the bound decide the shape. The classic free-form family is mean field: split ZZ into blocks Z1,…,ZmZ_1, \dots, Z_m and require

q(Z)=∏j=1mqj(Zj).q(Z) = \prod_{j=1}^{m} q_j(Z_j).

Correlations between blocks are lost by design; within a block anything goes.

Now optimize one factor qjq_j while the others are fixed. Write E−j\mathbb E_{-j} for the expectation over all factors except qjq_j. The terms of L\mathcal L that involve qjq_j are

L(q)=∫qj(Zj) E−j[log⁡p(X,Z)] dZj−∫qj(Zj)log⁡qj(Zj) dZj+const=−KL(qj ∥ p~j)+const,\mathcal L(q) = \int q_j(Z_j)\, \mathbb E_{-j}[\log p(X, Z)]\, dZ_j - \int q_j(Z_j) \log q_j(Z_j)\, dZ_j + \text{const} = -\mathrm{KL}\big(q_j \,\|\, \tilde p_j\big) + \text{const},

where p~j(Zj)∝exp⁡(E−j[log⁡p(X,Z)])\tilde p_j(Z_j) \propto \exp\big(\mathbb E_{-j}[\log p(X, Z)]\big). A KL is smallest, zero, when its arguments coincide. So the optimal factor is

log⁡qj∗(Zj)=E−j[log⁡p(X,Z)]+const.\log q_j^*(Z_j) = \mathbb E_{-j}[\log p(X, Z)] + \text{const}.

The constant is fixed by normalization. Terms of log⁡p(X,Z)\log p(X, Z) that do not contain ZjZ_j fall into it, so only the complete conditional p(Zj∣Z−j,X)p(Z_j \mid Z_{-j}, X) matters. When that conditional lies in an exponential family whose natural parameters are linear in the statistics of the other blocks (conditional conjugacy), qj∗q_j^* stays in the same family, and an update only recomputes its parameters.

Cycling through the blocks is coordinate ascent variational inference (CAVI). Every update maximizes L\mathcal L exactly over one factor, so the ELBO never decreases. It converges to a local optimum, which may depend on the starting point.

Example: the mean and precision of a Normal

Time for a worked example. The topic Conjugate priors and the exponential family estimated a Normal mean with known variance. Now both the mean μ\mu and the precision τ\tau are unknown, with the Normal–Gamma prior:

τ∼Gamma⁡(a0,b0),μ∣τ∼N(μ0,(κ0τ)−1),xi∣μ,τ∼N(μ,τ−1).\tau \sim \operatorname{Gamma}(a_0, b_0), \qquad \mu \mid \tau \sim \mathcal N\big(\mu_0, (\kappa_0 \tau)^{-1}\big), \qquad x_i \mid \mu, \tau \sim \mathcal N(\mu, \tau^{-1}).

The exact posterior is again Normal–Gamma, so we do not need VI here. That is the point: we can grade its answer. The data are four numbers x=(1,3,2,2)x = (1, 3, 2, 2), so n=4n = 4 and xˉ=2\bar x = 2; the prior is μ0=0\mu_0 = 0, κ0=1\kappa_0 = 1, a0=b0=1a_0 = b_0 = 1.

The mean-field family is q(μ,τ)=q(μ) q(τ)q(\mu, \tau) = q(\mu)\, q(\tau). Write the log joint, dropping constants:

log⁡p(X,μ,τ)=n2log⁡τ−τ2∑i=1n(xi−μ)2+12log⁡τ−κ0τ2(μ−μ0)2+(a0−1)log⁡τ−b0τ.\log p(X, \mu, \tau) = \tfrac n2 \log \tau - \tfrac{\tau}{2} \sum_{i=1}^{n} (x_i - \mu)^2 + \tfrac12 \log \tau - \tfrac{\kappa_0 \tau}{2} (\mu - \mu_0)^2 + (a_0 - 1) \log \tau - b_0 \tau.

Update of q(μ)q(\mu). Take Eτ\mathbb E_\tau of the terms with μ\mu. They are linear in τ\tau, so only E[τ]\mathbb E[\tau] enters:

log⁡q∗(μ)=−E[τ]2[∑i(xi−μ)2+κ0(μ−μ0)2]+const.\log q^*(\mu) = -\tfrac{\mathbb E[\tau]}{2} \Big[\sum_{i} (x_i - \mu)^2 + \kappa_0 (\mu - \mu_0)^2\Big] + \text{const}.

This is a quadratic in μ\mu, so q(μ)=N(m,λ−1)q(\mu) = \mathcal N(m, \lambda^{-1}) with

m=κ0μ0+nxˉκ0+n,λ=(κ0+n) E[τ].m = \frac{\kappa_0 \mu_0 + n \bar x}{\kappa_0 + n}, \qquad \lambda = (\kappa_0 + n)\, \mathbb E[\tau].

Here m=8/5=1.6m = 8/5 = 1.6 and λ=5 E[τ]\lambda = 5\, \mathbb E[\tau]. Notice that mm does not depend on q(τ)q(\tau) at all: precision scales both terms of the bracket equally.

Update of q(τ)q(\tau). Take Eμ\mathbb E_\mu of the whole log joint. Every term is either a multiple of log⁡τ\log \tau or a multiple of τ\tau, and that is the signature of a Gamma density ta−1e−btt^{a-1} e^{-bt}. Collect the coefficients of log⁡τ\log \tau to get the shape aNa_N; the rate bNb_N needs Eμ\mathbb E_\mu of the squares and comes in the next block.

Coordinate ascent in numbers

For the rate, use Eμ[(xi−μ)2]=(xi−m)2+λ−1\mathbb E_\mu[(x_i - \mu)^2] = (x_i - m)^2 + \lambda^{-1} and the same for (μ−μ0)2(\mu - \mu_0)^2:

bN=b0+12[∑i(xi−m)2+κ0(m−μ0)2+n+κ0λ],E[τ]=aNbN.b_N = b_0 + \tfrac12 \Big[\sum_{i} (x_i - m)^2 + \kappa_0 (m - \mu_0)^2 + \frac{n + \kappa_0}{\lambda}\Big], \qquad \mathbb E[\tau] = \frac{a_N}{b_N}.

With our data, ∑i(xi−1.6)2=2.64\sum_i (x_i - 1.6)^2 = 2.64 and κ0m2=2.56\kappa_0 m^2 = 2.56. The shape is aN=3.5a_N = 3.5. One CAVI sweep is: set mm and λ=5 E[τ]\lambda = 5\,\mathbb E[\tau] from the current q(τ)q(\tau), then bNb_N from the new q(μ)q(\mu), then E[τ]=3.5/bN\mathbb E[\tau] = 3.5 / b_N. The ELBO is also in closed form, since every expectation it needs is a Gaussian or Gamma moment, with E[log⁡τ]=ψ(aN)−log⁡bN\mathbb E[\log \tau] = \psi(a_N) - \log b_N (ψ\psi is the digamma function).

Start from the guess E[τ]=2\mathbb E[\tau] = 2:

sweep λ\lambda bNb_N E[τ]\mathbb E[\tau] ELBO
1 10.000 3.850 0.9091 −7.8443
2 4.545 4.150 0.8434 −7.7127
3 4.217 4.193 0.8348 −7.7112
4 4.174 4.199 0.8335 −7.7111
limit 4.167 4.200 0.8333 −7.7111

Three things to read off the table:

  • The ELBO grows at every sweep, as CAVI promises. A drop would mean a bug in an update or in the ELBO formula, not a feature of the data.
  • Convergence takes a handful of sweeps. Only E[τ]\mathbb E[\tau] really moves, because m=1.6m = 1.6 was right from the first update.
  • The limit is E[τ]=5/6\mathbb E[\tau] = 5/6, λ=25/6\lambda = 25/6, so q(μ)=N(1.6, 0.24)q(\mu) = \mathcal N(1.6,\ 0.24) and q(τ)=Gamma⁡(3.5, 4.2)q(\tau) = \operatorname{Gamma}(3.5,\ 4.2).

Run the same loop in your head from a different start and you land on the same limit: in this model the optimum is unique.

What mean field gets wrong

Now grade the answer against the exact Normal–Gamma posterior: κn=5\kappa_n = 5, mn=1.6m_n = 1.6, an=a0+n/2=3a_n = a_0 + n/2 = 3, bn=3.6b_n = 3.6.

  • Means are right. The exact E[μ∣X]=1.6\mathbb E[\mu \mid X] = 1.6 and E[τ∣X]=an/bn=5/6\mathbb E[\tau \mid X] = a_n / b_n = 5/6, the same as under qq. This is no coincidence: at the fixed point bN=bn+1/(2 E[τ])b_N = b_n + 1/(2\,\mathbb E[\tau]) and E[τ]=(an+12)/bN\mathbb E[\tau] = (a_n + \tfrac12)/b_N, which solve to E[τ]=an/bn\mathbb E[\tau] = a_n / b_n.
  • The spread of μ\mu is too small. Under qq the variance is 1/(κnE[τ])=bn/(κnan)=0.241/(\kappa_n \mathbb E[\tau]) = b_n/(\kappa_n a_n) = 0.24. The exact marginal of μ\mu is a Student tt with variance bn/(κn(an−1))=0.36b_n / (\kappa_n (a_n - 1)) = 0.36. Standard deviation 0.490.49 instead of 0.600.60.
  • The gap is small but not zero. The exact log⁡p(X)=−7.6301\log p(X) = -7.6301, so KL(q ∥ p(μ,τ∣X))=−7.6301−(−7.7111)=0.0810\mathrm{KL}(q \,\|\, p(\mu, \tau \mid X)) = -7.6301 - (-7.7111) = 0.0810 nats, and no number of sweeps will close it.

Where did the spread go? In the exact posterior μ\mu and τ\tau are dependent: the precision of μ\mu is κnτ\kappa_n \tau, so a small τ\tau goes with a wide μ\mu. Averaging over τ\tau fattens the tails of μ\mu. Mean field cuts that link and plugs in a single E[τ]\mathbb E[\tau].

The same effect, in its purest form: let the target be a Gaussian N(μ,Σ)\mathcal N(\mu, \Sigma) with precision matrix Λ=Σ−1\Lambda = \Sigma^{-1}. The update rule gives qj=N(μj,1/Λjj)q_j = \mathcal N(\mu_j, 1/\Lambda_{jj}) at the optimum, and 1/Λjj≤Σjj1/\Lambda_{jj} \le \Sigma_{jj}, with equality only for uncorrelated coordinates. With unit variances and correlation ρ\rho the factor variance is 1−ρ21 - \rho^2: for ρ=0.8\rho = 0.8, 0.360.36 instead of 11. For a Gaussian target the means still come out exact; for other targets mean field guarantees neither the means nor the variances. The safe rule: mean-field credible intervals tend to be too narrow.

The reverse KL seeks a mode

Why too narrow and not too wide? Write the divergence VI minimizes:

KL(q ∥ p)=Eq[log⁡q(Z)−log⁡p(Z∣X)].\mathrm{KL}(q \,\|\, p) = \mathbb E_q\big[\log q(Z) - \log p(Z \mid X)\big].

Wherever pp is close to zero but qq is not, −log⁡p-\log p is huge and the penalty explodes. So qq must vanish where pp vanishes. This is zero-forcing, and it makes the reverse KL mode-seeking: a qq that cannot cover everything retreats into one high-density region. The forward KL(p ∥ q)\mathrm{KL}(p \,\|\, q), which expectation propagation works with, behaves the other way and spreads qq over all the mass.

Take p=12N(−3,1)+12N(3,1)p = \tfrac12 \mathcal N(-3, 1) + \tfrac12 \mathcal N(3, 1) and a Gaussian qq. The reverse-KL optimum is essentially N(3,1)\mathcal N(3, 1) (or its mirror image), with KL≈0.69\mathrm{KL} \approx 0.69. The moment-matched N(0,10)\mathcal N(0, 10), which covers both bumps and sits on the valley between them, scores worse, 0.940.94. Under the forward KL the ranking flips: 0.460.46 for the wide Gaussian against 8.38.3 for the single bump.

The number 0.69≈log⁡20.69 \approx \log 2 is not an accident. A qq that matches one of two equal, separated modes misses exactly half the mass. Mixtures produce such modes for free. Put priors on the parameters of the two-component mixture from the topic EM and Bayesian PCA. Swapping the labels of the components changes nothing in the likelihood or in a symmetric prior, so the posterior over the component parameters has two mirror-image modes. A mean-field qq settles in one of them, and its bound sits below log⁡p(X)\log p(X) by about log⁡2\log 2 from the label symmetry alone. Since this part of the gap is the same for every mode, it is often added back by hand when the ELBO is used to compare models.

Variational inference is fast and deterministic, and its ELBO is a usable stand-in for the evidence. Its error, though, is built into the family: the gap of 0.0810.081 nats in our Normal–Gamma example and the missing modes of a mixture stay however long we optimize. The alternative is to give up a closed-form qq and draw samples whose distribution approaches the exact posterior, so the error shrinks as the run grows. That is the topic Monte Carlo and Markov chains.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.