Sub topic 8
AUC as the probability of ranking a positive above a negative
This chapter assumes you can build a ROC curve by sweeping a threshold and that you know AUC is the area under that curve. Here you will see a second, equivalent meaning of AUC. It describes how well a model ranks examples and does not depend on any one threshold.
The pairwise view
Pick one positive example and one negative example at random. The model gives each a score. AUC is the probability that the positive example gets the higher score.
- An AUC of 1.0 means every positive outscores every negative.
- An AUC of 0.5 means the ranking is no better than a coin flip.
- An AUC of 0.0 means every positive scores below every negative, so the ranking is perfectly reversed.
Ties count as one half. If a positive and a negative have the same score, the model has not ranked either one higher, so that pair is worth 0.5.
Written as a formula, with P the set of positive scores and N the set of negative scores:
AUC = (number of pairs with spos > sneg + 0.5 × number of tied pairs) / (|P| × |N|)
Worked example
The same four examples from earlier chapters have labels [0, 0, 1, 1] and scores [0.1, 0.4, 0.35, 0.8]. There are two positives (0.35 and 0.8) and two negatives (0.1 and 0.4), so there are 2 × 2 = 4 pairs.
The only misranked pair is the positive scored 0.35 against the negative scored 0.4. The other three pairs are correct, so AUC is 3/4 = 0.75. This matches the area under the ROC curve and the value roc_auc_score returns.
Computing it directly
You can compute AUC from pairs without building a curve. NumPy broadcasting forms every positive-minus-negative difference at once.
import numpy as np
y_true = np.array([0, 0, 1, 1])
y_score = np.array([0.1, 0.4, 0.35, 0.8])
pos = y_score[y_true == 1]
neg = y_score[y_true == 0]
diff = pos[:, None] - neg[None, :]
auc_rank = ((diff > 0).sum() + 0.5 * (diff == 0).sum()) / diff.size
print(auc_rank) # 0.75diff has one row per positive and one column per negative. A positive difference is a win, and a zero is a tie. Dividing by diff.size divides by the number of pairs.
This version uses memory proportional to the number of positives times the number of negatives. That is fine for small checks. For large datasets, use roc_auc_score, which gets the same result by sorting the scores.
Why the two views agree
Each step right or up on the ROC curve corresponds to one example. Moving up passes a positive. Moving right passes a negative. A positive that is scored above a negative is passed first, so the area under the curve counts exactly those pairs. Tied scores move the curve diagonally, which adds half a unit of area per tied pair.
Common errors
- Treating AUC as accuracy. An AUC of 0.75 does not mean 75% of predictions are right. It means 75% of positive-negative pairs are ordered correctly.
- Forgetting ties. A model that outputs only a few distinct score values has many tied pairs. If you count ties as losses, you will get an AUC lower than scikit-learn's.
- Passing hard labels as scores. If you give
roc_auc_scorethe 0/1 predictions instead of scores, nearly all pairs tie or collapse to one threshold. The result is a poor summary of the model.
Exercise
Positive examples have scores 0.9 and 0.5. Negative examples have scores 0.5 and 0.2. What is the AUC under the pairwise definition?
Show answer
There are 4 pairs. 0.9 vs 0.5 is a win, 0.9 vs 0.2 is a win, 0.5 vs 0.5 is a tie worth 0.5, and 0.5 vs 0.2 is a win. The total is 3.5 out of 4, so AUC = 0.875.