Skip to content

Computing the evidence: annealed importance and nested sampling

Bayes factors need the normalizing constant that every sampler avoids. Learn two ways to compute it, bridging through temperatures and shrinking prior mass, and why evidence is so sensitive to the prior.

Advanced ~14 min9 steps

What runs in CodeSplash

You will be able to

  • Say why posterior samplers don't give the evidence, and why it matters for Bayes factors
  • Describe annealed importance sampling's bridge and weight
  • Describe nested sampling's live points and prior-mass integral
  • Explain why an evidence estimate can look converged yet miss a mode, and why it depends on the prior's width

Read first

01

The constant everyone cancels

Read

Metropolis–Hastings, HMC and self-normalized importance sampling all work because the normalizing constant cancels. But that constant is the evidenceBayesian evidence (marginal likelihood)The probability of the data averaged over the prior, p(y) = ∫ p(y | θ) p(θ) dθ; ratios of it between models are Bayes factors.,

Z=p(y)=∫p(y∣θ) p(θ) dθ,Z=p(y)=\int p(y\mid\theta)\,p(\theta)\,d\theta,

and a Bayes factorBayes factorHow much more likely the data are under one hypothesis than another; it can support H₀ as well as H₁. is a ratio of two of them. Posterior draws alone don't give it: the "harmonic mean" trick that seems to can have infinite variance. Computing ZZ needs methods built for it. This lesson covers two of them.

02

Annealed importance sampling

Read

Importance sampling from the prior straight to the posterior fails when the posterior is much narrower. Annealed importance samplingAnnealed importance samplingBridges an easy distribution to the target through tempered steps, multiplying weight increments, to estimate a normalizing constant. (AIS) walks there through tempered bridges

fk=f0 1−βk fK βk,0=β0<β1<⋯<βK=1.f_k=f_0^{\,1-\beta_k}\,f_K^{\,\beta_k},\qquad 0=\beta_0<\beta_1<\cdots<\beta_K=1 .

Start each run with a draw from f0f_0 (often the prior, whose constant is known). At each temperature, multiply the weight by fk(x)/fk−1(x)f_k(x)/f_{k-1}(x) at the current state, then move the state with an MCMC step that keeps fkf_k. Then E[w]=ZK/Z0E[w]=Z_K/Z_0.

Reference · INF017 · Explained hereAnnealed importance sampling (AIS)Bridges an easy reference distribution and a difficult target through intermediate distributions, using invariant Markov transitions and importance weights.
03

AIS details that bite

Read
  • Average weights, not logs. E[w]E[w] is the ratio; the mean of log⁡w\log w is a biased, lower estimate. Use log-sum-exp to average stably.
  • A few runs dominate. Log-weights with a wide spread mean a handful of runs carry the estimate; add temperatures where the distributions change fast.
  • Missing modes are invisible. If no run reaches a mode, its mass is never counted, and repeated runs agree on the wrong value.

The reference's program only accumulates the increments for three supplied states (total −0.82); because those states weren't produced by MCMC moves, it illustrates the bookkeeping, not an evidence estimate.

04

Nested sampling

Read

Nested samplingNested samplingEstimates the evidence by repeatedly replacing the lowest-likelihood live point with a prior draw above it, shrinking the prior mass. rewrites the evidence as a one-dimensional integral over prior mass X(λ)X(\lambda), the prior probability of the region where the likelihood exceeds λ\lambda:

Z=∫01L(X) dX≈∑iLi (Xi−1−Xi),XiXi−1∼Beta(Nlive,1).Z=\int_0^1 L(X)\,dX\approx\sum_i L_i\,(X_{i-1}-X_i),\qquad \frac{X_i}{X_{i-1}}\sim\text{Beta}(N_{\text{live}},1).

Keep NliveN_{\text{live}} points drawn from the prior. Repeatedly remove the one with the lowest likelihood, record it, and replace it with a new prior draw whose likelihood is higher. The remaining prior mass shrinks by about e−1/Nlivee^{-1/N_{\text{live}}} each time. Weighted discarded points double as posterior samples.

Reference · INF019 · Explained hereNested samplingRewrites Bayesian evidence as a one-dimensional integral over remaining prior mass, estimated through increasingly restrictive likelihood contours. Posterior weighted samples arise as a by-product;
05

Where nested sampling breaks

Read

The hard part is drawing from the prior inside a likelihood contour; methods use bounding ellipsoids or short MCMC walks, and correlated replacements make the shrinkage formula optimistic. A mode the live points never find is silently left out, as in AIS.

The reference's program checks only the quadrature, on a curve with a known answer: the bounds it gets, 0.5000 to 0.5000, bracket the exact evidence 0.5. Real runs also report the evidence's uncertainty from random shrinkage; report it separately from sensitivity to the prior.

06

The prior decides the evidence

Read

Make a coefficient's prior ten times wider and the posterior hardly moves, but the evidence falls: the prior now spends most of its mass where the likelihood is negligible. That is why Bayes factors with "vague" priors can favor the simpler model almost regardless of the data. Evidence is a statement about the prior and the likelihood, and it is not a p-value or a posterior probability of a model by itself.

EExplorable · The Savage–Dickey Bayes factor

Can you read a Bayes factor off a picture of the prior and the posterior?

Do these data favor the null (θ = 0.5) over the alternative?

How sure?
1.0

Small values spread the prior out; large values pile it at 0.5.

62
100
θ = 0.5

The two dots are the densities at the null value. Posterior over prior: 0.45 / 1.00 = BF₀₁ = 0.452.

Commit your prediction above to see the result

Make your prediction first; the figure appears when you commit.

Make your prediction to see the result.

07

Where they stand in CodeSplash

Read
08

Check your understanding

Checkpoint

Check yourself

Your AIS runs give log-weights. How do you estimate the evidence ratio?

Show the answer

Answer: b

E[w]E[w] is unbiased for the ratio; the mean of log⁡w\log w is biased low. Unbiased for ZZ also doesn't mean unbiased for log⁡Z\log Z.

Check yourself

Twenty AIS runs all agree closely on the evidence of a model with two well-separated modes. Should you trust it?

Show the answer

Answer: b

Agreement shows low variance, not correctness. Missing regions are invisible to every Monte Carlo method that never visits them.

Check yourself

In nested sampling, what replaces the live point with the lowest likelihood?

Show the answer

Answer: b

The constrained prior draw keeps the live points spread over the shrinking region, so the prior mass shrinks by a known factor on average. Drawing it accurately is the hard part.

Check yourself

You widen a coefficient's prior from SD 10 to SD 1,000. The posterior barely changes. What happens to the evidence?

Show the answer

Answer: b

Evidence averages the likelihood over the prior. Wider priors dilute that average, which is why Bayes factors need deliberate priors.

09

Recap

Recap
  • Samplers cancel the evidence; Bayes factors need it.
  • AIS bridges prior to posterior through temperatures; average weights, not logs.
  • Nested sampling integrates likelihood over shrinking prior mass with live points.
  • Both miss unvisited modes silently, and evidence depends strongly on the prior's width.
  • Explained here: CodeSplash has no general evidence calculator yet.

Concepts in this lesson