Skip to content

Probabilistic inference, Monte Carlo, and posterior computation · Probabilistic computation and inference foundations · INF005 · Algorithm

Laplace approximation

Approximates a posterior by a Gaussian centered at a mode, with covariance determined by local curvature. It can provide fast uncertainty summaries, approximate evidence, or a proposal for more accurate sampling when…

Runs in CodeSplash Complete calculation CodeSplash's run and the reference's record

In CodeSplash

A CodeSplash method runs this entry in Python and R. Set it up from the method's page.

Do it in CodeSplash Run it on your own data, in Python or R.

Availability
Runs in CodeSplash
Code example
Complete calculation
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
Verified in Python and R through learn.bayes_glm, mixed.glmm. The reference's example ran in Python and R here, and they agree.
The reference's record
Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Approximates a posterior by a Gaussian centered at a mode, with covariance determined by local curvature. It can provide fast uncertainty summaries, approximate evidence, or a proposal for more accurate sampling when the posterior is sufficiently regular.

Question / hypotheses

How well does a quadratic expansion of the log unnormalized posterior describe the probability mass around a selected mode?

Architecture / computational structure

Optimize the log posterior, evaluate its Hessian at the selected solution, factor the precision matrix, and draw Gaussian perturbations or compute quadratic summaries. Determinant terms are needed for an evidence approximation. Constants omitted during optimization must be restored when computing evidence.

Assumptions

An interior, isolated mode with a positive-definite negative log-posterior Hessian is required for the usual Gaussian form. Coordinates matter: constrained variables should be transformed with the appropriate Jacobian. Multimodal, skewed, boundary, or weakly identified posteriors may violate the approximation’s practical accuracy.

Formula / computational definition

θ^=arg⁡max⁡θlog⁡p~(θ),H=−∇2log⁡p~(θ^),q(θ)=N(θ^,H−1);Z≈p~(θ^)(2π)d/2∣H∣−1/2.\hat\theta=\arg\max_\theta\log\tilde p(\theta),\quad H=-\nabla^2\log\tilde p(\hat\theta),\quad q(\theta)=\mathcal N(\hat\theta,H^{-1});\quad Z\approx\tilde p(\hat\theta)(2\pi)^{d/2}|H|^{-1/2}.

Parameters and notation

Estimated quantities are the posterior mode and Hessian, not a separately trained Gaussian network. Model priors and likelihood parameters define the target. Numerical choices include optimizer tolerance, coordinate transform, Hessian method, initialization, and any stabilization. A ridge added to H changes the approximation and must be reported.

Fitting, fine-tuning, and adaptation

After a likelihood, prior, data, or neural-weight change, recompute the mode and curvature. Warm starts can save optimization but do not justify reusing old uncertainty. A last-layer neural Laplace approximation conditions on the frozen feature extractor and does not integrate uncertainty in the entire network.

How to use and calibrate

Check optimizer gradients, Hessian eigenvalues, and multiple starts. Compare marginal shapes against grid integration in low dimensions or well-diagnosed MCMC. Plot the actual log posterior and quadratic expansion over a meaningful neighborhood; inspect transformed-back intervals.

Example application

Approximate uncertainty in a logistic intercept or a small Bayesian classifier head when a rapid posterior summary is useful and a direct curvature calculation is feasible.

Interpretation / pitfalls

Watch out

The inverse Hessian at a maximum-likelihood estimate is not automatically a Bayesian posterior covariance. Gaussian approximations can miss tails and separate modes even when the optimizer converges. Evidence is particularly sensitive to prior normalization and neglected modes.

Python and R code examples

Scope: Complete one-dimensional Laplace calculation for a binomial logistic intercept with a standard-normal prior; not an assessment of approximation error.

Implementation route: Python uses scipy.optimize and NumPy; R uses base optimize. Stan supplies Laplace sampling through its supported interfaces; it operates in specified unconstrained coordinates and requires consistent Jacobian conventions.

Dependencies and validation: Python — executed successfully on the supplied synthetic inputs; imports identify NumPy/SciPy dependencies. R — base R, reviewed but not executed because Rscript is unavailable. Random-number streams differ between languages; matching seed integers do not imply matching draws.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Complete calculation — Complete one-dimensional Laplace calculation for a binomial logistic intercept with a standard-normal prior; not an assessment of approximation error. · needs numpy, scipy

import numpy as np
from scipy.optimize import minimize_scalar
n=15; successes=10
nll=lambda t: n*np.logaddexp(0,t)-successes*t+.5*t*t
fit=minimize_scalar(nll,bounds=(-10,10),method='bounded'); mode=fit.x
p=1/(1+np.exp(-mode)); H=n*p*(1-p)+1
assert fit.success and H>0
print('mode',mode,'approx posterior SD',1/np.sqrt(H))
R

Complete calculation — Complete one-dimensional Laplace calculation for a binomial logistic intercept with a standard-normal prior; not an assessment of approximation error.

n <- 15; successes <- 10
nll <- function(t) n*log1p(exp(t))-successes*t+.5*t*t
fit <- optimize(nll,c(-10,10)); mode <- fit$minimum
p <- plogis(mode); H <- n*p*(1-p)+1
stopifnot(H>0); print(c(mode=mode,posterior.SD=1/sqrt(H)))

Sources

From the CodeSplash reference, entry INF005.