Speed

What Thresholds Do and Do Not Say

Derive the cost-balancing threshold t* = fp_cost / (fp_cost + fn_cost) and see why threshold tuning cannot reorder mixed rows.

The problem

Why did Threshold One Half test 0.50 while Threshold Three Quarters tested 0.75? Neither number is arbitrary: when a calibrated score p estimates P(y = 1), predicting 1 risks expected false-alarm cost (1 - p) * fp_cost while predicting 0 risks expected miss cost p * fn_cost. Predicting 1 has strictly lower expected cost whenever (1 - p) * fp_cost < p * fn_cost, which rearranges to p > fp_cost / (fp_cost + fn_cost).

On our fixed six-row table [0.90, 1], [0.80, 1], [0.65, 1], [0.60, 0], [0.40, 1], and [0.20, 0], we compute [t_star, fp, miss_pos, total_cost] from fp_cost and fn_cost.

Work through the numbers

Evaluate t_star = fp_cost / (fp_cost + fn_cost) and its realized table cost fp_cost * FP + fn_cost * FN:

  • Equal costs (fp_cost = 1, fn_cost = 1): t_star = 1 / (1 + 1) = 0.50. That reproduces [FP = 1, FN = 1] and total_cost = 1 * 1 + 1 * 1 = 2.
  • Expensive false alarms (fp_cost = 3, fn_cost = 1): t_star = 3 / (3 + 1) = 0.75. Applying prob > 0.75 yields [FP = 0, FN = 2] and total_cost = 3 * 0 + 1 * 2 = 2, cutting in half the cost 3 * 1 + 1 * 1 = 4 that 0.50 would incur.
  • Expensive misses (fp_cost = 1, fn_cost = 3): t_star = 1 / (1 + 3) = 0.25. Applying prob > 0.25 flags rows 0..4, giving [FP = 1, FN = 0] and total_cost = 1 * 1 + 3 * 0 = 1.

Notice also what thresholds do not say: at every possible cutoff on this six-row table, FP + FN >= 1 because negative row 3 (0.60) outranks positive row 4 (0.40). Moving a scalar line reallocates mistakes between FP and FN; it never swaps two rows' ranks.

The live model

The top gauge places t_star along [0, 1], while the lower cards report FP, FN, and total_cost under fp_cost and fn_cost. Adjust fp_cost and fn_cost from 1 to 6: raising fp_cost pushes t_star rightward toward 1 to suppress false alarms, while raising fn_cost pulls t_star leftward toward 0 to catch positives.

At fp_cost 3 and fn_cost 1, cost-optimal cutoff t_star is 0.75 with FP 0, FN 2, and total_cost 2.
let fp_cost = scene.parameter("fp_cost", 3.0, 1.0, 6.0, 1.0);
let fn_cost = scene.parameter("fn_cost", 1.0, 1.0, 6.0, 1.0);
let prob = input.select(1, 0);
let label = input.select(1, 1);
let neg_label = label.rsub(1.0);
let t_star = fp_cost / (fp_cost + fn_cost);
let pred = prob.gt(t_star).to(DType.F64);
let neg_pred = pred.rsub(1.0);
let fp = (pred * neg_label).sum();
let miss_pos = (neg_pred * label).sum();
let total_cost = fp * fp_cost + miss_pos * fn_cost;
let t_ten = (fp * 0.0 + t_star).reshape(1);
let p1 = torch.list().add(t_ten).add(fp.reshape(1));
let p2 = p1.add(miss_pos.reshape(1)).add(total_cost.reshape(1));
let answer = torch.cat(p2, 0);
out.set("cost_summary", answer);
{
  "cost_summary": [
    0.75,
    0,
    2,
    2
  ]
}
Watch the parameter change

Your turn

Compute t_star = fp_cost / (fp_cost + fn_cost) and return [t_star, fp, miss_pos, total_cost] under prob > t_star. The starter puts fn_cost in the numerator of t_star, moving the cutoff in the wrong direction whenever fp_cost != fn_cost. The check reruns the rule at fp_cost = 1, fn_cost = 3 on a five-row table and at fp_cost = 4, fn_cost = 1 on a six-row table, where the numerator decides the cutoff.

Starter model puts fn_cost in numerator of t_star, computing cutoff 0.25 instead of 0.75.
let fp_cost = scene.parameter("fp_cost", 3.0, 1.0, 6.0, 1.0);
let fn_cost = scene.parameter("fn_cost", 1.0, 1.0, 6.0, 1.0);
let prob = input.select(1, 0);
let label = input.select(1, 1);
let neg_label = label.rsub(1.0);
let t_star = fn_cost / (fp_cost + fn_cost);
let pred = prob.gt(t_star).to(DType.F64);
let neg_pred = pred.rsub(1.0);
let fp = (pred * neg_label).sum();
let miss_pos = (neg_pred * label).sum();
let total_cost = fp * fp_cost + miss_pos * fn_cost;
let t_ten = (fp * 0.0 + t_star).reshape(1);
let p1 = torch.list().add(t_ten).add(fp.reshape(1));
let p2 = p1.add(miss_pos.reshape(1)).add(total_cost.reshape(1));
let answer = torch.cat(p2, 0);
out.set("cost_summary", answer);
{
  "cost_summary": [
    0.25,
    1,
    0,
    3
  ]
}

Compute [t_star, fp, miss_pos, total_cost] with t_star = fp_cost / (fp_cost + fn_cost) and total_cost = fp * fp_cost + miss_pos * fn_cost.

Read the result carefully

Threshold tuning does translate asymmetric domain costs directly into an operating cutoff without touching model weights, turning 0.50 into a special case (fp_cost == fn_cost) rather than a universal law. Threshold tuning does not repair misranked pairs (0.60 above 0.40) and relies on probability calibration: on our finite six-row sample at fp_cost = 3, fn_cost = 1, empirical cutoff 0.625 achieves [FP = 0, FN = 1] (cost = 1), beating theoretical t_star = 0.75 (cost = 2) because row 2 (0.65) happened to be positive.

Recap

  • Expected-cost minimization sets t_star = fp_cost / (fp_cost + fn_cost), giving 0.50 at 1:1 costs and 0.75 at 3:1 costs.
  • Moving a threshold trades FP against FN along one fixed ranking; it cannot eliminate rank inversions like 0.60 (y=0) > 0.40 (y=1).
  • Theoretical t_star assumes calibrated probabilities, so finite validation tables should also check empirical fp_cost * FP + fn_cost * FN.