quivers.diagnostics¶
Adapter to the ArviZ Bayesian-analysis ecosystem. Converts quivers'
inference records into xarray.DataTree objects consumable by
ArviZ functions that accept this container, including plot_trace,
plot_forest, plot_ppc, loo, compare, and hdi.
diagnostics
¶
Adapter to the ArviZ Bayesian-analysis ecosystem.
Quivers' inference layer produces quivers.inference.MCMCResult
records and quivers.inference.guides.base.Guide fits. The
canonical 2026 Bayesian-analysis library is ArviZ,
whose
xarray.DataTree-based
data model is the lingua franca for posterior-summary, diagnostic,
posterior-predictive, model-comparison, and calibration tooling.
The functions in this subpackage convert quivers fits into ArviZ
DataTrees and wrap the canonical ArviZ entry points
(arviz.compare, arviz.loo, arviz.hdi,
arviz.plot_ppc) with shape-aware, quivers-typed signatures.
No posterior-analysis primitives are reimplemented here; ArviZ owns
the analytics.
STATISTICS
module-attribute
¶
STATISTICS: dict[str, Callable[[ndarray], float]] = {'mean': lambda y: float(np.mean(y)), 'median': lambda y: float(np.median(y)), 'sd': lambda y: float(np.std(y)), 'var': lambda y: float(np.var(y)), 'min': lambda y: float(np.min(y)), 'max': lambda y: float(np.max(y)), 'q05': lambda y: float(np.quantile(y, 0.05)), 'q95': lambda y: float(np.quantile(y, 0.95))}
The built-in scalar test statistics T(y) -> scalar, keyed by the
name posterior_predictive_check accepts for its statistic argument.
to_datatree
¶
to_datatree(posterior: MCMCResult, *, observed_data: Mapping[str, Tensor] | None = None, posterior_predictive: Mapping[str, Tensor] | None = None, log_likelihood: Mapping[str, Tensor] | None = None, constant_data: Mapping[str, Tensor] | None = None, coords: Mapping[str, list] | None = None, dims: Mapping[str, list[str]] | None = None) -> DataTree
Convert an MCMCResult into an ArviZ-style DataTree.
| PARAMETER | DESCRIPTION |
|---|---|
posterior
|
Sampler output.
TYPE:
|
observed_data
|
Site name to observed tensor (the original data used at
fit time). Becomes the
TYPE:
|
posterior_predictive
|
Site name to posterior-predictive draws of shape
TYPE:
|
log_likelihood
|
Site name to per-observation log-likelihood of shape
TYPE:
|
constant_data
|
Site name to fixed covariate tensor (e.g. design matrix).
Becomes the
TYPE:
|
coords
|
Coordinate values per named axis (e.g.
TYPE:
|
dims
|
Per-site axis names (e.g.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataTree
|
Canonical ArviZ DataTree consumable by every plotting and diagnostic function in the ArviZ 1.x API. |
Source code in src/quivers/diagnostics/arviz_io.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | |
to_datatree_any
¶
to_datatree_any(fit: MCMCResult | Mapping[str, Tensor] | tuple[Mapping[str, Tensor], Tensor], *, observed_data: Mapping[str, Tensor] | None = None, posterior_predictive: Mapping[str, Tensor] | None = None, log_likelihood: Mapping[str, Tensor] | None = None, constant_data: Mapping[str, Tensor] | None = None, coords: Mapping[str, list] | None = None, dims: Mapping[str, list[str]] | None = None) -> DataTree
Dispatch a DataTree conversion on the fit type.
Accepts:
- an
MCMCResult, converted byto_datatreewith its chain and draw semantics; - a mapping of site name to tensor (SVI or Predictive draws),
converted by
to_datatree_from_sviwith a single pseudo-chain; - a tuple
(samples, log_densities), converted like the mapping form withsample_stats/lppopulated from the second entry.
Consumers with a custom fit-container type can call the specific
to_datatree or to_datatree_from_svi entry point directly.
| PARAMETER | DESCRIPTION |
|---|---|
fit
|
The fit to convert, in one of the forms above.
TYPE:
|
observed_data
|
Site name to observed tensor; becomes
TYPE:
|
posterior_predictive
|
Site name to posterior-predictive draws; becomes
TYPE:
|
log_likelihood
|
Site name to per-observation log-likelihood; becomes
TYPE:
|
constant_data
|
Site name to fixed covariate tensor; becomes
TYPE:
|
coords
|
Coordinate values per named axis, forwarded to ArviZ.
TYPE:
|
dims
|
Per-site axis names, forwarded to ArviZ.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataTree
|
The ArviZ DataTree of the fit. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If |
Source code in src/quivers/diagnostics/arviz_io.py
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 | |
to_datatree_from_svi
¶
to_datatree_from_svi(samples: Mapping[str, Tensor], *, log_densities: Tensor | None = None, observed_data: Mapping[str, Tensor] | None = None, posterior_predictive: Mapping[str, Tensor] | None = None, log_likelihood: Mapping[str, Tensor] | None = None, constant_data: Mapping[str, Tensor] | None = None, coords: Mapping[str, list] | None = None, dims: Mapping[str, list[str]] | None = None) -> DataTree
Convert a set of SVI or Predictive draws into an ArviZ-style DataTree with a single pseudo-chain.
Each entry in samples is a (num_samples, *site_shape) tensor
(the standard shape produced by Predictive(model, guide,
num_samples=...)). A leading chain dimension of size 1 is
inserted so the resulting DataTree matches the (chain, draw,
*event) shape ArviZ expects. log_densities, when supplied,
becomes sample_stats/lp; ArviZ diagnostic functions (loo,
waic, plot_trace) work uniformly on the resulting object.
| PARAMETER | DESCRIPTION |
|---|---|
samples
|
Site name to draws of shape
TYPE:
|
log_densities
|
Per-draw log densities; becomes
TYPE:
|
observed_data
|
Site name to observed tensor; becomes
TYPE:
|
posterior_predictive
|
Site name to posterior-predictive draws of shape
TYPE:
|
log_likelihood
|
Site name to per-observation log-likelihood of shape
TYPE:
|
constant_data
|
Site name to fixed covariate tensor; becomes
TYPE:
|
coords
|
Coordinate values per named axis, forwarded to ArviZ.
TYPE:
|
dims
|
Per-site axis names, forwarded to ArviZ.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataTree
|
The ArviZ DataTree, with a chain dimension of size 1. |
Source code in src/quivers/diagnostics/arviz_io.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | |
compare
¶
compare(fits: Mapping[str, DataTree], *, method: Literal['stacking', 'BB-pseudo-BMA', 'pseudo-BMA'] = 'stacking', var_name: str | None = None, reference: str | None = None, round_to: int | Literal['auto', 'none', 'None'] | None = 'auto') -> object
Rank candidate models by expected log predictive density.
Delegates to ArviZ, which computes PSIS-LOO on each fit's log-likelihood group and combines the results into a ranked comparison table.
| PARAMETER | DESCRIPTION |
|---|---|
fits
|
Per-model fits produced by quivers.diagnostics.to_datatree. Every fit must carry a log-likelihood group.
TYPE:
|
method
|
Model-weight estimator. Default "stacking" follows Yao, Vehtari, Simpson, and Gelman (2018).
TYPE:
|
var_name
|
Observed variable in the log-likelihood group; required when a fit carries multiple log-likelihood variables.
TYPE:
|
reference
|
Fit name used as the reference for ELPD differences. Default is the top-ranked model.
TYPE:
|
round_to
|
ArviZ rounding policy. Default "auto" retains display rounding. An integer specifies decimal places, while "none" or "None" preserves full numeric precision. None uses ArviZ's configured default.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
ArviZ ranking table with one row per model. |
Source code in src/quivers/diagnostics/comparison.py
loo
¶
Estimate expected log predictive density with PSIS-LOO.
This thin wrapper delegates all computation to ArviZ. It preserves ArviZ's canonical log scale and, with pointwise=True, returns its per-observation elpd_i and pareto_k arrays unchanged, including nonfinite Pareto estimates.
| PARAMETER | DESCRIPTION |
|---|---|
fit
|
Fit containing posterior and log-likelihood groups.
TYPE:
|
pointwise
|
Whether to include one elpd_i and pareto_k value per observation. None uses ArviZ's configured default.
TYPE:
|
var_name
|
Log-likelihood variable to evaluate. Required when the log-likelihood group contains multiple variables.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ELPDData
|
ArviZ's canonical PSIS-LOO result. |
Source code in src/quivers/diagnostics/comparison.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
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]
|
|