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

04F1, the harmonic mean

4.1Why not the plain average?

Every threshold \tau (flag a transaction when its fraud probability p\ge\tau) gives a pair (P,R) of precision P and recall R, and comparing pairs is awkward: is (72.4\%,55.9\%) better than (54.0\%,69.7\%)? We would like one number. The obvious candidate is the arithmetic mean (P+R)/2, and it fails in the worst possible way. Flag every transaction: R=1 and P=\pi=0.0015, so the average is (0.0015+1)/2=50.1\%, for a model that does nothing at all. Murphy (2022, §5.1.4.3) makes the same point with a prevalence of 10^{-4}: the arithmetic mean is about 50\%, the harmonic mean about 0.02\%. The arithmetic mean lets a perfect score on one side buy any failure on the other (Figure 3a: the flag-everything point sits on the 0.5 contour).

4.2The harmonic mean

The F_1 score is the harmonic mean of precision and recall (Murphy, 2022, eqs. 5.26–5.27):

\frac1{F_1}=\frac12\Bigl(\frac1P+\frac1R\Bigr),\qquad F_1=\frac{2PR}{P+R}=\frac{2\,\mathrm{TP}}{2\,\mathrm{TP}+\mathrm{FP}+\mathrm{FN}}. \tag{4}

The last form follows by substituting (2) and (3): 1/P=(\mathrm{TP}+\mathrm{FP})/\mathrm{TP} and 1/R=(\mathrm{TP}+\mathrm{FN})/\mathrm{TP}, so 2/F_1=(2\mathrm{TP}+\mathrm{FP}+\mathrm{FN})/\mathrm{TP}. It says that F_1 is the fraction of correct flags among the flags and the frauds counted together, and again the true negatives do not appear. Flag-everything now scores 2\times0.0015\times1/1.0015=0.30\%, and our model at \tau=0.5 scores 2\times118/(236+45+93)=0.631.

Proposition 1 (The harmonic mean follows the smaller value). For P,R>0, \min(P,R)\le F_1\le 2\min(P,R), and F_1\le(P+R)/2 with equality only when P=R.

Let m=\min(P,R) and M=\max(P,R), so F_1=2mM/(m+M). Since M\ge m, 2M\ge m+M, so F_1\ge m; since m>0, m+M>M, so F_1<2m. Finally (P+R)/2-2PR/(P+R)=(P-R)^2/[2(P+R)]\ge0.

So F_1 can never be more than twice the weaker of the two, like a chain that is only as strong as its weakest link. In Figure 3b the contours of F_1 bend toward the axes: to reach F_1=0.8 both precision and recall must be at least 0.67. A useful mental picture is resistors in parallel: the harmonic mean, like the combined resistance, is dominated by the smallest term. And for averaging rates the harmonic mean is the natural choice, as in the classic puzzle of driving to work at 60 and home at 40 kilometres per hour: the average speed is 48, not 50.

Contours of two ways to average precision P and recall R. Green: the pairs (R,P) of our model as the threshold moves (its precision–recall curve). Red: flag everything (R=1, P=0.0015). (a) The arithmetic mean gives flag-everything 50.1\%. (b) F_1 gives it 0.3\% and our model at \tau=0.5 gives it 0.63. Computed for this tutorial, with the same data and model as the companion video.
Figure 3. Contours of two ways to average precision P and recall R. Green: the pairs (R,P) of our model as the threshold moves (its precision–recall curve). Red: flag everything (R=1, P=0.0015). (a) The arithmetic mean gives flag-everything 50.1\%. (b) F_1 gives it 0.3\% and our model at \tau=0.5 gives it 0.63. Computed for this tutorial, with the same data and model as the companion video.
Try itWhich average can be fooled?

Set a precision P and a recall R. Left: the point on the contours of F_1, as in Figure 3b. Right: the arithmetic mean, F_1 (equation (4)) and F_\beta (equation (5)) of the same pair.

4.3F-beta: when one side matters more

F_1 weights precision and recall equally. The weighted version (Murphy, 2022, eqs. 5.24–5.25), which goes back to the effectiveness measure of van Rijsbergen (1979, ch. 7), is

\frac1{F_\beta}=\frac{1}{1+\beta^2}\,\frac1P+\frac{\beta^2}{1+\beta^2}\,\frac1R,\qquad F_\beta=\frac{(1+\beta^2)PR}{\beta^2P+R}=\frac{(1+\beta^2)\mathrm{TP}}{(1+\beta^2)\mathrm{TP}+\beta^2\mathrm{FN}+\mathrm{FP}}. \tag{5}

In the harmonic average, 1/R gets \beta^2 times the weight of 1/P; van Rijsbergen (1979) defined \beta so that it describes a user who attaches \beta times as much importance to recall as to precision. So \beta=2 favors recall and \beta=0.5 favors precision. On our test day, \tau=0.5 (high precision) wins on F_{0.5}, 0.684 against 0.566 for \tau=0.1, while \tau=0.1 (higher recall) wins on F_2, 0.659 against 0.586. The metric decides the threshold, so choose \beta from the application before looking at the numbers.

Pause and think. Your F_1 is 0.60. What can you say about precision and recall without seeing them?

Show the answer

By Proposition 1, both are at least 0.30, since F_1\le2\min(P,R). And the smaller one is at most 0.60, since F_1\ge\min(P,R). With P=R both would be 0.60.

4.4What F1 still misses

F_1 grades one threshold. Change \tau and it changes: 0.551 at 0.9, 0.659 at 0.2, 0.197 at 0.01 (Table 1). It also depends on the prevalence: Murphy (2022, eq. 5.31) writes F_\beta=(1+\beta^2)\mathrm{TPR}/(\mathrm{TPR}+\mathrm{FPR}/r+\beta^2) with r=n_+/n_-, the number of frauds n_+ divided by the number of legitimate transactions n_-, so the same model gets a lower F_1 on a day with fewer frauds. To compare models before a threshold is chosen, we need a measure of the score itself.