alanjiach@gmail.com

← all writing

Likelihood, latent variables, and the ELBO

study notesprobability

Part of the generative visual computing notes.

Maximum likelihood

Suppose a coin has an unknown probability pp of heads. We observe eight heads and two tails in ten independent tosses. For that ordered sequence, the likelihood is p8(1−p)2p^8(1-p)^2. The log likelihood is

ℓ(p)=8log⁡p+2log⁡(1−p).\ell(p)=8\log p+2\log(1-p).

Setting its derivative to zero gives 8/p−2/(1−p)=08/p-2/(1-p)=0, so the maximum-likelihood estimate is p=0.8p=0.8. If we record only the number of heads, a binomial coefficient appears in the likelihood. It does not change the maximizing value because it is independent of pp.

For a dataset, maximum likelihood chooses parameters that assign high probability or density to the observed examples. It does not treat the parameter itself as a random variable unless a prior is introduced.

Cross-entropy and KL divergence

For discrete distributions PP and QQ,

H(P,Q)=−∑xP(x)log⁡Q(x),H(P,Q)=-\sum_x P(x)\log Q(x),

and

KL⁡(P∥Q)=H(P,Q)−H(P).\operatorname{KL}(P\|Q)=H(P,Q)-H(P).

The entropy H(P)H(P) does not depend on the fitted model QQ. Minimizing population negative log likelihood therefore minimizes forward KL, when these quantities are well defined. Empirical average negative log likelihood estimates the population objective from the available examples.

Direction matters. If P(x)>0P(x)>0 but Q(x)=0Q(x)=0, forward KL is infinite. Reversing the arguments changes which unsupported assignments receive that penalty. KL is nonnegative, but it is not a symmetric distance.

Hidden choices

Consider two coins chosen with equal prior probability. Coin A gives heads with probability 0.80.8, and coin B gives heads with probability 0.20.2. After observing heads,

P(A∣H)=0.8(0.5)0.8(0.5)+0.2(0.5)=0.8.P(A\mid H)=\frac{0.8(0.5)}{0.8(0.5)+0.2(0.5)}=0.8.

The hidden coin identity is a latent variable. In a larger model, the marginal likelihood sums or integrates over all latent possibilities:

pθ(x)=∫pθ(x,z) dz.p_\theta(x)=\int p_\theta(x,z)\,dz.

This integral can be difficult even when the joint density is easy to evaluate.

Deriving the lower bound

Introduce a tractable distribution qϕ(z∣x)q_\phi(z\mid x) and expand its KL divergence to the true posterior. Bayes’ rule gives

KL⁡(qϕ(z∣x)∥pθ(z∣x))=Eq[log⁡qϕ(z∣x)−log⁡pθ(x,z)]+log⁡pθ(x).\operatorname{KL}(q_\phi(z\mid x)\|p_\theta(z\mid x)) =\mathbb E_q[\log q_\phi(z\mid x)-\log p_\theta(x,z)]+\log p_\theta(x).

Rearranging,

log⁡pθ(x)=L(x)+KL⁡(qϕ(z∣x)∥pθ(z∣x)),\log p_\theta(x)=\mathcal L(x)+\operatorname{KL}(q_\phi(z\mid x)\|p_\theta(z\mid x)),

where

L(x)=Eq[log⁡pθ(x∣z)]−KL⁡(qϕ(z∣x)∥p(z)).\mathcal L(x)=\mathbb E_q[\log p_\theta(x\mid z)]-\operatorname{KL}(q_\phi(z\mid x)\|p(z)).

The second term in the identity is nonnegative, so L\mathcal L is a lower bound. Equality holds when the approximate posterior matches the true posterior, subject to the usual support conditions.

There are two different KL terms here. KL to the true posterior is the approximation gap. KL to the prior is the computable regularizer in the training objective. Confusing them makes the derivation difficult to follow.

For a fixed generative model, improving the variational distribution tightens the bound. When both networks are trained, the model and the approximation change together. A larger ELBO is useful evidence about the bound, but it does not separately identify how much the likelihood and the posterior approximation improved.

Worked example: the Gaussian KL term

Let the approximate posterior be q(z∣x)=N(1,0.25)q(z\mid x)=\mathcal N(1,0.25) and the prior be p(z)=N(0,1)p(z)=\mathcal N(0,1). The second argument here is the variance. Then

DKL(q∥p)=12(12+0.25−1−log⁡0.25)≈0.818.D_{\mathrm{KL}}(q\|p)=\frac12(1^2+0.25-1-\log 0.25)\approx0.818.

If the expected reconstruction log probability is −3-3, the ELBO is about −3.818-3.818. Minimizing the negative ELBO therefore gives a loss of 3.8183.818. A good reconstruction alone is not sufficient: moving the posterior away from the prior also has a cost. These numbers illustrate the objective for one observation, not a bound on every possible reconstruction sample.

Further reading

Auto-Encoding Variational Bayes derives the variational objective and reparameterized estimator. Compare its Gaussian KL expression with the calculation above, taking care to distinguish variance from standard deviation.