How do you pick a threshold when error costs differ?
Question
A calibrated fraud model scores 1,000 checkouts in five probability bands. Blocking a legitimate checkout (FP) costs $10 in lost margin. Missing a fraud (FN) costs $190 in chargebacks. Correct decisions cost $0.
Score band p | Orders | Frauds | Legit |
|---|---|---|---|
| 0.60 | 20 | 12 | 8 |
| 0.30 | 30 | 9 | 21 |
| 0.10 | 100 | 10 | 90 |
| 0.02 | 250 | 5 | 245 |
| 0.01 | 600 | 6 | 594 |
- Derive the exact Bayes-optimal threshold
p*. - Finish
threshold_costand compare total dollar costs att = 0.50,0.20,0.05, and0.02. - Why must probabilities be calibrated before applying
p*?
bands = [(0.60, 20, 12), (0.30, 30, 9), (0.10, 100, 10), (0.02, 250, 5), (0.01, 600, 6)] # (p, orders, frauds) FP_COST, FN_COST = 10, 190 def threshold_cost(rows, threshold, fp_cost=10, fn_cost=190): """Return (fp, fn, total_cost) when blocking score >= threshold.""" ...
Show hints (3)
- "COMPARE EXPECTED COSTS"
For an order with fraud chance
p, blocking costs$10 * (1 - p)in expectation. Approving costs$190 * p. Set them equal and solve forp. - "CHECK THE 0.10 BAND"
At
p = 0.10, approving 100 orders misses 10 frauds ($1,900). Blocking them causes 90 false alarms ($900). Which choice is cheaper? - "CHECK UNCALIBRATED SCORES"
The formula
p* = 0.05treats the score as a real fraud chance. What happens if 10x class weights push the 1% band to0.09and the 2% band to0.17?
Show checked explanation
Block when p >= p* = C_FP / (C_FP + C_FN) = 10 / 200 = 0.05. On these 1,000 checkouts, t = 0.05 costs $3,280, beating 0.50 ($5,780), 0.20 ($4,280), and 0.02 ($4,780). The formula holds only for calibrated probabilities. If scores are skewed, calibrate them first or tune t directly on validation cost.
from fractions import Fraction bands = [(0.60, 20, 12), (0.30, 30, 9), (0.10, 100, 10), (0.02, 250, 5), (0.01, 600, 6)] # (p, orders, frauds) FP_COST, FN_COST = 10, 190 def threshold_cost(rows, threshold, fp_cost=10, fn_cost=190): fp = sum(n - f for p, n, f in rows if p >= threshold) fn = sum(f for p, n, f in rows if p < threshold) return fp, fn, fp * fp_cost + fn * fn_cost p_star = Fraction(FP_COST, FP_COST + FN_COST) print(f"Bayes p* = {p_star} ({float(p_star):.2f})") for t in (0.50, 0.20, float(p_star), 0.02): fp, fn, cost = threshold_cost(bands, t, FP_COST, FN_COST) print(f"t={t:.2f}: FP={fp:3d} FN={fn:2d} cost=${cost:,}")
Bayes p* = 1/20 (0.05) t=0.50: FP= 8 FN=30 cost=$5,780 t=0.20: FP= 29 FN=21 cost=$4,280 t=0.05: FP=119 FN=11 cost=$3,280 t=0.02: FP=364 FN= 6 cost=$4,780
Code and exact rational cost calculations run on CPython 3.12.10 (stdlib only) against the 1,000-checkout fixture above; official scikit-learn documentation links checked by the author. Checked by an editor other than the author.
Walkthrough
The default cutoff 0.50 assumes a false alarm and a missed fraud cost the same. Here a missed fraud costs 19 times more ($190 vs $10).
1. Derive the Bayes-optimal cutoff. Take one checkout with calibrated fraud probability p:
- Expected cost of blocking:
(1 - p) * $10. - Expected cost of approving:
p * $190.
Blocking is cheaper whenever 10 * (1 - p) <= 190 * p, which simplifies to 10 <= 200 * p, or p >= 10 / 200 = 0.05. At p = 0.05 exact, both actions cost $9.50 per checkout.
2. Evaluate the 1,000-checkout table. Compare all four cutoffs:
Cutoff t | Blocked bands | TP | FP | FN | Precision | Recall | F1 | Total cost |
|---|---|---|---|---|---|---|---|---|
| 0.50 | 0.60 | 12 | 8 | 30 | 60.0% | 28.6% | 0.387 | $5,780 |
| 0.20 | 0.60, 0.30 | 21 | 29 | 21 | 42.0% | 50.0% | 0.457 | $4,280 |
| 0.05 | 0.60, 0.30, 0.10 | 31 | 119 | 11 | 20.7% | 73.8% | 0.323 | $3,280 |
| 0.02 | 0.60..0.02 | 36 | 364 | 6 | 9.0% | 85.7% | 0.163 | $4,780 |
Moving from 0.50 to 0.05 saves $2,500 per 1,000 orders. Even 0.20 wastes $1,000 because approving the 0.10 band costs $1,900 in chargebacks while blocking it costs $900. Going further down to 0.02 backfires: blocking the 0.02 band costs $2,450 in false blocks to prevent $950 of fraud, losing $1,500.
3. Why F1 and uncalibrated scores mislead you. F1 peaks at t = 0.20 (0.457 vs 0.323 at 0.05), because F1 weighs false positives and false negatives equally. Meanwhile, p* = 0.05 works only when score = 0.05 truly means 5% fraud. If 10x class weights push the true 0.02 band up to 0.17 and the 0.01 band up to 0.09, applying 0.05 blindly blocks all 1,000 orders. Cost jumps from $3,280 to $9,580 (958 false blocks, zero missed frauds). Calibrate scores on held-out data first, or sweep thresholds directly against dollar cost.
Common mistakes
- Keeping
t = 0.50by default. On asymmetric costs,0.50misses 30 of 42 frauds here and costs$5,780instead of$3,280. - Picking the threshold that maximizes F1. F1 picks
t = 0.20(0.457), which ignores the 19:1 cost ratio and wastes$1,000per 1,000 orders. - Applying
p* = 0.05to uncalibrated scores from a class-weighted or oversampled model. Inflated scores trigger false blocks on cheap bands and raise total cost. - Tuning the threshold on training data or the final test set. Fit the calibrator and pick
ton a held-out validation split.
Follow-up questions
- "What if every checkout has a different order value instead of a flat $190 chargeback?" Replace the fixed
C_FPandC_FNwith each order's own costs. For orderiwith calibrated fraud probabilityp_i, block when(1 - p_i) * margin_i <= p_i * (amount_i + fee). High-value orders get a stricter personal threshold than$15orders. - "If you never need probabilities elsewhere, should you still calibrate or just sweep
ton validation cost?" Sweepington held-out validation cost finds the right operating point for any monotonic score without calibration. Calibration still pays off when costs change by order value, by region, or over time, because you can recomputep*immediately without refitting thresholds.
What a strong answer covers
Derives p* = C_FP / (C_FP + C_FN) = 0.05 from expected costs and proves $3,280 beats 0.50, 0.20, and 0.02. Explains why F1 chooses the wrong cutoff (0.20) and why calibration matters before using the closed-form threshold.
Open in interactive practice deck →