Do predicted probabilities match what happens?

Group predicted probabilities into bands with pd.qcut, compare each band’s mean probability with its observed rate, and see why two models with the same AUC can flag different cases.

After this lesson, you can check whether predicted probabilities match the observed rates, and why a good ranking does not make probabilities right.

How do I check calibration?

A model is calibrated when its probabilities match the observed frequencies, so among reports with a probability of 0.25, about 25% happen. Grouping the reports into bands of similar probability and comparing each band’s mean probability with its observed rate checks this. On 12 made-up reports, model A predicts 0.25, 0.50 and 0.75 in its three bands, the observed rates exactly, while model B predicts 0.075, 0.254 and 0.575.

\[ \bar p_b = \frac{1}{n_b}\sum_{i \in b} p_i, \qquad \bar y_b = \frac{1}{n_b}\sum_{i \in b} y_i \]

Here \(b\) is a band, \(n_b\) its number of reports, \(p_i\) the predicted probability that report \(i\) is forwarded for investigation and \(y_i\) its outcome, 1 or 0.

The probabilities were predicted before the outcomes, on reports held back from fitting. Model B squares model A’s probabilities, which lowers them and keeps their order. pd.qcut(..., q=3) cuts the probabilities into three bands with equal numbers of reports, and .agg() computes one column per pair of source column and calculation. The mean of the zeros and ones is the observed rate.

The following box builds the reports, cuts the bands and compares the means with the rates.

import pandas as pd
from sklearn.metrics import roc_auc_score

reports = pd.DataFrame({
    "ticker": ["AAA", "BBB", "CCC"] * 4,
    "probability_a": [0.10, 0.20, 0.30, 0.40, 0.42, 0.47, 0.53, 0.58, 0.60, 0.70, 0.80, 0.90],
    "forwarded": [0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1],
})
reports["probability_b"] = reports["probability_a"] ** 2
reports["band"] = pd.qcut(reports["probability_a"], q=3, labels=["low", "middle", "high"])

bands = reports.groupby("band", observed=True).agg(reports_in_band=("forwarded", "size"), mean_a=("probability_a", "mean"), mean_b=("probability_b", "mean"), forwarded_rate=("forwarded", "mean"))
print(bands.round(3))
        reports_in_band  mean_a  mean_b  forwarded_rate
band                                                   
low                   4    0.25   0.075            0.25
middle                4    0.50   0.254            0.50
high                  4    0.75   0.575            0.75

Model B underestimates the rate in every band. Four reports per band show the method, and a real check needs many more.

Why can two models share an AUC but flag different reports?

The AUC measures how well a model ranks the reports, and squaring keeps the ranking, so both models have an AUC of 0.7778. At a threshold of 0.5, model A flags six reports and model B two, because the threshold reads the probabilities as levels.

\[ \text{AUC} = \frac{28}{36} = 0.7778 \]

Six reports were forwarded and six were not, which makes 36 pairs, and in 28 of them the forwarded report has the higher probability.

roc_auc_score takes the outcomes first and the probabilities second. >= 0.5 marks the flagged reports, and .sum() counts them.

The following box prints both AUCs and both counts of flagged reports.

print(round(roc_auc_score(reports["forwarded"], reports["probability_a"]), 4), round(roc_auc_score(reports["forwarded"], reports["probability_b"]), 4))
print((reports["probability_a"] >= 0.5).sum(), (reports["probability_b"] >= 0.5).sum())
0.7778 0.7778
6 2

A threshold of 0.5 suits calibrated probabilities and equal costs of the two errors. An investigator with limited time sets it from the costs.

Your turn

Compute each band’s observed rate minus model B’s mean probability. You should see 0.175, 0.246 and 0.175.

Subtracting two columns of bands gives one gap per band.

print((bands["forwarded_rate"] - bands["mean_b"]).round(3))
band
low       0.175
middle    0.246
high      0.175
dtype: float64