Search

Search papers and lessons

Area Under the ROC Curve

Level 2 · 11 min read

The area under the ROC curve is the chance that a randomly chosen positive case gets a higher score than a randomly chosen negative one, so it rates a classifier's ranking across every threshold.

On this page

  • Colour = topic mix
  • Discrete math
  • Learning basics
  • Calculus
  • Probability
  • Losses & regularization
  • ReadReady to readNot yet

TL;DR

A classifier that outputs a score, such as a probability that an email is spam, can be turned into a yes-or-no answer by choosing a threshold. Each threshold gives a different pair of true positive rate and false positive rate, and plotting those pairs gives the ROC curve. The area under that curve, written AUC, summarises how well the scores separate the two classes across all thresholds, with 1 for perfect ranking and 0.5 for guessing.

Builds on: Confusion Matrix, Precision and Recall · Leads to: model comparison in Supervised Learning projects, and threshold choice for deployed classifiers

Where it's used

Fraud scoring at a bank. A model gives every card transaction a risk score. The bank must pick a cut-off above which a transaction is held for review. The AUC of the score tells the team how well the model ranks fraudulent transactions above legitimate ones before any cut-off is chosen, so two candidate models can be compared on one number.

Medical screening tests. A blood test or an image-based model gives a number for each patient, and a clinician decides how high the number must be before follow-up. The ROC curve shows the trade-off between finding the sick patients and sending healthy people for unnecessary tests, and the AUC summarises that trade-off for the whole test. This is educational background only, not guidance on any test.

Spam filters and ranking models. An email filter scores each message. Users are harmed differently by a missed spam and by a real message sent to the spam folder, so the team wants to know how the scores behave across every cut-off, not only one. AUC gives that summary for comparing filters.

How it works

Here is one tiny instance with six items. Each item has a score from a classifier and a true label, positive (P) or negative (N). There are three positives and three negatives.

ItemScoreTrue label
A0.9P
B0.8N
C0.7P
D0.4P
E0.3N
F0.2N
  1. Pick a threshold. Predict positive when the score is at least the threshold. Start with 0.85. Check: only A has a score at least 0.85, so the classifier predicts positive for A alone.
  2. Count the outcomes. At threshold 0.85, A is a true positive and no negative is flagged. So TP = 1, FP = 0. Check: TP plus FN must equal the 3 positives, so FN = 2.
  3. Turn the counts into two rates. The true positive rate is TP divided by all positives, 1/31/3. The false positive rate is FP divided by all negatives, 0/3=00/3 = 0. Check: this gives the point (0,1/3)(0, 1/3) on the ROC plot, with false positive rate on the horizontal axis and true positive rate on the vertical.
  4. Lower the threshold and repeat. At 0.75, items A and B are flagged, so TP = 1 and FP = 1. The point is (1/3,1/3)(1/3, 1/3). At 0.65, A, B and C are flagged, giving (1/3,2/3)(1/3, 2/3). Check: each new threshold changes the counts by one item.
  5. Continue to the bottom. At 0.35, all four items A to D are flagged, giving (1/3,1)(1/3, 1). At 0.25 the point is (2/3,1)(2/3, 1), and at 0.15 everything is flagged and the point is (1,1)(1, 1). Check: the curve now runs from (0,0)(0, 0) to (1,1)(1, 1).
  6. Measure the area. The area under these points is 7/97/9, about 0.78. Check: the same number comes from counting pairs, which is the meaning of AUC in the math section.
00.20.40.60.8100.20.40.60.81false positive ratetrue positive rate
  • chance
  • classifier in the example
Each threshold gives one corner of the staircase. The dashed diagonal is a classifier that guesses at random.

The math

Definition: ROC curve and AUC

For a score ss and a threshold tt, the classifier predicts positive when s≥ts \ge t. Let PP be the number of positive items and NN the number of negatives. The true positive rate is TPR=TP/P\mathrm{TPR} = \mathrm{TP}/P, and the false positive rate is FPR=FP/N\mathrm{FPR} = \mathrm{FP}/N. The ROC curve is the set of points (FPR,TPR)(\mathrm{FPR}, \mathrm{TPR}) as tt runs from high to low. AUC is the area under this curve.

The true positive rate is also called recall or sensitivity. It answers: of the real positives, what fraction did we flag? The false positive rate answers: of the real negatives, what fraction did we flag by mistake?

Step 3 in symbols: the two rates

At threshold 0.85 in the example, TP=1\mathrm{TP} = 1, P=3P = 3, FP=0\mathrm{FP} = 0, N=3N = 3. So TPR=1/3\mathrm{TPR} = 1/3 and FPR=0\mathrm{FPR} = 0.

Step 6 in symbols: the area by trapezoids

Between consecutive points (xk,yk)(x_k, y_k) and (xk+1,yk+1)(x_{k+1}, y_{k+1}), the area under the curve is (xk+1−xk)(x_{k+1} - x_k) times the average height 12(yk+yk+1)\tfrac{1}{2}(y_k + y_{k+1}). Adding these pieces over the staircase:

AUC=19+13+13=79.\text{AUC} = \tfrac{1}{9} + \tfrac{1}{3} + \tfrac{1}{3} = \tfrac{7}{9}.

Here 19\tfrac{1}{9} comes from the flat step from x=0x = 0 to x=1/3x = 1/3 at height 1/31/3, and the two 13\tfrac{1}{3} terms come from the steps at height 1 from x=1/3x = 1/3 to x=2/3x = 2/3 and from x=2/3x = 2/3 to x=1x = 1.

The pair form of AUC

Pick one positive item and one negative item at random. Count a pair as correct when the positive has the higher score, and a tie as one half. Then

AUC=Pr⁡(s(positive)>s(negative))+12Pr⁡(s(positive)=s(negative)).\text{AUC} = \Pr\big(s(\text{positive}) > s(\text{negative})\big) + \tfrac{1}{2} \Pr\big(s(\text{positive}) = s(\text{negative})\big).

Here Pr⁡\Pr is probability over the random pair, and this is the same as the area under the ROC curve. In the example there are 3×3=93 \times 3 = 9 pairs. Positive A beats all three negatives. Positive C beats E and F but not B. Positive D beats E and F but not B. That is 3+2+2=73 + 2 + 2 = 7 correct pairs out of 9, giving 7/97/9, matching the trapezoid calculation.

Worked examples

Example
Example 1: one pair (tiny)

There is one positive with score s+s_+ and one negative with score s−s_-. Find the AUC in three cases.

  1. Case s+=0.6s_+ = 0.6, s−=0.4s_- = 0.4. The positive scores higher, so the pair is correct. AUC =1= 1.
  2. Case s+=0.5s_+ = 0.5, s−=0.5s_- = 0.5. The scores tie, which counts as one half. AUC =0.5= 0.5.
  3. Case s+=0.3s_+ = 0.3, s−=0.4s_- = 0.4. The positive scores lower, so the pair is wrong. AUC =0= 0.
  4. Interpretation: AUC runs from 0 for a fully reversed ranking, through 0.5 for a tie or a coin toss, to 1 for a perfect ranking.
Example
Example 2: three positives and four negatives (standard)

Positive scores are 0.950.95, 0.600.60 and 0.300.30. Negative scores are 0.800.80, 0.500.50, 0.200.20 and 0.100.10. There are 3×4=123 \times 4 = 12 pairs.

  1. Positive 0.950.95 beats all four negatives, so it contributes 4 correct pairs.
  2. Positive 0.600.60 beats 0.500.50, 0.200.20 and 0.100.10, but loses to 0.800.80. It contributes 3.
  3. Positive 0.300.30 beats 0.200.20 and 0.100.10, but loses to 0.800.80 and 0.500.50. It contributes 2.
  4. Total correct pairs: 4+3+2=94 + 3 + 2 = 9.
  5. AUC =9/12=0.75= 9/12 = 0.75.
  6. Interpretation: if you draw one positive and one negative at random, the positive has the higher score three times out of four.
Example
Example 3: choosing a threshold (realistic)

A hypothetical bank model scores 100 fraudulent transactions and 900 legitimate ones. Two thresholds are tried.

  1. At the first threshold, the model catches 80 frauds and flags 45 legitimate transactions. So TPR=80/100=0.80\mathrm{TPR} = 80/100 = 0.80 and FPR=45/900=0.05\mathrm{FPR} = 45/900 = 0.05.
  2. At a lower threshold, it catches 90 frauds and flags 135 legitimate transactions. So TPR=0.90\mathrm{TPR} = 0.90 and FPR=135/900=0.15\mathrm{FPR} = 135/900 = 0.15.
  3. Going from the first to the second threshold catches 10 more frauds and flags 90 more legitimate customers. The cost in false alarms is nine times the gain in frauds caught.
  4. Interpretation: both points lie on the ROC curve. AUC summarises every such point, but the bank still has to choose one, using the cost of each kind of error.

Try it: at a third threshold the model catches 60 frauds and flags 18 legitimate transactions. What are the TPR and FPR at this threshold?

Answer: TPR=60/100=0.60\mathrm{TPR} = 60/100 = 0.60 and FPR=18/900=0.02\mathrm{FPR} = 18/900 = 0.02.

Common mistakes and tradeoffs

It is tempting to think AUC is the accuracy of the classifier. In fact accuracy needs one threshold and counts all items, while AUC ignores the threshold and asks only whether positives rank above negatives. A model can have high AUC and poor accuracy at the threshold you use, because the threshold is not chosen by AUC.

It is tempting to think an AUC of 0.5 means the model is half-correct. In fact 0.5 means the ranking carries no information, like a coin toss. An AUC below 0.5 means the scores rank in the wrong order, and flipping the sign of the score would give 1−AUC1 - \text{AUC}.

It is tempting to think a high AUC means the model finds the rare positives well. In fact with very few positives, such as one fraud in ten thousand transactions, the false positive rate barely moves, so the ROC curve and its AUC can look excellent while the flags are mostly wrong. Precision, the fraction of flagged items that are truly positive, shows this directly, and a precision-recall curve is the better view for rare events.

When to use it: to compare scoring models on one number, when the classes are not too unbalanced and every threshold could be used.

When not to use it: when the cut-off is already fixed, when the classes are very unbalanced, or when the cost of errors is very unequal. Then look at precision and recall at the threshold you will actually use.

Alternatives: the precision-recall curve for rare positives; the Confusion Matrix at one chosen threshold; and cross-validation or held-out evaluation so that the AUC is measured on data the model did not see.

History

Receiver operating characteristic curves were developed during the Second World War. Radar engineers used them to measure how well operators could separate real enemy aircraft from noise, using a threshold on the operator's signal. The method came from signal detection theory.

In 1947, Henry Mann and Donald Whitney published a rank-sum test that counts how often values from one group exceed values from another. That count is the same quantity as the pair form of AUC, which is why the area under the ROC curve equals a test statistic about rankings.

Medical testing and then machine learning adopted the ROC curve and AUC as threshold-free summaries. They caught on because they compare models without fixing a cut-off in advance, though their limits with rare events became clear as more classification problems involved very unbalanced data.

Recap

  • A score becomes a yes-or-no answer only after a threshold is chosen.
  • Each threshold gives a pair of rates, TPR and FPR, and the pairs form the ROC curve.
  • AUC is the area under that curve, and equals the chance that a random positive outscores a random negative, with ties counting half.
  • AUC rates the ranking, not the accuracy at one threshold, so it does not choose a cut-off.
  • Read next: Precision and Recall for the view that matters when positives are rare, and Logistic Regression, a common source of the scores whose ranking AUC measures.

Check yourself

QuestionWhat does an AUC of 0.5 mean, and what does an AUC of 0.2 suggest?Show answer

An AUC of 0.5 means the scores rank positives and negatives no better than a coin toss. An AUC of 0.2 means the ranking is mostly reversed: negatives tend to score higher than positives. Flipping the sign of the score would give an AUC of 1−0.2=0.81 - 0.2 = 0.8.

QuestionA model has 4 positives and 5 negatives. It ranks 14 of the 20 positive-negative pairs correctly. What is its AUC?Show answer

The number of pairs is 4×5=204 \times 5 = 20, and 14 are correct, so AUC =14/20=0.7= 14/20 = 0.7.

QuestionWhy can a model with a high AUC still give many false alarms when one event in ten thousand is positive?Show answer

With very few positives, the false positive rate is FP divided by a large number of negatives, so it stays small even when the flagged items are mostly wrong. AUC uses that rate, so it can look excellent. Precision, the share of flagged items that are truly positive, shows the false alarms directly.

QuestionA screening model is applied at a threshold that gives TPR =0.75= 0.75 and FPR =0.10= 0.10, with 200 positives and 800 negatives. How many positives are caught, and how many negatives are flagged?Show answer

Positives caught: 0.75×200=1500.75 \times 200 = 150. Negatives flagged: 0.10×800=800.10 \times 800 = 80.

Sources