AI-generated. An AI assistant drafted this page, and no maintainer has reviewed it yet. Please report errors on the issue tracker.
Diagnostics¶
The diagnostic functions compute a diagnostic of a posterior and record it in the posterior's annotations, and posterior.diagnostics returns a view that reads them.
This page documents the MCMC, predictive, and leave-one-out diagnostics and the views, and the checks of an approximation against a reference posterior are on Validation.
MCMC diagnostics¶
probpipe.diagnostics.add_rhat(posterior, *, method='rank', threshold=_RHAT_THRESHOLD, force=False)
¶
Compute R-hat and attach to _annotations/diagnostics/mcmc/.
This is the in-place wrapper around _compute_rhat_op.
Skips computation if R-hat is already present unless force=True.
Emits one warning per parameter with R-hat > threshold, reporting a
scalar parameter's R-hat, or an array parameter's number of coordinates
above the threshold and its maximum R-hat.
Source code in probpipe/diagnostics/_mcmc.py
probpipe.diagnostics.add_ess(posterior, *, threshold=_ESS_THRESHOLD, force=False)
¶
Compute bulk and tail ESS and attach to _annotations/diagnostics/mcmc/.
This is the in-place wrapper around _compute_ess_op.
Skips computation if ESS is already present unless force=True.
Emits one warning per parameter and ESS kind below threshold, reporting
a scalar parameter's ESS, or an array parameter's number of coordinates
below the threshold and its minimum ESS.
Source code in probpipe/diagnostics/_mcmc.py
probpipe.diagnostics.add_mcse(posterior, *, force=False)
¶
Compute MCSE and attach to _annotations/diagnostics/mcmc/.
This is the in-place wrapper around _compute_mcse_op.
Skips computation if MCSE is already present unless force=True.
Source code in probpipe/diagnostics/_mcmc.py
probpipe.diagnostics.add_mcmc_diagnostics(posterior, *, metrics=None, rhat_method='rank', rhat_threshold=_RHAT_THRESHOLD, ess_threshold=_ESS_THRESHOLD, force=False)
¶
Compute MCMC diagnostics and attach to _annotations/diagnostics/mcmc/.
Computes R-hat, bulk ESS, tail ESS, and MCSE by default, and records the
number of divergent transitions when the run's sample statistics hold the
ArviZ variable diverging, which posterior.diagnostics.mcmc.n_divergences
reads. Skips any metric already present unless force=True.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
posterior
|
EmpiricalDistribution
|
The fitted posterior. Mutated in place. |
required |
metrics
|
str, sequence of str, or None
|
The metrics to compute, among |
None
|
rhat_method
|
str
|
ArviZ R-hat variant: |
'rank'
|
rhat_threshold
|
float
|
R-hat warning threshold. |
_RHAT_THRESHOLD
|
ess_threshold
|
int
|
ESS warning threshold. |
_ESS_THRESHOLD
|
force
|
bool
|
Recompute even if metrics are already stored. |
False
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If metrics is not a str, a sequence of str, or |
ValueError
|
If metrics names an unknown metric. |
Source code in probpipe/diagnostics/_mcmc.py
Predictive and leave-one-out diagnostics¶
probpipe.diagnostics.add_ppc(posterior, test_fns, observed_data=None, *, kernel, n_replications=500)
¶
Compute a PPC and write results into posterior._annotations.
This is the in-place wrapper around _ppc_op. Calls _ppc_op
and writes the resulting payload into posterior._annotations,
then returns None. The replications are draws of the kernel's event
from kernel * posterior, as predictive_check
draws them, and every test statistic is computed on the same replications.
The replications are one workflow-owned random event of the enclosing
workflow scope. A call inside workflow_run(seed=...) therefore
reproduces them, and a call outside every scope draws fresh replications.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
posterior
|
Distribution
|
Prior or posterior over the kernel's inputs. |
required |
test_fns
|
callable or sequence of callables
|
One or more test statistics mapping data to a scalar. |
required |
observed_data
|
optional
|
If provided, the statistics of the observed data and their p-values
are computed. It takes the forms
|
None
|
kernel
|
ConditionalDistribution
|
The law of the observations given the parameters, such as the
likelihood of a model |
required |
n_replications
|
int
|
Number of replicated datasets. |
500
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If kernel is not a |
ValueError
|
If posterior has no output for some input of kernel, naming the missing inputs; if posterior has an output of the same name as an output of kernel; if test_fns is empty or holds two test functions of the same name; or if n_replications is not positive. Use distinct named functions instead of multiple lambdas so result keys cannot collide. |
Source code in probpipe/diagnostics/_ppc_spc.py
probpipe.diagnostics.add_loo(posterior, *, log_likelihood=None, model=None, data=None, var_name='y', pointwise=True, scale=None, reff=None, force=False, store_pointwise=True)
¶
Compute PSIS-LOO and attach results to posterior._annotations.
This function mutates posterior._annotations in place and returns
None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
posterior
|
Distribution
|
Posterior distribution whose ArviZ-compatible xarray DataTree data are
stored under |
required |
log_likelihood
|
optional
|
Pointwise log likelihood. If provided, it is written to
|
None
|
model
|
Distribution
|
The factored joint the posterior was conditioned from, such as
|
None
|
data
|
Record or Mapping
|
The observed values the posterior was conditioned on, keyed by field, at which model scores each observation. |
None
|
var_name
|
str
|
Variable name to use when converting raw log-likelihood arrays into an xarray Dataset. |
'y'
|
pointwise
|
bool
|
Passed to |
True
|
scale
|
str or None
|
Optional ArviZ scale argument, e.g. |
None
|
reff
|
float or None
|
Optional relative MCMC efficiency passed to |
None
|
force
|
bool
|
If False and |
False
|
store_pointwise
|
bool
|
If True, store pointwise |
True
|
Returns:
| Type | Description |
|---|---|
None
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If log_likelihood holds a one-dimensional array, or if the annotations hold no pointwise log likelihood and model and data are not both given. |
TypeError
|
If model at data has no likelihood factor that scores each
observation, or if reff is |
Source code in probpipe/diagnostics/_loo.py
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 | |
Views¶
probpipe.diagnostics.DiagnosticsView(tree)
¶
Bases: DataTreeView
Structured read-only accessor over the /diagnostics subtree.
Source code in probpipe/diagnostics/_view_base.py
summary_table()
¶
Pretty-printed table of MCMC metrics for notebook display.
The table ends with the warnings, and then with a line that reports the run as not passed when a transition diverged or an R-hat is not computed, or else, when no warning was issued, as passed.
Source code in probpipe/diagnostics/_views.py
to_dict()
¶
JSON-serializable summary of all diagnostics.
Source code in probpipe/diagnostics/_views.py
probpipe.diagnostics.MCMCView(tree)
¶
Bases: DatasetView
Structured read-only accessor for /diagnostics/mcmc.
Source code in probpipe/diagnostics/_view_base.py
probpipe.diagnostics.PPCView(tree)
¶
Bases: DatasetView
Structured read-only accessor for /diagnostics/runs/ppc.
Source code in probpipe/diagnostics/_view_base.py
result
property
¶
PPC result keyed by test function.
Example
{ "var_mean_ratio": { "p_value": 0.43, "observed": 3.2, } }
probpipe.diagnostics.LOOView(tree)
¶
Bases: DatasetView
Structured read-only accessor for /diagnostics/runs/loo.
Source code in probpipe/diagnostics/_view_base.py
probpipe.diagnostics.DiagnosticRunView(name, tree)
¶
Bases: DatasetView
Generic accessor for one /diagnostics/runs/<name>/ node.
This class is generic. It does not know whether the run is PPC, LOO, etc.
Source code in probpipe/diagnostics/_view_base.py
result
property
¶
The run's variables as a plain Python dict keyed by variable name.
A scalar variable becomes its value, as read_scalar reads it. A
one-dimensional variable becomes the dict that read_indexed
reads, such as {"0": 0.12, "1": 0.31} for the pointwise
pareto_k of a LOO run. A variable of two or more dimensions becomes
NotComputed. The dict is empty when the run's node holds no
dataset.
probpipe.diagnostics.NotComputed(reason)
¶
View helpers¶
probpipe.diagnostics.views.DataTreeView(tree)
¶
Generic read-only view over an xarray DataTree node.
This base class intentionally contains only generic DataTree navigation helpers. Concrete diagnostic semantics belong in subclasses.
Source code in probpipe/diagnostics/_view_base.py
exists
property
¶
Whether this view points to an existing tree node.
attrs
property
¶
Node attributes, or an empty dict.
children
property
¶
Child nodes, or an empty dict.
has_child(name)
¶
child(name)
¶
Return child DataTree node or None.
dataset()
¶
Return this node's dataset, if available.
Source code in probpipe/diagnostics/_view_base.py
probpipe.diagnostics.views.DatasetView(tree)
¶
Bases: DataTreeView
Generic view over a DataTree node that is expected to hold a Dataset.
Source code in probpipe/diagnostics/_view_base.py
probpipe.diagnostics.views.read_scalar(da, *, label=None)
¶
Extract one scalar value from an xarray DataArray-like object.
Returns NotComputed instead of raising when the value is missing,
non-scalar, NaN, or marked with a not_computed attr.
Source code in probpipe/diagnostics/_view_base.py
probpipe.diagnostics.views.read_indexed(ds, field, *, dim)
¶
Read a one-dimensional variable of a dataset as a dict keyed by its labels.
Each element is keyed by the string form of its coordinate value along
dim, or of its position when dim has no coordinate. Its value is read
with read_scalar.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ds
|
Dataset or None
|
The dataset that holds the variable. |
required |
field
|
str
|
The name of the variable. |
required |
dim
|
str
|
The dimension that indexes the variable. |
required |
Returns:
| Type | Description |
|---|---|
dict of str to float or NotComputed
|
One entry per element, in the order of dim. The dict is empty when
ds is |
Examples:
A variable p_value over the coordinate
test_fn = ["var_mean_ratio", "zero_fraction"] reads as
{"var_mean_ratio": 0.43, "zero_fraction": 0.81}. A variable
pareto_k over the integer coordinate obs = [0, 1] reads as
{"0": 0.12, "1": 0.31}.
Source code in probpipe/diagnostics/_view_base.py
probpipe.diagnostics.views.read_json_attr(attrs, key, default=None)
¶
Read a JSON-encoded attribute safely.