Search

Search papers and lessons

Gelman-Rubin Diagnostic

Level 3 · 12 min read

The Gelman-Rubin diagnostic runs several chains from scattered starts and compares the spread between the chains with the spread within them; a value near 1 means the chains agree.

On this page

  • Colour = topic mix
  • Probability
  • Discrete math
  • Calculus
  • Linear algebra
  • Sampling
  • Stochastic processes
  • ReadReady to readNot yet

TL;DR

The Gelman-Rubin diagnostic checks whether a Markov chain Monte Carlo run has settled down. You run several chains from widely scattered starting points and compare two spreads: how much the chains' averages differ from each other, and how much each chain varies within itself. If the chains have all found the same region, those two spreads agree and the ratio, called R^\hat{R}, is close to 1. A large R^\hat{R} means the chains disagree, so the run has not converged.

Builds on: Markov Chain Monte Carlo, Mean and Variance · Leads to: effective sample size, which asks how many independent draws the converged chains are worth

Where it's used

Bayesian regression in applied science. A researcher fits a model of, say, drug response with a dozen parameters, using four chains of 2,000 draws each. Before reporting any posterior mean, the researcher checks that R^\hat{R} is close to 1 for every parameter. A parameter with R^\hat{R} well above 1 means the answer is not yet reliable, whatever its mean looks like.

Default checks in probabilistic programming. Tools such as Stan, PyMC and similar packages print R^\hat{R} next to each parameter after a sampling run. Many users read it as the first sign of trouble, before they look at trace plots.

Hierarchical models. Models with group-level parameters are known to mix slowly, because the group effects and their spread pull against each other. Chains often disagree on the spread, and R^\hat{R} on that parameter is the early warning that the run needs more draws or a better parameterisation.

How it works

Take the smallest case: two chains, each with n=4n = 4 draws of one scalar quantity, started from different places. The first chain's draws are 0,2,0,20, 2, 0, 2, and the second's are 4,6,4,64, 6, 4, 6. The chains have clearly not mixed, because the second never visits the first's values. The diagnostic should say so.

  1. Run the chains and compute each chain's mean. Chain 1 has mean (0+2+0+2)/4=1(0 + 2 + 0 + 2)/4 = 1. Chain 2 has mean (4+6+4+6)/4=5(4 + 6 + 4 + 6)/4 = 5. Check: the two chain means differ by 4, which is already a large gap for values that vary by about 2.

  2. Compute the within-chain variance. Each chain's sample variance uses the deviations ±1\pm 1, so the squares sum to 44, and dividing by n−1=3n - 1 = 3 gives 4/34/3 for each chain. The average over the two chains is W=4/3W = 4/3. Check: WW is the typical spread inside one chain. It does not know that the chains sit in different places.

  3. Compute the between-chain variance. The grand mean of the chain means is (1+5)/2=3(1 + 5)/2 = 3. The between-chain term is B=nm−1∑j(ψˉj−ψˉ)2=41[(1−3)2+(5−3)2]=4×8=32B = \dfrac{n}{m-1}\sum_j (\bar\psi_j - \bar\psi)^2 = \dfrac{4}{1}\left[(1-3)^2 + (5-3)^2\right] = 4 \times 8 = 32. Check: BB is large because the chains disagree about the level.

  4. Combine them into an estimate of the total variance. Var^+=n−1nW+Bn=34⋅43+324=1+8=9\widehat{\mathrm{Var}}^{+} = \dfrac{n-1}{n}W + \dfrac{B}{n} = \tfrac34 \cdot \tfrac43 + \tfrac{32}{4} = 1 + 8 = 9. Check: the estimate 9 is far above the within-chain value 4/34/3.

  5. Take the ratio. R^=Var^+/W=9/(4/3)=6.75≈2.60\hat{R} = \sqrt{\widehat{\mathrm{Var}}^{+}/W} = \sqrt{9/(4/3)} = \sqrt{6.75} \approx 2.60. Check: a value of 2.6 is far above 1, so the diagnostic correctly flags that the chains have not converged to the same answer.

The diagram below shows the sequence.

flowchart TD
  A["Run m chains from scattered starts, n draws each"] --> B["Within-chain variance W, the average of the chain variances"]
  A --> C["Between-chain term B/n, the variance of the chain means"]
  B --> D["Combine: Var plus = (n-1)/n times W, plus B/n"]
  C --> D
  D --> E["R-hat = square root of (Var plus / W)"]
  E --> F{"Close to 1?"}
  F -- yes --> G["Chains agree: check other parameters"]
  F -- no --> H["Chains disagree: run longer or reparameterise"]

The math

Setting up

Let ψ\psi be a scalar quantity of interest, such as one model parameter. Run m≥2m \ge 2 chains, each with nn draws after burn-in, the early draws that still remember the starting point being discarded. Let ψˉj\bar\psi_j be the mean of chain jj, and sj2s_j^2 its sample variance, which divides by n−1n - 1. Write ψˉ\bar\psi for the mean of the chain means.

Definition: Gelman-Rubin statistic
B=nm−1∑j=1m(ψˉj−ψˉ)2,W=1m∑j=1msj2,B = \frac{n}{m-1}\sum_{j=1}^{m}\bigl(\bar\psi_j - \bar\psi\bigr)^2, \qquad W = \frac{1}{m}\sum_{j=1}^{m} s_j^2,Var^+=n−1n W+1nB,R^=Var^+W.\widehat{\mathrm{Var}}^{+} = \frac{n-1}{n}\,W + \frac{1}{n}B, \qquad \hat{R} = \sqrt{\frac{\widehat{\mathrm{Var}}^{+}}{W}}.

Here BB is nn times the sample variance of the chain means, so B/nB/n is that sample variance. The factor nn appears because the variance of a mean of nn draws is 1/n1/n of the variance of one draw.

Step 1 in symbols: the within-chain estimate

Each chain's variance sj2s_j^2 estimates the variance of the target distribution, but only over the part of the space that the chain has explored. The average WW is therefore an estimate that is too small if the chains have not spread out. In the tiny example, W=4/3W = 4/3 looks only at the values inside each chain, 0 and 2 in one and 4 and 6 in the other, and misses the gap between the two groups.

Step 2 in symbols: the total-variance estimate

The quantity Var^+\widehat{\mathrm{Var}}^{+} combines the two spreads. If every chain has explored the whole target, the chain means differ only by sampling noise, so B/nB/n is small and Var^+\widehat{\mathrm{Var}}^{+} is close to WW. When the chains start far apart and have not yet mixed, B/nB/n is large, and Var^+\widehat{\mathrm{Var}}^{+} is larger than WW. The starting points are scattered on purpose, so the estimate deliberately overshoots while the chains are still apart.

Check: as nn grows for converged chains, (n−1)/n→1(n-1)/n \to 1 and B/n→0B/n \to 0, so Var^+→W\widehat{\mathrm{Var}}^{+} \to W and R^→1\hat{R} \to 1.

Step 3 in symbols: the ratio

The ratio R^\hat{R} compares the total estimate with the within-chain estimate. Its square root makes it a ratio of standard deviations. A value of 1 means the two estimates agree. A rule of thumb from the early literature on this statistic is that values above 1.1 signal trouble. Many modern users apply a stricter threshold near 1.01, following later refinements of the statistic.

Step 4 in symbols: a worked check on realistic numbers

Take m=4m = 4 chains with n=2000n = 2000 draws each. Let the chain means be 1.02,0.98,1.05,0.951.02, 0.98, 1.05, 0.95, and let each within-chain variance be about 0.250.25, so W=0.25W = 0.25.

  1. The mean of the chain means is ψˉ=4.00/4=1.00\bar\psi = 4.00/4 = 1.00.
  2. The squared deviations of the chain means are 0.0004,0.0004,0.0025,0.00250.0004, 0.0004, 0.0025, 0.0025, which sum to 0.00580.0058.
  3. B/n=0.0058/(m−1)=0.0058/3=0.00193B/n = 0.0058/(m-1) = 0.0058/3 = 0.00193.
  4. Var^+=(1999/2000)(0.25)+0.00193=0.24988+0.00193=0.25181\widehat{\mathrm{Var}}^{+} = (1999/2000)(0.25) + 0.00193 = 0.24988 + 0.00193 = 0.25181.
  5. R^=0.25181/0.25=1.0072≈1.004\hat{R} = \sqrt{0.25181/0.25} = \sqrt{1.0072} \approx 1.004.

Check: the chains agree to within about 0.05, which is small compared with their spread of about 0.5, so R^\hat{R} is barely above 1.

Worked examples

Example
1. Tiny: two chains that disagree

Use the two chains of the walkthrough, n=4n = 4 and m=2m = 2. Chain 1 has draws 0,2,0,20, 2, 0, 2, and chain 2 has 4,6,4,64, 6, 4, 6.

  1. Chain means: ψˉ1=1\bar\psi_1 = 1, ψˉ2=5\bar\psi_2 = 5, grand mean ψˉ=3\bar\psi = 3.
  2. Sample variances: each chain has squared deviations summing to 44, so s12=s22=4/3s_1^2 = s_2^2 = 4/3, and W=4/3W = 4/3.
  3. Between term: B=41 [(1−3)2+(5−3)2]=4×8=32B = \dfrac{4}{1}\,[(1-3)^2 + (5-3)^2] = 4 \times 8 = 32.
  4. Total estimate: Var^+=34⋅43+324=1+8=9\widehat{\mathrm{Var}}^{+} = \tfrac34 \cdot \tfrac43 + \tfrac{32}{4} = 1 + 8 = 9.
  5. Ratio: R^=9/(4/3)=6.75=2.60\hat{R} = \sqrt{9 / (4/3)} = \sqrt{6.75} = 2.60.

Interpretation: the diagnostic reads 2.6. The chains are centred 4 units apart, and nothing in a single chain reveals that, which is why the comparison between chains is essential.

Example
2. Standard: a converged run with 1,000 draws

Take m=2m = 2 chains of n=1000n = 1000 draws each, with within-chain variance W=1.00W = 1.00. Suppose the between-chain term is B/n=0.021B/n = 0.021.

  1. Compute (n−1)/n⋅W=(999/1000)(1.00)=0.999(n-1)/n \cdot W = (999/1000)(1.00) = 0.999.
  2. Add the between term: Var^+=0.999+0.021=1.020\widehat{\mathrm{Var}}^{+} = 0.999 + 0.021 = 1.020.
  3. Take the ratio: R^=1.020/1.00=1.020≈1.010\hat{R} = \sqrt{1.020 / 1.00} = \sqrt{1.020} \approx 1.010.
  4. Compare with the thresholds: 1.010 passes the strict rule near 1.01 and is far below the original 1.1.

Interpretation: the two chains disagree by about 0.021≈0.14\sqrt{0.021} \approx 0.14 in the units of ψ\psi, which is small compared with a within-chain standard deviation of 1. The parameter is estimated consistently.

Try it: with the same WW and nn, what between term would give R^=1.10\hat{R} = 1.10? Answer: R^2=1.21\hat{R}^2 = 1.21, so Var^+=1.21\widehat{\mathrm{Var}}^{+} = 1.21, and B/n=1.21−0.999=0.211B/n = 1.21 - 0.999 = 0.211. That is ten times the value above, which is why R^\hat{R} rises quickly once the chains separate.

Example
3. Realistic: one chain stuck in another mode

A sampler with four chains of n=2000n = 2000 draws is run on a posterior with two modes, near 1 and near 2. Three chains find the first mode, but one chain stays in the second. The chain means are 1.02,0.98,1.00,2.001.02, 0.98, 1.00, 2.00, and the within-chain variances are each about 0.250.25.

  1. The mean of the chain means is 5.00/4=1.255.00/4 = 1.25.
  2. The squared deviations from 1.25 are 0.0529,0.0729,0.0625,0.56250.0529, 0.0729, 0.0625, 0.5625, which sum to 0.75080.7508.
  3. B/n=0.7508/3=0.2503B/n = 0.7508/3 = 0.2503.
  4. Var^+=(1999/2000)(0.25)+0.2503=0.2499+0.2503=0.5002\widehat{\mathrm{Var}}^{+} = (1999/2000)(0.25) + 0.2503 = 0.2499 + 0.2503 = 0.5002.
  5. R^=0.5002/0.25=2.001≈1.41\hat{R} = \sqrt{0.5002/0.25} = \sqrt{2.001} \approx 1.41.

Interpretation: the diagnostic reads 1.41, well above the thresholds. It correctly reports that the four chains do not describe one distribution. Averaging all the draws would give a mean that belongs to neither mode, so the run must be repaired before any mean is reported, by running longer, tempering or changing the parameterisation.

Common mistakes and tradeoffs

It is tempting to think that R^\hat{R} near 1 proves convergence. In fact it only shows that the chains agree. If every chain starts in the same corner, or if the sampler cannot leave a region, all the chains can agree on a wrong answer. Scattered starting points and trace plots are the defence against this.

It is tempting to think that one long chain is enough to compute R^\hat{R}. In fact the statistic compares chains, so it needs at least two. A single chain can be split into halves, and the halves compared. This split version catches a slow drift within one chain, which the plain statistic can miss.

It is tempting to think that a single threshold settles every case. In fact the older 1.1 cut-off is lenient, and the stricter 1.01 is common in modern practice. The right threshold also depends on how many effective samples the run has, because a noisy estimate of R^\hat{R} is itself unreliable when the chains are short.

It is tempting to check only the mean. In fact a parameter's tails can mix much more slowly than its centre. Check R^\hat{R} for the quantiles you report, not only the mean, and for every parameter, not just the ones you care about most.

When to use it: for any scalar summary of a run with several chains, as a routine check before reporting results.

When not to use it: when the target has very heavy tails, where the variance itself is unstable. Rank-normalised versions of the statistic are more reliable there. Also, the statistic says nothing about a region the chains never reach, so it cannot replace checks on the sampler's design.

CheckWhat it tells you
R^\hat{R} near 1 across chainsThe chains agree on the spread and the mean
Trace plotsWhether the chains wander and whether they have settled
Effective sample sizeHow many independent draws the chains are worth
Divergent transitions (for Hamiltonian Monte Carlo)Whether the sampler is struggling with curved regions

History

Andrew Gelman and Donald Rubin introduced the statistic in 1992, in a paper in Statistical Science on inference from iterative simulation with several sequences. Their aim was to give a practical test that users could run on the output of a simulation, rather than relying on theory that could not be checked. The idea was to compare variation between sequences with variation within them, which is the same logic as an analysis of variance.

Stephen Brooks and Gelman extended the method in 1998 to monitor convergence more generally, including for multivariate summaries. Later refinements, including splitting each chain in half and rank normalisation proposed by Vehtari and co-authors in 2021, make the statistic more robust to heavy tails and to slow drift within a chain. These refinements became standard in the probabilistic programming tools that use Hamiltonian Monte Carlo.

The statistic caught on because it is simple to compute from the output users already have. It turned a question that had no answer in the abstract into a number printed next to every parameter.

Recap

  • Run several chains from scattered starting points, and compute each chain's mean and within-chain variance.
  • The between-chain term B/nB/n is the variance of the chain means. The total estimate Var^+\widehat{\mathrm{Var}}^{+} combines it with the within-chain estimate WW.
  • R^=Var^+/W\hat{R} = \sqrt{\widehat{\mathrm{Var}}^{+}/W} is near 1 when the chains agree and well above 1 when they do not.
  • A value near 1 is necessary, not sufficient. Pair it with trace plots, effective sample size and checks on the tails.

Read next: Effective Sample Size, which converts a converged run into a count of independent draws, and Autocorrelation Function, which explains why correlated draws are worth fewer than their number.

Check yourself

QuestionRecall: what are the two spreads that the Gelman-Rubin diagnostic compares, and which one stays small when the chains are stuck in different places?Show answer
The two spreads are the within-chain variance WW, the average variance inside each chain, and the between-chain term B/nB/n, the variance of the chain means. When the chains are stuck in different places, B/nB/n is large while WW stays small, so the total estimate is larger than the within-chain estimate.
QuestionTwo chains with n=4n = 4 have within-chain variance W=1W = 1 and chain means 00 and 44 (so ψˉ=2\bar\psi = 2). Compute the between term BB.Show answer
Use B=nm−1∑j(ψˉj−ψˉ)2=41[(0−2)2+(4−2)2]=4×8=32B = \dfrac{n}{m-1}\sum_j(\bar\psi_j - \bar\psi)^2 = \dfrac{4}{1}\left[(0-2)^2 + (4-2)^2\right] = 4 \times 8 = 32.
QuestionWhy does R^\hat{R} approach 1 as nn grows, provided the chains have converged?Show answer
When the chains have converged, their means differ only by sampling noise, so B/nB/n shrinks towards 0 as nn grows. The factor (n−1)/n(n-1)/n tends to 1. So Var^+\widehat{\mathrm{Var}}^{+} approaches WW and the ratio approaches 1.
QuestionA run with m=4m = 4 chains and n=1000n = 1000 reports R^=1.30\hat{R} = 1.30 for one parameter, and the chain means are very different. What should you do?Show answer
Treat the run as not converged. The chains disagree on the level of that parameter, so none of their means can be reported. Run longer, check trace plots, consider a better starting spread or a different parameterisation, and recompute R^\hat{R} before using any estimate for that parameter.
QuestionWhy can R^\hat{R} near 1 still be misleading?Show answer
If all chains start in the same region or cannot leave it, they agree with one another while describing only part of the target. The statistic compares chains, so it cannot detect a region that none of them visits. Scattered starts, trace plots and domain knowledge are needed alongside it.

Sources