Learning Objectives
By the end of this lesson, you will be able to:
- Understand why approximations are needed in Bayesian statistics.
- Derive the Laplace approximation.
- Approximate difficult posterior distributions.
- Use Laplace approximation in healthcare prediction problems.
- Implement Laplace approximation in Python.
Why Do We Need Approximations?
Suppose we observe patient data:
and want the posterior distribution
Using Bayes’ theorem:
The denominator
is often impossible to compute analytically.
Laplace approximation replaces the posterior with a Gaussian distribution near its peak.
Core Idea
Suppose
where
Let
be the posterior mode.
Use a second-order Taylor expansion:
because
at the maximum.
Because is the value where is maximum.
The Taylor expansion around is:
But at the maximum:
So the middle term disappears:
Since it is a maximum:
Now exponentiate:
So:
This has the same shape as a Normal distribution:
Match the terms:
Therefore:
Then
This is the Laplace approximation.
Visual Interpretation
The true posterior might be irregular.
Near its peak, we fit a parabola.
Since the exponential of a parabola is a Gaussian,
we obtain a normal approximation.
Healthcare Example
Suppose:
- if patient survives surgery.
- otherwise.
Logistic model:
Prior:
The posterior has no closed form.
Laplace approximation provides:
allowing confidence intervals and prediction.
Pharmaceutical Example
A drug company studies treatment response.
Parameter:
Posterior:
may be difficult to compute.
Laplace approximation quickly provides:
- Posterior mean
- Posterior variance
- Credible intervals
without expensive simulations.
Connection to GLMs
Laplace approximation is heavily used in:
- Logistic regression
- Poisson regression
- Mixed models
- Bayesian hierarchical models
Many software packages use Laplace internally.
Examples:
- INLA
- glmer
- TMB
Python Example
import numpy as np
from scipy.optimize import minimize
from scipy.stats import norm
y = np.array([1,1,1,0,1,0,1,1,0,1])
def log_posterior(beta):
p = 1/(1+np.exp(-beta))
loglik = np.sum(
y*np.log(p) +
(1-y)*np.log(1-p)
)
logprior = norm.logpdf(
beta,
loc=0,
scale=np.sqrt(10)
)
return loglik + logprior
result = minimize(
lambda b: -log_posterior(b),
x0=0
)
beta_hat = result.x[0]
eps = 1e-5
hessian = (
log_posterior(beta_hat+eps)
-
2*log_posterior(beta_hat)
+
log_posterior(beta_hat-eps)
)/(eps**2)
variance = -1/hessian
print(beta_hat)
print(variance)Explaining the code
Step 1: Laplace Approximation Idea
Suppose:
Take logs:
This is exactly your:
def log_posterior(beta):function.
Step 2: Find the Peak
Laplace says:
Find the value of that maximizes the log posterior.
That’s:
which you obtain using:
result = minimize( lambda b: -log_posterior(b), x0=0)because minimizing the negative is equivalent to maximizing.
Step 3: Taylor Expansion
Near the maximum:
At the maximum:
so:
This is the key Laplace step.
Step 4: Why We Need the Hessian
Notice that the approximation depends on:
the curvature at the peak.
If:
the peak is narrow.
If:
the peak is wide.
Therefore the curvature determines the variance.
Step 5: Your Code Computes That Curvature
The second derivative formula is:
This is exactly:
hessian = ( log_posterior(beta_hat+eps) - 2*log_posterior(beta_hat) + log_posterior(beta_hat-eps))/(eps**2)So the code is estimating:
numerically.
Step 6: Convert Curvature to Variance
Laplace says:
The variance is:
which is exactly:
variance = -1/hessianGeometric Interpretation
The second derivative measures how sharply it bends at the top.
- Sharp peak → large negative curvature → small variance.
- Flat peak → small negative curvature → large variance.
Laplace simply says:
“Replace the posterior by the normal distribution that has the same peak and curvature.”
The Hessian is the quantity that captures that curvature.
In Multiple Dimensions
Later, when you learn Bayesian logistic regression with many parameters:
the second derivative becomes a matrix:
called the Hessian matrix.
Then Laplace becomes:
Your one-dimensional code is the simplest possible version of this idea. The numerical second derivative is estimating the Hessian, and the inverse Hessian gives the posterior variance. That’s the central mathematical connection between the code and the Laplace approximation.
Advantages
- Extremely fast.
- Easy implementation.
- Useful for large datasets.
Limitations
- Assumes posterior is approximately Gaussian.
- Poor for multimodal distributions.
- Poor for highly skewed distributions.
References
- Bishop, Pattern Recognition and Machine Learning.
- Murphy, Machine Learning.
- Gelman et al., Bayesian Data Analysis.
- Rue et al., INLA Methodology.

Leave a Reply