Agent Wranglers

MLO-16Recommended

Check calibration when scores are shown as probabilities

When a score is shown or used as a probability, check calibration on the test set. Recalibrate when any bin's observed rate differs from its mean score by more than the limit you set, such as 5 percentage points.

How to do it

A score of 0.8 should mean that about 8 in 10 such cases are positive. Many models, such as boosted trees and neural networks, are too confident. Plot a calibration curve with ten bins and record the gap per bin. Fix it with a calibration step fitted on validation data, such as Platt scaling or isotonic regression. Calibration can shift between groups, so repeat the check per group in Measure every metric for each group of people.

Examples

Python example

from sklearn.calibration import calibration_curve

CALIBRATION_LIMIT = 0.05

def worst_calibration_gap(y_true, probabilities) -> float:
    observed, predicted = calibration_curve(y_true, probabilities, n_bins=10)
    return float(max(abs(observed - predicted)))

TypeScript example

const CALIBRATION_LIMIT = 0.05;
const BINS = 10;

export function worstCalibrationGap(labels: boolean[], probabilities: number[]): number {
  const bins = Array.from({ length: BINS }, () => ({ count: 0, positives: 0, sum: 0 }));
  probabilities.forEach((p, i) => {
    const bin = bins[Math.min(BINS - 1, Math.floor(p * BINS))];
    bin.count += 1; bin.sum += p; bin.positives += labels[i] ? 1 : 0;
  });
  return Math.max(...bins.filter(b => b.count > 0).map(b => Math.abs(b.positives / b.count - b.sum / b.count)));
}

How to check

The evaluation report holds a ten-bin calibration table for each model that exposes a probability. The gate fails when any bin with 100 or more rows is off by more than the limit.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/machine-learning/evaluation/calibrated-scores">Agent Wranglers machine learning playbook</a>. In code you write, add a comment next to it with the same link.