Skip to content
BIASED

The Laplace approximation and Bayesian classification

When the posterior is not Gaussian, replace it with a Gaussian at the mode whose covariance is the inverse Hessian: Bayesian logistic regression and IRLS, the moderated output via the probit approximation, the Laplace evidence and BIC, relevance vector classification and the cases where the approximation fails.

Where conjugacy ends

In the topics Bayesian linear regression and The relevance vector machine every step was exact. A Gaussian likelihood times a Gaussian prior gave a Gaussian posterior, and the evidence was a Gaussian integral. Classification breaks this at the first step.

Let the labels be ti∈{0,1}t_i \in \{0, 1\} and let the model say

p(ti=1∣w)=σ(w⊤φ(xi))=yi,σ(a)=11+e−a.p(t_i = 1 \mid w) = \sigma\big(w^\top \varphi(x_i)\big) = y_i, \qquad \sigma(a) = \frac{1}{1 + e^{-a}}.

This is logistic regression. The likelihood of all labels is a product of Bernoulli terms, and with the usual prior N(0,α−1I)\mathcal N(0, \alpha^{-1} I) the posterior is

p(w∣t)∝∏i=1nyiti(1−yi)1−ti⋅exp⁡(−α2 w⊤w).p(w \mid t) \propto \prod_{i=1}^{n} y_i^{t_i} (1 - y_i)^{1 - t_i} \cdot \exp\Big(-\frac{\alpha}{2}\, w^\top w\Big).

The logarithm of a sigmoid is not quadratic in ww, so this is not a Gaussian. No prior over ww is conjugate to it. Two integrals that were routine before now have no closed form: the normalizer p(t)=∫p(t∣w) p(w) dwp(t) = \int p(t \mid w)\, p(w)\, dw, which is the evidence, and the predictive probability ∫σ(w⊤φ(x∗)) p(w∣t) dw\int \sigma(w^\top \varphi(x_*))\, p(w \mid t)\, dw.

This topic replaces the posterior with a Gaussian chosen locally: centred at the posterior mode, with a width read off the curvature there. That is the Laplace approximation. It gives an approximate posterior, an approximate evidence and, for classification, a predictive probability that knows how uncertain the weights are.

A Gaussian at the mode

Write the posterior as p(θ∣X)=p~(θ)/Zp(\theta \mid X) = \tilde p(\theta) / Z, where p~(θ)=p(X∣θ) p(θ)\tilde p(\theta) = p(X \mid \theta)\, p(\theta) is the unnormalized posterior and Z=p(X)Z = p(X). Let θ^\hat\theta be the mode, so the gradient of log⁡p~\log \tilde p vanishes there. A second-order Taylor expansion of the log at the mode has no linear term:

log⁡p~(θ)≈log⁡p~(θ^)−12(θ−θ^)⊤H (θ−θ^),H=−∇∇log⁡p~(θ)∣θ=θ^.\log \tilde p(\theta) \approx \log \tilde p(\hat\theta) - \frac12 (\theta - \hat\theta)^\top H\, (\theta - \hat\theta), \qquad H = -\nabla\nabla \log \tilde p(\theta)\big|_{\theta = \hat\theta}.

Exponentiate, and the right-hand side is the kernel of a Gaussian:

p(θ∣X)≈N(θ∣θ^, H−1).p(\theta \mid X) \approx \mathcal N\big(\theta \mid \hat\theta,\ H^{-1}\big).

Three remarks.

  • Only p~\tilde p is needed. log⁡Z\log Z is a constant, and constants vanish under derivatives. This is what makes the method usable: ZZ is exactly what we cannot compute.
  • HH must be positive definite, which holds at a strict maximum. A sharp peak means large curvature and a narrow Gaussian.
  • When the posterior is Gaussian, log⁡p~\log \tilde p is exactly quadratic and the approximation is exact. In Bayesian linear regression HH is the posterior precision SN−1S_N^{-1} found in that topic.

Now the coin. Take a uniform prior on the probability of heads θ\theta and observe 3 heads and 1 tail. Then p~(θ)=θ3(1−θ)\tilde p(\theta) = \theta^3 (1 - \theta) and

ddθlog⁡p~=3θ−11−θ=0 ⇒ θ^=34,H=3θ^2+1(1−θ^)2=643.\frac{d}{d\theta} \log \tilde p = \frac{3}{\theta} - \frac{1}{1 - \theta} = 0 \ \Rightarrow\ \hat\theta = \frac34, \qquad H = \frac{3}{\hat\theta^2} + \frac{1}{(1 - \hat\theta)^2} = \frac{64}{3}.

The Laplace posterior is a Gaussian at 0.750.75 with standard deviation H−1/2≈0.217H^{-1/2} \approx 0.217. The exact posterior is Beta⁡(4,2)\operatorname{Beta}(4, 2), with mean 2/32/3 and standard deviation ≈0.178\approx 0.178. The Gaussian sits at the mode, not the mean, and it is too wide: the true posterior is skewed and bounded by 11, and a Gaussian can be neither.

The Laplace evidence and BIC

The same Gaussian also gives the normalizer. Integrate the approximation of p~\tilde p instead of p~\tilde p itself. The Gaussian integral in dd dimensions is known:

Z=∫p~(θ) dθ≈p~(θ^)∫exp⁡(−12(θ−θ^)⊤H(θ−θ^))dθ=p~(θ^) (2π)d/2det⁡(H)−1/2.Z = \int \tilde p(\theta)\, d\theta \approx \tilde p(\hat\theta) \int \exp\Big(-\frac12 (\theta - \hat\theta)^\top H (\theta - \hat\theta)\Big) d\theta = \tilde p(\hat\theta)\, (2\pi)^{d/2} \det(H)^{-1/2}.

In logarithms, with p~=p(X∣θ) p(θ)\tilde p = p(X \mid \theta)\, p(\theta),

log⁡p(X)≈log⁡p(X∣θ^)+log⁡p(θ^)+d2log⁡2π−12log⁡det⁡H.\log p(X) \approx \log p(X \mid \hat\theta) + \log p(\hat\theta) + \frac d2 \log 2\pi - \frac12 \log\det H.

The first term is the fit. The other three are the log of the Occam factor from the topic Bayesian model selection: the prior density at the mode times the volume of the posterior. In one dimension it is roughly the posterior width divided by the prior width. A model pays for every parameter that the data pin down much more tightly than the prior did.

For the coin, p~(3/4)=27/256≈0.105\tilde p(3/4) = 27/256 \approx 0.105 and 2π/H≈0.543\sqrt{2\pi / H} \approx 0.543, so the Laplace evidence is ≈0.0572\approx 0.0572. The exact evidence of this sequence is ∫01θ3(1−θ) dθ=1/20=0.05\int_0^1 \theta^3 (1 - \theta)\, d\theta = 1/20 = 0.05. Laplace overshoots by about 14.5%14.5\%, the price of the skew.

With many data points the formula simplifies further. For nn i.i.d. points HH is a sum of nn per-point curvatures plus the prior's, so H≈nIˉH \approx n \bar I for some fixed matrix Iˉ\bar I and log⁡det⁡H=dlog⁡n+O(1)\log\det H = d \log n + O(1). Keep only the terms that grow with nn, and θ^\hat\theta becomes the maximum likelihood estimate:

log⁡p(X)≈log⁡p(X∣θ^ML)−d2log⁡n.\log p(X) \approx \log p(X \mid \hat\theta_{\text{ML}}) - \frac d2 \log n.

This is the Bayesian information criterion (BIC); it is often multiplied by −2-2 and then minimized. Each parameter costs 12log⁡n\frac12 \log n nats. The prior and all O(1)O(1) terms are gone, so BIC needs large nn and a full-rank HH, and for small samples it is crude.

Bayesian logistic regression

Back to classification. Put the features into the design matrix Φ\Phi with rows φ(xi)⊤\varphi(x_i)^\top, as in Bayesian linear regression. The log-posterior is

log⁡p(w∣t)=∑i=1n[tilog⁡yi+(1−ti)log⁡(1−yi)]−α2 w⊤w+const.\log p(w \mid t) = \sum_{i=1}^{n} \Big[t_i \log y_i + (1 - t_i) \log(1 - y_i)\Big] - \frac{\alpha}{2}\, w^\top w + \text{const}.

With σ′(a)=σ(a)(1−σ(a))\sigma'(a) = \sigma(a)(1 - \sigma(a)) the derivatives come out short:

∇log⁡p(w∣t)=Φ⊤(t−y)−αw,∇∇log⁡p(w∣t)=−(Φ⊤BΦ+αI),B=diag⁡(yi(1−yi)).\nabla \log p(w \mid t) = \Phi^\top (t - y) - \alpha w, \qquad \nabla\nabla \log p(w \mid t) = -\big(\Phi^\top B \Phi + \alpha I\big), \qquad B = \operatorname{diag}\big(y_i (1 - y_i)\big).

The Hessian is negative definite for every ww, so the log-posterior is strictly concave: one mode, no local optima. Newton's method finds it,

wnew=w+(Φ⊤BΦ+αI)−1(Φ⊤(t−y)−αw),w^{\text{new}} = w + \big(\Phi^\top B \Phi + \alpha I\big)^{-1} \big(\Phi^\top (t - y) - \alpha w\big),

with yy and BB recomputed at the current ww. Each step solves a weighted least-squares problem with weights BB, hence the name iteratively reweighted least squares (IRLS). Without the prior (α=0\alpha = 0) and with separable classes, the likelihood keeps growing as ∥w∥→∞\lVert w \rVert \to \infty and there is no mode at all; the prior keeps it finite.

At the mode, the Laplace approximation is

p(w∣t)≈N(w∣wMAP,SN),SN−1=Φ⊤BΦ+αI.p(w \mid t) \approx \mathcal N(w \mid w_{\text{MAP}}, S_N), \qquad S_N^{-1} = \Phi^\top B \Phi + \alpha I.

Compare it with SN−1=αI+βΦ⊤ΦS_N^{-1} = \alpha I + \beta \Phi^\top \Phi in linear regression. The noise precision β\beta is replaced by a weight yi(1−yi)≤1/4y_i (1 - y_i) \le 1/4 per point. It is largest at yi=1/2y_i = 1/2: points near the decision boundary say the most about ww, and confidently classified points say little.

A running example with one feature and no bias: x=(−1.5,−0.5,0.5,1,2)x = (-1.5, -0.5, 0.5, 1, 2), labels t=(0,1,0,1,1)t = (0, 1, 0, 1, 1), prior precision α=1\alpha = 1. Newton from w=0w = 0 converges in three steps to wMAP≈0.6305w_{\text{MAP}} \approx 0.6305. There H=∑ixi2yi(1−yi)+α≈2.49H = \sum_i x_i^2 y_i (1 - y_i) + \alpha \approx 2.49, so the Laplace standard deviation is ≈0.634\approx 0.634. Five points barely pin the weight down.

The predictive and the probit approximation

The predictive probability for a new point averages the sigmoid over the posterior:

p(t∗=1∣x∗,t)=∫σ(w⊤φ∗) p(w∣t) dw,φ∗=φ(x∗).p(t_* = 1 \mid x_*, t) = \int \sigma\big(w^\top \varphi_*\big)\, p(w \mid t)\, dw, \qquad \varphi_* = \varphi(x_*).

The integrand depends on ww only through the activation a=w⊤φ∗a = w^\top \varphi_*. Under the Laplace posterior aa is a linear function of a Gaussian vector, so it is Gaussian:

μa=wMAP⊤φ∗,sa2=φ∗⊤SN φ∗.\mu_a = w_{\text{MAP}}^\top \varphi_*, \qquad s_a^2 = \varphi_*^\top S_N\, \varphi_*.

What remains is a one-dimensional integral ∫σ(a) N(a∣μa,sa2) da\int \sigma(a)\, \mathcal N(a \mid \mu_a, s_a^2)\, da, still without a closed form. The trick is the probit approximation. Let ΦN\Phi_{\mathcal N} be the standard normal CDF. Scaled to match the slope of σ\sigma at zero, it is very close to the sigmoid:

σ(a)≈ΦN(λa),λ2=π8,\sigma(a) \approx \Phi_{\mathcal N}(\lambda a), \qquad \lambda^2 = \frac{\pi}{8},

with a maximum error below 0.020.02. A Gaussian average of a probit is again a probit, ∫ΦN(λa) N(a∣μ,s2) da=ΦN(λμ/1+λ2s2)\int \Phi_{\mathcal N}(\lambda a)\, \mathcal N(a \mid \mu, s^2)\, da = \Phi_{\mathcal N}\big(\lambda \mu / \sqrt{1 + \lambda^2 s^2}\big). Converting back to the sigmoid gives the moderated output:

p(t∗=1∣x∗,t)≈σ(κ(sa2) μa),κ(s2)=(1+πs28)−1/2.p(t_* = 1 \mid x_*, t) \approx \sigma\big(\kappa(s_a^2)\, \mu_a\big), \qquad \kappa(s^2) = \Big(1 + \frac{\pi s^2}{8}\Big)^{-1/2}.

Since κ≤1\kappa \le 1, the activation is pulled toward zero, more strongly where the weights are uncertain. The sign of μa\mu_a does not change, so the decision boundary at probability 1/21/2 is the same as for wMAPw_{\text{MAP}}. What changes is the confidence away from the data.

In the running example take x∗=3x_* = 3. Then μa≈1.891\mu_a \approx 1.891, sa2≈3.61s_a^2 \approx 3.61 and κ≈0.643\kappa \approx 0.643. Plugging in wMAPw_{\text{MAP}} gives σ(1.891)≈0.869\sigma(1.891) \approx 0.869; the moderated output is ≈0.771\approx 0.771. Integrating over the exact posterior numerically gives ≈0.790\approx 0.790. The plug-in estimate is overconfident; the moderated one is close.

Relevance vector classification

The Laplace evidence lets classification use the machinery of The relevance vector machine. Give every weight its own precision, p(w)=N(0,A−1)p(w) = \mathcal N(0, A^{-1}) with A=diag⁡(α1,…,αM)A = \operatorname{diag}(\alpha_1, \dots, \alpha_M) for MM basis functions. The exact evidence p(t∣α)p(t \mid \alpha) is out of reach, but the Laplace one is not:

log⁡p(t∣α)≈log⁡p(t∣wMAP)+log⁡N(wMAP∣0,A−1)+M2log⁡2π−12log⁡det⁡(Φ⊤BΦ+A).\log p(t \mid \alpha) \approx \log p(t \mid w_{\text{MAP}}) + \log \mathcal N(w_{\text{MAP}} \mid 0, A^{-1}) + \frac M2 \log 2\pi - \frac12 \log\det\big(\Phi^\top B \Phi + A\big).

Setting its derivatives in αi\alpha_i to zero, with BB held fixed, gives the same fixed-point updates as in regression. The Gaussian posterior that was exact there is now the Laplace one. Training alternates two loops:

  1. For the current α\alpha, run Newton (IRLS) to the mode wMAPw_{\text{MAP}}, warm-started from the previous one.
  2. At the mode, form Σ=(Φ⊤BΦ+A)−1\Sigma = (\Phi^\top B \Phi + A)^{-1}.
  3. Update γi=1−αiΣii\gamma_i = 1 - \alpha_i \Sigma_{ii} and αi←γi/wMAP,i2\alpha_i \leftarrow \gamma_i / w_{\text{MAP}, i}^2.
  4. Remove the basis functions whose αi\alpha_i has run off to infinity, and repeat.

The result is a relevance vector classifier: sparse like an SVM, usually with far fewer kept vectors, and with probabilities as outputs through the moderated predictive. The cost is the nested loop: every evidence step needs a fresh Newton solve.

Where Laplace fails and what comes next

A Gaussian fitted at one point sees only the local curvature. Four things escape it.

  • Skew and bounded support. For the coin the Gaussian sits at the mode 0.750.75 while the mean is 2/32/3, puts 12.4%12.4\% of its mass above 11, and overestimates the evidence by 14.5%14.5\%.
  • Parametrization. A density changes by a Jacobian when the variable changes, so the mode moves and the Laplace answer depends on the coordinates. In the log-odds η=log⁡θ1−θ\eta = \log\frac{\theta}{1 - \theta} the coin's Gaussian covers the whole line and maps back inside [0,1][0, 1]. It gives a mean of θ\theta of ≈0.645\approx 0.645, a standard deviation ≈0.174\approx 0.174 and an evidence ≈0.0476\approx 0.0476, much closer to the exact 0.6670.667, 0.1780.178 and 0.050.05.
  • Several modes. Observe t=2t = 2 with t=θ2+εt = \theta^2 + \varepsilon, noise standard deviation 0.50.5 and prior N(0,22)\mathcal N(0, 2^2). The posterior has peaks at θ≈±1.403\theta \approx \pm 1.403. The Gaussian at one of them has standard deviation ≈0.178\approx 0.178, while the whole posterior spreads with standard deviation ≈1.377\approx 1.377: almost eight times wider. The other peak is invisible.
  • Heavy tails and cost. The curvature at the mode says nothing about mass far away. And HH is a d×dd \times d matrix: for large models it is expensive to form and invert, and nearly flat directions make it close to singular. Laplace for neural networks returns in Bayesian deep learning.

The method works well when the posterior is unimodal and close to Gaussian, typically with plenty of data. Log-concave posteriors such as logistic regression's are the safe case.

Everything here started from the mode. With latent variables even that becomes hard. In a mixture model each point has a hidden component label, and the likelihood p(X∣θ)=∑Zp(X,Z∣θ)p(X \mid \theta) = \sum_Z p(X, Z \mid \theta) puts a sum inside the logarithm: no closed-form maximum, and many equivalent modes from relabelling the components. The next topic, EM and Bayesian PCA, builds an algorithm that climbs such likelihoods by filling in the hidden variables.

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.