The target is p(θ) ∝ N(θ | 0, 1) exp(−c (θ² − 1)²), two bumps near θ = ±1 separated by a barrier of height c. The sampler starts from the Gaussian prior (λ = 0) and raises the inverse temperature λ to 1. Each increment is chosen so that the effective sample size of the new weights equals a fixed fraction of N; the particles are then resampled and moved by random-walk Metropolis steps. Use the stage slider to watch the particles follow the tempered curve; the end result is compared with the exact density, and the estimate of the normalising constant with the exact one. A single random-walk Metropolis chain started at θ = 1 (pink) is shown for contrast: once the barrier is high (try c = 10 or more) a chain with a small step never leaves the right-hand mode, so its fraction of time at θ > 0 is 1 instead of the true 1/2.
Algorithm 13.5 of the notes: δt solves ESS(exp(−δ E)) = fraction × N by bisection (or reaches λ = 1), the weights are exp(−δt E(θn)) with E = c(θ² − 1)², the evidence estimate is the product of the mean weights, particles are resampled systematically and moved by K sweeps of random-walk Metropolis targeting the current tempered density with proposal standard deviation (step) × (standard deviation of the particles). The exact normalising constant is computed by numerical integration. The baseline chain has 5000 steps from θ = 1.
code/ch13_sequential_monte_carlo.py (dk_tempered, dk_rwmh_chain, bimodal_Z).