Lesson 1: Integral Approximations: Laplace Approximation

4–6 minutes

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:

D={y1,y2,…,yn}D=\{y_1,y_2,\dots,y_n\}

and want the posterior distribution

p(θ|D)p(\theta|D)

Using Bayes’ theorem:

p(θ|D)=p(D|θ)p(θ)∫p(D|θ)p(θ)dθp(\theta|D)=\frac{p(D|\theta)p(\theta)}{\int p(D|\theta)p(\theta)\,d\theta}

The denominator

∫p(D|θ)p(θ)dθ\int p(D|\theta)p(\theta)d\theta

is often impossible to compute analytically.

Laplace approximation replaces the posterior with a Gaussian distribution near its peak.


Core Idea

Suppose

p(θ|D)∝exp⁡(h(θ))p(\theta|D)\propto \exp(h(\theta))

where

h(θ)=log⁡p(D|θ)+log⁡p(θ)h(\theta)=\log p(D|\theta)+\log p(\theta)

Let

θ^\hat{\theta}

be the posterior mode.

Use a second-order Taylor expansion:

h(θ)≈h(θ^)+12h′′(θ^)(θ−θ^)2h(\theta) \approx h(\hat{\theta}) + \frac{1}{2} h”(\hat{\theta})(\theta-\hat{\theta})^2

because

h′(θ^)=0h'(\hat{\theta})=0

at the maximum.

Because (θ^)(\hat{\theta}) is the value where (h(θ))(h(\theta)) is maximum.

The Taylor expansion around (θ^)(\hat{\theta}) is:

[h(θ)≈h(θ^)+h′(θ^)(θ−θ^)+12h′′(θ^)(θ−θ^)2][ h(\theta) \approx h(\hat{\theta}) + h'(\hat{\theta})(\theta-\hat{\theta}) + \frac12 h”(\hat{\theta})(\theta-\hat{\theta})^2 ]

But at the maximum:

[h′(θ^)=0][ h'(\hat{\theta})=0 ]

So the middle term disappears:

[h(θ)≈h(θ^)+12h′′(θ^)(θ−θ^)2][ h(\theta) \approx h(\hat{\theta}) + \frac12 h”(\hat{\theta})(\theta-\hat{\theta})^2 ]

Since it is a maximum:

[h′′(θ^)<0][ h”(\hat{\theta})<0 ]

Now exponentiate:

[p(θ|D)∝eh(θ)][ p(\theta|D) \propto e^{h(\theta)} ]

So:

[p(θ|D)≈eh(θ^)e12h′′(θ^)(θ−θ^)2][ p(\theta|D) \approx e^{h(\hat{\theta})} e^{\frac12 h”(\hat{\theta})(\theta-\hat{\theta})^2} ]

This has the same shape as a Normal distribution:

[N(μ,σ2)∝e−12σ2(θ−μ)2][ N(\mu,\sigma^2) \propto e^{-\frac{1}{2\sigma^2}(\theta-\mu)^2} ]

Match the terms:

[μ=θ^][ \mu=\hat{\theta} ]
[−1σ2=h′′(θ^)][ -\frac{1}{\sigma^2}=h”(\hat{\theta}) ]

Therefore:

[σ2=−1h′′(θ^)][ \sigma^2=-\frac{1}{h”(\hat{\theta})} ]

Then

p(θ|D)≈N(θ^,−1h′′(θ^))p(\theta|D) \approx N\left(\hat{\theta}, -\frac{1}{h”(\hat{\theta})}\right)

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,

e−x2e^{-x^2}

we obtain a normal approximation.


Healthcare Example

Suppose:

  • yi=1y_i=1  if patient survives surgery.
  • yi=0y_i=0 otherwise.

Logistic model:

P(yi=1)=11+exp⁡(−β)P(y_i=1)=\frac{1}{1+\exp(-\beta)}

Prior:

β∼N(0,10)\beta \sim N(0,10)

The posterior has no closed form.

Laplace approximation provides:

β|D≈N(β^,V)\beta|D \approx N(\hat{\beta},V)

allowing confidence intervals and prediction.


Pharmaceutical Example

A drug company studies treatment response.

Parameter:

θ=treatment effect\theta = \text{treatment effect}

Posterior:

p(θ|D)p(\theta|D)

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:p(β∣D)∝p(D∣β)p(β)

Take logs:h(β)=log⁡p(D∣β)+log⁡p(β)

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:β^=arg⁡max⁡h(β)

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:h(β)≈h(β^)+h′(β^)(β−β^)+12h′′(β^)(β−β^)2

At the maximum:h′(β^)=0

so:h(β)≈h(β^)+12h′′(β^)(β−β^)2

This is the key Laplace step.


Step 4: Why We Need the Hessian

Notice that the approximation depends on:h′′(β^)

the curvature at the peak.

If:h′′(β^)=−100

the peak is narrow.

If:h′′(β^)=−1

the peak is wide.

Therefore the curvature determines the variance.


Step 5: Your Code Computes That Curvature

The second derivative formula is:h′′(x)≈h(x+ε)−2h(x)+h(x−ε)ε2

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:h′′(β^)

numerically.


Step 6: Convert Curvature to Variance

Laplace says:p(β∣D)≈N(β^,−1h′′(β^))

The variance is:V=−1h′′(β^)

which is exactly:

variance = -1/hessian

Geometric 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:β=(β1,…,βp)

the second derivative becomes a matrix:H=∂2h∂β∂βT

called the Hessian matrix.

Then Laplace becomes:p(β∣D)≈N(β^,−H−1)

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

Discover more from nerd-ish

Subscribe now to keep reading and get access to the full archive.

Continue reading