Sub topic 11
Learning to Predict the Model's Own Error
Assume you know binary classification metrics, Python, pandas, and scikit-learn. Also assume that your production model writes a prediction log, but the true label may arrive days or months later.
A loss model is a second model that predicts whether the primary model will be wrong. It learns from older rows whose labels have matured. You can then apply it to recent, unlabeled rows and estimate current error before ground truth arrives.
The working model
For each historical prediction, create an error target:
error_i =
\begin{cases}
1 & \text{if the primary prediction is wrong}\\
0 & \text{otherwise}
\end{cases}The loss model uses information available at prediction time, such as:
- The primary model's probability.
- Input features or a compact feature representation.
- Segment, channel, geography, or product type.
- Missing-value or data-quality indicators.
- Drift statistics calculated without future labels.
It must not use the eventual label or information created after the prediction. For a new row, the loss model returns the estimated probability that the primary model is wrong. The average of those probabilities estimates the primary model's error rate.
import numpy as np
from sklearn.ensemble import HistGradientBoostingClassifier
from sklearn.metrics import brier_score_loss, roc_auc_score
rng = np.random.default_rng(7)
n = 6000
# Features available when the primary model makes its prediction.
X = rng.normal(size=(n, 3))
primary_score = 1 / (1 + np.exp(-(1.2 * X[:, 0] - 0.8 * X[:, 1])))
# Simulated delayed outcome. The third feature changes error risk.
true_probability = 1 / (
1 + np.exp(-(1.2 * X[:, 0] - 0.8 * X[:, 1] + 0.9 * X[:, 2]))
)
y = rng.binomial(1, true_probability)
primary_prediction = (primary_score >= 0.5).astype(int)
error = (primary_prediction != y).astype(int)
# Use an older, matured period to train the loss model.
split = 4000
loss_features = np.column_stack([X, primary_score])
loss_model = HistGradientBoostingClassifier(max_iter=100, random_state=7)
loss_model.fit(loss_features[:split], error[:split])
# Estimate error on newer rows whose labels are not required.
p_error = loss_model.predict_proba(loss_features[split:])[:, 1]
estimated_error_rate = p_error.mean()
# Backtest against labels that arrived later.
actual_error_rate = error[split:].mean()
print(f"Estimated error rate: {estimated_error_rate:.3f}")
print(f"Actual error rate: {actual_error_rate:.3f}")
print(f"Loss-model Brier score: "
f"{brier_score_loss(error[split:], p_error):.3f}")
print(f"Loss-model ROC AUC: "
f"{roc_auc_score(error[split:], p_error):.3f}")The loss model should be evaluated as a probability model, not only as a classifier. The Brier score measures the squared difference between predicted error probability and observed error. Lower is better. ROC AUC measures ranking: whether rows likely to be errors receive higher scores than rows likely to be correct.
Preventing misleading estimates
The most common mistake is training and evaluating the loss model on the same rows. A flexible loss model can memorize errors and produce an unrealistically low apparent error rate. Use a time-based split or out-of-fold predictions.
The error target is often imbalanced. If the primary model is correct 98% of the time, most loss targets are zero. Check precision-recall behavior and calibration. A loss model that ranks risky rows well may still produce a biased average probability.
Also match the training population to today's population. If the feature distribution has changed, the loss model may be extrapolating. Report an applicability or coverage measure, such as the fraction of current rows inside the historical feature range.
Exercise
Exercise
Why should a loss model use only features available when the primary prediction was made?
Show answer
Later information can leak the true outcome into the error prediction, making the estimated performance look better than it will be in production.