Speed

Confusion at One Half

Group the six cutoff-0.50 decisions by true label into [TP, FP, FN, TN] = [3, 1, 1, 1] and verify the fixed class totals.

The problem

Knowing from Threshold One Half that threshold = 0.50 predicts [1, 1, 1, 1, 0, 0] tells us how many rows were flagged (4), not which errors occurred. Pairing each row's binary decision pred with its true label y partitions all six rows into four mutually exclusive confusion cells: [TP, FP, FN, TN].

On [0.90, 1], [0.80, 1], [0.65, 1], [0.60, 0], [0.40, 1], and [0.20, 0], we compute those four counts under prob > threshold and track which sums stay fixed when the cutoff moves.

Work through the numbers

Classify each of the six rows at threshold = 0.50 by (pred, y):

  • Rows 0, 1, 2 have pred = 1 and y = 1: three true positives (TP = 3).
  • Row 3 (0.60) has pred = 1 and y = 0: one false positive (FP = 1).
  • Row 4 (0.40) has pred = 0 and y = 1: one false negative (FN = 1).
  • Row 5 (0.20) has pred = 0 and y = 0: one true negative (TN = 1).

Together [TP, FP, FN, TN] = [3, 1, 1, 1], summing to 6. Notice the two row invariants of the true-class pools: actual positives always satisfy TP + FN = 3 + 1 = 4, and actual negatives always satisfy FP + TN = 1 + 1 = 2. Stepping threshold to 0.625 moves row 3 from FP into TN ([3, 0, 1, 2]), while stepping down to 0.375 moves row 4 from FN into TP ([4, 1, 0, 1]).

The live model

The 2x2 confusion grid arranges actual positives (y = 1, top row: TP and FN) above actual negatives (y = 0, bottom row: FP and TN), printing row pool sums on the right. Drag threshold across 0.125 to 0.875: counts shift horizontally between TP and FN or between FP and TN, yet the right-hand pool totals remain 4 and 2 at every stop.

At cutoff 0.5, the 2x2 confusion grid shows TP 3, FN 1 (4 pos pool) and FP 1, TN 1 (2 neg pool).
let threshold = scene.parameter("threshold", 0.5, 0.125, 0.875, 0.125);
let prob = input.select(1, 0);
let label = input.select(1, 1);
let pred = prob.gt(threshold).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 all = top.add(miss_pos.reshape(1)).add(tn.reshape(1));
let answer = torch.cat(all, 0);
out.set("confusion_half", answer);
{
  "confusion_half": [
    3,
    1,
    1,
    1
  ]
}
Watch the parameter change

Your turn

Complete [tp, fp, miss_pos, tn] by computing fp (pred = 1 on y = 0) and miss_pos (pred = 0 on y = 1). The starter swaps the two off-diagonal masks, confusing false alarms with missed positives whenever fp != miss_pos. Check tests asymmetric tables and multiple thresholds.

Starter model swaps FP and FN masks, which matches [3, 1, 1, 1] at 0.5 by coincidence and fails when FP != FN.
let threshold = scene.parameter("threshold", 0.5, 0.125, 0.875, 0.125);
let prob = input.select(1, 0);
let label = input.select(1, 1);
let pred = prob.gt(threshold).to(DType.F64);
let neg_pred = pred.rsub(1.0);
let neg_label = label.rsub(1.0);
let tp = (pred * label).sum();
let fp = (neg_pred * label).sum();
let miss_pos = (pred * neg_label).sum();
let tn = (neg_pred * neg_label).sum();
let top = torch.list().add(tp.reshape(1)).add(fp.reshape(1));
let all = top.add(miss_pos.reshape(1)).add(tn.reshape(1));
let answer = torch.cat(all, 0);
out.set("confusion_half", answer);
{
  "confusion_half": [
    3,
    1,
    1,
    1
  ]
}

Compute [tp, fp, miss_pos, tn] under rule prob > threshold so fp counts flagged negatives and miss_pos counts missed positives.

Read the result carefully

At threshold = 0.50, FP = 1 and FN = 1 happen to look symmetrical ([3, 1, 1, 1]), which can mask a subtle bug if code swaps FP and FN. Testing a neighboring cutoff immediately breaks that accidental tie: [3, 0, 1, 2] at 0.625 has zero false alarms and one miss, whereas [4, 1, 0, 1] at 0.375 has one false alarm and zero misses.

Recap

  • Pairing pred = [1, 1, 1, 1, 0, 0] with y = [1, 1, 1, 0, 1, 0] yields [TP, FP, FN, TN] = [3, 1, 1, 1].
  • True-class sums TP + FN = 4 and FP + TN = 2 stay constant across every threshold on these six rows.
  • Equal off-diagonal counts (FP = FN = 1) at 0.50 can hide swapped FP/FN logic until another cutoff or asymmetric split is tested.