Sub topic 4
True positive rate and false positive rate
This chapter assumes you know binary classification, the four confusion matrix cells (TP, FP, TN, FN), and basic Python and NumPy. It also assumes you have a model that outputs scores, such as logistic regression probabilities. The previous chapter gave you a confusion matrix at one threshold. This chapter turns that matrix into the two numbers that make up each point on a ROC curve.
Two rates, two denominators
The confusion matrix holds counts. Counts are hard to compare across datasets or thresholds, so we divide them by the size of the group they come from.
The true positive rate (TPR) is the fraction of actual positives that the classifier labels positive. It is also called recall or sensitivity.
TPR = TP / (TP + FN)
The false positive rate (FPR) is the fraction of actual negatives that the classifier wrongly labels positive.
FPR = FP / (FP + TN)
Each rate uses one row of the confusion matrix. TPR looks only at the actual positives. FPR looks only at the actual negatives. This is why neither rate changes if you add many more examples of the other class, and it is why ROC curves behave as they do under class imbalance (a later chapter covers that).
A worked example
Use the four-example set from earlier. The true labels are [0, 0, 1, 1] and the scores are [0.1, 0.4, 0.35, 0.8]. The rule is to predict positive when the score is at or above the threshold.
At threshold 0.35, the predicted positives are the examples scored 0.35, 0.4, and 0.8. The 0.35 and 0.8 examples are real positives, so TP = 2 and FN = 0. The 0.4 example is a real negative, so FP = 1 and TN = 1.
- TPR = 2 / (2 + 0) = 1.0
- FPR = 1 / (1 + 1) = 0.5
The code below computes both rates for several thresholds.
import numpy as np
y_true = np.array([0, 0, 1, 1])
y_score = np.array([0.1, 0.4, 0.35, 0.8])
def rates(y_true, y_score, threshold):
pred = y_score >= threshold
tp = (pred & (y_true == 1)).sum()
fn = (~pred & (y_true == 1)).sum()
fp = (pred & (y_true == 0)).sum()
tn = (~pred & (y_true == 0)).sum()
return tp / (tp + fn), fp / (fp + tn)
for t in [0.8, 0.4, 0.35, 0.1]:
tpr, fpr = rates(y_true, y_score, t)
print(f"threshold={t}: TPR={tpr:.2f}, FPR={fpr:.2f}")The output is:
threshold=0.8: TPR=0.50, FPR=0.00
threshold=0.4: TPR=0.50, FPR=0.50
threshold=0.35: TPR=1.00, FPR=0.50
threshold=0.1: TPR=1.00, FPR=1.00Each line is one (FPR, TPR) pair, which is one point on the ROC curve. The next chapter plots these points and connects them.
How the rates move with the threshold
Lowering the threshold labels more examples positive. That can only add predicted positives, so both rates stay the same or rise.
| Threshold | TPR | FPR | Effect |
|---|---|---|---|
| Very high (above every score) | 0 | 0 | Nothing is predicted positive |
| Lower | rises | rises | More true and false positives |
| Very low (at or below every score) | 1 | 1 | Everything is predicted positive |
The two extremes give the corners (0, 0) and (1, 1). Between them, you trade one rate against the other. A threshold that catches more positives also lets in more false alarms.
Common errors
- Mixing up the denominators. FPR divides by all actual negatives (FP + TN). Dividing FP by all predicted positives gives a different quantity, the complement of precision.
- Confusing FPR with 1 − TPR. The complement of TPR is the miss rate, FN / (TP + FN). FPR is a statement about negatives, so the two are unrelated in general.
- Dividing by zero. If the data contains no positives or no negatives, one of the rates is undefined. ROC analysis needs both classes present.
Exercise
A model is evaluated at one threshold on 50 actual positives and 950 actual negatives. It produces TP = 40, FN = 10, FP = 95, TN = 855. What are the TPR and the FPR? Why is the FPR a more useful number here than the raw count of false positives?
Show answer
TPR = 40 / (40 + 10) = 0.80. FPR = 95 / (95 + 855) = 0.10. The count of 95 false positives looks large but is only 10% of the 950 negatives. FPR scales the count by the number of negatives, so it can be compared across datasets of different sizes.