quivers.diagnostics.predictive_checks¶
A posterior predictive check compares a statistic of the observations with its distribution over replicated data (Gelman et al., 1996; Gelman et al., 2013). An observed upper quantile beyond the range of the replicated ones, for instance, exposes a poorly modeled tail. The module gives posterior predictive p-values and leave-one-out probability integral transform (LOO-PIT) checks of calibration for held-out observations.
predictive_checks
¶
Posterior-predictive checks (PPCs) and calibration diagnostics.
ArviZ ships arviz.plot_ppc, arviz.loo_pit, and a
broad family of PPC visualisations. This module exposes a small
typed surface that delegates to those, and adds a registry of
common test statistics that map cleanly onto quivers' plate-and-
group structure (per-Verb mean, per-Subject sd, etc.).
STATISTICS
module-attribute
¶
STATISTICS: dict[str, Callable[[ndarray], float]] = {'mean': lambda v: float(np.mean(v)), 'median': lambda v: float(np.median(v)), 'sd': lambda v: float(np.std(v, ddof=1)), 'var': lambda v: float(np.var(v, ddof=1)), 'min': lambda v: float(np.min(v)), 'max': lambda v: float(np.max(v)), 'mad': lambda v: float(1.4826 * np.median(np.abs(v - np.median(v)))), 'IQR': lambda v: float(np.subtract(*np.quantile(v, [0.75, 0.25]))), 'q05': lambda v: float(np.quantile(v, 0.05)), 'q95': lambda v: float(np.quantile(v, 0.95))}
The built-in scalar test statistics T(y) -> scalar, keyed by the name
that posterior_predictive_check and the ppc_* tables accept. They follow
R's conventions: sd and var divide by n - 1, mad is scaled by
1.4826, and quantiles are type 7.
posterior_predictive_check
¶
posterior_predictive_check(idata: DataTree, *, observed_name: str, statistic: str | Callable[[ndarray], float] = 'mean', by: str | None = None) -> Mapping[str, float | ndarray | str]
Compute a posterior-predictive p-value (PPP-value) for a user-chosen test statistic.
| PARAMETER | DESCRIPTION |
|---|---|
idata
|
Fit produced by
TYPE:
|
observed_name
|
Name of the observed site (must appear in both groups).
TYPE:
|
statistic
|
Either a key into
TYPE:
|
by
|
If given, computes the statistic per group along the named
dim (e.g.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Mapping[str, float or ndarray]
|
|
Source code in src/quivers/diagnostics/predictive_checks.py
loo_pit
¶
Leave-one-out probability-integral-transform calibration.
Delegates to arviz.loo_pit. The canonical use of the PIT values
is arviz.plot_loo_pit for the calibration diagnostic plot.
| PARAMETER | DESCRIPTION |
|---|---|
idata
|
Fit produced by
TYPE:
|
observed_name
|
Name of the observed site, which must appear in those groups.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dataset
|
The LOO-PIT value at each observed data point, under the
variable |
Source code in src/quivers/diagnostics/predictive_checks.py
The data behind bayesplot's checks¶
The ppc_* functions of quivers.diagnostics compute the tables bayesplot draws its
ppc_* plots from (Gabry et al., 2019), with bayesplot's column names
and row order. pp_check on a fit selects the draws and data as brms does
(see the analysis guide).
_ppc_data
¶
The data behind graphical posterior predictive checks, as bayesplot computes it.
brms pp_check [@burkner2017brms] hands observed responses y and
predictive draws yrep (an S x N matrix) to a bayesplot ppc_*
function [@gabry2019visualization]. Each plot is drawn from a data frame that
bayesplot computes first: the long table of observed and replicated values,
test statistics of every replicate, predictive quantile intervals, predictive
errors and their binned averages, frequency tables of discrete values, the
rootogram table [@kleiber2016visualizing], the leave-one-out probability
integral transform (LOO-PIT) under PSIS weights, Kaplan-Meier curves, and the
empirical CDF of PIT values [@sailynoja2022graphical]. The functions here
return those tables with bayesplot's column names and row order, so they can
be plotted with any library or checked numerically. Quantiles are R's type 7.
STATISTICS
module-attribute
¶
STATISTICS: dict[str, Callable[[ndarray], float]] = {'mean': lambda v: float(np.mean(v)), 'median': lambda v: float(np.median(v)), 'sd': lambda v: float(np.std(v, ddof=1)), 'var': lambda v: float(np.var(v, ddof=1)), 'min': lambda v: float(np.min(v)), 'max': lambda v: float(np.max(v)), 'mad': lambda v: float(1.4826 * np.median(np.abs(v - np.median(v)))), 'IQR': lambda v: float(np.subtract(*np.quantile(v, [0.75, 0.25]))), 'q05': lambda v: float(np.quantile(v, 0.05)), 'q95': lambda v: float(np.quantile(v, 0.95))}
The built-in scalar test statistics T(y) -> scalar, keyed by the name
that posterior_predictive_check and the ppc_* tables accept. They follow
R's conventions: sd and var divide by n - 1, mad is scaled by
1.4826, and quantiles are type 7.
Statistic
¶
A test statistic: a name of STATISTICS or a function of one vector.
GroupValues
¶
Group labels, one per observation.
ppc_data
¶
ppc_data(y: ndarray | None, yrep: ndarray, *, group: GroupValues | None = None) -> DataFrame
Return the long table of observed and replicated values (ppc_data).
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observed values, or None for
TYPE:
|
yrep
|
Draws
TYPE:
|
group
|
Group label per observation.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
Replicated rows, replicate fastest, then the observed rows, with
|
Source code in src/quivers/diagnostics/_ppc_data.py
ppc_stat_data
¶
ppc_stat_data(y: ndarray | None, yrep: ndarray, *, stat: Statistic | tuple[Statistic, Statistic] = 'mean', group: GroupValues | None = None) -> DataFrame
Return test statistics of the observed and each replicated data set.
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observed values, or None for
TYPE:
|
yrep
|
Draws
TYPE:
|
stat
|
One statistic, or two for
TYPE:
|
group
|
Group label per observation; statistics are computed within groups.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
|
Source code in src/quivers/diagnostics/_ppc_data.py
ppc_intervals_data
¶
ppc_intervals_data(y: ndarray | None, yrep: ndarray, *, x: Sequence[float] | ndarray | None = None, group: GroupValues | None = None, prob: float = 0.5, prob_outer: float = 0.9) -> DataFrame
Return predictive quantile intervals per observation (ppc_intervals_data).
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observed values, or None for
TYPE:
|
yrep
|
Draws
TYPE:
|
x
|
Horizontal coordinate; the observation index by default.
TYPE:
|
group
|
Group label per observation.
TYPE:
|
prob
|
Inner and outer interval probabilities.
TYPE:
|
prob_outer
|
Inner and outer interval probabilities.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the probabilities are outside (0, 1]. |
Source code in src/quivers/diagnostics/_ppc_data.py
predictive_errors
¶
Return predictive errors y - yrep (rstantools predictive_error).
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observed values.
TYPE:
|
yrep
|
Draws
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ndarray
|
Errors |
Source code in src/quivers/diagnostics/_ppc_data.py
ppc_error_data
¶
ppc_error_data(y: ndarray, yrep: ndarray, *, group: GroupValues | None = None) -> DataFrame
Return the long table of predictive errors (ppc_error_data).
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observed values.
TYPE:
|
yrep
|
Draws
TYPE:
|
group
|
Group label per observation.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
|
Source code in src/quivers/diagnostics/_ppc_data.py
ppc_error_binned_data
¶
Return binned errors per draw (ppc_error_binned).
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observed values.
TYPE:
|
epred
|
Expected values
TYPE:
|
bins
|
Bins; bayesplot's default by sample size.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
|
Source code in src/quivers/diagnostics/_ppc_data.py
ppc_bars_data
¶
ppc_bars_data(y: ndarray, yrep: ndarray, *, group: GroupValues | None = None, prob: float = 0.9, freq: bool = True) -> DataFrame
Return frequency tables of discrete values (ppc_bars_data).
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observed whole numbers.
TYPE:
|
yrep
|
Draws
TYPE:
|
group
|
Group label per observation.
TYPE:
|
prob
|
Interval probability of the replicated counts.
TYPE:
|
freq
|
Counts; False gives proportions within each data set and group.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If values are not whole numbers. |
Source code in src/quivers/diagnostics/_ppc_data.py
ppc_rootogram_data
¶
ppc_rootogram_data(y: ndarray, yrep: ndarray, *, style: str = 'standing', prob: float = 0.9) -> DataFrame
Return the rootogram table of count data (ppc_rootogram).
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observed counts.
TYPE:
|
yrep
|
Draws
TYPE:
|
style
|
Rootogram style [@kleiber2016visualizing].
TYPE:
|
prob
|
Interval probability of the replicated square-root frequencies.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If values are not nonnegative whole numbers or the style is unknown. |
Source code in src/quivers/diagnostics/_ppc_data.py
ppc_scatter_data
¶
Return observed values against each replicate (ppc_scatter_data).
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observed values.
TYPE:
|
yrep
|
Draws
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
|
Source code in src/quivers/diagnostics/_ppc_data.py
ppc_scatter_avg_data
¶
ppc_scatter_avg_data(y: ndarray, yrep: ndarray, *, group: GroupValues | None = None) -> DataFrame
Return observed values against the mean replicate (ppc_scatter_avg_data).
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observed values (or a covariate for
TYPE:
|
yrep
|
Draws
TYPE:
|
group
|
Group label per observation.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
|
Source code in src/quivers/diagnostics/_ppc_data.py
loo_pit_values
¶
Return LOO-PIT values from PSIS log weights (rstantools loo_pit).
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observed values.
TYPE:
|
yrep
|
Draws
TYPE:
|
log_weights
|
Normalized PSIS log weights
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ndarray
|
:math: |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the weights do not match the draws. |
Source code in src/quivers/diagnostics/_ppc_data.py
km_data
¶
Return Kaplan-Meier curves of observed and replicated data (ppc_km_overlay).
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observed times.
TYPE:
|
yrep
|
Draws
TYPE:
|
status
|
Event indicator of each observed time (one event, zero right censored).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If a status is not zero or one. |
Source code in src/quivers/diagnostics/_ppc_data.py
pit_values
¶
Return posterior predictive PIT values, as bayesplot ppc_pit_ecdf.
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observed values.
TYPE:
|
yrep
|
Draws
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ndarray
|
The share of draws with :math: |
Source code in src/quivers/diagnostics/_ppc_data.py
pit_ecdf_data
¶
Return PIT values' empirical CDF on a uniform grid (ppc_pit_ecdf).
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observed values.
TYPE:
|
yrep
|
Draws
TYPE:
|
K
|
Grid size;
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
|
Source code in src/quivers/diagnostics/_ppc_data.py
Cited sources¶
Andrew Gelman, Xiao-Li Meng, and Hal Stern. Posterior predictive assessment of model fitness via realized discrepancies. Statistica Sinica, 6(4):733–807, 1996. With discussion. URL: https://www3.stat.sinica.edu.tw/statistica/J6N4/J6N41/J6N41.htm.
Andrew Gelman, John B. Carlin, Hal S. Stern, David B. Dunson, Aki Vehtari, and Donald B. Rubin. Bayesian Data Analysis. Chapman and Hall/CRC, 3 edition, 2013. ISBN 9781439840955. URL: https://www.routledge.com/Bayesian-Data-Analysis/Gelman-Carlin-Stern-Dunson-Vehtari-Rubin/p/book/9781439840955.
Jonah Gabry, Daniel Simpson, Aki Vehtari, Michael Betancourt, and Andrew Gelman. Visualization in Bayesian Workflow. Journal of the Royal Statistical Society Series A: Statistics in Society, 182(2):389–402, 2019. doi:10.1111/rssa.12378.