Contents
Precision, Recall, F1 and ROC-AUC/Chapter 10 of 10
10Summary
Notation. y=1: a real positive (fraud); \hat y=1: flagged by the model; p or s(x): the model’s score; \tau: the threshold (flag when p\ge\tau); n_+, n_-: numbers of positives and negatives; \pi=n_+/(n_++n_-): the prevalence; s^+, s^-: scores of a random positive and a random negative; U: the number of (positive, negative) pairs won by the positive.
- Accuracy lies when positives are rare: \frac{\mathrm{TP}+\mathrm{TN}}{n} is dominated by \mathrm{TN}. Never flagging scored 99.85\%, our model 99.90\%.
- Precision P=\frac{\mathrm{TP}}{\mathrm{TP}+\mathrm{FP}}=p(y=1\mid\hat y=1) and recall R=\frac{\mathrm{TP}}{\mathrm{TP}+\mathrm{FN}}=p(\hat y=1\mid y=1) look only at the positives. At \tau=0.5: 72.4\% and 55.9\%. Lowering the threshold raises recall and usually lowers precision.
- F_1 =\frac{2PR}{P+R}=\frac{2\mathrm{TP}}{2\mathrm{TP}+\mathrm{FP}+\mathrm{FN}}, the harmonic mean: \min(P,R)\le F_1\le2\min(P,R). Flag-everything: 0.3\% instead of 50\%. F_\beta weights recall \beta^2 times as much as precision.
- ROC curve: \mathrm{TPR} against \mathrm{FPR} over all thresholds; independent of prevalence and of any increasing transformation of the scores. \mathrm{AUC}=\Pr(s^+>s^-)=U/(n_+n_-)=0.958; Gini =2\mathrm{AUC}-1; \mathrm{SE}\approx0.01, set by the rarer class.
- Precision from the rates: P=\frac{\mathrm{TPR}\,\pi}{\mathrm{TPR}\,\pi+\mathrm{FPR}(1-\pi)}. At \mathrm{FPR}=1\%: 1{,}348 false alarms for 174 frauds.
- Precision–recall curve: average precision =\frac1{n_+}\sum_k P(\text{rank of the k-th positive})=0.643, random baseline \pi=0.0015; the same model at \pi=0.5 would have 0.97. Use it when positives are rare.
- Threshold from costs: \tau^*=\frac{C_{\mathrm{FP}}}{C_{\mathrm{FP}}+C_{\mathrm{FN}}}, 1/26 for a 25-to-1 cost ratio; tune on validation data. AUC says nothing about calibration: check log loss.
| metric | use it when |
|---|---|
| accuracy | classes are balanced and both errors cost about the same |
| precision | false alarms are costly or the number of alarms is limited |
| recall | misses are costly; report it with the precision or the threshold |
| F_1, F_\beta | one number at one chosen threshold; \beta from the application |
| ROC-AUC | comparing rankings over all thresholds, or across data with different prevalences |
| precision–recall curve, AP | positives are rare and the cost of acting on false alarms matters |
| log loss, reliability diagram | the probabilities themselves will be used (costs, risk, expected value) |