Sub topic 10
Choosing an operating threshold
This chapter assumes you can read a ROC curve and compute TPR and FPR from a confusion matrix. It also assumes you know how roc_curve returns fpr, tpr, and thresholds.
Why the curve does not pick a threshold
A ROC curve shows every trade-off a scoring model can offer. A deployed system still needs one rule: predict positive when the score is at or above a single number. That number is the operating threshold, and the curve cannot choose it for you. The choice depends on what each kind of error costs.
Criterion 1: Youden's J
Youden's J statistic is TPR minus FPR. It picks the point on the curve farthest above the diagonal. This is a reasonable default when a false positive and a false negative cost about the same and the classes are roughly balanced.
import numpy as np
from sklearn.metrics import roc_curve
y_true = np.array([0, 0, 1, 1])
y_score = np.array([0.1, 0.4, 0.35, 0.8])
fpr, tpr, thresholds = roc_curve(y_true, y_score)
j = tpr - fpr
best = np.argmax(j)
print(thresholds[best], tpr[best], fpr[best]) # 0.8 0.5 0.0Two thresholds tie here, 0.8 and 0.35, both with J = 0.5. np.argmax returns the first one, which is 0.8. That threshold catches only half the positives. If missed positives are worse than false alarms, J is not the right criterion.
Criterion 2: Minimum expected cost
When you can put numbers on the errors, use them. Let c_fn be the cost of a missed positive and c_fp the cost of a false alarm. The total cost at each threshold is:
- missed positives:
c_fn * n_pos * (1 - tpr) - false alarms:
c_fp * n_neg * fpr
import numpy as np
from sklearn.metrics import roc_curve
y_true = np.array([0, 0, 1, 1])
y_score = np.array([0.1, 0.4, 0.35, 0.8])
c_fn, c_fp = 5.0, 1.0
fpr, tpr, thresholds = roc_curve(y_true, y_score)
n_pos = (y_true == 1).sum()
n_neg = (y_true == 0).sum()
cost = c_fn * n_pos * (1 - tpr) + c_fp * n_neg * fpr
best = np.argmin(cost)
print(thresholds[best], cost[best]) # 0.35 1.0A missed positive costs five times a false alarm, so the minimum moves to 0.35. At that threshold every positive is caught and one negative is flagged. Youden's J is a special case of this method. It corresponds to equal error costs and equal class sizes.
Criterion 3: A constraint
Many systems state the requirement as a limit instead of a cost. For example, "keep the false positive rate at or below 5%" or "catch at least 90% of positives". To apply one, filter the points that satisfy it and take the best of the rest. For the first constraint, keep fpr <= 0.05 and take the largest tpr.
Common errors
- Choosing on the test set. If you pick the threshold on the same data you report results on, the reported TPR and FPR are optimistic. Choose on validation data, then report on held-out data.
- Treating the threshold as portable. A threshold depends on the score scale. If you retrain, recalibrate, or change the model, find it again.
- Ignoring noise. On small data the best point on the curve is partly luck. Neighboring thresholds often perform almost as well, so prefer a stable region over a sharp peak.
- Reading
thresholds[0]. In recent scikit-learn versions the first threshold isinf, which means no example is predicted positive.
Exercise
Using the data above, a false alarm costs 1 and a missed positive costs 1, with two positives and two negatives. Which thresholds minimize the cost, and what is the minimum cost?
Show answer
The cost is (1 - tpr) 2 + fpr 2. The point at threshold 0.8 gives 1.0. The point at 0.35 gives 0 + 1 = 1.0. These two thresholds tie at the minimum cost of 1.0, matching the tie in Youden's J.