Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 38 Β· Markov Chain Monte Carlo

MCMC Diagnostics: Is My Chain Any Good?

Check an MCMC run with trace plots, autocorrelation, effective sample size and the Gelman-Rubin R-hat. Implement each diagnostic from scratch.

An MCMC sampler always returns numbers. It never raises an error to say "I did not converge". A chain stuck in one corner of the space looks perfectly fine unless you check. This lesson gives you the standard checklist: trace plots, burn-in, autocorrelation, effective sample size and the Gelman-Rubin statistic.

The golden rule: you can never prove a chain has converged, only fail to find evidence that it has not. So use several diagnostics, and run several chains from different starting points.

The trace plot and burn-in

A trace plot shows the sampled value against the iteration number. A healthy chain looks like a fuzzy horizontal band with no trend and no long flat stretches. The early part, still drifting from the starting point, is the burn-in. Discard it.

flowchart LR
  A["Start far from target"] --> B["Burn-in: drifting"]
  B --> C["Stationary: fuzzy band"]
  C --> D["Keep these samples"]

Only the stationary part of the chain is used for estimates.

Autocorrelation and effective sample size

Successive MCMC samples are dependent. The autocorrelation at lag k, ρ_k, is the correlation between Xβ‚œ and Xβ‚œβ‚Šβ‚–. If it decays slowly, the chain explores slowly.

The effective sample size (ESS) converts that into "how many independent samples is this worth?":

Text
ESS = N / (1 + 2 Β· Ξ£_{kβ‰₯1} ρ_k)

In practice the sum is truncated where the autocorrelation first becomes small or negative. A chain of 10,000 draws with ESS 500 carries about as much information as 500 independent draws, so its Monte Carlo standard error is Οƒ/√500, not Οƒ/√10000.

Python Trace, autocorrelation and ESS of a Metropolis chain

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The trace moves down from 20 into the band around zero during the burn-in, which is why we discard the first 500 draws. The ESS is much smaller than 9,500, because a step of 0.5 gives strongly dependent draws.

Gelman-Rubin R-hat: many chains agree

Run m chains of length n from dispersed starting points. If they have all converged, they should be indistinguishable. Compare the variance within each chain with the variance between chain means:

Text
W = average of the within-chain sample variances
B = n/(m βˆ’ 1) Β· Ξ£ (chain mean βˆ’ overall mean)Β²
V = ((n βˆ’ 1)/n) Β· W + B/n
RΜ‚ = √(V / W)

RΜ‚ close to 1 means agreement. A common rule of thumb is to worry when RΜ‚ > 1.01 (older advice used 1.1). Modern practice also splits each chain in half before computing it, so a single drifting chain is detected.

Python R-hat on a good sampler and a stuck one

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The target has two peaks at βˆ’3 and 3, separated by a valley of near-zero density. With a tiny step each chain stays near its starting peak, so their means disagree and RΜ‚ is far above 1. A trace plot of any single bad chain would have looked perfectly healthy; only comparing chains reveals the problem.

A practical checklist

  1. Run at least 4 chains from over-dispersed starts.
  2. Look at trace plots for trends and sticking.
  3. Discard burn-in; compute RΜ‚ (aim for below about 1.01).
  4. Check ESS: it should be in the hundreds at least for each quantity you report, more for tail probabilities.
  5. Report the Monte Carlo standard error Οƒ/√ESS, not Οƒ/√N.
  6. If diagnostics fail: change the step size or proposal, reparametrize, run longer, or switch method.

Thinning (keeping every 10th sample) rarely improves accuracy. It saves memory but throws away information. Use it only if storage is a real problem.

Check your understanding

Quick check Β· 4 questions

of 4 answered You scored / 4 Β· perfect!

  1. 1. What does a trace plot of a healthy chain look like?

    A stationary chain fluctuates around a fixed level without drifting or sticking.
  2. 2. A chain of 10,000 draws has ESS = 400. Its Monte Carlo standard error is about:

    ESS counts the equivalent number of independent draws, so the error is sigma / sqrt(ESS).
  3. 3. R-hat much larger than 1 means:

    R-hat = sqrt(V/W) grows when chain means differ more than their internal spread suggests.
  4. 4. Why run several chains from dispersed starting points?

    A single chain can look fine while stuck in one mode; disagreement between chains exposes this.

Summary

  • Always inspect trace plots, discard burn-in, and run several chains from dispersed starts.
  • Autocorrelation shows how dependent draws are; ESS = N / (1 + 2Σρ_k) is their worth in independent draws.
  • RΜ‚ compares between-chain and within-chain variance; values well above 1.01 signal non-convergence.
  • Report errors using ESS, and treat good diagnostics as absence of evidence of failure, not proof.

Next chapter: Random Walks and Their Simulation