Speed

Confusion Counts Show the Miss

Split matches and misses into four confusion counts so a missed rare positive cannot hide behind negative matches.

The problem

Counting 4 matches and 1 miss on the Always-N baseline tells us *how many* errors happened, not *which direction* failed. Missing a defective part (true 1, pred 0) and raising a false alarm (true 0, pred 1) carry different consequences, yet a plain error count adds them into one bucket.

Take the five scored rows [0.10, 0], [0.15, 0], [0.35, 1], [0.20, 0], and [0.42, 0]. Under a rare-event model, predicted probabilities often stay below 0.5 everywhere. We apply the strict decision rule prob > threshold and partition all five rows into the four confusion counts [TP, FP, FN, TN].

Work through the numbers

At the default threshold = 0.5, no row exceeds 0.5, so pred is [0, 0, 0, 0, 0] and its complement 1 - pred is [1, 1, 1, 1, 1]. True labels are label = [0, 0, 1, 0, 0], with complement 1 - label = [1, 1, 0, 1, 1].

Each cell multiplies one prediction indicator by one label indicator and sums across rows:

  • True positives (TP): pred * label sums to 0.
  • False positives (FP): pred * (1 - label) sums to 0.
  • False negatives (FN): (1 - pred) * label picks out row 2 and sums to 1.
  • True negatives (TN): (1 - pred) * (1 - label) picks out rows 0, 1, 3, and 4, summing to 4.

The vector [0, 0, 1, 4] makes the failure explicit: TP = 0 and FN = 1 show that every defective part was missed. Lowering threshold to 0.4 flips row 4 (0.42, true 0) first, producing [0, 1, 1, 3]. Lowering further to 0.3 catches row 2 (0.35, true 1), moving the counts to [1, 1, 0, 3]; at 0.1 they become [1, 3, 0, 1].

Rows 0 and 3 score 0.10 and 0.20 right on slider stops 0.1 and 0.2, so at those two stops the counts depend on the strict > rule (0.20 > 0.2 and 0.10 > 0.1 are both 0, keeping each tied negative in TN).

The live model

The two-by-two grid places actual classes on the rows (y = 1 above y = 0), predicted classes on the columns (pred = 1 left of pred = 0), and fixed class pools (1 pos, 4 neg) on the right. Drag threshold from 0.5 down to 0.3 to see row 2 move horizontally from FN into TP after row 4 moves from TN into FP.

Two-by-two confusion grid at threshold 0.5 showing TP 0, FP 0, FN 1, and TN 4 alongside five probability bars all left of 0.5.
let threshold = scene.parameter("threshold", 0.5, 0.1, 0.6, 0.1);
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_row = torch.list().add(tp.reshape(1)).add(fp.reshape(1));
let all_four = top_row.add(miss_pos.reshape(1)).add(tn.reshape(1));
let answer = torch.cat(all_four, 0);
out.set("predictions", pred).set("counts", answer);
{
  "predictions": [
    0,
    0,
    0,
    0,
    0
  ],
  "counts": [
    0,
    0,
    1,
    4
  ]
}
Watch the parameter change

Your turn

Fix the false-negative count miss_pos inside [tp, fp, miss_pos, tn]. The starting model accidentally repeats the false-positive product for miss_pos, reporting [0, 0, 0, 4] at threshold = 0.5 and erasing the missed positive row. Check runs unseen tables and thresholds where all four counts differ.

Starter confusion grid where FN repeats FP and reports 0 instead of 1 at threshold 0.5.
let threshold = scene.parameter("threshold", 0.5, 0.1, 0.6, 0.1);
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 = (pred * neg_label).sum();
let tn = (neg_pred * neg_label).sum();
let top_row = torch.list().add(tp.reshape(1)).add(fp.reshape(1));
let all_four = top_row.add(miss_pos.reshape(1)).add(tn.reshape(1));
let answer = torch.cat(all_four, 0);
out.set("predictions", pred).set("counts", answer);
{
  "predictions": [
    0,
    0,
    0,
    0,
    0
  ],
  "counts": [
    0,
    0,
    0,
    4
  ]
}

Compute [TP, FP, FN, TN] under strict rule prob > threshold so miss_pos counts actual positive rows predicted as 0.

Read the result carefully

Notice two invariants across every slider stop: TP + FN stays equal to 1 (total actual positives) and FP + TN stays equal to 4 (total actual negatives). Changing the decision threshold moves a row only between the two cells of its own true class (FN to TP, or TN to FP); it never moves a row from one true class to the other. Any confusion grid whose four entries fail to sum to the dataset size has dropped or double-counted rows.

Recap

  • Pair pred and 1 - pred with label and 1 - label to compute [TP, FP, FN, TN].
  • At threshold = 0.5, [0, 0, 1, 4] exposes TP = 0 and FN = 1 immediately even though four rows matched.
  • Actual class totals TP + FN and FP + TN stay fixed whenever only the threshold changes.