Speed

Threshold One Half Counts

Tally [TP, FP, FN, TN] = [3, 2, 1, 3] at cutoff 3/6 = 0.50 on the nine scored rows and decompose both positive pools.

The problem

In Same Scored Rows, cutoff 3 / 6 = 0.50 gave actual_pos = 4, pred_pos = 5, and tp = 3. Before dividing ratios, we need all four cells [TP, FP, FN, TN] so every row in the nine-row table is accounted for without gaps or double-counting.

Applying prob > sixths / 6.0 at sixths = 3 to [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], we compute [tp, fp, miss_pos, tn].

Work through the numbers

Partition the nine rows around 3 / 6 = 0.50:

  • Five rows score above 0.50 (pred = 1): rows 0, 1, 2 (0.95, 0.90, 0.75) carry y = 1 (TP = 3), while rows 3, 4 (0.70, 0.60) carry y = 0 (FP = 2). Check: TP + FP = 3 + 2 = 5 = pred_pos.
  • Four rows score at or below 0.50 (pred = 0): row 5 (0.40) carries y = 1 (FN = 1), while rows 6, 7, 8 (0.30, 0.15, 0.10) carry y = 0 (TN = 3). Check: TP + FN = 3 + 1 = 4 = actual_pos.

All nine rows sum cleanly: 3 + 2 + 1 + 3 = 9. Raising sixths to 4 (4 / 6 ≈ 0.67) moves row 4 (0.60, y = 0) from FP into TN, turning [3, 2, 1, 3] into [3, 1, 1, 4]. Lowering sixths to 2 (2 / 6 ≈ 0.33) moves row 5 (0.40, y = 1) from FN into TP, producing [4, 2, 0, 3].

The live model

Two stacked bars decompose the flagged pool (TP + FP) and the actual-positive pool (TP + FN), sharing the same green TP segment on the left, while the bottom card displays TN. Move sixths from 1 to 5: at sixths = 3, the amber FP = 2 tail makes the flagged bar (5) longer than the actual-positive bar (4).

At cutoff 3/6, flagged bar shows 3 TP + 2 FP = 5, actual pos bar shows 3 TP + 1 FN = 4, and TN is 3.
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 neg_pred = pred.rsub(1.0);
let neg_label = label.rsub(1.0);
let tp = (pred * label).sum();
let fp = (pred * neg_label).sum();
let miss_pos = (neg_pred * label).sum();
let tn = (neg_pred * neg_label).sum();
let top = torch.list().add(tp.reshape(1)).add(fp.reshape(1));
let answer = torch.cat(top.add(miss_pos.reshape(1)).add(tn.reshape(1)), 0);
out.set("counts_at_half", answer);
{
  "counts_at_half": [
    3,
    2,
    1,
    3
  ]
}
Watch the parameter change

Your turn

Compute [tp, fp, miss_pos, tn] under prob > sixths / 6.0. The starter computes fp as pred.sum() instead of subtracting tp (or masking by neg_label), counting all 5 flagged rows as false positives. Check tests unseen nine-row and six-row tables across sixths cutoffs.

Starter model counts all 5 flagged rows as FP instead of masking by negative true labels.
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 neg_pred = pred.rsub(1.0);
let neg_label = label.rsub(1.0);
let tp = (pred * label).sum();
let fp = pred.sum();
let miss_pos = (neg_pred * label).sum();
let tn = (neg_pred * neg_label).sum();
let top = torch.list().add(tp.reshape(1)).add(fp.reshape(1));
let answer = torch.cat(top.add(miss_pos.reshape(1)).add(tn.reshape(1)), 0);
out.set("counts_at_half", answer);
{
  "counts_at_half": [
    3,
    5,
    1,
    3
  ]
}

Compute [tp, fp, miss_pos, tn] under rule prob > sixths / 6.0 so fp counts only flagged negative rows.

Read the result carefully

Writing out [3, 2, 1, 3] before computing percentages makes the inequality between precision and recall obvious before any division happens: both fractions start with TP = 3, and FP = 2 adds two extra rows to precision's denominator whereas FN = 1 adds only one extra row to recall's denominator. Whenever FP > FN (with TP > 0), precision must sit strictly below recall.

Recap

  • At sixths = 3 (0.50), the nine rows split into [TP, FP, FN, TN] = [3, 2, 1, 3].
  • Flagged rows decompose as TP + FP = 3 + 2 = 5; actual positives decompose as TP + FN = 3 + 1 = 4.
  • Comparing FP = 2 against FN = 1 predicts immediately that precision (3 / 5) will be smaller than recall (3 / 4).