Precision-Recall Curve
The precision-recall curve (PR curve) plots precision against recall for a scoring classifier. The threshold sweeps from high to low. Recall is on the horizontal axis and precision on the vertical axis. Lowering flags more points, so recall never falls and precision usually does. This is the precision-recall tradeoff. At the lowest threshold every point is flagged. The curve then ends at recall 1 and precision , the prevalence of positives. Precision is undefined until at least one point is flagged. By convention the curve starts at the precision of the top-ranked point.
Precision at each threshold follows from the ROC rates and the prevalence:
Here TPR is the recall and FPR is the false positive rate from the confusion matrix. For a fixed , each ROC point maps to exactly one PR point. A random scorer has expected precision at every recall, so the baseline is a horizontal line at height .Average precision (AP) summarizes the curve in one number. It sums the precision at each threshold, weighted by the gain in recall at that threshold:
Here and are the precision and recall at the -th threshold, with . The count is the number of positives. The second form averages, over the positives, the precision among the points ranked at or above each one.
- Range: AP lies in . It is 1 for a perfect ranking and about for a random one.
- Name: AP is often reported as PR-AUC or AUPRC.
- Interpolation: the trapezoidal area under the PR points is a different number, because precision does not vary linearly between two points.
- Threshold: the maximum- threshold is the point on the curve with the largest . It is the usual choice when precision and recall matter equally.
The PR curve is the preferred threshold-free summary when positives are rare and the top of the ranking matters most. Fraud alerts, search and screening for rare diseases are typical cases. The ROC curve stays preferable when both classes matter or the prevalence differs between deployments.
Intuition
The names come from information retrieval. A search engine’s recall is the fraction of relevant documents it returns. Its precision is the fraction of returned documents that are relevant. The PR curve takes the viewpoint of whoever acts on the flagged cases. A fraud analyst who works down a ranked list of alerts catches more frauds with each step. Each step also adds legitimate transactions to review, and the curve records both effects together.
The curve never uses true negatives. In fraud detection or web search the negatives number in the millions, and almost all of them are easy. Dividing by that count keeps the false positive rate tiny, even when false alarms outnumber the real cases. Precision divides the false alarms by the number of flags, which stays small. The PR curve therefore changes sharply where the ROC curve moves only slightly to the right.
Properties
(Baseline Equals Prevalence) A random scorer has AP close to , so an AP value means little without the prevalence next to it.
An AP of 0.30 is a strong result when , thirty times the random baseline. The same AP is worse than random when . AP values from test sets with different prevalence cannot be compared directly. The same model scores a lower AP on a test set with fewer positives. Its ROC curve stays the same. Report AP together with , or report the ratio . Benchmarks that balance the classes by subsampling negatives report an AP that will not hold at the real prevalence.
(Why PR Beats ROC for Rare Positives) With rare positives, a small false positive rate can yield more false alarms than true positives.
Suppose a fraud model reaches at . With 1,000 frauds among 1,000,000 transactions, it flags 800 frauds and 9,990 legitimate transactions. The ROC point looks excellent. The PR point shows that about 93% of the alerts are false. ROC-AUC also gives equal weight to high-FPR regions that no fraud team would use. Saito and Rehmsmeier (2015) found that PR curves separate models on imbalanced data that ROC curves rate as nearly equal. Davis and Goadrich (2006) proved that one curve dominates another in ROC space exactly when it dominates in PR space. The two plots disagree only on how they weight the regions where neither model dominates.
The animation runs the threshold sweep on 6 positives and 34 negatives. The best is , and precision ends at the prevalence . The same scores have ROC AUC but AP only .
\usepackage{tikz}
\usetikzlibrary{animations, arrows.meta}
\begin{document}
\definecolor{hot}{HTML}{D9483B}
\definecolor{cold}{HTML}{2F80ED}
\definecolor{mid}{HTML}{8A8A8A}
\newcommand{\pf}[1]{\filldraw[draw=black, fill=hot] (#1) circle (2pt);}
\newcommand{\ph}[1]{\draw[hot, thick, fill=white] (#1) circle (1.8pt);}
\newcommand{\nf}[1]{\filldraw[draw=black, fill=cold] (#1) circle (2pt);}
\newcommand{\nh}[1]{\draw[cold, thick, fill=white] (#1) circle (1.8pt);}
\begin{tikzpicture}[font=\small, >={Stealth[length=2.2mm]}]
\draw (-0.10,1.5) -- (5.90,1.5);
\draw (0.00,1.43) -- (0.00,1.57) node[pos=0, below] {\footnotesize $0$};
\draw (2.90,1.43) -- (2.90,1.57) node[pos=0, below] {\footnotesize $0.5$};
\draw (5.80,1.43) -- (5.80,1.57) node[pos=0, below] {\footnotesize $1$};
\node at (2.90,0.78) {\footnotesize score $s$};
\node[anchor=east] at (-0.2,3.05) {$y = 1$};
\node[anchor=east] at (-0.2,2.25) {$y = 0$};
\draw (8.00,-1.40) rectangle (13.00,3.60);
\draw (8.00,-1.47) -- (8.00,-1.33) node[pos=0, below] {\footnotesize $0$};
\draw (7.93,-1.40) -- (8.07,-1.40) node[pos=0, left] {\footnotesize $0$};
\draw (10.50,-1.47) -- (10.50,-1.33) node[pos=0, below] {\footnotesize $0.5$};
\draw (7.93,1.10) -- (8.07,1.10) node[pos=0, left] {\footnotesize $0.5$};
\draw (13.00,-1.47) -- (13.00,-1.33) node[pos=0, below] {\footnotesize $1$};
\draw (7.93,3.60) -- (8.07,3.60) node[pos=0, left] {\footnotesize $1$};
\node at (10.50,-2.12) {recall};
\node[anchor=south] at (8.00,3.72) {precision};
\draw[mid, dashed, thick] (8.00,-0.65) -- (13.00,-0.65);
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 16.82s = "true", 25.02s = "true", repeats}}}]
\draw[mid, thick] (8.00,-1.40) rectangle (8.83,3.60) (8.83,-1.40) rectangle (9.67,3.60) (9.67,-1.40) rectangle (10.50,1.60) (10.50,-1.40) rectangle (11.33,1.46) (11.33,-1.40) rectangle (12.17,0.68) (12.17,-1.40) rectangle (13.00,0.48);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "true", 16.82s = "false", 25.02s = "false", repeats}}}]
\node[anchor=west] at (-0.95,0.3) {precision $= \mathrm{TP}/(\mathrm{TP} + \mathrm{FP})$};
\node[anchor=west] at (-0.95,-0.25) {recall $= \mathrm{TP}/6$};
\node[anchor=west] at (-0.95,-0.8) {$F_1 = 2\mathrm{TP}/(2\mathrm{TP} + \mathrm{FP} + \mathrm{FN})$};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 2.64s = "true", 25.02s = "true", repeats}}}]
\draw[hot, line width=1.8pt] (8.83,3.60) -- (9.67,3.60);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 5.04s = "true", 25.02s = "true", repeats}}}]
\draw[cold, line width=1.8pt] (9.67,3.60) -- (9.67,1.93);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 5.28s = "true", 25.02s = "true", repeats}}}]
\draw[cold, line width=1.8pt] (9.67,1.93) -- (9.67,1.10);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 5.52s = "true", 25.02s = "true", repeats}}}]
\draw[hot, line width=1.8pt] (9.67,1.10) -- (10.50,1.60);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 5.76s = "true", 25.02s = "true", repeats}}}]
\draw[cold, line width=1.8pt] (10.50,1.60) -- (10.50,1.10);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 6s = "true", 25.02s = "true", repeats}}}]
\draw[hot, line width=1.8pt] (10.50,1.10) -- (11.33,1.46);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 8.6s = "true", 25.02s = "true", repeats}}}]
\draw[cold, line width=1.8pt] (11.33,1.46) -- (11.33,1.10);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 8.84s = "true", 25.02s = "true", repeats}}}]
\draw[cold, line width=1.8pt] (11.33,1.10) -- (11.33,0.82);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 9.08s = "true", 25.02s = "true", repeats}}}]
\draw[cold, line width=1.8pt] (11.33,0.82) -- (11.33,0.60);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 9.32s = "true", 25.02s = "true", repeats}}}]
\draw[cold, line width=1.8pt] (11.33,0.60) -- (11.33,0.42);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 9.56s = "true", 25.02s = "true", repeats}}}]
\draw[hot, line width=1.8pt] (11.33,0.42) -- (12.17,0.68);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 9.8s = "true", 25.02s = "true", repeats}}}]
\draw[cold, line width=1.8pt] (12.17,0.68) -- (12.17,0.52);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10.04s = "true", 25.02s = "true", repeats}}}]
\draw[cold, line width=1.8pt] (12.17,0.52) -- (12.17,0.39);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10.28s = "true", 25.02s = "true", repeats}}}]
\draw[cold, line width=1.8pt] (12.17,0.39) -- (12.17,0.27);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10.52s = "true", 25.02s = "true", repeats}}}]
\draw[hot, line width=1.8pt] (12.17,0.27) -- (13.00,0.48);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 12.92s = "true", 25.02s = "true", repeats}}}]
\draw[cold, line width=1.8pt] (13.00,0.48) -- (13.00,0.36);
\draw[cold, line width=1.8pt] (13.00,0.36) -- (13.00,0.27);
\draw[cold, line width=1.8pt] (13.00,0.27) -- (13.00,0.18);
\draw[cold, line width=1.8pt] (13.00,0.18) -- (13.00,0.10);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 13.22s = "true", 25.02s = "true", repeats}}}]
\draw[cold, line width=1.8pt] (13.00,0.10) -- (13.00,0.03);
\draw[cold, line width=1.8pt] (13.00,0.03) -- (13.00,-0.04);
\draw[cold, line width=1.8pt] (13.00,-0.04) -- (13.00,-0.10);
\draw[cold, line width=1.8pt] (13.00,-0.10) -- (13.00,-0.15);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 13.52s = "true", 25.02s = "true", repeats}}}]
\draw[cold, line width=1.8pt] (13.00,-0.15) -- (13.00,-0.20);
\draw[cold, line width=1.8pt] (13.00,-0.20) -- (13.00,-0.25);
\draw[cold, line width=1.8pt] (13.00,-0.25) -- (13.00,-0.29);
\draw[cold, line width=1.8pt] (13.00,-0.29) -- (13.00,-0.33);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 13.82s = "true", 25.02s = "true", repeats}}}]
\draw[cold, line width=1.8pt] (13.00,-0.33) -- (13.00,-0.37);
\draw[cold, line width=1.8pt] (13.00,-0.37) -- (13.00,-0.40);
\draw[cold, line width=1.8pt] (13.00,-0.40) -- (13.00,-0.43);
\draw[cold, line width=1.8pt] (13.00,-0.43) -- (13.00,-0.46);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 14.12s = "true", 25.02s = "true", repeats}}}]
\draw[cold, line width=1.8pt] (13.00,-0.46) -- (13.00,-0.49);
\draw[cold, line width=1.8pt] (13.00,-0.49) -- (13.00,-0.52);
\draw[cold, line width=1.8pt] (13.00,-0.52) -- (13.00,-0.54);
\draw[cold, line width=1.8pt] (13.00,-0.54) -- (13.00,-0.57);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 14.42s = "true", 25.02s = "true", repeats}}}]
\draw[cold, line width=1.8pt] (13.00,-0.57) -- (13.00,-0.59);
\draw[cold, line width=1.8pt] (13.00,-0.59) -- (13.00,-0.61);
\draw[cold, line width=1.8pt] (13.00,-0.61) -- (13.00,-0.63);
\draw[cold, line width=1.8pt] (13.00,-0.63) -- (13.00,-0.65);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "true", 2.4s = "false", 25.02s = "false", repeats}}}]
\draw[thick] (5.684,1.38) -- (5.684,3.47) node[above] {$t$};
\node[anchor=west] at (3.55,0.3) {$= 0/0$, undefined};
\node[anchor=west] at (3.55,-0.25) {$= 0/6 = 0.00$};
\node[anchor=west] at (3.55,-0.8) {$= 0/6 = 0.00$};
\node[anchor=west] at (-0.95,-2.75) {Six of the $40$ points are positive, so the prevalence is $6/40 = 0.15$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 2.4s = "true", 2.64s = "false", 25.02s = "false", repeats}}}]
\draw[thick] (5.423,1.38) -- (5.423,3.47) node[above] {$t$};
\filldraw[draw=black, fill=black] (8.83,3.60) circle (2.8pt);
\node[anchor=west] at (3.55,0.3) {$= 1/1 = 1.00$};
\node[anchor=west] at (3.55,-0.25) {$= 1/6 = 0.17$};
\node[anchor=west] at (3.55,-0.8) {$= 2/7 = 0.29$};
\node[anchor=west] at (-0.95,-2.75) {A positive passes: recall and precision rise.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 2.64s = "true", 5.04s = "false", 25.02s = "false", repeats}}}]
\draw[thick] (5.162,1.38) -- (5.162,3.47) node[above] {$t$};
\filldraw[draw=black, fill=black] (9.67,3.60) circle (2.8pt);
\node[anchor=west] at (3.55,0.3) {$= 2/2 = 1.00$};
\node[anchor=west] at (3.55,-0.25) {$= 2/6 = 0.33$};
\node[anchor=west] at (3.55,-0.8) {$= 4/8 = 0.50$};
\node[anchor=west] at (-0.95,-2.75) {The two top scores are positive, so precision is $1$ at recall $0.33$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 5.04s = "true", 5.28s = "false", 25.02s = "false", repeats}}}]
\draw[thick] (4.930,1.38) -- (4.930,3.47) node[above] {$t$};
\filldraw[draw=black, fill=black] (9.67,1.93) circle (2.8pt);
\node[anchor=west] at (3.55,0.3) {$= 2/3 = 0.67$};
\node[anchor=west] at (3.55,-0.25) {$= 2/6 = 0.33$};
\node[anchor=west] at (3.55,-0.8) {$= 4/9 = 0.44$};
\node[anchor=west] at (-0.95,-2.75) {A negative passes: precision falls, recall stays the same.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 5.28s = "true", 5.52s = "false", 25.02s = "false", repeats}}}]
\draw[thick] (4.698,1.38) -- (4.698,3.47) node[above] {$t$};
\filldraw[draw=black, fill=black] (9.67,1.10) circle (2.8pt);
\node[anchor=west] at (3.55,0.3) {$= 2/4 = 0.50$};
\node[anchor=west] at (3.55,-0.25) {$= 2/6 = 0.33$};
\node[anchor=west] at (3.55,-0.8) {$= 4/10 = 0.40$};
\node[anchor=west] at (-0.95,-2.75) {A negative passes: precision falls, recall stays the same.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 5.52s = "true", 5.76s = "false", 25.02s = "false", repeats}}}]
\draw[thick] (4.495,1.38) -- (4.495,3.47) node[above] {$t$};
\filldraw[draw=black, fill=black] (10.50,1.60) circle (2.8pt);
\node[anchor=west] at (3.55,0.3) {$= 3/5 = 0.60$};
\node[anchor=west] at (3.55,-0.25) {$= 3/6 = 0.50$};
\node[anchor=west] at (3.55,-0.8) {$= 6/11 = 0.55$};
\node[anchor=west] at (-0.95,-2.75) {A positive passes: recall and precision rise.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 5.76s = "true", 6s = "false", 25.02s = "false", repeats}}}]
\draw[thick] (4.292,1.38) -- (4.292,3.47) node[above] {$t$};
\filldraw[draw=black, fill=black] (10.50,1.10) circle (2.8pt);
\node[anchor=west] at (3.55,0.3) {$= 3/6 = 0.50$};
\node[anchor=west] at (3.55,-0.25) {$= 3/6 = 0.50$};
\node[anchor=west] at (3.55,-0.8) {$= 6/12 = 0.50$};
\node[anchor=west] at (-0.95,-2.75) {A negative passes: precision falls, recall stays the same.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 6s = "true", 8.6s = "false", 25.02s = "false", repeats}}}]
\draw[thick] (4.060,1.38) -- (4.060,3.47) node[above] {$t$};
\filldraw[draw=black, fill=black] (11.33,1.46) circle (2.8pt);
\node[anchor=west] at (3.55,0.3) {$= 4/7 = 0.57$};
\node[anchor=west] at (3.55,-0.25) {$= 4/6 = 0.67$};
\node[anchor=west] at (3.55,-0.8) {$= 8/13 = 0.62$};
\node[anchor=west] at (-0.95,-2.75) {Here $F_1 = 0.62$, the highest of any threshold. Three of seven flags are false.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 8.6s = "true", 8.84s = "false", 25.02s = "false", repeats}}}]
\draw[thick] (3.828,1.38) -- (3.828,3.47) node[above] {$t$};
\filldraw[draw=black, fill=black] (11.33,1.10) circle (2.8pt);
\node[anchor=west] at (3.55,0.3) {$= 4/8 = 0.50$};
\node[anchor=west] at (3.55,-0.25) {$= 4/6 = 0.67$};
\node[anchor=west] at (3.55,-0.8) {$= 8/14 = 0.57$};
\node[anchor=west] at (-0.95,-2.75) {A negative passes: precision falls, recall stays the same.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 8.84s = "true", 9.08s = "false", 25.02s = "false", repeats}}}]
\draw[thick] (3.625,1.38) -- (3.625,3.47) node[above] {$t$};
\filldraw[draw=black, fill=black] (11.33,0.82) circle (2.8pt);
\node[anchor=west] at (3.55,0.3) {$= 4/9 = 0.44$};
\node[anchor=west] at (3.55,-0.25) {$= 4/6 = 0.67$};
\node[anchor=west] at (3.55,-0.8) {$= 8/15 = 0.53$};
\node[anchor=west] at (-0.95,-2.75) {A negative passes: precision falls, recall stays the same.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 9.08s = "true", 9.32s = "false", 25.02s = "false", repeats}}}]
\draw[thick] (3.422,1.38) -- (3.422,3.47) node[above] {$t$};
\filldraw[draw=black, fill=black] (11.33,0.60) circle (2.8pt);
\node[anchor=west] at (3.55,0.3) {$= 4/10 = 0.40$};
\node[anchor=west] at (3.55,-0.25) {$= 4/6 = 0.67$};
\node[anchor=west] at (3.55,-0.8) {$= 8/16 = 0.50$};
\node[anchor=west] at (-0.95,-2.75) {A negative passes: precision falls, recall stays the same.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 9.32s = "true", 9.56s = "false", 25.02s = "false", repeats}}}]
\draw[thick] (3.190,1.38) -- (3.190,3.47) node[above] {$t$};
\filldraw[draw=black, fill=black] (11.33,0.42) circle (2.8pt);
\node[anchor=west] at (3.55,0.3) {$= 4/11 = 0.36$};
\node[anchor=west] at (3.55,-0.25) {$= 4/6 = 0.67$};
\node[anchor=west] at (3.55,-0.8) {$= 8/17 = 0.47$};
\node[anchor=west] at (-0.95,-2.75) {A negative passes: precision falls, recall stays the same.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 9.56s = "true", 9.8s = "false", 25.02s = "false", repeats}}}]
\draw[thick] (2.958,1.38) -- (2.958,3.47) node[above] {$t$};
\filldraw[draw=black, fill=black] (12.17,0.68) circle (2.8pt);
\node[anchor=west] at (3.55,0.3) {$= 5/12 = 0.42$};
\node[anchor=west] at (3.55,-0.25) {$= 5/6 = 0.83$};
\node[anchor=west] at (3.55,-0.8) {$= 10/18 = 0.56$};
\node[anchor=west] at (-0.95,-2.75) {A positive passes: recall and precision rise.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 9.8s = "true", 10.04s = "false", 25.02s = "false", repeats}}}]
\draw[thick] (2.755,1.38) -- (2.755,3.47) node[above] {$t$};
\filldraw[draw=black, fill=black] (12.17,0.52) circle (2.8pt);
\node[anchor=west] at (3.55,0.3) {$= 5/13 = 0.38$};
\node[anchor=west] at (3.55,-0.25) {$= 5/6 = 0.83$};
\node[anchor=west] at (3.55,-0.8) {$= 10/19 = 0.53$};
\node[anchor=west] at (-0.95,-2.75) {A negative passes: precision falls, recall stays the same.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10.04s = "true", 10.28s = "false", 25.02s = "false", repeats}}}]
\draw[thick] (2.581,1.38) -- (2.581,3.47) node[above] {$t$};
\filldraw[draw=black, fill=black] (12.17,0.39) circle (2.8pt);
\node[anchor=west] at (3.55,0.3) {$= 5/14 = 0.36$};
\node[anchor=west] at (3.55,-0.25) {$= 5/6 = 0.83$};
\node[anchor=west] at (3.55,-0.8) {$= 10/20 = 0.50$};
\node[anchor=west] at (-0.95,-2.75) {A negative passes: precision falls, recall stays the same.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10.28s = "true", 10.52s = "false", 25.02s = "false", repeats}}}]
\draw[thick] (2.378,1.38) -- (2.378,3.47) node[above] {$t$};
\filldraw[draw=black, fill=black] (12.17,0.27) circle (2.8pt);
\node[anchor=west] at (3.55,0.3) {$= 5/15 = 0.33$};
\node[anchor=west] at (3.55,-0.25) {$= 5/6 = 0.83$};
\node[anchor=west] at (3.55,-0.8) {$= 10/21 = 0.48$};
\node[anchor=west] at (-0.95,-2.75) {A negative passes: precision falls, recall stays the same.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10.52s = "true", 12.92s = "false", 25.02s = "false", repeats}}}]
\draw[thick] (2.175,1.38) -- (2.175,3.47) node[above] {$t$};
\filldraw[draw=black, fill=black] (13.00,0.48) circle (2.8pt);
\node[anchor=west] at (3.55,0.3) {$= 6/16 = 0.38$};
\node[anchor=west] at (3.55,-0.25) {$= 6/6 = 1.00$};
\node[anchor=west] at (3.55,-0.8) {$= 12/22 = 0.55$};
\node[anchor=west] at (-0.95,-2.75) {The last positive passes: recall reaches $1$, precision is $6/16 = 0.38$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 12.92s = "true", 13.22s = "false", 25.02s = "false", repeats}}}]
\draw[thick] (1.740,1.38) -- (1.740,3.47) node[above] {$t$};
\filldraw[draw=black, fill=black] (13.00,0.10) circle (2.8pt);
\node[anchor=west] at (3.55,0.3) {$= 6/20 = 0.30$};
\node[anchor=west] at (3.55,-0.25) {$= 6/6 = 1.00$};
\node[anchor=west] at (3.55,-0.8) {$= 12/26 = 0.46$};
\node[anchor=west] at (-0.95,-2.75) {Only negatives remain. Each one lowers precision; recall stays at $1$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 13.22s = "true", 13.52s = "false", 25.02s = "false", repeats}}}]
\draw[thick] (1.363,1.38) -- (1.363,3.47) node[above] {$t$};
\filldraw[draw=black, fill=black] (13.00,-0.15) circle (2.8pt);
\node[anchor=west] at (3.55,0.3) {$= 6/24 = 0.25$};
\node[anchor=west] at (3.55,-0.25) {$= 6/6 = 1.00$};
\node[anchor=west] at (3.55,-0.8) {$= 12/30 = 0.40$};
\node[anchor=west] at (-0.95,-2.75) {Only negatives remain. Each one lowers precision; recall stays at $1$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 13.52s = "true", 13.82s = "false", 25.02s = "false", repeats}}}]
\draw[thick] (1.044,1.38) -- (1.044,3.47) node[above] {$t$};
\filldraw[draw=black, fill=black] (13.00,-0.33) circle (2.8pt);
\node[anchor=west] at (3.55,0.3) {$= 6/28 = 0.21$};
\node[anchor=west] at (3.55,-0.25) {$= 6/6 = 1.00$};
\node[anchor=west] at (3.55,-0.8) {$= 12/34 = 0.35$};
\node[anchor=west] at (-0.95,-2.75) {Only negatives remain. Each one lowers precision; recall stays at $1$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 13.82s = "true", 14.12s = "false", 25.02s = "false", repeats}}}]
\draw[thick] (0.725,1.38) -- (0.725,3.47) node[above] {$t$};
\filldraw[draw=black, fill=black] (13.00,-0.46) circle (2.8pt);
\node[anchor=west] at (3.55,0.3) {$= 6/32 = 0.19$};
\node[anchor=west] at (3.55,-0.25) {$= 6/6 = 1.00$};
\node[anchor=west] at (3.55,-0.8) {$= 12/38 = 0.32$};
\node[anchor=west] at (-0.95,-2.75) {Only negatives remain. Each one lowers precision; recall stays at $1$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 14.12s = "true", 14.42s = "false", 25.02s = "false", repeats}}}]
\draw[thick] (0.435,1.38) -- (0.435,3.47) node[above] {$t$};
\filldraw[draw=black, fill=black] (13.00,-0.57) circle (2.8pt);
\node[anchor=west] at (3.55,0.3) {$= 6/36 = 0.17$};
\node[anchor=west] at (3.55,-0.25) {$= 6/6 = 1.00$};
\node[anchor=west] at (3.55,-0.8) {$= 12/42 = 0.29$};
\node[anchor=west] at (-0.95,-2.75) {Only negatives remain. Each one lowers precision; recall stays at $1$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 14.42s = "true", 16.82s = "false", 25.02s = "false", repeats}}}]
\draw[thick] (0.058,1.38) -- (0.058,3.47) node[above] {$t$};
\filldraw[draw=black, fill=black] (13.00,-0.65) circle (2.8pt);
\node[anchor=west] at (3.55,0.3) {$= 6/40 = 0.15$};
\node[anchor=west] at (3.55,-0.25) {$= 6/6 = 1.00$};
\node[anchor=west] at (3.55,-0.8) {$= 12/46 = 0.26$};
\node[anchor=west] at (-0.95,-2.75) {Below every score, precision falls to the prevalence, $0.15$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 16.82s = "true", 20.42s = "false", 25.02s = "false", repeats}}}]
\node[anchor=west] at (-0.95,0.3) {average precision (AP): the mean precision};
\node[anchor=west] at (-0.95,-0.25) {at the six positives, the bar heights};
\node[anchor=west] at (-0.95,-0.9) {AP $= (1 + 1 + 0.60 + 0.57 + 0.42 + 0.38)/6 = 0.66$};
\node[anchor=west] at (-0.95,-2.75) {AP is the total area of the six bars. Each bar has width $1/6$, one per positive.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 20.42s = "true", 25.02s = "true", repeats}}}]
\draw (0.5,-1.8) rectangle (2.45,0.15);
\draw[mid, dashed] (0.5,-1.8) -- (2.45,0.15);
\draw[very thick] (0.50,-1.80) -- (0.50,-1.48) -- (0.50,-1.15) -- (0.56,-1.15) -- (0.61,-1.15) -- (0.61,-0.83) -- (0.67,-0.83) -- (0.67,-0.50) -- (0.73,-0.50) -- (0.79,-0.50) -- (0.84,-0.50) -- (0.90,-0.50) -- (0.90,-0.18) -- (0.96,-0.18) -- (1.02,-0.18) -- (1.07,-0.18) -- (1.07,0.15) -- (1.13,0.15) -- (1.19,0.15) -- (1.25,0.15) -- (1.30,0.15) -- (1.36,0.15) -- (1.42,0.15) -- (1.48,0.15) -- (1.53,0.15) -- (1.59,0.15) -- (1.65,0.15) -- (1.70,0.15) -- (1.76,0.15) -- (1.82,0.15) -- (1.88,0.15) -- (1.93,0.15) -- (1.99,0.15) -- (2.05,0.15) -- (2.11,0.15) -- (2.16,0.15) -- (2.22,0.15) -- (2.28,0.15) -- (2.34,0.15) -- (2.39,0.15) -- (2.45,0.15);
\node[below] at (1.48,-1.80) {\footnotesize FPR};
\node[left] at (0.50,-0.83) {\footnotesize TPR};
\node[anchor=west] at (2.65,-0.10) {ROC of the};
\node[anchor=west] at (2.65,-0.60) {same scores:};
\node[anchor=west] at (2.65,-1.20) {AUC $= 0.89$};
\node[anchor=west] at (-0.95,-2.75) {ROC AUC is $0.89$, but AP is $0.66$. With few positives, each false alarm costs much precision.};
\end{scope}
\begin{scope}[]
\ph{5.57,3.05} \ph{5.28,3.05} \ph{4.58,3.05} \ph{4.18,3.05} \ph{3.07,3.05} \ph{2.26,3.05}
\nh{5.05,2.25} \nh{4.81,2.25} \nh{4.41,2.25} \nh{3.94,2.25} \nh{3.71,2.25} \nh{3.54,2.25} \nh{3.31,2.25} \nh{2.84,2.25} \nh{2.67,2.25} \nh{2.49,2.25} \nh{0.12,2.25} \nh{0.23,2.46} \nh{0.35,2.25} \nh{0.46,2.04} \nh{0.52,2.25} \nh{0.64,2.46} \nh{0.70,2.25} \nh{0.81,2.46} \nh{0.93,2.25} \nh{0.99,2.46} \nh{1.10,2.25} \nh{1.22,2.46} \nh{1.28,2.25} \nh{1.39,2.46} \nh{1.51,2.25} \nh{1.57,2.46} \nh{1.68,2.25} \nh{1.80,2.46} \nh{1.86,2.25} \nh{1.97,2.46} \nh{2.09,2.25} \nh{0.75,2.04} \nh{0.41,2.46} \nh{1.33,2.04}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 2.4s = "true", 25.02s = "true", repeats}}}]
\pf{5.57,3.05}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 2.64s = "true", 25.02s = "true", repeats}}}]
\pf{5.28,3.05}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 5.04s = "true", 25.02s = "true", repeats}}}]
\nf{5.05,2.25}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 5.28s = "true", 25.02s = "true", repeats}}}]
\nf{4.81,2.25}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 5.52s = "true", 25.02s = "true", repeats}}}]
\pf{4.58,3.05}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 5.76s = "true", 25.02s = "true", repeats}}}]
\nf{4.41,2.25}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 6s = "true", 25.02s = "true", repeats}}}]
\pf{4.18,3.05}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 8.6s = "true", 25.02s = "true", repeats}}}]
\nf{3.94,2.25}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 8.84s = "true", 25.02s = "true", repeats}}}]
\nf{3.71,2.25}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 9.08s = "true", 25.02s = "true", repeats}}}]
\nf{3.54,2.25}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 9.32s = "true", 25.02s = "true", repeats}}}]
\nf{3.31,2.25}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 9.56s = "true", 25.02s = "true", repeats}}}]
\pf{3.07,3.05}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 9.8s = "true", 25.02s = "true", repeats}}}]
\nf{2.84,2.25}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10.04s = "true", 25.02s = "true", repeats}}}]
\nf{2.67,2.25}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10.28s = "true", 25.02s = "true", repeats}}}]
\nf{2.49,2.25}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10.52s = "true", 25.02s = "true", repeats}}}]
\pf{2.26,3.05}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 12.92s = "true", 25.02s = "true", repeats}}}]
\nf{2.09,2.25} \nf{1.97,2.46} \nf{1.86,2.25} \nf{1.80,2.46}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 13.22s = "true", 25.02s = "true", repeats}}}]
\nf{1.68,2.25} \nf{1.57,2.46} \nf{1.51,2.25} \nf{1.39,2.46}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 13.52s = "true", 25.02s = "true", repeats}}}]
\nf{1.33,2.04} \nf{1.28,2.25} \nf{1.22,2.46} \nf{1.10,2.25}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 13.82s = "true", 25.02s = "true", repeats}}}]
\nf{0.99,2.46} \nf{0.93,2.25} \nf{0.81,2.46} \nf{0.75,2.04}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 14.12s = "true", 25.02s = "true", repeats}}}]
\nf{0.70,2.25} \nf{0.64,2.46} \nf{0.52,2.25} \nf{0.46,2.04}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 14.42s = "true", 25.02s = "true", repeats}}}]
\nf{0.41,2.46} \nf{0.35,2.25} \nf{0.23,2.46} \nf{0.12,2.25}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 6s = "true", 25.02s = "true", repeats}}}]
\draw[very thick] (11.33,1.46) circle (5pt);
\node[anchor=south] at (11.33,1.66) {\footnotesize max $F_1 = 0.62$};
\end{scope}
\node[anchor=west, align=left] at (13.20,-0.65) {\footnotesize prevalence \\ \footnotesize $0.15$};
\end{tikzpicture}
\end{document}(Precision Is Not Monotone) Precision can rise or fall as the threshold drops, so the PR curve zigzags.
Each positive added at the next threshold raises precision or keeps it at 1, and each negative lowers it. The ROC curve, by contrast, only moves up or right. Straight lines between PR points are wrong. Between two achievable points, true and false positives grow together, and precision follows a curve. Take 10 positives and two points on the curve. One has 5 true and 0 false positives, and the other has 10 true and 50 false. Recall goes from 0.5 to 1 and precision from 1 to 0.167. A straight line predicts precision 0.583 at recall 0.75. Interpolating the counts gives 7.5 true and 25 false positives, a precision of only 0.231. AP avoids interpolation by using the step sum, and libraries such as scikit-learn compute it that way.
(The Max-F1 Threshold) For calibrated probabilities, the threshold that maximizes equals half of the best (Lipton, Elkan and Naryanaswamy 2014).
Write and use . The denominator becomes the number of flagged points plus . Flagging one more point with calibrated probability adds expected true positives and 1 to the denominator. The expected then rises exactly when . The optimal rule therefore flags every point with , where is the best achievable . Since , the threshold never exceeds 0.5. When positives are rare and is low, the threshold falls well below 0.5. For uncalibrated scores, choose the threshold on a validation set instead.
Examples
(Easy) Average Precision for Ten Alerts
A fraud model ranks ten transactions by score. Four of them are fraud. Write F for fraud and L for legitimate. From the top, the labels read F, L, F, F, L, L, F, L, L, L.
Compute precision and recall at each cutoff, the average precision and the best . Flagging the top transactions for gives precision 1, 0.5, 0.667, 0.75, 0.6, 0.5, 0.571, 0.5, 0.444 and 0.4. Recall reaches 0.25 at the first fraud, 0.5 at the second, 0.75 at the third and 1 at the fourth. AP averages the precision at the four frauds, . The score is largest at , where precision and recall both equal 0.75, so . The cutoff catches every fraud with . The random baseline is the prevalence, .
(Medium) ROC and PR Disagree
A test set has 1,000 legitimate transactions and 10 frauds, so . Model A ranks five frauds at positions 1 to 5. It ranks the other five at positions 200, 250, 300, 350 and 400. Model B ranks its ten frauds at positions 30, 45, 60, …, 165, in steps of 15. A fraud analyst reviews the top 20 alerts each day.
Compute ROC-AUC and AP for both models. Which model should the analyst use? , and the same count gives 0.908 for Model B. For AP, Model A has precision 1 at each of its first five frauds. Its later frauds add . Its AP is . Model B's first fraud sits at position 30 with precision 0.033, and its AP is 0.053. ROC-AUC prefers Model B, while AP prefers Model A by a factor of ten. The analyst's top 20 alerts hold five frauds under Model A and none under Model B. AP weights the top of the ranking, which is where the analyst works.
For ROC-AUC, count the legitimate transactions ranked below each fraud. Model A’s first five frauds beat all 1,000. The fraud at position 200 has 194 legitimate transactions above it, so it beats 806. The other four beat 757, 708, 659 and 610. Model A’s AUC is
(Hard) The Max-F1 Threshold for a Readmission Model
A hospital model sorts discharged patients into four groups with calibrated readmission probabilities 0.9, 0.6, 0.3 and 0.05. The groups hold 5%, 10%, 20% and 65% of patients. The hospital will phone the flagged patients and wants the largest .
Which groups should the hospital flag? Check the answer with the rule. The prevalence is . Work per patient. Flagging only the 0.9 group gives expected , and , so . Adding the 0.6 group gives , , and . Adding the 0.3 group gives , , and . Flagging everyone gives . The best choice flags the top two groups, with . The rule flags a group exactly when its probability exceeds . The 0.3 group falls slightly below this line, so adding it changes by only 0.0016. Any threshold above 0.3 and up to 0.6 flags the best groups, so the default of 0.5 also works here. The rule places the boundary at 0.302, well below 0.5.
See Also
- 2.2.1. Confusion Matrix, for precision, recall and at one threshold.
- 2.2.2. ROC Curve, the curve that ignores prevalence.
- 2.2.4. Log Loss, for checking the calibration that the rule assumes.
- 1.6 Bayes’ Rule, which links precision to TPR, FPR and prevalence.