Sub topic 4
True positive rate and false positive rate
This chapter assumes you know the four confusion matrix cells (true positive, false positive, true negative, false negative). It also assumes you can read basic NumPy and know what a decision threshold does to a model's scores. Here you turn those four counts into the two rates that form the axes of a ROC curve.
Why counts are not enough
A confusion matrix at one threshold gives four counts. Counts depend on how many examples you evaluated. A model that makes 90 true positive predictions on 100 positives and one that makes 900 on 1000 positives behave identically, but the raw counts differ by a factor of ten.
Dividing by the right group size removes that dependence. The two rates used for ROC curves each divide by the size of one true class:
- The true positive rate divides by the number of actual positives.
- The false positive rate divides by the number of actual negatives.
Each rate answers a question about a single class. That is why the two rates can be plotted together without either one being distorted by the class mix.
Definitions
Let TP, FN, FP and TN be the counts at a fixed threshold.
| Quantity | Formula | Question it answers | Other names |
|---|---|---|---|
| True positive rate (TPR) | TP / (TP + FN) | Of all real positives, what fraction did we flag? | Recall, sensitivity, hit rate |
| False positive rate (FPR) | FP / (FP + TN) | Of all real negatives, what fraction did we wrongly flag? | Fall-out, 1 − specificity |
The denominator of TPR, TP + FN, is the total number of positives. The denominator of FPR, FP + TN, is the total number of negatives. Both denominators are fixed by the data and do not change when you move the threshold. Only the numerators change.
Notice that TPR uses only the positive row of the confusion matrix and FPR uses only the negative row. Neither rate mixes information from both classes.
A probability reading
Each rate is also a conditional probability. If the model flags an example when its score is at or above a threshold t, then:
- TPR(t) = P(score ≥ t | the example is positive)
- FPR(t) = P(score ≥ t | the example is negative)
So TPR(t) is the fraction of the positive score distribution that lies at or above t. FPR(t) is the same quantity for the negative score distribution. A ROC curve therefore compares two score distributions. This view explains why AUC can later be read as a ranking probability, and why prevalence (how common positives are) does not enter either rate.
Computing the rates by hand
The function below counts the four cells at one threshold and returns both rates. It uses the same four-example data set that the rest of the tutorial uses.
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_at(y_true, y_score, threshold):
pred = y_score >= threshold
pos = y_true == 1
tp = int((pred & pos).sum())
fn = int((~pred & pos).sum())
fp = int((pred & ~pos).sum())
tn = int((~pred & ~pos).sum())
tpr = tp / (tp + fn)
fpr = fp / (fp + tn)
return tp, fn, fp, tn, tpr, fpr
for t in [0.8, 0.4, 0.35, 0.1]:
tp, fn, fp, tn, tpr, fpr = rates_at(y_true, y_score, t)
print(f"t={t:.2f} TP={tp} FN={fn} FP={fp} TN={tn} TPR={tpr:.2f} FPR={fpr:.2f}")Running it prints:
t=0.80 TP=1 FN=1 FP=0 TN=2 TPR=0.50 FPR=0.00
t=0.40 TP=1 FN=1 FP=1 TN=1 TPR=0.50 FPR=0.50
t=0.35 TP=2 FN=0 FP=1 TN=1 TPR=1.00 FPR=0.50
t=0.10 TP=2 FN=0 FP=2 TN=0 TPR=1.00 FPR=1.00Work through the t=0.35 row to check your understanding. The predictions are [0, 1, 1, 1] for scores [0.1, 0.4, 0.35, 0.8]. Both positives (0.35 and 0.8) are caught, so TPR is 2/2 = 1.0. One of the two negatives (0.4) is wrongly flagged, so FPR is 1/2 = 0.5.
Each row is one point (FPR, TPR). Plotting these points, with a start at (0, 0), gives the ROC curve of the next chapter. For this data the points are (0, 0.5), (0.5, 0.5), (0.5, 1.0) and (1.0, 1.0).
How the rates move with the threshold
Lowering the threshold can only add predicted positives. It never removes one. That has two consequences:
- TPR never decreases as the threshold goes down, because TP can only grow while the total positives stay fixed.
- FPR never decreases as the threshold goes down, for the same reason applied to FP and the total negatives.
The two rates therefore rise together. Every gain in TPR is paid for, sooner or later, with a higher FPR. Choosing a threshold means choosing where on this shared climb to stop.
The extremes are fixed:
- A threshold above every score predicts nothing positive. TP and FP are both 0, so the point is
(0, 0). - A threshold at or below every score predicts everything positive. FN and TN are both 0, so the point is
(1, 1).
Every ROC curve starts at the first point and ends at the second.
Rates ignore class prevalence
TPR and FPR each condition on the true class, so they do not change if you add more examples of one class that behave the same way. Metrics that mix the classes do change. Precision, TP / (TP + FP), is one of them.
Suppose a classifier has TPR = 0.90 and FPR = 0.05, and compare two data sets.
| Data set | Positives | Negatives | TP | FP | Precision |
|---|---|---|---|---|---|
| Balanced | 1000 | 1000 | 900 | 50 | 900 / 950 ≈ 0.95 |
| Imbalanced | 10 | 1000 | 9 | 50 | 9 / 59 ≈ 0.15 |
The rates are identical in both rows, yet precision drops from about 0.95 to about 0.15. This is the main strength of TPR and FPR: they describe how the model treats each class, regardless of how common each class is. It is also a weakness when positives are rare, because a small FPR can still produce many false alarms compared with true hits. A later chapter on imbalance and precision-recall curves returns to this.
Edge cases and common errors
- No positives or no negatives in the data. If
y_truehas no positives, TPR divides by zero. If it has no negatives, FPR does. Evaluation subsets, such as a small cross-validation fold or one patient group, can end up in this state. scikit-learn'sroc_curvewarns that the rate is undefined, androc_auc_scoreraises aValueErrorfor a single-classy_true. Check class counts before you compute. - Using
>instead of>=. The convention in this tutorial is to predict positive when the score is at or above the threshold. scikit-learn uses the same convention. A strict>shifts every point by one threshold on tied scores and gives results that differ from the library. - Tied scores. Examples with equal scores cannot be separated by any threshold. They move into or out of the predicted-positive set together. The curve then jumps diagonally rather than stepping horizontally or vertically.
- Swapping the denominators. A common slip is dividing FP by the number of predicted positives. That value is a different metric, the false discovery rate (1 − precision), not FPR. FPR always divides by the actual negatives.
- Hard labels instead of scores. If you pass 0/1 predictions where a score is expected, only one meaningful threshold exists, and you get a single operating point instead of a curve. Pass probabilities or decision scores.
Verifying against scikit-learn
You can confirm the hand computation by comparing it with the library. roc_curve returns every (FPR, TPR) pair for the distinct thresholds. With drop_intermediate=False it keeps all of them.
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, drop_intermediate=False)
for f, t_, th in zip(fpr, tpr, thresholds):
print(f"threshold={th:.2f} FPR={f:.2f} TPR={t_:.2f}")The rows for thresholds 0.80, 0.40, 0.35 and 0.10 match the table from rates_at. The first row has a threshold above every score (recent scikit-learn versions show inf there) and the point (0, 0). Matching the library to your own counts is a quick sanity check whenever you write evaluation code.
Summary of the key facts
- TPR = TP / (TP + FN) is the fraction of actual positives that are flagged. It is also called recall or sensitivity.
- FPR = FP / (FP + TN) is the fraction of actual negatives that are flagged. It equals 1 minus specificity.
- Each rate conditions on one true class, so neither depends on class prevalence.
- Lowering the threshold raises or keeps both rates. It never lowers either.
- Each threshold gives one point
(FPR, TPR), and the full set of points is the ROC curve.
Exercise
A classifier is evaluated on 40 actual positives and 160 actual negatives at one threshold. It flags 30 of the positives and 24 of the negatives as positive. Compute TPR, FPR, and precision. Then say what happens to the TPR, FPR, and precision if the same model and threshold are evaluated on a data set with the same per-class behavior but 1600 negatives.
Show answer
TP = 30 and FN = 10, so TPR = 30 / 40 = 0.75. FP = 24 and TN = 136, so FPR = 24 / 160 = 0.15. Precision = TP / (TP + FP) = 30 / 54 ≈ 0.56. With 1600 negatives and the same per-class behavior, FP becomes 240. TPR stays 0.75 and FPR stays 0.15, because each rate conditions on one true class. Precision falls to 30 / 270 ≈ 0.11, because it mixes the two classes and depends on how many negatives there are.