Blog

Precision-Recall Relationship

Swaroop Nath · August 2023

For the purpose of this discussion, we will assume the following things –

  1. A-1.We are looking at classifiers which define a linear decision boundary in some embedding space, such as SVM, Logistic Regression, etc.
  2. A-2.The data is nearly linearly separable, that is with the linear decision boundary most of the negative class data (-1) lie on one side and most of the positive class data (+1) lie on the other side.
  3. A-3.The data is nearly balanced, that is \(|{-1}| \sim |{+1}|\).

Precision is defined as the fraction of predicted positive samples which are actually positive (Equation 1). Recall is defined as the fraction of true positive samples which are predicted as positive (Equation 2).

(1)
\[ \begin{aligned} P &= \frac{\textit{true\_positive}}{\textit{true\_positive} + \textit{false\_positive}} \\ &= \frac{TP}{TP + FP} \end{aligned} \]
(2)
\[ \begin{aligned} R &= \frac{\textit{true\_positive}}{\textit{true\_positive} + \textit{false\_negative}} \\ &= \frac{\textit{true\_positive}}{\textit{total\_actual\_true}} \\ &= \frac{TP}{TAT} \end{aligned} \]

The entities \(TP\), \(FP\) and \(FN\) are all dependent on the decision threshold \((\theta_t)\). Formally, we state certain properties in the Equations below, 3 – 5. Note that \(TP + FN\) denote the total number of positive samples in the test set, we denote that by \(TAT\) in the equations above.

(3)
\[ TP = f(\theta_t), \quad FP = g(\theta_t) \]
(4)
\[ f(\theta_t) \ge 0,\ \forall\, \theta_t; \quad g(\theta_t) \ge 0,\ \forall \theta_t \]
(5)
\[ f'(\theta_t) < 0,\ \forall\, \theta_t; \quad g'(\theta_t) < 0,\ \forall \theta_t \]
+1 density −1 density TP FP FN
Figure 1. Typical density of +1 (green) and -1 (red) classes in a well trained classifier. The black line denotes a possible decision boundary – in this case, it is the optimal decision boundary, \(\theta_c\). Interactive The solid line is now the threshold \(\theta_t\): drag it on the plot or use the slider. Dotted lines mark \(\theta_c\), where the two densities cross, and \(\theta_{+1}\), past which no -1 examples remain. The two classes are mixtures of Gaussians shaped like the original figure, with 1,000 examples each.

In the context of Figure 1, one can appreciate the following inequalities (Equations 6 – 8). Equation 6 highlights that the amount of True Positives is always greater than the False Positives. For a nearly balanced dataset, and well-learned classifier, this inequality holds invariably. Equations 7 and 8 depict the relation between the magnitude of gradients \(f'(\theta_t)\) and \(g'(\theta_t)\). The equilibrium \((\theta_c)\) is an optimal decision threshold when the density of FP and FN are roughly the same. To the left of \(\theta_c\), as \(\theta_t\) increases, the rate of reduction of TP is less than the rate of reduction of FP. And, to the right of \(\theta_c\), the rate of reduction of TP is more than the rate of reduction of FP. This can be appreciated with reference to Figure 1.

(6)
\[ f(\theta_t) > g(\theta_t),\ \forall\, \theta_t \]
(7)
\[ |f'(\theta_t)| < |g'(\theta_t)| \implies f'(\theta_t) > g'(\theta_t),\ \forall\, \theta_t < \theta_c \]
(8)
\[ |f'(\theta_t)| > |g'(\theta_t)| \implies f'(\theta_t) < g'(\theta_t),\ \forall\, \theta_t > \theta_c \]

Interactive \(f\) and \(g\) are the counts above the threshold. The dashed segments are their slopes at \(\theta_t\). Move \(\theta_t\) across \(\theta_c\) to see Equation 7 hand over to Equation 8.

\(f(\theta) = TP\) \(g(\theta) = FP\)

    Let us now look at the derivatives of Precision and Recall wrt \(\theta_t\).

    (9)
    \[ \begin{aligned} \frac{d}{d\theta_t} R &= \frac{1}{TAT} f'(\theta_t) < 0 \\ \frac{d}{d\theta_t} R &< 0 \end{aligned} \]

    From Equation 9 it is understandable that Recall decreases with increase in \(\theta_t\).

    \[ \frac{d}{d\theta_t} P = \frac{f'(\theta_t)\, g(\theta_t) - f(\theta_t)\, g'(\theta_t)}{\big(f(\theta_t) + g(\theta_t)\big)^2} \]

    For \(\theta_t < \theta_c\), we have \(f(\theta_t) > g(\theta_t) \implies f(\theta_t)f'(\theta_t) < g(\theta_t)f'(\theta_t)\). And we also have \(f'(\theta_t) > g'(\theta_t) \implies f(\theta_t)f'(\theta_t) > f(\theta_t)g'(\theta_t)\). Thus, we have \(f(\theta_t)g'(\theta_t) < g(\theta_t)f'(\theta_t) \implies f'(\theta_t)g(\theta_t) - g'(\theta_t)f(\theta_t) > 0\). Hence, we have \(\frac{d}{d\theta_t} P > 0\).

    Let \(\theta_{+1}\) be the value of threshold beyond which no -1 examples exist. Hence, for \(\theta_t > \theta_{+1}\), we have \(g(\theta_t) = 0\) and \(g'(\theta_t) = 0\). From Equation 1, we can conclude that the Precision is 1. For \(\theta_c < \theta_t < \theta_{+1}\), \(g(\theta_t) > 0\), hence Precision \(< 1\). Intuitively, we can thus see that Precision increases within this range too. Although, in this mathematical treatment there is no justification to suggest a monotonical increase, yet, the overall Precision increases from \(\theta_c\) to \(\theta_{+1}\). Thus, we roughly have

    (10)
    \[ \frac{d}{d\theta_t} P > 0 \]

    From Equations 9 and 10, we can understand why Precision and Recall roughly follow an opposing relationship.

    Interactive Precision and recall as the threshold moves (left), and the precision–recall curve they trace (right). Hover either plot for values; click to move \(\theta_t\).

    Precision \(P\) Recall \(R\)
      Show values as a table

      What if the assumptions break?

      Interactive The argument leans on A-2 (the classes are well separated) and A-3 (they are balanced). Bend either one; every figure above updates, and the checks below are recomputed across the whole range of thresholds.