Speed

Same Scored Rows

Inspect nine scored rows and separate the fixed recall denominator from the moving precision denominator across cutoffs.

The problem

Precision and recall look similar at a glance because both put true positives (TP) in the numerator. Confusion starts in the denominator: recall divides TP by actual positives (TP + FN), a property of the dataset, whereas precision divides TP by predicted positives (TP + FP), a property of the chosen cutoff.

Across this book we hold one nine-row table fixed: [0.95, 1], [0.90, 1], [0.75, 1], [0.70, 0], [0.60, 0], [0.40, 1], [0.30, 0], [0.15, 0], and [0.10, 0]. Measuring cutoffs in sixths (threshold = sixths / 6.0 for sixths = 1..5) lets us compare 3 / 6 = 1 / 2 and 5 / 6 exactly while returning [actual_pos, pred_pos, tp].

Work through the numbers

First count actual labels across all nine rows: rows 0, 1, 2, 5 (0.95, 0.90, 0.75, 0.40) carry y = 1, giving actual_pos = 4, while rows 3, 4, 6, 7, 8 carry y = 0 (5 negatives). That 4 never changes when the cutoff moves.

Now trace pred_pos (prob > sixths / 6) and tp across sixths:

  • At sixths = 3 (threshold = 3 / 6 = 0.50): five rows (0.95, 0.90, 0.75, 0.70, 0.60) clear 0.50, so pred_pos = 5 and three of them are truly positive (tp = 3), yielding [4, 5, 3].
  • At sixths = 4 (threshold = 4 / 6 ≈ 0.67): row 4 (0.60, y = 0) drops out, shrinking pred_pos to 4 while tp stays 3 ([4, 4, 3]).
  • At sixths = 5 (threshold = 5 / 6 ≈ 0.83): only 0.95 and 0.90 clear 5 / 6, shrinking pred_pos to 2 and tp to 2 ([4, 2, 2]).

Lowering sixths to 2 (2 / 6 ≈ 0.33) flags six rows and catches all four positives ([4, 6, 4]).

The live model

Three horizontal bars compare actual_pos (recall's denominator), pred_pos (precision's denominator), and tp (their shared numerator) out of 9 rows. Slide sixths from 1 to 5: the top green actual_pos bar stays locked at 4 at every stop, while pred_pos contracts from 7 down to 2 and tp steps from 4 down to 2.

At cutoff 3/6, actual_pos is 4 fixed, pred_pos is 5 flagged, and shared numerator TP is 3 caught out of 9 rows.
let sixths = scene.parameter("sixths", 3.0, 1.0, 5.0, 1.0);
let cutoff = sixths / 6.0;
let prob = input.select(1, 0);
let label = input.select(1, 1);
let pred = prob.gt(cutoff).to(DType.F64);
let actual_pos = label.sum();
let pred_pos = pred.sum();
let tp = (pred * label).sum();
let parts = torch.list().add(actual_pos.reshape(1)).add(pred_pos.reshape(1));
let answer = torch.cat(parts.add(tp.reshape(1)), 0);
out.set("cutoff", cutoff).set("denominators_and_tp", answer);
{
  "cutoff": 0.5,
  "denominators_and_tp": [
    4,
    5,
    3
  ]
}
Watch the parameter change

Your turn

Compute [actual_pos, pred_pos, tp] under prob > sixths / 6.0. The starter uses pred_pos for both denominators, erasing the distinction between actual positives and predicted positives whenever FP != FN. Check tests unseen tables and multiple sixths cutoffs.

Starter model sets actual_pos equal to pred_pos (5 instead of 4) at cutoff 3/6.
let sixths = scene.parameter("sixths", 3.0, 1.0, 5.0, 1.0);
let cutoff = sixths / 6.0;
let prob = input.select(1, 0);
let label = input.select(1, 1);
let pred = prob.gt(cutoff).to(DType.F64);
let actual_pos = pred.sum();
let pred_pos = pred.sum();
let tp = (pred * label).sum();
let parts = torch.list().add(actual_pos.reshape(1)).add(pred_pos.reshape(1));
let answer = torch.cat(parts.add(tp.reshape(1)), 0);
out.set("cutoff", cutoff).set("denominators_and_tp", answer);
{
  "cutoff": 0.5,
  "denominators_and_tp": [
    5,
    5,
    3
  ]
}

Compute [actual_pos, pred_pos, tp] under rule prob > sixths / 6.0 on any scored table.

Read the result carefully

Every precision-recall curve on a held-out set comes from this exact contrast on one fixed score column: recall's denominator (actual_pos = 4) is anchored before any model runs, so recall moves strictly in lockstep with tp, whereas precision's denominator (pred_pos) shrinks alongside tp as sixths rises, allowing tp / pred_pos to climb even while tp falls.

Recap

  • Our nine-row table contains 4 actual positives (0.95, 0.90, 0.75, 0.40) and 5 actual negatives.
  • At sixths = 3 (1 / 2), [actual_pos, pred_pos, tp] = [4, 5, 3]; at sixths = 5 (5 / 6), it becomes [4, 2, 2].
  • Recall's denominator (actual_pos) is invariant to the cutoff, while precision's denominator (pred_pos) changes whenever a row crosses the cutoff.