Signature
psi_bar = (psi_1 + psi_2 + psi_3 + psi_4) / 4; B = n / 3 * ((psi_1 - psi_bar)^2 + (psi_2 - psi_bar)^2 + (psi_3 - psi_bar)^2 + (psi_4 - psi_bar)^2); W = (s2_1 + s2_2 + s2_3 + s2_4) / 4; var_plus = (n - 1) / n * W + B / n; R_hat = sqrt(var_plus / W)
| Inputs | Definition | Unit |
|---|---|---|
psi_1 | Mean of the n retained draws of the scalar estimand in split chain 1 | units of the estimand |
psi_2 | Mean of the n retained draws of the scalar estimand in split chain 2 | units of the estimand |
psi_3 | Mean of the n retained draws of the scalar estimand in split chain 3 | units of the estimand |
psi_4 | Mean of the n retained draws of the scalar estimand in split chain 4 | units of the estimand |
n | Number of draws in each split chain after warm-up, the same for every split chain, at least 2 | draws |
s2_1 | Sample variance, with divisor n minus 1, of the n retained draws in split chain 1 | squared units of the estimand |
s2_2 | Sample variance, with divisor n minus 1, of the n retained draws in split chain 2 | squared units of the estimand |
s2_3 | Sample variance, with divisor n minus 1, of the n retained draws in split chain 3 | squared units of the estimand |
s2_4 | Sample variance, with divisor n minus 1, of the n retained draws in split chain 4 | squared units of the estimand |
psi_bar | Average of the four split-chain means | units of the estimand |
|---|---|---|
B | n times the sample variance of the split-chain means, which carries the factor n because each mean averages n draws | squared units of the estimand |
W | Mean of the four within-chain variances, above zero | squared units of the estimand |
var_plus | Weighted average of W and B with weights (n minus 1) / n and 1 / n, the estimate written with a plus sign in the article | squared units of the estimand |
R_hat | Square root of var_plus over W, near 1 when the split chains have mixed | ratio, no unit |
Function
Convergence checks on MCMC draws by split chains, rank normal scores and Monte Carlo error
Maps stored simulation output to the checks that show whether a Bayesian result has settled. For one scalar estimand it returns the split-chain potential scale reduction factor R_hat from split-chain means and variances (HE-FM-CON-001, with HE-CF-CON-001 for stored draws), the folded draw and rank fraction behind the rank-normalised version (HE-FM-CON-002), the conservative reported value with its use rule (HE-FM-CON-003) and the ratio of Monte Carlo error to posterior standard deviation (HE-FM-CON-004). The precision of independent probabilistic runs is live and is not restated. HE-FN-PSA-001 sets out the Monte Carlo standard error of a mean as the form of HE-FM-ANTV-002 with one observation per draw, HE-CF-PSA-001 computes it, the standard error of a share of runs has the form of SE_p in HE-FM-BINOM-004, an interval around the mean is HE-FM-AN-001, and the draws needed for a target standard error are HE-FM-ABM-003 and HE-FM-ABM-004. The article's sizing example applies these records, its 14,400 runs being HE-FM-ABM-003 with s_run 12,000 and e_target 100, and its 6,147 runs the same record with e_target 300 / 1.96, rounded up. The effective sample size has its own concept page and enters here as an input. Notation follows the Convergence article.
Computational function
Computational function: split-chain and rank-normalised R-hat from stored MCMC draws
Takes stored MCMC draws of one scalar estimand and returns split-chain R_hat by HE-FM-CON-001, then the rank-normalised and folded versions of HE-FM-CON-002 and the reported maximum of HE-FM-CON-003. Each chain is cut in half, the split-chain means and variances are worked out, and the same steps are repeated on normal scores of the draws and of the folded draws. The site calculator has no ranks, sorting or inverse normal, so the Math / Code line writes out the plain split-chain case of two chains of six draws term by term, and the rank steps are in the Excel, R and Python code. The fixed tables here are far too short for real use and only check the arithmetic.
Inputs and outputs:
x_1tox_6are the six retained draws of the first chain andx_7tox_12those of the second, in iteration order, in units of the estimand.psi_1topsi_4ands2_1tos2_4are the means and variances of the four split chains of three draws, withpsi_1andpsi_2from the first chain.psi_bar,B,W,var_plusandR_hatare as in HE-FM-CON-001, and the factor3 / 3is n over m minus 1. In the R and Python code the input is a table of draws with one column or list per chain and an even number of draws in each, and the functions returnsplit_rhatand the reportedrhat_max.Assumption: The conditions of HE-FM-CON-001 hold and warm-up has been removed. Vehtari and colleagues recommend relying on the statistic only if each split chain has an average effective sample size of at least 50, which short tables like these cannot reach.
Worked example (Illustrative second chain out of line): The second chain sits about 0.2 above the first, so B is large against W and R_hat is 2.8321. Run on the same table, the R and Python functions give a rank-normalised value of 1.6984, a folded value of 0.8872 and a reported maximum of 1.6984.
x_1 = -0.52; x_2 = -0.47; x_3 = -0.55; x_4 = -0.49; x_5 = -0.44; x_6 = -0.53; x_7 = -0.31; x_8 = -0.26; x_9 = -0.35; x_10 = -0.28; x_11 = -0.33; x_12 = -0.24; psi_1 = -0.5133; psi_2 = -0.4867; psi_3 = -0.3067; psi_4 = -0.2833; s2_1 = 0.00163; s2_2 = 0.00203; s2_3 = 0.00203; s2_4 = 0.00203; psi_bar = -0.3975; B = 0.04265; W = 0.00193; var_plus = 0.01551; R_hat = 2.8321Worked example (Illustrative mixed chains with three draws per split chain): The chains overlap, B is below W and R_hat is 0.8991, below 1 as HE-ER-CON-004 describes. The reported maximum is 0.9202.
x_1 = -0.52; x_2 = -0.47; x_3 = -0.55; x_4 = -0.49; x_5 = -0.44; x_6 = -0.53; x_7 = -0.50; x_8 = -0.46; x_9 = -0.54; x_10 = -0.48; x_11 = -0.51; x_12 = -0.45; psi_1 = -0.5133; psi_2 = -0.4867; psi_3 = -0.5; psi_4 = -0.48; s2_1 = 0.00163; s2_2 = 0.00203; s2_3 = 0.0016; s2_4 = 0.0009; psi_bar = -0.495; B = 0.00066; W = 0.00154; var_plus = 0.00125; R_hat = 0.8991Worked example (Illustrative trend shared by both chains): Both chains rise, so their whole-chain means agree and the unsplit statistic is 0.9129, while the halves differ and split-chain R_hat is 4.4159.
x_1 = -0.60; x_2 = -0.58; x_3 = -0.56; x_4 = -0.44; x_5 = -0.42; x_6 = -0.40; x_7 = -0.59; x_8 = -0.57; x_9 = -0.55; x_10 = -0.45; x_11 = -0.43; x_12 = -0.41; psi_1 = -0.58; psi_2 = -0.42; psi_3 = -0.57; psi_4 = -0.43; s2_1 = 0.0004; s2_2 = 0.0004; s2_3 = 0.0004; s2_4 = 0.0004; psi_bar = -0.5; B = 0.0226; W = 0.0004; var_plus = 0.0078; R_hat = 4.4159Worked example (Illustrative chains with one location and different scales, for the R and Python code): The first chain is
c(-0.46, -0.44, -0.45, -0.47, -0.43, -0.45, -0.44, -0.46, -0.45, -0.47, -0.43, -0.45)and the secondc(-0.55, -0.35, -0.45, -0.65, -0.25, -0.45, -0.35, -0.55, -0.45, -0.65, -0.25, -0.45). Plain split-chain R_hat and the rank-normalised value are both 0.9129, and only the folded value, 1.0239, exceeds 1.01, so the reported maximum is 1.0239. This example is for reading against the R and Python code, and the scalar lines above run on the site. Several folded values of these two-decimal draws are equal as decimals but differ in the last binary digits, and ranks taken on the stored numbers do not tie them. Tied as decimals, by rounding the folded values to 10 decimals before ranking, the folded value here is 1.0244, which still exceeds 1.01 and still passes the test below, and the folded values on the three tables above are 0.8860, 0.8487 and 0.8839, which change no reported maximum (computed here).Excel: Put each split chain in its own column of a range named DrawTable, with a first and second half of each chain as separate columns. Below each column enter
=AVERAGE(column)and=VAR.S(column), naming those two rows SplitMeansCF and SplitVarsCF, enter=ROWS(DrawTable)in SplitLengthCF, and enter=SQRT(((SplitLengthCF-1)/SplitLengthCF*AVERAGE(SplitVarsCF)+VAR.S(SplitMeansCF))/AVERAGE(SplitVarsCF))in SplitRhatCF, since B divided by n is the variance of the means. For the rank-normalised value build a second table of the same shape with=NORM.S.INV((RANK.AVG(B2,DrawTable,1)-3/8)/(COUNT(DrawTable)+1/4)), where B2 is the first cell of DrawTable, filled across and down, and repeat the helper rows to fill BulkRhatCF. For the folded value build=ABS(B2-MEDIAN(DrawTable))in a table named FoldedTable, apply the same normal score formula with FoldedTable in place of DrawTable and the first cell of FoldedTable in place of B2, and repeat the helper rows to fill FoldRhatCF. Then enter=MAX(BulkRhatCF,FoldRhatCF)in ConservativeRhatCF. VAR.S returns #DIV/0! for a column with one draw, and a table with no spread within any column returns #DIV/0!.R:
split_rhat <- function(d) { if (nrow(d) %% 2 != 0) stop("even number of draws needed"); n <- nrow(d)/2; s <- cbind(d[1:n, , drop = FALSE], d[(n+1):(2*n), , drop = FALSE]); B <- n*var(colMeans(s)); W <- mean(apply(s, 2, var)); sqrt(((n-1)/n*W+B/n)/W) }withrank_norm <- function(d) matrix(qnorm((rank(d)-3/8)/(length(d)+1/4)), nrow(d))andrhat_max <- function(d) max(split_rhat(rank_norm(d)), split_rhat(rank_norm(abs(d-median(d)))))Base R only. Withda matrix with one column per chain,round(split_rhat(cbind(c(-0.52, -0.47, -0.55, -0.49, -0.44, -0.53), c(-0.31, -0.26, -0.35, -0.28, -0.33, -0.24))), 4)is 2.8321. An odd number of draws stops with a message, so one draw is dropped first, and all chains must have the same length. An empty matrix stops with a subscript error. A single chain runs and returns a value, but it can test only stationarity (derived). Two draws per chain return NA. A table with no spread within any split chain returns NaN, or Inf when the split-chain means differ, andrhat_maxthen returns NaN if either of its two parts is NaN. None of these is a value below 1.01.Python:
def split_rhat(c): assert all(len(x) % 2 == 0 for x in c), 'even number of draws needed'; n = len(c[0])//2 if c else 0; s = [x[:n] for x in c]+[x[n:] for x in c]; return float('nan') if n < 2 else _rhat(n*variance([mean(x) for x in s]), mean([variance(x) for x in s]), n)with the helpersdef _rhat(B, W, n): return (float('inf') if B > 0 else float('nan')) if W == 0 else math.sqrt(((n-1)/n*W+B/n)/W),def rank_norm(c): p = sorted(x for y in c for x in y); S = len(p); r = lambda x: bisect_left(p, x)+(bisect_right(p, x)-bisect_left(p, x)+1)/2; return [[NormalDist().inv_cdf((r(x)-3/8)/(S+1/4)) for x in y] for y in c]anddef rhat_max(c): p = [x for y in c for x in y]; med = median(p) if p else 0; a = split_rhat(rank_norm(c)); b = split_rhat(rank_norm([[abs(x-med) for x in y] for y in c])); return float('nan') if math.isnan(a) or math.isnan(b) else max(a, b)Needsimport math; from statistics import NormalDist, mean, variance, median; from bisect import bisect_left, bisect_rightand takes a list of chains.round(split_rhat([[-0.52, -0.47, -0.55, -0.49, -0.44, -0.53], [-0.31, -0.26, -0.35, -0.28, -0.33, -0.24]]), 4)is 2.8321. An odd number of draws raises an AssertionError with the same message as R. Two draws per chain, an empty chain or an empty list return nan, and the no-spread cases return nan or inf as in R. Chains must have the same length, as in R, since unequal chains raise a StatisticsError or return a wrong value without warning.Test (Second chain out of line): With the first table in DrawTable,
=ABS(SplitRhatCF-2.8321)<0.0005returns TRUE. An unsplit calculation gives 3.5978 and FALSE, and doubling the cell to 5.6641 or halving it to 1.4160 also gives FALSE.Test (Shared trend): With the trending table in DrawTable,
=ABS(SplitRhatCF-4.4159)<0.0005returns TRUE. An unsplit calculation gives 0.9129 and FALSE, and doubling the cell to 8.8318 or halving it to 2.2079 also gives FALSE.Test (Different scales): With the two chains of the different scales example split into four columns,
=ABS(ConservativeRhatCF-1.0239)<0.0005returns TRUE. A reported value that leaves out the folded part gives 0.9129 and FALSE, and doubling the cell to 2.0478 or halving it to 0.5120 also gives FALSE.Common error (Keeping the two halves together in the helper rows): A column that holds a whole chain gives the unsplit statistic, 0.9129 on the shared trend table in place of 4.4159. The Excel layout needs one column for each half.
Common error (Ranking within each chain): Ranks must come from all draws pooled. Ranks taken within each chain remove the differences in location, and the table with the second chain out of line then returns 0.8826 in place of the rank-normalised 1.6984 (computed here). Ranks taken within each column of the Excel layout, which holds one half of a chain, return 0.8165 (computed here).
Source: Gelman A, Carlin JB, Stern HS, Dunson DB, Vehtari A, Rubin DB. Bayesian Data Analysis. 3rd edition. 2013. Section 11.4, equations 11.3 and 11.4. Vehtari A, Gelman A, Simpson D, Carpenter B, Bürkner PC. arXiv:1903.08008v5, preprint sections 3.1, 4.1 and 4.2. The tables, the code and their results are illustrative and computed here.
psi_1 = (x_1 + x_2 + x_3) / 3; psi_2 = (x_4 + x_5 + x_6) / 3; psi_3 = (x_7 + x_8 + x_9) / 3; psi_4 = (x_10 + x_11 + x_12) / 3; s2_1 = ((x_1 - psi_1)^2 + (x_2 - psi_1)^2 + (x_3 - psi_1)^2) / 2; s2_2 = ((x_4 - psi_2)^2 + (x_5 - psi_2)^2 + (x_6 - psi_2)^2) / 2; s2_3 = ((x_7 - psi_3)^2 + (x_8 - psi_3)^2 + (x_9 - psi_3)^2) / 2; s2_4 = ((x_10 - psi_4)^2 + (x_11 - psi_4)^2 + (x_12 - psi_4)^2) / 2; psi_bar = (psi_1 + psi_2 + psi_3 + psi_4) / 4; B = 3 / 3 * ((psi_1 - psi_bar)^2 + (psi_2 - psi_bar)^2 + (psi_3 - psi_bar)^2 + (psi_4 - psi_bar)^2); W = (s2_1 + s2_2 + s2_3 + s2_4) / 4; var_plus = 2 / 3 * W + B / 3; R_hat = sqrt(var_plus / W)
Try this function
Implementations
Excel
Potential scale reduction factor from ranges of split-chain means and variances
With the split-chain means in a range named SplitChainMeans, the within-chain variances in SplitChainVariances and n in DrawsPerSplitChain, the four formulas fill BetweenChainVariance, WithinChainVariance, PooledPosteriorVariance and PotentialScaleReduction. VAR.S divides by m minus 1, so the first formula is the B of the line for any number of split chains. AVERAGE and VAR.S skip blank cells, so a missing split chain silently lowers m rather than returning an error. A blank DrawsPerSplitChain is treated as 0 and returns #DIV/0!, and W of zero returns #DIV/0!.
=DrawsPerSplitChain*VAR.S(SplitChainMeans); =AVERAGE(SplitChainVariances); =(DrawsPerSplitChain-1)/DrawsPerSplitChain*WithinChainVariance+BetweenChainVariance/DrawsPerSplitChain; =SQRT(PooledPosteriorVariance/WithinChainVariance)
Assumptions
Chains split in half after warm-up
The warm-up draws have been discarded, by default the first half of each simulated sequence, and each remaining chain is split into a first and a second half of equal length n. Bayesian Data Analysis always simulates at least two sequences, so m is at least 4. Comparing the two halves is what tests stationarity within a chain as well as mixing between chains.
Dispersed starting points for the compared chains
Starting points are drawn from an overdispersed distribution. Bayesian Data Analysis states that var_plus overestimates the posterior variance under that condition and is unbiased under stationarity or in the limit of large n. Two chains are the least that book allows and are what this line and its examples write out. For real use NICE DSU Technical Support Document 2 suggests at least three chains from widely different yet sensible initial values, and Vehtari and colleagues recommend at least four by default, which the Excel row and HE-CF-CON-001 take.
One scalar estimand at a time with a roughly normal posterior
R_hat is computed separately for each scalar quantity of interest, such as one log odds ratio. Because it rests on means and variances, Bayesian Data Analysis describes it as most effective when the marginal posterior is approximately normal. A log or logit transformation can help, and HE-FM-CON-002 gives the rank-based route for heavy tails.
Within-chain variance above zero
W is above zero and n is at least 2, otherwise the ratio is undefined (derived). Excel returns #DIV/0! when W is zero, which happens when every split chain holds one repeated value.
Worked examples
Article's first run with one split chain out of line
The article's hypothetical network meta-analysis estimates a log odds ratio with two chains split in half after warm-up, giving four split chains of 500 draws, each with a within-chain variance of 0.010. One split-chain mean sits at minus 0.30, so B is 5.1333 and R_hat is about 1.42, far above both 1.1 and 1.01. The draws should not be pooled. The inputs are hypothetical.
psi_1 = -0.52; psi_2 = -0.48; psi_3 = -0.50; psi_4 = -0.30; s2_1 = 0.010; s2_2 = 0.010; s2_3 = 0.010; s2_4 = 0.010; n = 500; psi_bar = -0.45; B = 5.1333; W = 0.01; var_plus = 0.0202; R_hat = 1.42
Article's second run with the chains mixed
After a longer run in the same hypothetical analysis the split-chain means lie close together, B falls to 0.1 and R_hat is about 1.009. The value passes a 1.01 rule, although the article adds that the draws are usable only if the effective sample size checks also pass.
psi_1 = -0.47; psi_2 = -0.45; psi_3 = -0.44; psi_4 = -0.44; s2_1 = 0.010; s2_2 = 0.010; s2_3 = 0.010; s2_4 = 0.010; n = 500; psi_bar = -0.45; B = 0.1; W = 0.01; var_plus = 0.01018; R_hat = 1.009
Limiting case with between-chain and within-chain variance equal
Illustrative means chosen so that B equals W, both 0.16, with 300 draws per split chain. The weighted average then equals W whatever n is, so R_hat is exactly 1 (derived). The case checks an implementation.
psi_1 = -0.47; psi_2 = -0.43; psi_3 = -0.47; psi_4 = -0.43; s2_1 = 0.16; s2_2 = 0.16; s2_3 = 0.16; s2_4 = 0.16; n = 300; psi_bar = -0.45; B = 0.16; W = 0.16; var_plus = 0.16; R_hat = 1
Limiting case with identical split-chain means
Illustrative split chains with the same mean have B equal to 0, so var_plus is (n minus 1) / n times W and R_hat is the square root of 499 / 500, about 0.999. The value sits just below 1, as it does whenever B is below W (derived).
psi_1 = -0.45; psi_2 = -0.45; psi_3 = -0.45; psi_4 = -0.45; s2_1 = 0.010; s2_2 = 0.010; s2_3 = 0.010; s2_4 = 0.010; n = 500; psi_bar = -0.45; B = 0; W = 0.01; var_plus = 0.00998; R_hat = 0.999
Common errors
Leaving the factor n out of the between-chain variance
Using the plain variance of the split-chain means as B removes the very signal the statistic reads. In the article's first run B falls from 5.1333 to 0.0103 and R_hat from 1.42 to 1.00003, so a run with one chain out of line appears to have mixed (derived).
Computing the statistic on whole chains without splitting
Bayesian Data Analysis states that the unsplit statistic of its earlier editions would not correctly diagnose chains that have mixed without reaching stationarity. On the illustrative trending draws in HE-CF-CON-001, where both chains rise together, the unsplit value is 0.9129 and the split value is 4.4159 (computed here).
Relying on the 1.1 threshold alone
Bayesian Data Analysis reports being generally satisfied with 1.1, but Vehtari and colleagues found that the statistic can dip below 1.1 well before convergence in some examples and recommend 1.01, adding that it cannot in general be read as a potential scale reduction factor. In their Appendix A simulations a threshold of 1.1 detects a linear trend only when it accounts for 30% or more of the total marginal variance, against 2% or more for 1.01.
Reading a value below 1 as a stronger pass
R_hat falls below 1 exactly when B is below W, which the derived identity that R_hat squared equals 1 + (B minus W) / (n W) shows and a grid of 210 cases confirmed. Short chains can do this by chance, as the mixed table in HE-CF-CON-001 does at 0.8991 with three draws per split chain. Such a value is not better evidence than a value of exactly 1.
Treating a low value as proof of a usable sample or a sound model
Vehtari and colleagues state that a small value is not enough to ensure a useful MCMC sample, since the effective sample size must also be large enough. NICE DSU Technical Support Document 2 reports a set of starting values that converges to a different posterior. The statistic describes the computation and, as the article says, a chain can converge to the posterior of a misspecified model.
Applying the statistic to independent probabilistic runs
Parameter sets drawn independently in a probabilistic sensitivity analysis have no starting point to forget, so the article's question for them is precision. That question is answered by the Monte Carlo standard error set out in HE-FN-PSA-001 and the draws required in HE-FM-ABM-003, and on independent runs B and W estimate the same variance, so R_hat averages near 1 whether the runs are few or many and does not measure whether enough were done (derived; seeded repeats with two chains computed here give a mean of 1.002 at 40 runs over 20,000 repeats and 1.000 at 4,000 runs over 500 repeats).
Sources
Bayesian Data Analysis on split chains and the potential scale reduction factor
Gelman A, Carlin JB, Stern HS, Dunson DB, Vehtari A, Rubin DB. Bayesian Data Analysis. 3rd edition. Boca Raton: Chapman & Hall/CRC; 2013 (free electronic edition read). Section 11.4, subsections Splitting each saved sequence into two parts and Assessing mixing using between- and within-sequence variances (pp. 284-285). It defines m as the number of chains after splitting and n as their length, gives B and W, equation 11.3 for the weighted average, and equation 11.4 for the square root of its ratio to W, which declines to 1 as n grows. Footnote 2 explains the change to split chains. The end of section 11.4 (p. 286) describes the method as most effective for approximately normal marginal posteriors. Section 11.5, Effective number of simulation draws, holds the subsections Bounded or long-tailed distributions (p. 287), which suggests log, logit and rank transformations, and Stopping the simulations (p. 287), which gives the 1.1 threshold.
Vehtari and colleagues on split R-hat and its 1.01 threshold
Vehtari A, Gelman A, Simpson D, Carpenter B, Bürkner PC. Rank-normalization, folding, and localization: an improved R-hat for assessing convergence of MCMC. arXiv:1903.08008v5. Published as Bayesian Analysis. 2021;16(2):667-718, doi:10.1214/20-BA1221 (published version not seen). Preprint section 3.1, equations 1 to 4, restates the same split-chain formulas. Preprint section 2 recommends at least four chains and using the sample only if the value is below 1.01, says the value can dip below 1.1 well before convergence and says that a small value is not enough on its own. Appendix A reports the trend detection shares.
Gelman and Rubin on several sequences from overdispersed starting points
Gelman A, Rubin DB. Inference from iterative simulation using multiple sequences. Statistical Science. 1992;7(4):457-472. The abstract on p. 457 recommends several independent sequences with starting points sampled from an overdispersed distribution, and for each univariate estimand an estimate of how much sharper the distributional estimate might become if the simulations continued indefinitely.
NICE DSU TSD 2 on multiple chains and starting values
Dias S, Welton NJ, Sutton AJ, Ades AE. NICE DSU Technical Support Document 2: A generalised linear modelling framework for pairwise and network meta-analysis of randomised controlled trials. Sheffield: Decision Support Unit, ScHARR, University of Sheffield; 2011, last updated September 2016. Section 6, Technical issues in Bayesian MCMC, suggests at least three chains started from widely different yet sensible initial values and asks that each chain samples from the same posterior after convergence. The note after the initial values for Program 4(a) (p. 75) reports one set of starting values which converges to a different posterior.
Canonical Identity
Stable URI · Machine-readable · Resolvable · CC BY 4.0
