mixle.inference.calibration module¶
Calibration diagnostics: “is my probability / interval actually calibrated?”
A forecast is calibrated when its stated probabilities match observed frequencies: events given 70% should happen ~70% of the time, and 90% intervals should contain the truth ~90% of the time. This is distinct from sharpness (how concentrated the forecast is) and from accuracy – a forecaster can be perfectly calibrated and useless (always predict the base rate), so calibration is a necessary, not sufficient, condition that you check separately. These diagnostics are model-free: they look only at predicted probabilities/intervals and what happened.
Three families, by forecast type:
Probability classifiers –
reliability_curve()(the reliability diagram), and theexpected_calibration_error()/maximum_calibration_error()summaries. For multiclass usetop_label_confidence()to reduce to the (confidence, correct) calibration problem.Full predictive distributions – the Probability Integral Transform:
pit_values()/pit_ensemble(), thepit_histogram(), andpit_calibration_error(). Under a calibrated forecast the PIT values are Uniform(0, 1); a U-shaped histogram means under-dispersion, a hump means over-dispersion, a slope means bias.Intervals / quantiles –
interval_coverage()(coverage and mean width at one level) andcoverage_curve()(empirical-vs-nominal coverage across a grid of levels).
Several functions take ci=True to attach a nonparametric bootstrap confidence band, so a
reliability diagram or ECE comes with honest error bars rather than a bare point estimate.
- reliability_curve(prob, outcome, *, bins=10, strategy='uniform', ci=False, n_boot=1000, ci_level=0.95, seed=0)[source]
Reliability diagram: observed frequency vs mean forecast probability, per bin.
Bins the forecasts, then within each bin compares the mean predicted probability to the observed event frequency. A perfectly calibrated forecaster lies on the diagonal
observed == predicted.- Parameters:
prob (ndarray) –
(n,)predicted probabilities of the positive class (or top-label confidences).outcome (ndarray) –
(n,)0/1 outcomes (or correctness indicators).bins (int) – number of bins.
strategy (str) –
"uniform"(equal-width) or"quantile"(equal-count) bins.ci (bool) – if True attach a percentile bootstrap band on the observed frequency in each bin.
n_boot (int) – bootstrap resamples when
ciis True.ci_level (float) – central probability of the bootstrap band (e.g. 0.95).
seed (int | RandomState | None) – RNG seed for the bootstrap.
- Returns:
{'mean_pred', 'obs_freq', 'count', 'bin_edges'}(one entry per non-empty bin), plus'obs_lo'/'obs_hi'whenciis True.- Return type:
- expected_calibration_error(prob, outcome, *, bins=10, strategy='uniform', norm='l1', ci=False, n_boot=1000, ci_level=0.95, seed=0)[source]
Expected Calibration Error: count-weighted average gap between confidence and accuracy.
ECE = sum_b (n_b / n) |obs_b - pred_b|over bins (norm='l2'uses the squared gap, square- rooted). Zero is perfect calibration. For multiclass classifiers reduce withtop_label_confidence()first.- Parameters:
prob (ndarray) –
(n,)predicted probabilities / confidences.outcome (ndarray) –
(n,)0/1 outcomes / correctness indicators.bins (int) – number of bins.
strategy (str) –
"uniform"or"quantile"binning.norm (str) –
"l1"(mean absolute gap) or"l2"(root mean squared gap).ci (bool) – if True also return a percentile bootstrap interval.
n_boot (int) – bootstrap controls.
ci_level (float) – bootstrap controls.
seed (int | RandomState | None) – bootstrap controls.
- Returns:
The ECE (float), or
(ece, lo, hi)whenciis True.- Return type:
- maximum_calibration_error(prob, outcome, *, bins=10, strategy='uniform')[source]
Maximum Calibration Error: the worst per-bin gap
max_b |obs_b - pred_b|.Unlike
expected_calibration_error()this is not count-weighted, so it surfaces a small but badly-miscalibrated region that the average would hide.
- top_label_confidence(prob, labels)[source]
Reduce a multiclass classifier to the (confidence, correct) top-label calibration problem.
- Parameters:
- Returns:
the max predicted probability per row and a 0/1 indicator of whether that argmax class was the true label. Feed these to
reliability_curve()/expected_calibration_error().- Return type:
(confidence, correct)
- pit_values(y, cdf)[source]
Probability Integral Transform values
u_i = F_i(y_i).Under a calibrated continuous predictive distribution the PIT values are Uniform(0, 1). Pass either the precomputed CDF values
F_i(y_i)or a callablecdf(y) -> F(y).
- pit_ensemble(y, forecasts, *, randomize=True, seed=0)[source]
Rank-based PIT from a finite predictive ensemble.
u_iis the fraction of ensemble members<= y_i. Withrandomize=Trueties are broken by a uniform jitter within the rank gap, which makes the PIT exactly Uniform(0, 1) under calibration even for discrete ensembles (the randomized PIT of Czado et al. 2009).
- pit_histogram(pit, *, bins=10)[source]
Histogram of PIT values with the uniform reference level.
- pit_calibration_error(pit, *, bins=10)[source]
Calibration error of a PIT histogram: mean absolute deviation from uniform mass.
sum_b |count_b/n - 1/bins|– 0 when the PIT histogram is perfectly flat (calibrated), larger when it is U-shaped (under-dispersed) or humped (over-dispersed).
- interval_coverage(lower, upper, y)[source]
Empirical coverage and mean width of a set of prediction intervals.
- Parameters:
- Returns:
{'coverage', 'mean_width'}– the fraction ofyinside[lower, upper]and the mean interval width. Comparecoverageto the nominal level the interval was built for.- Return type:
- coverage_curve(forecasts, y, *, levels=None)[source]
Empirical-vs-nominal coverage of central intervals across a grid of nominal levels.
For each nominal central level
cthe per-observation central interval[quantile((1-c)/2), quantile((1+c)/2)]is read off the predictive ensemble and its empirical coverage is measured. A calibrated forecast traces the diagonalempirical == nominal; bowing below the diagonal means the intervals are too narrow (over-confident).
- class ProbabilityCalibrator(method='isotonic')[source]
Bases:
objectMap raw scores to calibrated probabilities – fit against binary outcomes.
A raw score (a model’s confidence, a self-consistency fraction, a token likelihood) need not be a probability of anything: it can be monotone-but-miscalibrated, or have no relationship to the outcome at all. This learns the transform
score -> P(outcome = 1 | score)from labeled data, so the output is a probability of the event you calibrated against.method="isotonic"– monotone, non-parametric (pool-adjacent-violators). Assumes higher score => not-lower probability; flexible, needs enough calibration points.method="platt"– logisticsigmoid(a * score + b). Two parameters, robust on little data, but assumes a sigmoidal relationship.
A near-flat fitted curve is itself the finding: it means the raw score carried little information about the outcome (its “likelihood” was unrelated to the event).
- Parameters:
method (str)
- fit(scores, outcomes)[source]
Fit the score->probability map on
scoreswith binaryoutcomes(0/1).