← SciSim / Statistics
MCMC — Metropolis-Hastings
Tier: Graduate
§1 Interactive Simulation — Metropolis-Hastings Sampling
Target dist
Normal
Accept rate
Current x
Samples n
0
Sample mean
Proposal sigma
Target distribution
Proposal sigma 1.0
Burn-in 200
Speed 30/s
§2 The Idea, Step by Step

Picture wandering a dark, hilly park at night with a flashlight that only lights the ground right where you stand. You want to spend most of your time on the brightest hills, but you can't see the whole park at once. Here's a rule that works anyway: take a small random step. If the new spot is brighter, move there. If it's dimmer, sometimes move anyway, sometimes stay put. Wander like this for a long time and — without ever seeing a map — you'll end up spending the most time exactly where it's brightest. That is the whole idea behind MCMC.

Now name the pieces. The "brightness" is a probability distribution $\pi(x)$ that we want samples from but can't draw from directly. From your current point $x$ you propose a nearby point $x'$ by taking a random step of typical size $\sigma$. Then you compare the two heights with a ratio $r=\pi(x')/\pi(x)$. If $r\ge 1$ the new point is at least as likely, so you always move. If $r<1$ you move only with probability $r$. For example, if $\pi(x)=0.20$ and $\pi(x')=0.10$, then $r=0.5$: you accept that downhill step about half the time. Those occasional downhill moves are what let the walker escape one peak and explore the whole landscape instead of getting stuck.

Formally, the Metropolis–Hastings acceptance probability is

$$\alpha(x,x')=\min\!\left(1,\frac{\pi(x')\,q(x\mid x')}{\pi(x)\,q(x'\mid x)}\right).$$

With a symmetric proposal — like the Gaussian random step this sim uses, $q(x'\mid x)=q(x\mid x')$ — the proposal terms cancel and it reduces to $\alpha=\min(1,\pi(x')/\pi(x))$. The deep payoff: $\pi$ only has to be known up to a constant, because any normalizing constant cancels in the ratio. That is exactly why MCMC can sample Bayesian posteriors whose normalizer is impossible to compute.

In the controls above, the Proposal sigma slider sets the step size $\sigma$; Burn-in throws away the early samples taken before the walker reaches the bright region; and Speed sets how many steps run per second.

TRY THIS IN THE SIM ABOVE

1. Drag Proposal sigma down to ~0.05 and press Play: the acceptance rate climbs toward 100%, but the Trace Plot barely moves — tiny steps mean slow mixing.
2. Now push Proposal sigma up to 5: acceptance crashes toward 0 and the chain gets stuck for long stretches.
3. Switch the target to Bimodal with a medium sigma (~1) and watch the Sample Histogram slowly fill both humps as the chain occasionally leaps between them.

§3 Mathematical Derivation

Metropolis-Hastings Algorithm — Metropolis et al. 1953 / Hastings 1970

At state $x$: propose $x'\sim q(x'\mid x)$. Accept with probability:

$$\boxed{\alpha(x,x')=\min\!\left(1,\frac{\pi(x')\,q(x\mid x')}{\pi(x)\,q(x'\mid x)}\right)}$$

Symmetric proposal $q(x'|x)=q(x|x')$: $\alpha=\min(1,\pi(x')/\pi(x))$

WHY IT WORKS

The chain satisfies detailed balance: $\pi(x)\,p(x\to x')=\pi(x')\,p(x'\to x)$, which implies $\pi$ is the stationary distribution. By the ergodic theorem, time averages converge to $E_\pi[f]$. Key: only need $\pi$ up to a normalizing constant — the ratio $\pi(x')/\pi(x)$ cancels it!

OPTIMAL ACCEPTANCE RATE

For RWM (Random Walk Metropolis) in d dimensions: optimal acceptance rate is ~44% for d=1, ~23.4% for d→∞ (Roberts, Gelman, Gilks 1997). Too high rate → small proposals → slow exploration. Too low → mostly rejected → slow exploration. Aim for 20-40%.

Robert & Casella — Monte Carlo Statistical Methods, Springer
Gelman et al. — Bayesian Data Analysis, CRC Press, 3rd ed. 2013
§4 FAQ
§5 Misconceptions & Common Errors
❌ Misconception 1

"MCMC samples are independent."
MCMC samples are DEPENDENT — consecutive samples are correlated. The effective sample size (ESS) = n/(1+2*sum(autocorrelations)) is much smaller than n. For accurate inference, you need ESS, not n. The Trace Plot tab shows this correlation: consecutive x values are not independent jumps.
📖 Gelman et al. — BDA Ch. 11

❌ Error 1

Not discarding burn-in samples
✅ The chain takes time to move from its starting point to the typical set of pi. Samples during this burn-in period are not representative of pi and should be discarded. Set burn-in = 10-50% of total iterations, or until the trace plot stabilizes.
🔍 Including the first 100 samples where x starts at 0 but target has mean=2.

❌ Error 2

Proposal too wide or too narrow
✅ Too wide: acceptance rate near 0 — chain stays stuck. Too narrow: acceptance rate near 100% — chain moves in tiny steps, slow mixing. Optimal: 20-40% acceptance rate. Use the Proposal Tuning tab to find the right sigma.
🔍 Using proposal sigma=0.001 for a target with sigma=1 — chain barely moves.

Robert & Casella — Monte Carlo Statistical Methods
Gelman et al. — Bayesian Data Analysis, Ch. 11