Can you trust the draws? R-hat, divergences, LOO and SBC
Read the checks a Stan fit reports about itself (R-hat, effective sample sizes, divergent transitions, E-BFMI), judge leave-one-out with Pareto k̂, and test a whole model setup with simulation-based calibration.
What runs in CodeSplash
- Bayesian regression model (Stan) Experimental
- Simulation-based calibration (Bayesian model check) Experimental
You will be able to
- Say what R-hat and bulk and tail effective sample size measure, and their thresholds
- Explain what a divergent transition is and how to remove one
- Decide when a PSIS-LOO estimate can be trusted from Pareto k̂
- Read simulation-based calibration ranks and say what a ∪ shape means
Read first
Draws you can check
ReadMarkov chain Monte Carlo doesn't hand you the posterior; it hands you draws, and the draws only describe the posterior when the sampler worked. Stan's sampler, NUTSNUTS (No-U-Turn Sampler)Stan's default MCMC: it follows the posterior's gradient and stops each trajectory when it starts to turn back., is unusual in one good way: when it fails, it usually says so. Every bayes.model fit reports those signals, and the Diagnose step of the Bayesian models studio shows them before any estimate.
A fit to check
ReadBayesian gaussian (normal) model of Reaction (n = 177), NUTS (Hamiltonian Monte Carlo). Posterior: Days has median 10.6 (95% credible interval 7.03 to 13.9); P(> 0) = 1.00. Largest R-hat 1.008, 0 divergent transitions. PSIS-LOO elpd = -846.7 (SE 22.7).
- Days (posterior median)
- 10.610
- 95% credible interval
- [7.029, 13.895]
- P(effect > 0)
- 1
- Largest R-hat
- 1.008
- elpd (PSIS-LOO)
- −846.70
- Bayesian R² (median)
- 0.7936
Assumptions
- Sampler converged (R-hat, ESS) — Largest R-hat 1.008 (want ≤ 1.01); smallest bulk ESS 147 and tail ESS 185 (want ≥ 400).
- No divergent transitions — No divergent transitions.
- Trajectories within the tree depth — No draw hit the maximum tree depth.
- Energy (E-BFMI ≥ 0.3) — Smallest E-BFMI across chains 0.68 (below 0.3 means the sampler explores the tails poorly).
- Leave-one-out is reliable (Pareto k̂) — 9 of 177 rows have k̂ above 0.64 (largest 1.62); those rows are influential and their LOO estimate is unreliable.
- Replicated data look like the observed data — Posterior predictive p-values are between 0.01 and 0.99.
- Priors stated — Results depend on the priors in the Priors table; rerun with wider priors to see how much.
Posterior summary
| Parameter | Mean | SD | 95% lower | Median | 95% upper | R-hat | Bulk ESS | Tail ESS | P(> 0) |
|---|---|---|---|---|---|---|---|---|---|
| (Intercept) | 250.94 | 7.397 | 236.23 | 251.03 | 265.05 | 1.006 | 378.30 | 408.08 | 1 |
| Days | 10.565 | 1.704 | 7.029 | 10.610 | 13.895 | 0.9980 | 384.06 | 302.39 | 1 |
| sigma | 25.808 | 1.543 | 23.047 | 25.734 | 29.051 | 1.002 | 624.86 | 317.04 | 1 |
| sd(Subject: (Intercept)) | 26.328 | 6.765 | 15.022 | 25.630 | 40.962 | 1.008 | 303.12 | 345.91 | 1 |
| sd(Subject: Days) | 6.504 | 1.390 | 4.332 | 6.340 | 9.961 | 1.002 | 434.12 | 415.56 | 1 |
| cor(Subject: (Intercept), Days) | 0.1420 | 0.2936 | −0.4283 | 0.1504 | 0.7129 | 1.006 | 147.45 | 185.34 | 0.6867 |
Priors
| Parameter | Prior | Note |
|---|---|---|
| (Intercept) | Normal(298.1, 141.5) | at the predictors' means |
| Days | Normal(0, 48.85) | autoscaled |
| sigma | Exponential(0.01767) | residual SD |
| sd(Subject: (Intercept)) | Exponential(0.01767) | |
| sd(Subject: Days) | Exponential(0.01767) | |
| cor(Subject) | LKJ( 1) |
Sampler by chain
| Chain | Divergent | Max tree depth hits | E-BFMI | Step size | Mean acceptance |
|---|---|---|---|---|---|
| 1 | 0 | 0 | 0.6824 | 0.1005 | 0.9460 |
| 2 | 0 | 0 | 0.6765 | 0.0910 | 0.9580 |
Leave-one-out cross-validation (PSIS-LOO)
| Quantity | Estimate | SE |
|---|---|---|
| elpd_loo (expected log predictive density) | −846.70 | 22.724 |
| p_loo (effective number of parameters) | 33.927 | 8.768 |
| LOOIC (−2 × elpd_loo) | 1693.40 | 45.447 |
Posterior predictive checks
| Statistic | Observed | Replicated (mean) | P(rep ≥ obs) |
|---|---|---|---|
| Mean | 298.07 | 298.19 | 0.5217 |
| Standard deviation | 56.591 | 56.726 | 0.5350 |
| Maximum | 466.35 | 471.20 | 0.5750 |
| Minimum | 194.33 | 168.59 | 0.0417 |
Varying effects by level
| Group | Level | Effect | Mean | 95% lower | 95% upper |
|---|---|---|---|---|---|
| Subject | 308 | (Intercept) | 1.764 | −26.568 | 27.838 |
| Subject | 308 | Days | 8.925 | 3.669 | 14.197 |
| Subject | 309 | (Intercept) | −39.735 | −66.942 | −11.624 |
| Subject | 309 | Days | −8.812 | −14.520 | −3.886 |
| Subject | 310 | (Intercept) | −38.562 | −65.376 | −10.413 |
| Subject | 310 | Days | −5.616 | −11.018 | −0.1130 |
| Subject | 330 | (Intercept) | 22.462 | −3.873 | 49.486 |
| Subject | 330 | Days | −4.662 | −10.269 | 0.7299 |
| Subject | 331 | (Intercept) | 22.159 | −5.978 | 47.655 |
| Subject | 331 | Days | −2.363 | −7.980 | 2.891 |
| Subject | 332 | (Intercept) | 8.390 | −15.436 | 32.571 |
| Subject | 332 | Days | −0.3063 | −5.486 | 5.174 |
| Subject | 333 | (Intercept) | 17.060 | −9.295 | 42.335 |
| Subject | 333 | Days | −0.1652 | −5.374 | 5.842 |
| Subject | 334 | (Intercept) | −6.429 | −31.168 | 18.210 |
| Subject | 334 | Days | 0.9945 | −4.002 | 5.914 |
| Subject | 335 | (Intercept) | −0.6551 | −24.853 | 25.528 |
| Subject | 335 | Days | −10.683 | −16.370 | −5.427 |
| Subject | 337 | (Intercept) | 34.407 | 10.627 | 60.525 |
| Subject | 337 | Days | 8.700 | 3.339 | 13.934 |
| Subject | 349 | (Intercept) | −23.572 | −53.515 | 3.232 |
| Subject | 349 | Days | 0.9461 | −4.373 | 6.403 |
| Subject | 350 | (Intercept) | −11.630 | −40.389 | 14.241 |
| Subject | 350 | Days | 6.243 | 0.7660 | 11.784 |
| Subject | 351 | (Intercept) | 5.436 | −20.205 | 33.931 |
| Subject | 351 | Days | −3.034 | −8.709 | 2.455 |
| Subject | 352 | (Intercept) | 16.442 | −10.144 | 41.397 |
| Subject | 352 | Days | 4.048 | −1.017 | 9.816 |
| Subject | 369 | (Intercept) | 3.525 | −23.937 | 30.338 |
| Subject | 369 | Days | 0.8796 | −4.575 | 6.816 |
| Subject | 370 | (Intercept) | −23.620 | −49.997 | 4.088 |
| Subject | 370 | Days | 4.546 | −0.6811 | 10.270 |
| Subject | 371 | (Intercept) | 0.7514 | −24.788 | 27.692 |
| Subject | 371 | Days | −0.9866 | −6.376 | 4.122 |
| Subject | 372 | (Intercept) | 11.995 | −13.728 | 36.499 |
| Subject | 372 | Days | 1.203 | −4.114 | 6.358 |
- 3 of 180 rows were left out because a value was missing.
- Outcome model: Gaussian (normal).
- Credible intervals are equal-tailed posterior quantiles; P(> 0) is the share of draws above zero.
- Bayesian results are not p-values and are not entered in the multiple-testing ledger.
The exact R script · R R version 4.5.1 (2025-06-13)
# Bayesian regression model (Stan)
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method bayes.model 1.0.1 · spec de47c4fd3c3948ac
# Dataset: s1_sleep (version 1)
# Runs on its own: outside the app, cs_publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
library(codesplashr) # cs_bayes_fit_model (twin of codesplash.bayes.model) on the shared Stan programs (cs_stan_*)
# %% Data
df <- as.data.frame(nanoparquet::read_parquet("tests/parity/fixtures/s1_sleep.parquet", col_select = c("Reaction", "Days", "Subject")))
n_input <- nrow(df)
df[["Subject"]] <- factor(df[["Subject"]], levels = c("308", "309", "310", "330", "331", "332", "333", "334", "335", "337", "349", "350", "351", "352", "369", "370", "371", "372")) # stored level order
df <- df[stats::complete.cases(df[c("Reaction", "Days", "Subject")]), , drop = FALSE] # listwise deletion
df[] <- lapply(df, function(x) if (is.factor(x)) droplevels(x) else x)
n_dropped <- n_input - nrow(df)
warnings <- list()
if (n_dropped > 0) {
warnings <- c(warnings, cs_warning("CS_W_ROWS_DROPPED", dropped = n_dropped, total = n_input))
}
if (n_input > 0 && nrow(df) == 0) cs_error("CS_E_NO_ROWS")
cs_check_finite_columns(df, c("Reaction", "Days", "Subject")) # infinite values stop the run (registry/errors.yaml)
outcome <- "Reaction"
# %% Model: the precompiled Stan program runs on a data file Python writes byte for byte the same (identical draws)
fit <- cs_bayes_fit_model(
df,
outcome = outcome,
predictors = c("Days"),
family = "gaussian",
trials = NULL,
exposure = NULL,
group = "Subject",
slopes = c("Days"),
group2 = NULL,
nesting = "nested",
interactions = "none",
event_level = NULL,
prior_dist = "normal",
prior_scale = 2.5,
prior_df = 3,
autoscale = TRUE,
intercept_scale = 2.5,
lkj = 1,
algorithm = "nuts",
chains = as.integer(2),
iter_warmup = as.integer(300),
iter_sampling = as.integer(300),
seed = 1234,
adapt_delta = 0.95,
max_treedepth = as.integer(10),
use_qr = FALSE,
prior_only = FALSE,
loo = TRUE,
ppc = TRUE,
cred = 0.95,
out_dir = cs_ml_out_dir("target/academy-parity/run-8142/bayes.model/gaussian-sleep-slopes/out-r")
)
values <- fit$values
prep <- fit$prep
# %% Result
cred <- values$cred_level
fixed <- Filter(function(r) r$kind == "fixed", fit$rows)
slopes_only <- Filter(function(r) r$term != "(Intercept)", fixed)
focus <- if (length(slopes_only)) slopes_only[[1]] else if (length(fixed)) fixed[[1]] else fit$rows[[1]]
labels <- c(gaussian = "Gaussian (normal)", student_t = "Student-t (robust)", logistic = "Logistic (Bernoulli)",
binomial = "Logistic (successes of trials)", poisson = "Poisson", negbinomial = "Negative binomial",
ordinal = "Ordinal (cumulative logit)")
label <- labels[[prep$family]]
alg_labels <- c(nuts = "NUTS (Hamiltonian Monte Carlo)", pathfinder = "Pathfinder (approximate)", meanfield = "ADVI mean-field (approximate)",
fullrank = "ADVI full-rank (approximate)", laplace = "Laplace approximation at the mode")
what <- if (isTRUE(values$prior_only)) "Prior predictive draws" else "Posterior"
g3 <- function(x) formatC(signif(x, 3), digits = 3, format = "fg", flag = "#")
summary <- sprintf("Bayesian %s model of %s (n = %d), %s. %s: %s has median %s (%.0f%% credible interval %s to %s); P(> 0) = %.2f.",
tolower(label), outcome, values$n, alg_labels[[values$algorithm]], what, focus$term,
g3(focus$median), cred * 100, g3(focus$lower), g3(focus$upper), focus$p_positive)
if (!is.null(values$rhat_max) && is.finite(values$rhat_max)) {
summary <- paste0(summary, sprintf(" Largest R-hat %.3f", values$rhat_max),
if (is.finite(values$n_divergent)) sprintf(", %d divergent transitions", as.integer(values$n_divergent)) else "", ".")
}
if (is.finite(values$elpd_loo)) summary <- paste0(summary, sprintf(" PSIS-LOO elpd = %.1f (SE %.1f).", values$elpd_loo, values$elpd_loo_se))
statistics <- list(
list(key = "focus_median", label = sprintf("%s (posterior median)", focus$term), value = focus$median),
list(key = "focus_cri", label = sprintf("%.0f%% credible interval", cred * 100), value = c(focus$lower, focus$upper)),
list(key = "focus_p_positive", label = "P(effect > 0)", value = focus$p_positive)
)
if (!is.null(values$rhat_max) && is.finite(values$rhat_max)) statistics[[length(statistics) + 1L]] <- list(key = "rhat_max", label = "Largest R-hat", value = values$rhat_max)
if (is.finite(values$elpd_loo)) statistics[[length(statistics) + 1L]] <- list(key = "elpd_loo", label = "elpd (PSIS-LOO)", value = values$elpd_loo)
if (is.finite(values$r2_median)) statistics[[length(statistics) + 1L]] <- list(key = "r2_median", label = "Bayesian R² (median)", value = values$r2_median)
notes <- c(
paste0("Outcome model: ", label, if (!is.null(prep$event) && prep$family == "logistic") paste0("; event: ", prep$event) else "", "."),
"Credible intervals are equal-tailed posterior quantiles; P(> 0) is the share of draws above zero.",
"Bayesian results are not p-values and are not entered in the multiple-testing ledger.",
fit$notes
)
cs_publish(list(
method = "bayes.model",
methodVersion = "1.0.1",
title = if (isTRUE(values$prior_only)) "Prior predictive check (Stan)" else sprintf("Bayesian %s model (Stan)", tolower(label)),
engine = cs_engine_info("posterior", "loo"),
n = values$n,
summary = summary,
statistics = statistics,
tables = fit$tables,
assumptions = fit$assumptions,
warnings = warnings,
notes = notes,
values = values,
charts = fit$charts,
artifacts = fit$artifacts
))
Bayesian gaussian (normal) model of Reaction (n = 177), NUTS (Hamiltonian Monte Carlo). Posterior: Days has median 10.6 (95% credible interval 7.03 to 13.9); P(> 0) = 1.00. Largest R-hat 1.008, 0 divergent transitions. PSIS-LOO elpd = -846.7 (SE 22.7).
- Days (posterior median)
- 10.610
- 95% credible interval
- [7.029, 13.895]
- P(effect > 0)
- 1
- Largest R-hat
- 1.008
- elpd (PSIS-LOO)
- −846.70
- Bayesian R² (median)
- 0.7936
Assumptions
- Sampler converged (R-hat, ESS) — Largest R-hat 1.008 (want ≤ 1.01); smallest bulk ESS 147 and tail ESS 185 (want ≥ 400).
- No divergent transitions — No divergent transitions.
- Trajectories within the tree depth — No draw hit the maximum tree depth.
- Energy (E-BFMI ≥ 0.3) — Smallest E-BFMI across chains 0.68 (below 0.3 means the sampler explores the tails poorly).
- Leave-one-out is reliable (Pareto k̂) — 9 of 177 rows have k̂ above 0.64 (largest 1.62); those rows are influential and their LOO estimate is unreliable.
- Replicated data look like the observed data — Posterior predictive p-values are between 0.01 and 0.99.
- Priors stated — Results depend on the priors in the Priors table; rerun with wider priors to see how much.
Posterior summary
| Parameter | Mean | SD | 95% lower | Median | 95% upper | R-hat | Bulk ESS | Tail ESS | P(> 0) |
|---|---|---|---|---|---|---|---|---|---|
| (Intercept) | 250.94 | 7.397 | 236.23 | 251.03 | 265.05 | 1.006 | 378.30 | 408.08 | 1 |
| Days | 10.565 | 1.704 | 7.029 | 10.610 | 13.895 | 0.9980 | 384.06 | 302.39 | 1 |
| sigma | 25.808 | 1.543 | 23.047 | 25.734 | 29.051 | 1.002 | 624.86 | 317.04 | 1 |
| sd(Subject: (Intercept)) | 26.328 | 6.765 | 15.022 | 25.630 | 40.962 | 1.008 | 303.12 | 345.91 | 1 |
| sd(Subject: Days) | 6.504 | 1.390 | 4.332 | 6.340 | 9.961 | 1.002 | 434.12 | 415.56 | 1 |
| cor(Subject: (Intercept), Days) | 0.1420 | 0.2936 | −0.4283 | 0.1504 | 0.7129 | 1.006 | 147.45 | 185.34 | 0.6867 |
Priors
| Parameter | Prior | Note |
|---|---|---|
| (Intercept) | Normal(298.1, 141.5) | at the predictors' means |
| Days | Normal(0, 48.85) | autoscaled |
| sigma | Exponential(0.01767) | residual SD |
| sd(Subject: (Intercept)) | Exponential(0.01767) | |
| sd(Subject: Days) | Exponential(0.01767) | |
| cor(Subject) | LKJ(1) |
Sampler by chain
| Chain | Divergent | Max tree depth hits | E-BFMI | Step size | Mean acceptance |
|---|---|---|---|---|---|
| 1 | 0 | 0 | 0.6824 | 0.1005 | 0.9460 |
| 2 | 0 | 0 | 0.6765 | 0.0910 | 0.9580 |
Leave-one-out cross-validation (PSIS-LOO)
| Quantity | Estimate | SE |
|---|---|---|
| elpd_loo (expected log predictive density) | −846.70 | 22.724 |
| p_loo (effective number of parameters) | 33.927 | 8.768 |
| LOOIC (−2 × elpd_loo) | 1693.40 | 45.447 |
Posterior predictive checks
| Statistic | Observed | Replicated (mean) | P(rep ≥ obs) |
|---|---|---|---|
| Mean | 298.07 | 298.19 | 0.5217 |
| Standard deviation | 56.591 | 56.726 | 0.5350 |
| Maximum | 466.35 | 471.20 | 0.5750 |
| Minimum | 194.33 | 168.59 | 0.0417 |
Varying effects by level
| Group | Level | Effect | Mean | 95% lower | 95% upper |
|---|---|---|---|---|---|
| Subject | 308 | (Intercept) | 1.764 | −26.568 | 27.838 |
| Subject | 308 | Days | 8.925 | 3.669 | 14.197 |
| Subject | 309 | (Intercept) | −39.735 | −66.942 | −11.624 |
| Subject | 309 | Days | −8.812 | −14.520 | −3.886 |
| Subject | 310 | (Intercept) | −38.562 | −65.376 | −10.413 |
| Subject | 310 | Days | −5.616 | −11.018 | −0.1130 |
| Subject | 330 | (Intercept) | 22.462 | −3.873 | 49.486 |
| Subject | 330 | Days | −4.662 | −10.269 | 0.7299 |
| Subject | 331 | (Intercept) | 22.159 | −5.978 | 47.655 |
| Subject | 331 | Days | −2.363 | −7.980 | 2.891 |
| Subject | 332 | (Intercept) | 8.390 | −15.436 | 32.571 |
| Subject | 332 | Days | −0.3063 | −5.486 | 5.174 |
| Subject | 333 | (Intercept) | 17.060 | −9.295 | 42.335 |
| Subject | 333 | Days | −0.1652 | −5.374 | 5.842 |
| Subject | 334 | (Intercept) | −6.429 | −31.168 | 18.210 |
| Subject | 334 | Days | 0.9945 | −4.002 | 5.914 |
| Subject | 335 | (Intercept) | −0.6551 | −24.853 | 25.528 |
| Subject | 335 | Days | −10.683 | −16.370 | −5.427 |
| Subject | 337 | (Intercept) | 34.407 | 10.627 | 60.525 |
| Subject | 337 | Days | 8.700 | 3.339 | 13.934 |
| Subject | 349 | (Intercept) | −23.572 | −53.515 | 3.232 |
| Subject | 349 | Days | 0.9461 | −4.373 | 6.403 |
| Subject | 350 | (Intercept) | −11.630 | −40.389 | 14.241 |
| Subject | 350 | Days | 6.243 | 0.7660 | 11.784 |
| Subject | 351 | (Intercept) | 5.436 | −20.205 | 33.931 |
| Subject | 351 | Days | −3.034 | −8.709 | 2.455 |
| Subject | 352 | (Intercept) | 16.442 | −10.144 | 41.397 |
| Subject | 352 | Days | 4.048 | −1.017 | 9.816 |
| Subject | 369 | (Intercept) | 3.525 | −23.937 | 30.338 |
| Subject | 369 | Days | 0.8796 | −4.575 | 6.816 |
| Subject | 370 | (Intercept) | −23.620 | −49.997 | 4.088 |
| Subject | 370 | Days | 4.546 | −0.6811 | 10.270 |
| Subject | 371 | (Intercept) | 0.7514 | −24.788 | 27.692 |
| Subject | 371 | Days | −0.9866 | −6.376 | 4.122 |
| Subject | 372 | (Intercept) | 11.995 | −13.728 | 36.499 |
| Subject | 372 | Days | 1.203 | −4.114 | 6.358 |
- 3 of 180 rows were left out because a value was missing.
- Outcome model: Gaussian (normal).
- Credible intervals are equal-tailed posterior quantiles; P(> 0) is the share of draws above zero.
- Bayesian results are not p-values and are not entered in the multiple-testing ledger.
The exact Python script · Python Python 3.13.5 · scipy 1.18.1 · pandas 3.1.0
# Bayesian regression model (Stan)
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method bayes.model 1.0.1 · spec de47c4fd3c3948ac
# Dataset: s1_sleep (version 1)
# Runs on its own: outside the app, cs.publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
import numpy as np
import pandas as pd
import codesplash as cs
from codesplash.bayes import model as bm # data, priors and summaries (twin of codesplashr cs_bayes_*)
from codesplash.ml.run import out_dir
# %% Data
df = pd.read_parquet("tests/parity/fixtures/s1_sleep.parquet", columns=["Reaction", "Days", "Subject"])
n_input = len(df)
df["Subject"] = pd.Categorical(df["Subject"], categories=["308", "309", "310", "330", "331", "332", "333", "334", "335", "337", "349", "350", "351", "352", "369", "370", "371", "372"]) # stored level order
df = df.dropna(subset=["Reaction", "Days", "Subject"]) # listwise deletion over the analysis columns
df["Subject"] = df["Subject"].cat.remove_unused_categories()
n_dropped = n_input - len(df)
warnings = []
if n_dropped:
warnings.append(cs.cs_warning("CS_W_ROWS_DROPPED", dropped=n_dropped, total=n_input))
if n_input and not len(df):
cs.cs_error("CS_E_NO_ROWS")
cs.check_finite_columns(df, ["Reaction", "Days", "Subject"]) # infinite values stop the run (registry/errors.yaml)
outcome = "Reaction"
# %% Model: the precompiled Stan program runs on a data file R writes byte for byte the same (identical draws)
fit = bm.fit_model(
df,
outcome=outcome,
predictors=["Days"],
family="gaussian",
trials=None,
exposure=None,
group="Subject",
slopes=["Days"],
group2=None,
nesting="nested",
interactions="none",
event_level=None,
prior_dist="normal",
prior_scale=2.5,
prior_df=3,
autoscale=True,
intercept_scale=2.5,
lkj=1,
algorithm="nuts",
chains=int(2),
iter_warmup=int(300),
iter_sampling=int(300),
seed=int(1234),
adapt_delta=0.95,
max_treedepth=int(10),
use_qr=False,
prior_only=False,
loo=True,
ppc=True,
cred=0.95,
out_dir=out_dir("target/academy-parity/run-8142/bayes.model/gaussian-sleep-slopes/out-python"),
)
values = fit["values"]
prep = fit["prep"]
# %% Result
cred = values["cred_level"]
fixed = [r for r in fit["rows"] if r["kind"] == "fixed"]
focus = next((r for r in fixed if r["term"] != "(Intercept)"), fixed[0] if fixed else fit["rows"][0])
label = bm.FAMILIES[prep.family]["label"]
what = "Prior predictive draws" if values["prior_only"] else "Posterior"
summary = (
f"Bayesian {label.lower()} model of {outcome} (n = {values['n']}), {bm.ALGORITHM_LABELS[values['algorithm']]}. "
f"{what}: {focus['term']} has median {focus['median']:.3g} ({cred:.0%} credible interval {focus['lower']:.3g} to {focus['upper']:.3g}); "
f"P(> 0) = {focus['p_positive']:.2f}."
)
if values.get("rhat_max") is not None:
summary += f" Largest R-hat {values['rhat_max']:.3f}"
if values.get("n_divergent") is not None:
summary += f", {values['n_divergent']} divergent transitions"
summary += "."
if values.get("elpd_loo") is not None:
summary += f" PSIS-LOO elpd = {values['elpd_loo']:.1f} (SE {values['elpd_loo_se']:.1f})."
statistics = [
{"key": "focus_median", "label": f"{focus['term']} (posterior median)", "value": focus["median"]},
{"key": "focus_cri", "label": f"{cred:.0%} credible interval", "value": [focus["lower"], focus["upper"]]},
{"key": "focus_p_positive", "label": "P(effect > 0)", "value": focus["p_positive"]},
]
if values.get("rhat_max") is not None:
statistics.append({"key": "rhat_max", "label": "Largest R-hat", "value": values["rhat_max"]})
if values.get("elpd_loo") is not None:
statistics.append({"key": "elpd_loo", "label": "elpd (PSIS-LOO)", "value": values["elpd_loo"]})
if values.get("r2_median") is not None:
statistics.append({"key": "r2_median", "label": "Bayesian R² (median)", "value": values["r2_median"]})
notes = [
f"Outcome model: {label}" + (f"; event: {prep.event}" if prep.event and prep.family == "logistic" else "") + ".",
"Credible intervals are equal-tailed posterior quantiles; P(> 0) is the share of draws above zero.",
"Bayesian results are not p-values and are not entered in the multiple-testing ledger.",
] + fit["notes"]
cs.publish({
"method": "bayes.model",
"methodVersion": "1.0.1",
"title": "Prior predictive check (Stan)" if values["prior_only"] else f"Bayesian {label.lower()} model (Stan)",
"engine": cs.engine_info("pandas", "numpy", "scipy"),
"n": values["n"],
"summary": summary,
"statistics": statistics,
"tables": fit["tables"],
"assumptions": fit["assumptions"],
"warnings": warnings,
"notes": notes,
"values": values,
"charts": fit["charts"],
"artifacts": fit["artifacts"],
})
Shortened for the Academy: 9 chart(s) dropped to stay under 100 KB; 9 chart(s) dropped to stay under 100 KB.
This fit models reaction time over ten days of sleep deprivation, with a varying intercept and slope for each subject. Its largest R-hatR-hat (rank-normalized)Compares the spread between chains with the spread within them; 1.01 or less means the chains agree. is 1.008, its smallest bulk effective sample size 147, and 0 draws ended in a divergent transition. The example keeps only two short chains (600 draws) so it runs fast, which is why its effective sample size falls short of 400; the default four chains of 1,000 draws fix that.
R-hat and effective sample size
ReadStan runs several chains from different starting points.
- R-hat compares the spread between chains with the spread within them. CodeSplash reports the rank-normalized split R-hat (Vehtari et al. 2021): it splits each chain in half, so a chain that drifts is caught, and takes the larger of a bulk and a tail version. Want or less.
- Effective sample size (ESS) says how many independent draws the correlated draws are worth. Bulk ESS covers the center (means, medians), tail ESS the 5% and 95% quantiles that interval ends rest on. Want at least 400 of each.
The same checks by eye
ReadThe trace plot and the rank plot show the same thing by eye: chains that overlap like noise, and rank lines that stay flat near the dashed level.
Check: R-hat and ESS
CheckpointCheck yourself
A fit has R-hat 1.003 for every parameter, but the group standard deviation's tail ESS is 85. You want to report its 95% credible interval. What do you do?
Show the answer
Answer: b
R-hat only says the chains agree with each other. Interval ends are tail quantiles, and 85 effective draws make them wobble from run to run (their Monte Carlo standard error is large). More iterations, or a reparameterization that mixes better, raise tail ESS.
Divergent transitions
ReadNUTS simulates a trajectory through the posterior using its gradient. Where the posterior curves sharply, the simulation can blow up: that's a divergent transitionDivergent transitionA NUTS step whose simulated trajectory blew up: the sampler hit curvature it can't follow, and may miss that region.. The sampler then can't enter that region, so the draws miss it and the estimates are biased, even with a perfect R-hat.
The classic place is Neal's funnelNeal's funnelIn a multilevel model the group effects squeeze together as their SD shrinks; NUTS struggles in the funnel's neck.: in a multilevel model, when a group SD gets small, every group effect squeezes toward zero and the posterior narrows into a neck. The pair plot with divergences shows them piling up there.
What helps with divergences
ReadWhat helps, in order:
- Raise
adapt_delta(for example to 0.95): smaller steps follow tighter curves. The result's remedy row reruns with it. - Give the group SDs a more informative prior, so the neck matters less.
- Simplify the varying effects (a varying slope the data can't support makes a funnel).
Two other rows complete the sampler's report. Tree-depth hits mean slow trajectories, not wrong ones. E-BFMI below in a chain means the momentum refresh can't move through the energy levels, so the tails are explored poorly.
Check: divergences
CheckpointCheck yourself
A varying-intercept model reports 14 divergent transitions out of 4,000 draws, all at small values of the group SD. R-hat and ESS look fine. What's the best first step?
Show the answer
Answer: b
Divergences bunched at small group SDs are the funnel: the sampler is missing that region, so the estimates of the SD (and the shrinkage of every group) are biased upward. Dropping the divergent draws doesn't add the missing region back. Smaller steps, then more prior information, fix the geometry.
Leave-one-out without refitting
ReadPSIS-LOOPSIS-LOOEstimates how well a model predicts each observation left out, from one fit, by reweighting the posterior draws. estimates how well the model predicts each row as if it had been left out, from one fit: it reweights the posterior draws by how much that row pulled them. When one row pulls very hard, the weights get heavy-tailed and the estimate breaks. Pareto k̂Pareto k̂Per row, how heavy-tailed PSIS-LOO's importance weights are; above min(1 − 1/log10(S), 0.7) that row's LOO is unreliable. measures that per row.
In the sleep model above, the largest k̂ is 1.62 against a threshold of 0.64, and the expected log predictive density is −846.7 (SE 22.7). A k̂ above the threshold means at least one row is too influential for importance sampling: the sleep study has a few extreme reaction times, and those rows are the ones to look at.
Check: Pareto k̂
CheckpointCheck yourself
Two models on the same 300 rows: model A has elpd −812 with 2 rows above the k̂ threshold; model B has elpd −790 with 41 rows above it. What can you conclude?
Show the answer
Answer: b
Forty-one rows with high k̂ means importance sampling fails for more than a tenth of the data; B's elpd could be badly off (and B is probably too flexible or misspecified for those rows). Inspect those rows, refit B without them for an exact LOO, or use K-fold cross-validation before comparing.
Testing the whole setup: simulation-based calibration
ReadEvery check so far looks at one fit of one dataset. Simulation-based calibrationCalibration check (simulation-based calibration)Simulated data sets with known true values: if the algorithm is calibrated, each true value's rank in its posterior is uniform. (SBC) checks the computation itself, model, priors and sampler together. It draws parameters from the priors, simulates a dataset from the model with them, fits the model to it, and records the true value's rank among the posterior draws. Repeat many times: if everything is right, the ranks are uniform.
Simulation-based calibration of a Bayesian gaussian model of mpg: 20 datasets simulated from the priors, ranks among 19 posterior draws. Smallest ECDF-test p = 0.315 ((Intercept)) across 4 quantities.
- Smallest ECDF-test p
- 0.3150
- Smallest χ² p
- 0.1117
- Divergent transitions (all fits)
- 0
Plots
View data
| Rank bin | Simulations | 99% band (high) | 99% band (low) | Quantity |
|---|---|---|---|---|
| 1 | 5 | 9 | −0 | (Intercept) |
| 2 | 1 | 9 | −0 | (Intercept) |
| 3 | 8 | 9 | −0 | (Intercept) |
| 4 | 4 | 9 | −0 | (Intercept) |
| 5 | 2 | 9 | −0 | (Intercept) |
| 1 | 2 | 9 | −0 | wt |
| 2 | 8 | 9 | −0 | wt |
| 3 | 4 | 9 | −0 | wt |
| 4 | 3 | 9 | −0 | wt |
| 5 | 3 | 9 | −0 | wt |
| 1 | 3 | 9 | −0 | sigma |
| 2 | 4 | 9 | −0 | sigma |
| 3 | 4 | 9 | −0 | sigma |
| 4 | 5 | 9 | −0 | sigma |
| 5 | 4 | 9 | −0 | sigma |
| 1 | 5 | 9 | −0 | Joint log-likelihood |
| 2 | 4 | 9 | −0 | Joint log-likelihood |
| 3 | 3 | 9 | −0 | Joint log-likelihood |
| 4 | 5 | 9 | −0 | Joint log-likelihood |
| 5 | 3 | 9 | −0 | Joint log-likelihood |
View data
| ECDF − uniform | band high | band low | Quantity | Fractional rank |
|---|---|---|---|---|
| 0 | 0.1500 | −0.0500 | (Intercept) | 0.0500 |
| 0 | 0.2000 | −0.1000 | (Intercept) | 0.1000 |
| 0 | 0.2000 | −0.1500 | (Intercept) | 0.1500 |
| 0.0500 | 0.2500 | −0.2000 | (Intercept) | 0.2000 |
| 0 | 0.2500 | −0.2000 | (Intercept) | 0.2500 |
| 0 | 0.2500 | −0.2500 | (Intercept) | 0.3000 |
| −0.0500 | 0.3000 | −0.2500 | (Intercept) | 0.3500 |
| −0.1000 | 0.3000 | −0.2500 | (Intercept) | 0.4000 |
| −0.1000 | 0.3000 | −0.2500 | (Intercept) | 0.4500 |
| −0.0500 | 0.2500 | −0.2500 | (Intercept) | 0.5000 |
| 0.1000 | 0.2500 | −0.3000 | (Intercept) | 0.5500 |
| 0.1000 | 0.2500 | −0.3000 | (Intercept) | 0.6000 |
| 0.1000 | 0.2500 | −0.3000 | (Intercept) | 0.6500 |
| 0.0500 | 0.2500 | −0.2500 | (Intercept) | 0.7000 |
| 0.1500 | 0.2000 | −0.2500 | (Intercept) | 0.7500 |
| 0.1000 | 0.2000 | −0.2500 | (Intercept) | 0.8000 |
| 0.1500 | 0.1500 | −0.2500 | (Intercept) | 0.8500 |
| 0.1000 | 0.1000 | −0.2000 | (Intercept) | 0.9000 |
| 0.0500 | 0.0500 | −0.1500 | (Intercept) | 0.9500 |
| −0.0500 | 0.1500 | −0.0500 | wt | 0.0500 |
| −0.1000 | 0.2000 | −0.1000 | wt | 0.1000 |
| −0.1500 | 0.2000 | −0.1500 | wt | 0.1500 |
| −0.1000 | 0.2500 | −0.2000 | wt | 0.2000 |
| −0.0500 | 0.2500 | −0.2000 | wt | 0.2500 |
| 0 | 0.2500 | −0.2500 | wt | 0.3000 |
| 0.0500 | 0.3000 | −0.2500 | wt | 0.3500 |
| 0.1000 | 0.3000 | −0.2500 | wt | 0.4000 |
| 0.1000 | 0.3000 | −0.2500 | wt | 0.4500 |
| 0.1500 | 0.2500 | −0.2500 | wt | 0.5000 |
| 0.1000 | 0.2500 | −0.3000 | wt | 0.5500 |
| 0.1000 | 0.2500 | −0.3000 | wt | 0.6000 |
| 0.1000 | 0.2500 | −0.3000 | wt | 0.6500 |
| 0.0500 | 0.2500 | −0.2500 | wt | 0.7000 |
| 0.0500 | 0.2000 | −0.2500 | wt | 0.7500 |
| 0.0500 | 0.2000 | −0.2500 | wt | 0.8000 |
| 0 | 0.1500 | −0.2500 | wt | 0.8500 |
| 0 | 0.1000 | −0.2000 | wt | 0.9000 |
| 0.0500 | 0.0500 | −0.1500 | wt | 0.9500 |
| 0 | 0.1500 | −0.0500 | sigma | 0.0500 |
| 0 | 0.2000 | −0.1000 | sigma | 0.1000 |
| 0 | 0.2000 | −0.1500 | sigma | 0.1500 |
| −0.0500 | 0.2500 | −0.2000 | sigma | 0.2000 |
| 0 | 0.2500 | −0.2000 | sigma | 0.2500 |
| −0.0500 | 0.2500 | −0.2500 | sigma | 0.3000 |
| 0 | 0.3000 | −0.2500 | sigma | 0.3500 |
| −0.0500 | 0.3000 | −0.2500 | sigma | 0.4000 |
| −0.0500 | 0.3000 | −0.2500 | sigma | 0.4500 |
| −0.0500 | 0.2500 | −0.2500 | sigma | 0.5000 |
| −0.0500 | 0.2500 | −0.3000 | sigma | 0.5500 |
| −0.0500 | 0.2500 | −0.3000 | sigma | 0.6000 |
| −0.1000 | 0.2500 | −0.3000 | sigma | 0.6500 |
| −0.0500 | 0.2500 | −0.2500 | sigma | 0.7000 |
| −0.0500 | 0.2000 | −0.2500 | sigma | 0.7500 |
| 0 | 0.2000 | −0.2500 | sigma | 0.8000 |
| 0.0500 | 0.1500 | −0.2500 | sigma | 0.8500 |
| 0 | 0.1000 | −0.2000 | sigma | 0.9000 |
| 0.0500 | 0.0500 | −0.1500 | sigma | 0.9500 |
| 0 | 0.1500 | −0.0500 | Joint log-likelihood | 0.0500 |
| 0.0500 | 0.2000 | −0.1000 | Joint log-likelihood | 0.1000 |
| 0 | 0.2000 | −0.1500 | Joint log-likelihood | 0.1500 |
First 60 of 76 rows.
Assumptions
- Ranks are uniform (SBC) — Smallest ECDF-test p = 0.315 ((Intercept)); 1.000 after a Bonferroni correction for 4 quantities. No sign that the computation misstates the posterior.
- No divergent transitions in the simulation fits — No divergent transitions.
Simulation-based calibration
| Quantity | Mean rank | χ² | df | χ² p | ECDF γ | ECDF test p |
|---|---|---|---|---|---|---|
| (Intercept) | 8.850 | 7.500 | 4 | .112 | 0.0775 | .315 |
| wt | 9.050 | 5.500 | 4 | .240 | 0.0775 | .315 |
| sigma | 9.950 | 0.5000 | 4 | .974 | 0.4752 | .950 |
| Joint log-likelihood | 8.850 | 1 | 4 | .910 | 0.4503 | .910 |
- Each simulated dataset was fitted with the prior scales computed from the observed outcome, so the prior is the same for every fit.
- Ranks count the 19 thinned posterior draws (every 2th) below the true value, so they run from 0 to 19.
- SBC checks the computation (model, priors and sampler together), not how well the model fits your data.
The exact R script · R R version 4.5.1 (2025-06-13)
# Simulation-based calibration (Bayesian model check)
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method bayes.sbc 1.0.0 · spec addabb6f58331e08
# Dataset: mtcars (version 1)
# Runs on its own: outside the app, cs_publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
library(codesplashr) # cs_bayes_run_sbc (twin of codesplash.bayes.sbc) on the shared Stan programs (cs_stan_*)
# %% Data
df <- as.data.frame(nanoparquet::read_parquet("tests/parity/fixtures/mtcars.parquet", col_select = c("mpg", "wt")))
n_input <- nrow(df)
df <- df[stats::complete.cases(df[c("mpg", "wt")]), , drop = FALSE] # listwise deletion
df[] <- lapply(df, function(x) if (is.factor(x)) droplevels(x) else x)
n_dropped <- n_input - nrow(df)
warnings <- list()
if (n_dropped > 0) {
warnings <- c(warnings, cs_warning("CS_W_ROWS_DROPPED", dropped = n_dropped, total = n_input))
}
if (n_input > 0 && nrow(df) == 0) cs_error("CS_E_NO_ROWS")
cs_check_finite_columns(df, c("mpg", "wt")) # infinite values stop the run (registry/errors.yaml)
outcome <- "mpg"
# %% Simulate from the priors, fit every simulated dataset and rank the true values
res <- cs_bayes_run_sbc(
df,
outcome = outcome,
predictors = c("wt"),
family = "gaussian",
trials = NULL,
exposure = NULL,
group = NULL,
slopes = c(),
group2 = NULL,
nesting = "nested",
interactions = "none",
event_level = NULL,
prior_dist = "normal",
prior_scale = 2.5,
prior_df = 3,
autoscale = TRUE,
intercept_scale = 2.5,
lkj = 1,
n_sims = as.integer(20),
posterior_draws = as.integer(19),
thin = as.integer(2),
iter_warmup = as.integer(150),
adapt_delta = 0.8,
max_treedepth = as.integer(10),
bins = as.integer(5),
seed = 1234,
n_null = as.integer(200),
out_dir = cs_ml_out_dir("target/academy-parity/run-20754/bayes.sbc/gaussian-mtcars/out-r")
)
values <- res$values
# %% Result
gp <- vapply(res$rows, function(r) r$gamma_p, 0)
worst <- res$rows[[which.min(gp)]]
summary <- paste0(
sprintf("Simulation-based calibration of a Bayesian %s model of %s: %d datasets simulated from the priors, ranks among %d posterior draws. ",
values$family, outcome, values$n_sims, values$posterior_draws),
sprintf("Smallest ECDF-test p = %.3f (%s) across %d quantities", worst$gamma_p, worst$parameter, values$n_params),
if (values$n_divergent > 0) sprintf("; %d fits had divergent transitions.", values$sims_with_divergences) else "."
)
cs_publish(list(
method = "bayes.sbc",
methodVersion = "1.0.0",
title = "Simulation-based calibration (Stan)",
engine = cs_engine_info("posterior", "loo"),
n = values$n,
summary = summary,
statistics = list(
list(key = "gamma_p_min", label = "Smallest ECDF-test p", value = values$gamma_p_min),
list(key = "chi2_p_min", label = "Smallest χ² p", value = values$chi2_p_min),
list(key = "n_divergent", label = "Divergent transitions (all fits)", value = values$n_divergent, format = "integer")
),
tables = res$tables,
assumptions = res$assumptions,
warnings = warnings,
notes = c(res$notes, "SBC checks the computation (model, priors and sampler together), not how well the model fits your data."),
values = values,
charts = res$charts,
artifacts = res$artifacts
))
Simulation-based calibration of a Bayesian gaussian model of mpg: 20 datasets simulated from the priors, ranks among 19 posterior draws. Smallest ECDF-test p = 0.315 ((Intercept)) across 4 quantities.
- Smallest ECDF-test p
- 0.3150
- Smallest χ² p
- 0.1117
- Divergent transitions (all fits)
- 0
To keep this page light, the plots are shown once, under the R tab.
Assumptions
- Ranks are uniform (SBC) — Smallest ECDF-test p = 0.315 ((Intercept)); 1.000 after a Bonferroni correction for 4 quantities. No sign that the computation misstates the posterior.
- No divergent transitions in the simulation fits — No divergent transitions.
Simulation-based calibration
| Quantity | Mean rank | χ² | df | χ² p | ECDF γ | ECDF test p |
|---|---|---|---|---|---|---|
| (Intercept) | 8.850 | 7.500 | 4 | .112 | 0.0775 | .315 |
| wt | 9.050 | 5.500 | 4 | .240 | 0.0775 | .315 |
| sigma | 9.950 | 0.5000 | 4 | .974 | 0.4752 | .950 |
| Joint log-likelihood | 8.850 | 1 | 4 | .910 | 0.4503 | .910 |
- Each simulated dataset was fitted with the prior scales computed from the observed outcome, so the prior is the same for every fit.
- Ranks count the 19 thinned posterior draws (every 2th) below the true value, so they run from 0 to 19.
- SBC checks the computation (model, priors and sampler together), not how well the model fits your data.
The exact Python script · Python Python 3.13.5 · scipy 1.18.1 · pandas 3.1.0
# Simulation-based calibration (Bayesian model check)
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method bayes.sbc 1.0.0 · spec addabb6f58331e08
# Dataset: mtcars (version 1)
# Runs on its own: outside the app, cs.publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
import numpy as np
import pandas as pd
import codesplash as cs
from codesplash.bayes import sbc # simulation-based calibration (twin of codesplashr cs_bayes_run_sbc)
from codesplash.ml.run import out_dir
# %% Data
df = pd.read_parquet("tests/parity/fixtures/mtcars.parquet", columns=["mpg", "wt"])
n_input = len(df)
df = df.dropna(subset=["mpg", "wt"]) # listwise deletion over the analysis columns
n_dropped = n_input - len(df)
warnings = []
if n_dropped:
warnings.append(cs.cs_warning("CS_W_ROWS_DROPPED", dropped=n_dropped, total=n_input))
if n_input and not len(df):
cs.cs_error("CS_E_NO_ROWS")
cs.check_finite_columns(df, ["mpg", "wt"]) # infinite values stop the run (registry/errors.yaml)
outcome = "mpg"
# %% Simulate from the priors, fit every simulated dataset and rank the true values
res = sbc.run_sbc(
df,
outcome=outcome,
predictors=["wt"],
family="gaussian",
trials=None,
exposure=None,
group=None,
slopes=[],
group2=None,
nesting="nested",
interactions="none",
event_level=None,
prior_dist="normal",
prior_scale=2.5,
prior_df=3,
autoscale=True,
intercept_scale=2.5,
lkj=1,
n_sims=int(20),
posterior_draws=int(19),
thin=int(2),
iter_warmup=int(150),
adapt_delta=0.8,
max_treedepth=int(10),
bins=int(5),
seed=int(1234),
n_null=int(200),
out_dir=out_dir("target/academy-parity/run-20754/bayes.sbc/gaussian-mtcars/out-python"),
)
values = res["values"]
# %% Result
worst = min(res["rows"], key=lambda r: r["gamma_p"])
summary = (
f"Simulation-based calibration of a Bayesian {values['family']} model of {outcome}: {values['n_sims']} datasets simulated "
f"from the priors, ranks among {values['posterior_draws']} posterior draws. Smallest ECDF-test p = {worst['gamma_p']:.3f} "
f"({worst['parameter']}) across {values['n_params']} quantities"
+ (f"; {values['sims_with_divergences']} fits had divergent transitions." if values["n_divergent"] else ".")
)
cs.publish({
"method": "bayes.sbc",
"methodVersion": "1.0.0",
"title": "Simulation-based calibration (Stan)",
"engine": cs.engine_info("pandas", "numpy", "scipy"),
"n": values["n"],
"summary": summary,
"statistics": [
{"key": "gamma_p_min", "label": "Smallest ECDF-test p", "value": values["gamma_p_min"]},
{"key": "chi2_p_min", "label": "Smallest χ² p", "value": values["chi2_p_min"]},
{"key": "n_divergent", "label": "Divergent transitions (all fits)", "value": values["n_divergent"], "format": "integer"},
],
"tables": res["tables"],
"assumptions": res["assumptions"],
"warnings": warnings,
"notes": res["notes"] + ["SBC checks the computation (model, priors and sampler together), not how well the model fits your data."],
"values": values,
"charts": res["charts"],
"artifacts": res["artifacts"],
})
Here the smallest ECDF testECDF test for SBC ranksChecks SBC ranks for uniformity from their empirical CDF, with a band that covers the whole curve at once. p-value across the parameters is 0.315. The shapes say what's wrong when ranks aren't uniform:
- ∪ shape (ranks pile at both ends): posteriors are too narrow, overconfident.
- ∩ shape (ranks pile in the middle): posteriors are too wide.
- A slope: posteriors are shifted up or down.
Check: reading SBC ranks
CheckpointCheck yourself
An SBC run's rank histogram for the residual SD has tall bars in the first and last bins and short bars in the middle. What does it say?
Show the answer
Answer: b
If the posterior were calibrated, the true value would be equally likely to land at any rank. Landing at the extremes too often means the posterior draws cluster too tightly around the wrong place: overconfidence. Look for a prior or likelihood mismatch, or a sampler problem (divergences in the simulation fits).
Check yourself
Your model passes SBC. Does that mean it fits your data?
Show the answer
Answer: b
SBC simulates every dataset from the model, so it can't notice that real data look different. It tells you the sampler recovers the parameters when the model is true; posterior predictive checks and leave-one-out tell you whether the model is a good description of the data you have.
Try it
In the appIn Studios, open Bayesian models (or find Bayesian regression model (Stan) in Analyze → Bayesian tests) and fit the sleep study with Subject as the grouping factor and Days as a varying slope. Read the convergence summary and the pair plot with divergences, then rerun with the remedy if there are any. Then run Simulation-based calibration with the same roles and look at the rank histograms.
Recap
Recap- R-hat at most and at least 400 effective draws in the bulk and the tails, before reading estimates.
- Divergent transitions bias results even with good R-hat; raise adapt_delta, then add prior information.
- Trust PSIS-LOO only for rows with Pareto k̂ under the threshold.
- SBC checks the computation; predictive checks and LOO check the fit.
Concepts in this lesson