ehs-risk-sem
Back to ehs-risk-sem

calibration

CalibrationResult

CalibrationResult(n_obs: 'int', n_events: 'int', base_rate: 'float', brier: 'dict', in_the_large: 'dict', intercept_slope: 'dict', ece: 'dict', auc: 'float', diagram: 'pd.DataFrame', warnings: 'List[str]' = <factory>) -> None

Bundle of calibration, discrimination and decision measures.

assess_calibration

assess_calibration(y: 'np.ndarray', p: 'np.ndarray', n_bins: 'int' = 10, strategy: 'str' = 'quantile') -> 'CalibrationResult'

Run the full calibration assessment and flag rare-event problems.

Used by: study_02_rare_events.py

auc

auc(y: 'np.ndarray', p: 'np.ndarray') -> 'float'

Area under the ROC curve, by the Mann-Whitney rank formula with ties.

Returns NaN when either class is empty.

Used by: study_02_rare_events.py

brier_decomposition

brier_decomposition(y: 'np.ndarray', p: 'np.ndarray', n_bins: 'int' = 10, strategy: 'str' = 'quantile') -> 'dict'

Partition the Brier score into reliability, resolution and uncertainty.

``Brier = reliability - resolution + uncertainty``

frequency within a bin. Lower is better; zero means perfectly calibrated at this binning.

better; zero means the model separates nothing.

not of the model. At a base rate near zero it dominates, which is why a very low Brier score at a low base rate says almost nothing.

The partition is binning-dependent, and the residual returned as ``identity_gap`` reports how far the three terms are from reproducing the Brier score exactly.

Citation: Murphy, A. H. (1973). A new vector partition of the probability score. *Journal of Applied Meteorology*, 12(4), 595-600. Verified against the journal record; that article is where the reliability / resolution / uncertainty partition of the Brier score originates.

  • reliability: mean squared gap between predicted probability and observed
  • resolution: how far bin frequencies depart from the base rate. Higher is
  • uncertainty: ``base_rate * (1 - base_rate)``, a property of the outcome,
Used by: study_02_rare_events.py

brier_score

brier_score(y: 'np.ndarray', p: 'np.ndarray') -> 'float'

Mean squared error of probability predictions.

calibration_in_the_large

calibration_in_the_large(y: 'np.ndarray', p: 'np.ndarray') -> 'dict'

Mean predicted probability against observed event rate.

The weakest calibration requirement: if these two disagree, the model is wrong about the overall level of risk regardless of how well it ranks.

calibration_intercept_slope

calibration_intercept_slope(y: 'np.ndarray', p: 'np.ndarray') -> 'dict'

Calibration intercept and slope from logistic recalibration.

The slope comes from regressing the outcome on ``logit(p)``. A slope below 1 means the predictions are too extreme (overfitted); above 1 means too compressed. The intercept is estimated with ``logit(p)`` as an offset, so it measures the remaining level shift once the slope is held at 1.

Perfect calibration is intercept 0 and slope 1.

Used by: study_02_rare_events.py

confusion_at_threshold

confusion_at_threshold(y: 'np.ndarray', p: 'np.ndarray', threshold: 'float') -> 'dict'

Counts and derived rates when predictions above ``threshold`` trigger action.

``alerts_per_true_event`` is included explicitly because it is the number an operations manager experiences and the number that decides whether a system gets ignored.

Used by: study_02_rare_events.py

decision_curve

decision_curve(y: 'np.ndarray', p: 'np.ndarray', thresholds: 'Optional[np.ndarray]' = None) -> 'pd.DataFrame'

Net benefit of the model, of treating all, and of treating none.

A model is worth deploying at a given threshold only where its net benefit exceeds both reference strategies. At low base rates that window is often empty, which is the point of computing it.

expected_calibration_error

expected_calibration_error(y: 'np.ndarray', p: 'np.ndarray', n_bins: 'int' = 10, strategy: 'str' = 'quantile') -> 'dict'

Expected and maximum calibration error over bins.

ECE is the sample-weighted mean absolute gap between predicted probability and observed frequency; MCE is the largest such gap. Both depend on the binning, which is reported alongside them.

net_benefit

net_benefit(y: 'np.ndarray', p: 'np.ndarray', threshold: 'float') -> 'float'

Net benefit of acting on predictions above ``threshold``.

``NB = TP/n - (FP/n) * (t / (1 - t))``

The threshold encodes the exchange rate between a missed event and a needless intervention: acting at threshold ``t`` says one true positive is worth ``(1-t)/t`` false positives. Comparing net benefit against the treat-all and treat-none strategies is the decision-analytic evaluation that Steyerberg et al. (2010) put alongside discrimination and calibration.

reliability_diagram

reliability_diagram(y: 'np.ndarray', p: 'np.ndarray', n_bins: 'int' = 10, strategy: 'str' = 'quantile') -> 'pd.DataFrame'

Data for a reliability diagram: mean prediction against observed frequency.

Returns one row per bin with the bin bounds, count, mean predicted probability, observed frequency and a Wilson interval for the observed frequency. The interval is included because at low base rates a bin can easily contain zero events, and a plotted point with no uncertainty attached invites overreading.