Bayesian Monte Carlo and MCMCLesson 75 of 76 · 11 min

Compare multiple chains

Compute rank-normalised and folded split R-hat and explain its diagnostic limits.

Run multiple chains from dispersed starting points. Split R-hat compares variation within each half-chain against disagreement between halves. Splitting makes an early/late drift easier to detect. Near-one values suggest agreement in the feature compared, not convergence or coverage of every target region.

Rank-normalisation pools all split draws, assigns ranks, then converts rank fractions to standard-normal quantiles. For sorted values [1,2,4,9], ranks r are [1,2,3,4]. With N=4 entries, the code uses u=(r−.375)/(N+.25)u=(r-.375)/(N+.25), giving approximately [.147,.382,.618,.853] before normal quantiles. Folding takes absolute distance from the pooled median: median 3 gives [2,1,1,6]. Rank those distances to expose scale disagreement. Ties receive average ranks; the code flattens entries to rank them, then reshapes ranks back into chain rows.

Report the larger rank and folded split R-hat. The code compares invented independent normal draws with a second collection whose fourth chain has twice the scale; these are diagnostic examples, not posterior traces. Also inspect traces, target knowledge and statistic-specific error. Bulk ESS (central behaviour), tail ESS and sampler-specific energy diagnostics are further-study topics, outside this calculation.

Compare location and scale across chains
  1. Split multiple chains

    compare within and between

  2. Rank-normalise and fold
    R^rank,R^folded\widehat R_{\rm rank},\quad\widehat R_{\rm folded}
  3. Inspect more than agreement

    traces, ESS, target exploration

Reference illustration. The constructed collections illustrate a diagnostic failure: raw split R-hat can miss scale disagreement. A near-one diagnostic alone is not proof of convergence or exploration.

Could chains have similar means yet disagree about the distribution’s scale?

Write a prediction (optional)

Constructed diagnostic draws

Select Run Python below with the code unchanged.

Tools
About Python ↗

The code is editable. Press Ctrl+Enter or ⌘+Enter to run Python from the editor.

Published example output

This result comes from the original Python. Your runs appear above. Build environment ↗

Agreement raw split R-hat: 0.9999 rank/folded maximum: 1.0002
Scale disagreement raw split R-hat: 0.9999 rank/folded maximum: 1.0638

Read the first result

The folded comparison exposes the deliberately changed scale. Neither collection demonstrates real MCMC exploration of a difficult posterior.

Try an edit

Replace good*np.array([1,1,1,2])[:,None] with good+np.array([0,0,0,1])[:,None] and "Scale disagreement",scale_mismatch with "Location disagreement",scale_mismatch. Select Run Python again. Compare raw split R-hat and rank/folded maximum for Location disagreement with the original scale disagreement.

Back to the Python editor

Scope. This implements the rank/folded R-hat calculation, not the full modern MCMC diagnostic suite. Close-to-one R-hat is necessary evidence of agreement, not sufficient evidence of correct inference.

Try a fresh case

Four chains remain trapped in the same local mode and agree closely. What can R-hat establish?

Choose an answer
Need a hint?
Hint 1

Consider a second mode absent from every sample.

Hint 2

A comparison only has access to observed chains.

Your notes