Skip to content
Raisalon

Machine Learning

Average precision vs. PR AUC: why the numbers differ

Compute average precision and trapezoidal PR AUC from four scored examples. See exactly which segment of the precision–recall curve changes the reported area.

Author
3 min read

Average precision (AP) and trapezoidal area under the precision–recall curve (PR AUC) can disagree even when the labels, scores and thresholds are identical. AP weights the precision reached at each increase in recall. The trapezoidal rule draws straight lines between adjacent points. Four predictions are enough to see exactly where the two calculations part company.

Walk down the ranked predictions

Suppose a classifier assigns four cases scores of 0.90, 0.80, 0.70 and 0.10. The first and third cases are positive. Include a case when its score is at least the threshold. Lowering the threshold admits the cases one at a time. Precision counts positives among the cases flagged, while recall counts the fraction of the two actual positives found.

Threshold Cases flagged Positives found Recall Precision
Above 0.90 0 0 0 1 by plotting convention
0.90 1 1 0.5 1
0.80 2 1 0.5 0.5
0.70 3 2 1 2/3
0.10 4 2 1 0.5

The precision value at the empty starting point closes the plotted curve. No one reviewed any cases there. The final point’s precision equals the positive fraction in this dataset because every case has been flagged. When reproducing a library result, include its documented endpoints and check which direction the recall array runs. scikit-learn precision–recall curve reference

Compare the two areas by hand

Recall increases twice, each time by one half. On the first increase the endpoint precision is one. On the second it is two thirds. AP therefore equals half of one plus half of two thirds, or 5/6 ≈ 0.8333. The negative at threshold 0.80 contributes no recall on its own, yet it reduces the precision available when the second positive enters. scikit-learn average precision definition

For trapezoidal area, the first interval again contributes 0.5. The next interval begins at precision 0.5 and ends at two thirds. Its contribution is the interval width, 0.5, multiplied by the mean of those endpoint values. That gives 7/24 for the second interval and 19/24 ≈ 0.7917 overall. Vertical moves have zero width and add no area to either total.

Precision versus recall for four thresholds. Between recall 0.5 and 1, average precision uses 2/3 across the interval while the trapezoid rises from 0.5 to 2/3.

The gap is 1/24 in this example. Its sign depends on the direction of the precision change over intervals with nonzero recall width. Saying that one summary always exceeds the other would give the wrong intuition for another ranking. A number labeled “PR AUC” in an experiment tracker needs the calculation method beside it.

Verify the calculation with exact fractions

The snippet uses Python’s standard library and takes the ordered recall and precision points from the table. Fractions keep the difference visible without rounding until the end. The function assumes recall proceeds from zero to one. Reverse both arrays together if your plotting function returns decreasing recall.

from fractions import Fraction as F

points = [(0, 1), (F(1, 2), 1), (F(1, 2), F(1, 2)),
          (1, F(2, 3)), (1, F(1, 2))]
ap = F(0)
trapezoid = F(0)
for (r0, p0), (r1, p1) in zip(points, points[1:]):
    width = r1 - r0
    ap += width * p1
    trapezoid += width * (p0 + p1) / 2
print(ap, trapezoid, ap - trapezoid)  # 5/6 19/24 1/24

When several cases share the same score, a threshold admits them together. Breaking a tie by input order creates intermediate operating points that a score threshold cannot select. Group tied scores before computing the curve. Also pass continuous scores when evaluating ranking quality. Hard class predictions throw away most of the ranking before either area calculation starts.

Translate a curve back into a work queue

At threshold 0.90 the reviewer sees one case, finds one positive and misses the other. At threshold 0.70 the reviewer sees three cases and finds both positives. Neither area tells you which threshold to deploy. A team that can inspect one case needs a different operating point from one that can inspect three.

If two reported scores disagree, compare the actual positive class, the exact evaluation rows and the sample weights. Then check the interpolation rule, how ties were grouped, and how results were averaged across classes. For the last point, macro, weighted and micro averaging illustrates how much a change in weighting can matter even with the same predictions.

Next steps

Give each stored metric an unambiguous name such as “binary average precision” or “trapezoidal precision–recall area.” Keep the score-and-label fixture with your evaluation code. It is small enough to calculate by hand and distinguishes both implementations with exact expected answers.

Share

Written by