Sub topic 7
Choosing a metric by the cost of errors
This chapter assumes you can build a confusion matrix and compute accuracy, precision, and recall from its counts. It also assumes basic Python and a working idea of supervised classification. Here you will learn how to pick the metric that matches what a mistake costs in your application.
Two errors, two different prices
A binary classifier makes two kinds of mistakes. A false positive (FP) flags a negative case as positive. A false negative (FN) misses a real positive. Accuracy treats both as equal and counts them together. In real applications they rarely cost the same.
Precision is TP / (TP + FP). It falls when false positives rise. Recall is TP / (TP + FN). It falls when false negatives rise. So choosing between them means deciding which error hurts more.
A working rule
Ask one question: what happens after a wrong prediction?
- If a false positive is expensive, optimize precision. Examples: a spam filter that sends a real job offer to the junk folder, or a system that automatically blocks customer accounts.
- If a false negative is expensive, optimize recall. Examples: screening for a serious disease, or detecting fraud where one missed case costs far more than one extra review.
- If both matter and the classes are imbalanced, report both. F1 combines them, but it assumes equal weight, which may not match your costs.
Worked example: attaching costs
Suppose a fraud model reviews 1,000 transactions. A missed fraud (FN) costs 500 dollars. A false alarm (FP) costs 5 dollars, the price of an analyst's review. Compare two models.
FN_COST = 500
FP_COST = 5
models = {
"cautious": {"tp": 40, "fp": 60, "fn": 10},
"strict": {"tp": 25, "fp": 5, "fn": 25},
}
for name, m in models.items():
precision = m["tp"] / (m["tp"] + m["fp"])
recall = m["tp"] / (m["tp"] + m["fn"])
cost = m["fp"] * FP_COST + m["fn"] * FN_COST
print(name, round(precision, 2), round(recall, 2), cost)
# cautious 0.4 0.8 5300
# strict 0.83 0.5 12525The strict model has much higher precision, but it misses 25 frauds. Its total cost is 12,525 dollars, against 5,300 for the cautious model. With these costs, recall wins, even though the cautious model's precision looks poor.
Reverse the costs and the answer reverses. If each false alarm cost 500 dollars and each miss cost 5, the strict model would be far cheaper. The metric follows the costs, not the other way around.
Common errors
- Choosing a metric before asking about costs. Teams often default to accuracy or F1 because they are familiar.
- Assuming costs are equal. Equal weighting is itself a business assumption, and often a wrong one.
- Ignoring the cost of acting on predictions. A high-recall model that floods reviewers with false alarms can overwhelm the team that must check each flag.
- Forgetting that the metric choice is not final. Costs change over time, so revisit the choice. The next chapters show how the decision threshold lets you shift between precision and recall without retraining.
Summary
Pick the metric by naming the costlier error. False positives point to precision. False negatives point to recall. When you can estimate dollar costs, compute total cost directly and compare models on it.
Exercise
A hospital uses a model to screen patients for a serious but treatable disease. A positive result leads to a cheap follow-up test. A missed case can be fatal. Which metric should the team prioritize, and why? What do they give up?
Show answer
They should prioritize recall. A false negative (a missed patient) is far more costly than a false positive, which only triggers a cheap follow-up test. The trade-off is lower precision: more healthy patients will be flagged and need the extra test.
Exercise
Using the fraud example (FN costs 500 dollars, FP costs 5 dollars), a third model has tp=35, fp=20, fn=15. Compute its total cost and say whether it beats the cautious model, whose cost is 5,300 dollars.
Show answer
Cost = 20 5 + 15 500 = 100 + 7,500 = 7,600 dollars. It does not beat the cautious model, which costs 5,300 dollars.