Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 36 Β· MCMC and Sampling

MCMC Diagnostics

Check whether an MCMC run can be trusted: multiple chains, trace behavior, R-hat, autocorrelation, effective sample size, Monte Carlo error and stuck chains, with Python code.

An MCMC sampler always returns numbers, even when it has failed. A chain that is stuck in one mode, has not converged, or moves so slowly that it holds little information looks perfectly fine in a table of means. Diagnostics are the checks that tell you whether the draws deserve trust. Never skip them.

What can go wrong

  • Not converged: the chain has not yet reached the region where the posterior has mass.
  • Poor mixing: successive draws are highly correlated, so you have far fewer effective draws than you think.
  • Stuck chains: the sampler is trapped in one mode of a multimodal posterior.
  • Wrong model or bug: diagnostics cannot fix these but can reveal symptoms.

The standard defense is to run several chains from different starting points and check that they agree with each other.

A reusable sampler

We reuse the Metropolis sampler from the previous lesson and apply it to a target with an easy-to-verify answer: a standard normal.

Python Four chains, visual check in numbers

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.

All four chains start far apart, yet after warm-up they agree on a mean near 0 and sd near 1. Agreement among overdispersed starts is the heart of convergence checking.

R-hat: do the chains agree?

R-hat (the potential scale reduction factor, Gelman-Rubin) compares the variance between chains with the variance within them. If the chains explore the same distribution, the two are similar and R-hat is close to 1. Rule of thumb: values above about 1.01 (some older sources say 1.1) deserve investigation. Modern practice uses split R-hat: split every chain in half, which also detects a chain that drifts.

Python Split R-hat from scratch

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 properly run chains give R-hat very close to 1. The short, slow chains that have not mixed give a value far above the threshold, flagging the problem.

Autocorrelation and effective sample size

Draws in a chain are correlated, so N draws are worth fewer independent ones. The effective sample size (ESS) estimates that number: roughly ESS β‰ˆ N / (1 + 2 Ξ£ ρₖ) where ρₖ is the autocorrelation at lag k.

Python ESS from autocorrelation

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 best step size (near 2.4 for a standard normal) gives the largest ESS. A tiny step leaves an ESS of only a few dozen out of 10,000 draws. When the ESS is small, the draws still estimate the posterior but with a large Monte Carlo error: aim for hundreds of effective draws per parameter for means and more (1,000+) for tail quantiles and intervals.

Monte Carlo standard error

The error caused by sampling is MCSE = posterior sd / √ESS. It should be much smaller than the posterior sd and than the precision you report.

Python Monte Carlo standard error

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 estimate lies within a couple of MCSE of the true value 0. If the MCSE were comparable to the quantity you care about, you would run longer.

A multimodal trap

Chains can look healthy and still be wrong when the posterior has separated modes: each chain stays in the mode where it started. Within one chain the mean and spread look stable. Between chains they disagree, and R-hat catches it.

Python Chains stuck in different modes

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 chain means are about -6 and 6, nothing like the true mean of 0, and R-hat is huge. A single chain would have fooled us completely; this is why we always run several chains from dispersed starts.

A practical checklist

  1. Run at least four chains from dispersed starting values.
  2. Discard warm-up, then check R-hat below about 1.01 for every parameter.
  3. Check ESS: hundreds at the minimum, thousands for intervals.
  4. Look at trace plots: they should look like fuzzy "hairy caterpillars" with all chains overlapping.
  5. Compare the posterior with the prior and with a simple alternative (grid, conjugate) when possible.
  6. With Hamiltonian samplers (Stan, PyMC), also check divergences and energy diagnostics; they signal posterior geometry problems that need reparameterizing.

Check your understanding

Quick check Β· 4 questions

of 4 answered You scored / 4 Β· perfect!

  1. 1. Why run several chains from dispersed starting points?

    If chains started in different places agree, it is strong evidence of convergence; disagreement exposes problems.
  2. 2. R-hat compares which two quantities?

    If chains sample the same distribution, between-chain and within-chain variability match and R-hat is near 1.
  3. 3. What does a low effective sample size mean?

    ESS counts how many independent draws the correlated chain is worth.
  4. 4. A single long chain stays in one mode of a two-mode posterior. What reveals this?

    Chains started in different modes report different means, so between-chain variance is large.

Summary

  • Samplers return numbers even when they fail; always diagnose.
  • Run four or more dispersed chains, discard warm-up, and check split R-hat close to 1.
  • ESS measures information after accounting for autocorrelation; MCSE is posterior sd divided by √ESS.
  • Trace plots should show overlapping fuzzy bands; separated chains mean multiple modes or non-convergence.
  • Hamiltonian samplers add divergence checks; fix geometry problems by reparameterizing.

Next: back to the Bayesian Statistics track page