download

How do you pick a threshold when error costs differ?

Machine learning~12 minReviewed 3 Dec 2026Free · No sign-in

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 pOrdersFraudsLegit
0.6020128
0.3030921
0.101001090
0.022505245
0.016006594
  1. Derive the exact Bayes-optimal threshold p*.
  2. Finish threshold_cost and compare total dollar costs at t = 0.50, 0.20, 0.05, and 0.02.
  3. Why must probabilities be calibrated before applying p*?
"CODE · PYTHON"
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)
  1. "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 for p.

  2. "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?

  3. "CHECK UNCALIBRATED SCORES"

    The formula p* = 0.05 treats the score as a real fraud chance. What happens if 10x class weights push the 1% band to 0.09 and the 2% band to 0.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.

"SOLUTION · PYTHON"
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:,}")
"OUTPUT"
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 tBlocked bandsTPFPFNPrecisionRecallF1Total cost
0.500.601283060.0%28.6%0.387$5,780
0.200.60, 0.3021292142.0%50.0%0.457$4,280
0.050.60, 0.30, 0.10311191120.7%73.8%0.323$3,280
0.020.60..0.023636469.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.50 by default. On asymmetric costs, 0.50 misses 30 of 42 frauds here and costs $5,780 instead 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,000 per 1,000 orders.
  • Applying p* = 0.05 to 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 t on 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_FP and C_FN with each order's own costs. For order i with calibrated fraud probability p_i, block when (1 - p_i) * margin_i <= p_i * (amount_i + fee). High-value orders get a stricter personal threshold than $15 orders.
  • "If you never need probabilities elsewhere, should you still calibrate or just sweep t on validation cost?" Sweeping t on 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 recompute p* 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 →

Original OfferVibe question. Charles Elkan, "The Foundations of Cost-Sensitive Learning" (IJCAI, 2001) · scikit-learn Tuning the Decision Threshold · scikit-learn Probability Calibration.
© 2026 OfferVibe · Text CC BY-NC-SA 4.0 · code MITFree to share and adapt with credit, not for commercial use. Code samples are free for any use.