symphony_dt.calibration¶
symphony_dt.calibration ¶
Calibration: simulation-based calibration and posterior predictive checks.
IdentifiabilityReport
dataclass
¶
Contraction and, when true values are known, posterior z-scores per parameter.
table has one row per parameter with the prior and posterior sd, the posterior mean, the
contraction, the z-score and a flag. The flag holds "weak" for contraction below weak and
"off truth" for |z| above z_limit, joined by "; " when both apply, or is empty.
Validation: TESTED.
SBCDesign
dataclass
¶
An SBC design: how to draw a truth and data, and how to fit.
generator(rng) returns (data, truth) with truth a dict of named scalars. fitter(data, seed)
returns a dict of named posterior draws (one array per parameter) or None for a failed fit.
For the posterior regime, the fitting prior must equal the generating distribution; set
fitting_prior_matches_generator=True only when that is true by construction.
Validation: TESTED. The posterior regime is EXPERIMENTAL.
SBCResult
dataclass
¶
Ranks per replicate and parameter, failures, uniformity tests and the verdict.
Validation: TESTED.
SBCVerdict
dataclass
¶
identifiability_report ¶
identifiability_report(
posterior: Any,
prior: Mapping[str, ArrayLike],
truth: Mapping[str, float] | None = None,
*,
weak: float = WEAK_CONTRACTION,
z_limit: float = Z_LIMIT,
) -> IdentifiabilityReport
Score contraction, and z-scores when truth is given, for every parameter in prior.
prior maps Stan names to prior draws, for example from pd_v2_prior_draws. posterior is a
FitResult or a mapping from the same names to posterior draws. A parameter missing from the
posterior is an error, so a renamed parameter cannot drop out of the report unseen.
Validation: TESTED.
pd_v2_prior_draws ¶
pd_v2_prior_draws(
n_draws: int = 4000,
*,
seed: int = 0,
hyper: Mapping[str, Any] | None = None,
) -> dict[str, NDArray[np.float64]]
Return draws of every global pd_v2 parameter from its prior, keyed by Stan name.
Names follow Stan's convention, such as mu_r[1] and Gamma[2,3]. The draws come from
draw_prior, so they match the Stan program's priors, half-normals included. Pathway loadings
are left out because the primary model has no pathway covariates.
Validation: TESTED.
posterior_contraction ¶
posterior_contraction(
prior_draws: ArrayLike, posterior_draws: ArrayLike
) -> float
Return 1 - var(posterior) / var(prior) for one parameter.
Both arguments are draws of the same scalar; any shape is flattened. The result is NaN when the prior variance is zero. It can be negative when the posterior is wider than the prior.
Validation: TESTED.
posterior_z_score ¶
posterior_z_score(
posterior_draws: ArrayLike, truth: float
) -> float
Return (posterior mean - truth) / posterior sd for one parameter.
Validation: TESTED.
boundary_statistics ¶
boundary_statistics(
draws: ArrayLike,
y: ArrayLike,
lower: float,
upper: float,
*,
round_to_integer: bool = False,
) -> dict[str, Any]
Compare the observed share of values at each scale boundary with the replicated share.
Returns the observed share, the replicated mean share and its 95% interval across draws, at the floor and at the ceiling.
Validation: TESTED.
pit_values ¶
pit_values(
draws: ArrayLike, y: ArrayLike, *, seed: int = 0
) -> np.ndarray
Return randomised probability integral transform values, one per observation.
PIT = P(X < y) + U x P(X = y), with U uniform, so ties at a scale boundary do not pile up.
draws is (draws, observations). Uniform PIT values indicate calibration.
Validation: TESTED.
ppc_coverage ¶
ppc_coverage(
draws: ArrayLike,
y: ArrayLike,
levels: Sequence[float] = (0.5, 0.95),
) -> dict[str, float]
Return the share of observations inside the inclusive central predictive interval per level.
Validation: TESTED.
ppc_summary ¶
ppc_summary(
draws: ArrayLike,
stan_data: Mapping[str, Any],
*,
round_endpoints: Sequence[int] = (1, 2),
pit_bins: int = 10,
) -> dict[str, Any]
Summarise posterior predictive draws per endpoint: coverage, boundaries and a PIT histogram.
draws is (draws, n_obs), for example y_rep_c from the GQ program run on the fitted data.
Endpoints in round_endpoints are rounded to integers before the boundary comparison, as the
observed scores are integers. Aggregates only.
Validation: TESTED.
chi_squared_uniformity ¶
chi_squared_uniformity(
ranks: ArrayLike,
n_draws: int,
bins: int = N_BINS,
alpha: float | None = None,
) -> dict[str, Any]
Test rank uniformity by chi-squared over bins equal bins.
alpha is the per-parameter threshold; None uses the family level, which is right only for a
single test. The uncorrected p-value is always returned.
Validation: VALIDATED.
judge ¶
judge(
per_parameter_p: Mapping[str, float],
*,
alpha: float = CHI2_ALPHA,
method: Literal[
"bonferroni", "bh", "none"
] = "bonferroni",
) -> SBCVerdict
Judge a family of per-parameter uniformity p-values with explicit multiplicity control.
bonferroni controls the family-wise error rate at alpha and is right when any single
rejection fails the campaign. bh (Benjamini-Hochberg) controls the false discovery rate.
none applies alpha to each test; its family-wise false-rejection risk under independence,
1 - (1 - alpha)^m, is reported whatever the method.
Validation: TESTED.
ks_uniformity ¶
ks_uniformity(
ranks: ArrayLike, n_draws: int
) -> dict[str, float]
Kolmogorov-Smirnov test of ranks against the uniform distribution on 0 to n_draws.
Ranks are mapped to (rank + 0.5) / (n_draws + 1), the midpoints of their cells, which makes the test slightly conservative for discrete ranks.
Validation: TESTED.
pd_bb_prior_design ¶
pd_bb_prior_design(
n_participants: int = 40,
*,
chains: int = 4,
iter_warmup: int = 300,
iter_sampling: int = 300,
patterns: Sequence[Any] | None = None,
practice_tau_years: float = 1.0,
require_gates: bool = True,
output_root: str | None = None,
) -> SBCDesign
Return a prior-regime SBC design for pd_bb, the discrete observation model, on synthetic visits.
Each replicate draws the global parameters from the pd_bb prior and simulates integer sum scores.
The fitter runs symphony_dt.fit.fit on pd_bb. It returns None when the convergence gates fail,
unless require_gates is False. Turn the gates off only for a smoke run of the machinery,
never for a calibration claim.
Validation: EXPERIMENTAL.
pd_v2_prior_design ¶
pd_v2_prior_design(
n_participants: int = 60,
*,
chains: int = 4,
iter_warmup: int = 300,
iter_sampling: int = 300,
patterns: Sequence[Any] | None = None,
config: Any = None,
output_root: str | None = None,
) -> SBCDesign
Return a prior-regime SBC design for pd_v2 on synthetic visit patterns.
The fitter runs symphony_dt.fit.fit and returns None when the convergence gates fail.
Validation: TESTED.
rank_histogram_bands ¶
rank_histogram_bands(
n_ranks: int, bins: int = N_BINS, level: float = 0.99
) -> tuple[float, float]
Return the binomial band for one bin count of a uniform rank histogram.
Each bin count is Binomial(n_ranks, 1 / bins); the band holds its central level mass.
Validation: TESTED.
run_sbc ¶
run_sbc(
design: SBCDesign,
*,
n_replicates: int,
seed: int = 0,
method: Literal[
"bonferroni", "bh", "none"
] = "bonferroni",
alpha: float = CHI2_ALPHA,
) -> SBCResult
Run an SBC campaign: draw, fit, rank, then judge every parameter.
A failed fit is recorded by index and excluded from the ranks; it is never refitted until it passes, which would select on the outcome. The share of failures belongs in any report.
Validation: TESTED.
sbc_rank ¶
sbc_rank(draws: ArrayLike, truth: float) -> int
Return the SBC rank: how many posterior draws fall below the truth.
Validation: VALIDATED.
thin_for_rank ¶
thin_for_rank(
draws: ArrayLike, target: int = RANK_DRAWS
) -> NDArray[Any]
Thin to target draws at uniform spacing, so the rank uses near-independent values.
Validation: VALIDATED.