Functions & Formulae

Each applied formula has its own function page, with a signature, implementations, and tests.

Convergence checks on MCMC draws by split chains, rank normal scores and Monte Carlo error

c(psi_j, s2_j, n, theta, theta_med, r, S, R_bulk, R_fold, S_bulk, e_MC, s_post) = (R_hat, zeta, f_r, R_max, use_draws, r_MC, ok_MC)

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.

  • Split-chain potential scale reduction factor from four split chains

    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)

    Compares the spread between split chains with the spread inside them for one scalar estimand. After warm-up each chain is split in half, so two chains give four split chains of n draws, the case this line writes out. B is n times the sample variance of the split-chain means, W is the mean of the within-chain variances, and var_plus is their weighted average, an estimate of the posterior variance. R_hat is the square root of var_plus over W, which Bayesian Data Analysis describes as an estimate of the factor by which the scale of the current distribution might fall if the simulation ran on, and it declines to 1 as n grows. For any other number of split chains m the same formulas hold with m and m minus 1 in place of 4 and 3, as in the Excel row and HE-CF-CON-001.

  • Folded draw and Blom rank fraction for a rank-normalised R-hat

    zeta = abs(theta - theta_med); f_r = (r - 3 / 8) / (S + 1 / 4)

    Prepares one draw for the rank-normalised diagnostic of Vehtari and colleagues. zeta is the folded draw, the absolute deviation of the draw from the median of all draws, which turns a difference in the scale of the chains into a difference in location. f_r is the fraction with the offset that the authors take from Blom, for a value of rank r among the S pooled draws. The normal score is the inverse standard normal of f_r, which the site calculator does not hold, so the line stops at f_r and the Excel row takes the inverse. Normal scores of theta, kept in the positions of their draws and put through HE-FM-CON-001 in place of the draws, give the rank-normalised value, and normal scores of zeta, ranked among all the folded draws, give the folded value. Ranks keep the statistic defined when the posterior has no finite mean or variance.

  • Reported maximum of rank-normalised and folded R-hat with its use rule

    R_max = max(R_bulk, R_fold); use_draws = (R_max < 1.01) and (S_bulk > 400)

    Reduces the two rank-based values to the single conservative figure that Vehtari and colleagues propose, the larger of the rank-normalised and the folded R-hat, and applies their first-level check on each parameter. The sample is used only if that figure is below 1.01 and the rank-normalised effective sample size is above 400. Section 2 of the source writes both inequalities as strict, and its section 3.2 says that the total effective sample size should be at least 400. The line follows section 2, so a value of exactly 1.01 or exactly 400 fails. use_draws equals 1 when both conditions hold. R_bulk and R_fold are the output of HE-FM-CON-001 on the scores of HE-FM-CON-002, or of HE-CF-CON-001, and S_bulk is read as an input.

  • Monte Carlo error as a share of the posterior standard deviation with the 5% guideline

    r_MC = e_MC / s_post; ok_MC = (r_MC <= 0.05)

    NICE DSU Technical Support Document 2 reports an often cited guideline that the Monte Carlo error, which reflects both the number of simulations and the degree of autocorrelation, should be no more than 5% of the posterior standard deviation of each parameter of interest. r_MC is that share and ok_MC equals 1 when the guideline is met, with exactly 0.05 counting as met because the wording is no more than. The share compares simulation noise with the width of the posterior itself. If e_MC is written as s_post / sqrt(n_eff), the form that equations 11.5 and 11.6 of Bayesian Data Analysis give for a posterior mean, r_MC is 1 / sqrt(n_eff) and the guideline asks for n_eff of 400 or more (derived). HE-FM-ABM-003 with e_target at 0.05 times s_run returns the same 400 whatever the spread (derived).

This set of formulae is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0).

Author: Dr Darrin Baines · Publisher: Intelligent Parameters Ltd · Identifier: https://healtheconomics.wiki/concept/convergence/formulae