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

03Precision and recall

3.1Two questions about the frauds

Recall the decision rule: the model flags a transaction, written \hat y=1, when its predicted fraud probability p is at least the threshold \tau, and passes it, \hat y=0, otherwise; y=1 means the transaction really is fraud and y=0 that it is legitimate. Precision and recall ask two different questions, and neither one looks at the true negatives (Murphy, 2022, eqs. 5.20–5.21):

\text{precision}\quad P=\frac{\mathrm{TP}}{\mathrm{TP}+\mathrm{FP}}=p(y=1\mid\hat y=1), \tag{2}
\text{recall}\quad R=\frac{\mathrm{TP}}{\mathrm{TP}+\mathrm{FN}}=p(\hat y=1\mid y=1). \tag{3}

Precision: of everything we flagged, what fraction was really fraud? The denominator is the column of flagged transactions. Recall: of all the frauds, what fraction did we catch? The denominator is the row of true frauds. Recall is also called the true positive rate, sensitivity or hit rate; precision is the positive predictive value. One is a probability conditioned on the decision, the other on the truth, and confusing the two is the classic base-rate fallacy of Chapter 6.

Worked example 1 (Precision and recall at \tau=0.5). From Figure 1c: the model raised 118+45=163 alarms, of which 118 were fraud, so P=118/163=72.4\%. There were 118+93=211 frauds, of which 118 were caught, so R=118/211=55.9\%. In words: when the model blocks a card it is right almost three times out of four, but it lets almost half of the frauds through. Neither number contains the 139{,}765 true negatives.

Different applications care about different questions. A spam filter that sends a real message to the junk folder (a false positive) annoys more than one spam message in the inbox, so precision matters. A cancer screening test that misses a tumor (a false negative) is far worse than an unnecessary follow-up, so recall matters. A search engine shows ten results: their precision is what the user sees, while recall, the fraction of all relevant pages that were found, is invisible to the user and hard even to measure.

3.2The threshold moves the pair

The model outputs a score, and the threshold decides. Figure 2a shows the scores of all test transactions on a stretched axis, the log-odds \log_{10}[p/(1-p)], which is the logistic model’s own linear score divided by \ln10; the threshold p=0.5 is the line at 0. Legitimate transactions (top) mostly sit far to the left, near p=10^{-4}; half of the frauds (bottom) have p\ge0.72, but a quarter have p<0.037 and look perfectly normal to the model.

(a) Scores of the 140{,}021 test transactions as log-odds; top: legitimate (thousands per bin), bottom: fraud (count per bin). A transaction is flagged when its score is to the right of the threshold. (b) Precision, recall and F_1 as the threshold moves (logarithmic axis); dotted lines at \tau=0.1 and 0.5. Computed for this tutorial, with the same data and model as the companion video.
Figure 2. (a) Scores of the 140{,}021 test transactions as log-odds; top: legitimate (thousands per bin), bottom: fraud (count per bin). A transaction is flagged when its score is to the right of the threshold. (b) Precision, recall and F_1 as the threshold moves (logarithmic axis); dotted lines at \tau=0.1 and 0.5. Computed for this tutorial, with the same data and model as the companion video.
Try itMove the threshold

The scores of all 140,021 test transactions, as in Figure 2: legitimate on top (thousands per bin), fraud below (count per bin). The line is the threshold \tau: a transaction is flagged when its fraud probability p \ge \tau, so everything to the right of the line is flagged. Right: the four boxes of the confusion matrix at this threshold, and the scores computed from them.

Lower the threshold to \tau=0.1. The line moves left, past 29 more frauds and 80 more legitimate transactions: \mathrm{TP}=147, \mathrm{FP}=125, \mathrm{FN}=64, so recall rises to 147/211=69.7\% and precision falls to 147/272=54.0\%. Table 1 shows the whole trade. Lowering the threshold can only add flagged transactions, so \mathrm{TP} and \mathrm{FP} never decrease and recall never decreases. Precision usually falls, but not always: it is a ratio, and it can go up when the newly flagged transactions are frauds, which is why the precision curve in Figure 2b wiggles.

threshold \taualarms\mathrm{TP}\mathrm{FP}\mathrm{FN}precisionrecallF_1
0.9130943611772.3\%44.5\%0.551
0.71471064110572.1\%50.2\%0.592
0.5163118459372.4\%55.9\%0.631
0.3189130598168.8\%61.6\%0.650
0.2205137687466.8\%64.9\%0.659
0.12721471256454.0\%69.7\%0.609
0.053881562325540.2\%73.9\%0.521
0.028061656414620.5\%78.2\%0.324
0.011{,}5581741{,}3843711.2\%82.5\%0.197
Table 1. The test day at nine thresholds (211 frauds, 139{,}810 legitimate transactions). Computed for this tutorial, with the same data and model as the companion video.

This trade-off was the first big finding of experimental information retrieval. In the Cranfield tests of indexing systems in the 1960s, Cleverdon et al. (1966, vol. 1, p. 2) described an “inevitable inverse relationship between recall and precision”. In one of their series of searches, as the number of index terms a document had to match was raised step by step, recall fell from 94.7\% to 3.2\% while precision rose from 0.3\% to 14.3\% (Cleverdon et al., 1966, vol. 2, p. 223): the same knob as our threshold.

The extremes are instructive. Flag nothing, and precision is undefined (0/0) while recall is 0. Flag everything, and recall is 100\% while precision falls to the prevalence, 211/140{,}021=0.151\%. Any recall can be bought; the question is at what precision. This is why a precision or a recall reported alone means nothing: each must come with the other, or with the threshold.

Pause and think. A colleague reports a fraud model with 99\% recall. What do you ask next?

Show the answer

“At what precision?” Flagging every transaction gives 100\% recall. Ask for the precision (or the number of alarms per day) at that threshold. On our test day, recall above 96\% needs a threshold near 10^{-4}, where more than 66{,}000 of the 140{,}021 transactions are flagged and the precision is about 0.3\%.