Normalizing Constants and Annealed Importance Sampling
Model comparison needs the constant Bayes rule divides by, and annealing from an easy distribution to the hard one estimates it without bias.
Why Does This Exist?
Bayes rule divides by the marginal likelihood , and energy models divide by a partition function of the same shape. These constants are the price of model comparison: the ratio of two values is the Bayes factor. Yet plain Monte Carlo cannot touch them, because the integrand concentrates where the prior rarely lands.
Annealed importance sampling (Neal, 2001) builds a bridge. Start from an easy distribution with known , heat it through intermediates toward the target, move samples along with MCMC at each temperature, and multiply the stepwise ratios. The product estimates unbiasedly. This page covers the constant and the annealing estimator; one-step importance sampling is the single-bridge special case, and posterior sampling itself is under Bayesian inference.
The one idea you need first
Importance sampling estimates from draws by weighting each draw with . When misses where concentrates, a few draws carry all the weight and the estimate collapses. Annealing fixes the mismatch gradually instead of in one jump.
Think of It Like This
Crossing a river on chained boats
You must cross a wide river but cannot jump it. Nobody can throw a rope across either. Instead a chain of boats anchors between the banks, each close enough to toss a rope to the next. You cross boat by boat, and the product of each small crossing's odds gives the odds of the whole trip.
The near bank is the easy base distribution, the far bank the target. Each boat is an intermediate temperature; adjacent boats overlap enough that one-step importance weights stay tame. A single leap (plain importance sampling) drowns when the banks are far apart.
How It Actually Works
Define intermediates with . Here is the easy base (known ) and the target. One AIS run: draw from , then for each apply a transition leaving invariant, and accumulate
with unnormalized densities . Average over runs times estimates unbiasedly. In log space the product becomes a sum, which is how it is implemented.
Worked example
Estimate for against base with . True (since ). Use temperatures . A typical run through gives stepwise ratios about , product . Average over runs lands near : the bridge recovers the constant one leap would have missed, since direct importance weights here have variance several times larger.
Schedule and diagnostics
Geometric or sigmoid schedules with in the hundreds to thousands are standard for real models. Diagnose with the effective sample size of the values: below a few percent of runs, add temperatures near where the stepwise variance spikes.
Code
def ais_estimate(n_runs: int = 200) -> float: import random import math betas = [0.0, 0.25, 0.5, 0.75, 1.0] total = 0.0 for _ in range(n_runs): x = random.gauss(0, 1) # draw from the base w = 1.0 for b_prev, b in zip(betas, betas[1:]): # Unnormalized intermediate densities at the current x. def tilde(bb: float) -> float: return math.exp(-0.5 * x * x) * (1 + bb * x * x / 10) w *= tilde(b) / tilde(b_prev) # stepwise ratio x = random.gauss(x * 0.9, 0.4) # short MCMC move toward p_b total += w return round(total / n_runs, 3)
print(ais_estimate()) # -> about 1.1 (typical draws give 1.07 to 1.13)The mean over runs targets ; the comment records the typical band rather than a fixed digit, since sampling varies run to run.
Watch Out For
Averaging weights in raw space
AIS weights span orders of magnitude, so a naive mean overflows or underflows past a few dozen temperatures. Always accumulate and combine runs with log-sum-exp. If your estimate changes by factors of two when you reorder runs, you averaged in the wrong space.
Too few temperatures with a distant target
With tiny and a far target, one stepwise ratio carries everything and AIS degrades to plain importance sampling with all its variance. Symptoms: effective sample size under . Double (halving gaps) before adding runs; temperatures are cheaper than wasted samples.
The Quick Version
- The normalizing constant prices model comparison but resists plain Monte Carlo.
- AIS bridges an easy base to the target through heated intermediates with MCMC moves.
- The product of stepwise unnormalized ratios estimates unbiasedly.
- The worked bridge recovers within about over runs.
- Implement in log space with log-sum-exp; raw-space averaging overflows.
- Diagnose with effective sample size and add temperatures where stepwise variance spikes.