pytyche.calibrate.layered¶
Layered calibration deployment primitive — R(p) below ceiling, scale-family above.
The deployment path does NOT read the fitted c parameter; c is
persisted only for the matplotlib diagnostic plot. The past-ceiling regime
anchors on the SBC-measured pair (max_recorded_nominal, max_recorded_actual)
and scales per-test-point empirical anchor quantiles by the Gaussian z-score
ratio. This preserves the per-point asymmetry of hurdle CATEs that a
fully-Gaussian-σ inflation would average away.
Functions
|
Apply a fitted layered calibration to posterior CATE draws. |
Classes
|
Per-test-point calibrated intervals + provenance. |
|
Frozen view of a serialized R(p) isotonic correction. |
|
Discriminated container for a fitted layered SBC correction. |
|
Frozen view of a fitted scale-family correction. |
- class pytyche.calibrate.layered.CoverageCorrection(x_thresholds, y_values)[source]¶
Bases:
objectFrozen view of a serialized R(p) isotonic correction.
Mirrors the
coverage_correction.jsonschema produced byscripts/fit_sbc_correction.py: a pair of monotone arrays defining the isotonic step functionnominal → actual.- Parameters:
x_thresholds (
tuple[float,...])y_values (
tuple[float,...])
- class pytyche.calibrate.layered.ScaleFamilyCorrection(c, max_recorded_nominal, max_recorded_actual, fit_method, n_points_fit)[source]¶
Bases:
objectFrozen view of a fitted scale-family correction.
cis the Gaussian-scale LSQ fit, persisted for diagnostic-plot rendering only.apply_layered_calibration()deploys against the empirical anchor(max_recorded_nominal, max_recorded_actual).- Parameters:
c (
float)max_recorded_nominal (
float)max_recorded_actual (
float)fit_method (
str)n_points_fit (
int)
- class pytyche.calibrate.layered.CalibratedIntervals(lower, upper, desired_coverage, achieved_actual_coverage, method, applied_corrections)[source]¶
Bases:
objectPer-test-point calibrated intervals + provenance.
Returned by
apply_layered_calibration().methodrecords which regime fired ("r_of_p"or"scale_family");applied_correctionscarries regime-specific provenance keys.- Parameters:
lower (
ndarray)upper (
ndarray)desired_coverage (
float)achieved_actual_coverage (
float|None)method (
Literal['r_of_p','scale_family'])applied_corrections (
dict)
- class pytyche.calibrate.layered.LayeredCalibrationCorrection(mode, coverage_correction, scale_family)[source]¶
Bases:
objectDiscriminated container for a fitted layered SBC correction.
The
modefield selects which deployment regimes are supported:"layered": both R(p) (below ceiling) and scale-family (past ceiling) regimes available.scale_familyMUST be non-None."r_of_p_only": only R(p) is available. Past-ceiling deployment raises rather than silently degrading.scale_familyMUST be None.
The construction-time invariant is validated in
__post_init__and at load time byfrom_directory(). No silent fallback: a caller wanting r_of_p_only semantics on a sweep that DOES have a scale-family file must passmode="r_of_p_only"explicitly and will see aUserWarningnoting the ignored file.- Parameters:
mode (
Literal['layered','r_of_p_only'])coverage_correction (
CoverageCorrection)scale_family (
ScaleFamilyCorrection|None)
- classmethod from_directory(path, *, mode)[source]¶
Load a correction pair from a directory.
- Parameters:
path (
str|Path) – Directory containingcoverage_correction.jsonand, for layered mode,scale_family_correction.json.mode (
Literal['layered','r_of_p_only']) – Explicit deployment mode. No default; the caller must declare their intent at the call site rather than receive silent fallback.
- Returns:
Loaded correction with typed inner dataclasses.
- Return type:
- Raises:
FileNotFoundError – If a required JSON file is missing for the requested mode. The message names the missing file.
- Warns:
UserWarning – If
mode == "r_of_p_only"andscale_family_correction.jsonis present in the directory — the file is being ignored due to the explicit mode choice (visible signal, not silent).
- pytyche.calibrate.layered.apply_layered_calibration(result, correction, desired_coverage)[source]¶
Apply a fitted layered calibration to posterior CATE draws.
Stateless: does not mutate
result. Reads onlyresult.rpv_cate_samples(shape(n, S)) — the per-test-point posterior draws produced by a joint hurdle BCF fit.Regime selection (hard boundary, no smoothing — see spec “hard boundary switch with no smoothing”):
desired_coverage <= ceiling→ R(p) regime: invert the isotonic curve to find nominaln*such thatR(n*) == desired_coverage, then return the symmetric(1-n*)/2/1-(1-n*)/2quantile pair on the per-test-point draws.desired_coverage > ceiling→ scale-family regime: anchor on the empirical(q_low, q_high)atmax_recorded_nominaland inflate both half-widths around the per-point posterior mean by the Gaussian z-score ratios. This preserves per-point empirical asymmetry that a fully-Gaussian-σ inflation would average away.
The ceiling is
correction.scale_family.max_recorded_actualfor layered mode andmax(correction.coverage_correction.y_values)for r_of_p_only mode.- Parameters:
result (
_HasCateDraws) – Fit result providingrpv_cate_samples: ndarray[(n, S)].correction (
LayeredCalibrationCorrection) – Fitted layered calibration loaded viaLayeredCalibrationCorrection.from_directory().desired_coverage (
float) – Requested coverage level in the open interval(0, 1).
- Returns:
Calibrated per-test-point endpoints plus provenance.
- Return type:
- Raises:
ValueError – If
desired_coverageis outside(0, 1). Ifcorrection.mode == "r_of_p_only"anddesired_coverageexceeds the R(p) ceiling — past-ceiling calibration requires a scale-family correction.