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 , 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.
- Split multiple chains
compare within and between
- Rank-normalise and fold
- Inspect more than agreement
traces, ESS, target 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.
A saved draft is open. To run the published example, open Tools and select Restore example. Recover draft brings your code back.
Execution error
Technical error details
Your result
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.
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?
Need a hint?
Hint 1
Consider a second mode absent from every sample.
Hint 2
A comparison only has access to observed chains.