Sub topic 16
Using calibrated probabilities for thresholds and decisions
This chapter assumes you can fit a classifier in scikit-learn and have read the earlier chapters on calibration, CalibratedClassifierCV, and held-out data. Here you turn calibrated probabilities into decisions.
Why 0.5 is rarely the right threshold
Most classifiers label an example positive when the probability is at least 0.5. That rule is optimal only when a false positive and a false negative cost the same. It also assumes the probability is calibrated.
Suppose a missed fraud case (false negative) costs 10 and a false alarm (false positive) costs 1. Flagging an example with positive probability p costs (1 - p) * C_FP. Not flagging it costs p * C_FN. Flag it when the first is smaller:
p >= C_FP / (C_FP + C_FN)
With these costs the threshold is 1/11, about 0.09. A cost-based threshold is only meaningful if p is a real probability. On uncalibrated scores, 0.09 has no defined meaning.
Applying the cost rule
The code below calibrates a naive Bayes model, then compares total cost at the default threshold and at the cost-derived threshold.
import numpy as np
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.calibration import CalibratedClassifierCV
COST_FP, COST_FN = 1.0, 10.0
cost_threshold = COST_FP / (COST_FP + COST_FN)
X, y = make_classification(
n_samples=20000, n_features=20, n_informative=5, n_redundant=10,
weights=[0.8, 0.2], random_state=0,
)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.5, random_state=0
)
def total_cost(y_true, p, threshold):
pred = p >= threshold
fp = np.sum(pred & (y_true == 0))
fn = np.sum(~pred & (y_true == 1))
return COST_FP * fp + COST_FN * fn
model = CalibratedClassifierCV(GaussianNB(), method="isotonic", cv=5)
model.fit(X_train, y_train)
p = model.predict_proba(X_test)[:, 1]
print("cost at 0.50:", total_cost(y_test, p, 0.5))
print(f"cost at {cost_threshold:.2f}:", total_cost(y_test, p, cost_threshold))The lower number shows what the costs justify. Because the threshold comes from the costs and not from the test labels, the test set stays an honest estimate.
Common errors
- Tuning the threshold on the test set. Pick it from costs, or from a validation set. Evaluate on the test set once.
- Using a threshold from uncalibrated scores elsewhere. A cutoff tuned on raw scores breaks when you retrain or switch models. A cost-derived threshold on calibrated probabilities carries over.
- Ignoring base-rate changes. If the share of positives in production differs from training, probabilities shift and the best threshold moves with them. Recheck calibration before reusing the threshold.
- Calibrating when you only rank. If you review the top 100 cases, only the ordering matters. Monotonic calibration leaves ranking unchanged, apart from ties that isotonic regression can create.
Exercise
Exercise
A false positive costs 5 and a false negative costs 20. What is the cost-optimal threshold on calibrated probabilities, and why does it fail on uncalibrated scores?
Show answer
The threshold is 5 / (5 + 20) = 0.2. The derivation treats the score as the true chance that the example is positive. Uncalibrated scores do not carry that meaning, so 0.2 would flag the wrong share of cases.