MoonSay /Tutorials/Machine Learning Interview #12
Contents
  1. 01Introduction
  2. 02Accuracy lies
  3. 03Precision and recall
  4. 04F1, the harmonic mean
  5. 05ROC curve and AUC
  6. 06The precision-recall curve
  7. 07Interview follow-ups
  8. 08Common misconceptions
  9. 09Interview questions and model answers
  10. 10Summary
  11. References

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.
metricuse it when
accuracyclasses are balanced and both errors cost about the same
precisionfalse alarms are costly or the number of alarms is limited
recallmisses are costly; report it with the precision or the threshold
F_1, F_\betaone number at one chosen threshold; \beta from the application
ROC-AUCcomparing rankings over all thresholds, or across data with different prevalences
precision–recall curve, APpositives are rare and the cost of acting on false alarms matters
log loss, reliability diagramthe probabilities themselves will be used (costs, risk, expected value)
Table 3. Which metric when.