The Laplace approximation: a normal posterior from optim and the Hessian
How to approximate a non-conjugate posterior, like Bayesian logistic regression, with a normal distribution centred at the posterior mode, using nothing but a numerical optimiser and its Hessian.
University lab From my coursework in the MSc in Statistics and Machine Learning at Linköping University, rewritten as a note to review later.
Logistic regression with a normal prior has no closed-form posterior. But with a decent amount of data the posterior is usually close to a normal bump, so we can approximate it with one: put the centre at the posterior mode and get the width from the curvature there. In the second Bayesian Learning lab (pair work) we did exactly this, and the whole thing turned out to be one call to optim.
The idea
Take the log posterior and expand it around its maximum (the posterior mode). The first derivative is zero there, so a second-order Taylor expansion gives
where is the negative Hessian (the observed information). A log density that’s quadratic in is a normal, so
Sharp peak (big curvature) means small posterior variance; flat peak means large variance. That’s the whole approximation. It’s the Bayesian cousin of the usual “MLE plus inverse-Hessian standard errors” story, with the prior included in .
Logistic regression
The model is , where is the feature vector of observation (with a leading 1 for the intercept). The log-likelihood is
With the prior the log posterior is this plus (plus a constant).
A bug worth remembering. In my draft I wrote the first term as , and coded it as linFun^y. The correct term is : it comes from . The power belongs to the exponential, not to the linear predictor. With the wrong version gives instead of for , which shifts things around quietly without crashing. Our final report had the right form, and its posterior mode matched glm closely, which is a great sanity check: with a vague prior (), the mode should be almost the MLE.
Code
set.seed(3)
n <- 200
X <- cbind(1, rnorm(n), rbinom(n, 2, 0.4))
y <- rbinom(n, 1, plogis(X %*% c(0.5, 1, -1.2)))
tau <- 10
log_post <- function(beta, X, y, tau) {
eta <- drop(X %*% beta)
sum(y * eta - log1p(exp(eta))) + # log-likelihood
sum(dnorm(beta, 0, tau, log = TRUE)) # N(0, tau^2 I) prior
}
fit <- optim(rep(0, ncol(X)), log_post, X = X, y = y, tau = tau,
method = "BFGS", control = list(fnscale = -1), hessian = TRUE)
beta_mode <- fit$par
beta_cov <- solve(-fit$hessian) # J(mode)^-1
cbind(mode = beta_mode, sd = sqrt(diag(beta_cov)),
glm = coef(glm(y ~ X - 1, family = binomial)))
fnscale = -1 makes optim maximise instead of minimise. hessian = TRUE returns the Hessian of the log posterior at the optimum, which is negative definite at a maximum, hence the minus sign.
Reading the result
From the approximate posterior you get a credible interval for each coefficient: . In the lab, the coefficient for the number of small children came out with a posterior mode of about and a 95% interval of roughly .
Was it “an important determinant”? Our report argued yes because its absolute value was the largest. That’s a weak argument: coefficient size depends on the units of each feature (a coefficient on income in thousands and one on a count of children aren’t comparable). The better argument is that the whole interval sits well away from zero, so the data are clear about the sign, and on the odds scale per extra small child is a big effect. Comparing magnitudes only makes sense after standardising the features.
When to trust it
The approximation is good when the posterior really is close to normal: lots of data relative to the number of parameters, and parameters away from boundaries. It gets worse with small samples, with separation in logistic regression (where the MLE runs off to infinity), and for parameters like variances that are bounded below (better to approximate than ). In the next lab we checked it against MCMC, see random-walk Metropolis.
Key takeaways
- Laplace approximation: posterior of the log posterior.
- One
optim(..., hessian = TRUE)call gives you both pieces. - Logistic log-likelihood: ; the multiplies, it isn’t a power.
- With a vague prior, the mode should match
glm; use that as a check. - Judge a coefficient by its interval and on a standardised scale, not by raw magnitude.
Resources
- Andrew Gelman et al., Bayesian Data Analysis, 3rd ed., Section 4.1: http://www.stat.columbia.edu/~gelman/book/
- Christopher M. Bishop, Pattern Recognition and Machine Learning, Section 4.4 (Laplace approximation) and 4.5 (Bayesian logistic regression): https://www.microsoft.com/en-us/research/people/cmbishop/prml-book/
- R documentation for
optim: https://stat.ethz.ch/R-manual/R-devel/library/stats/html/optim.html - 732A91 Bayesian Learning, Linköping University (course lab, spring 2020).