Sub topic 7
Choosing a Metric by the Cost of Errors
This chapter assumes you can already read a confusion matrix (TP, FP, TN, FN) and compute accuracy, precision, and recall from it. You should also know why accuracy misleads on imbalanced data. You need basic Python. Here you will learn to turn the question "which metric should I optimize?" into a question about what each kind of mistake costs.
Every error has a price
A binary classifier makes two kinds of mistakes, and they rarely cost the same.
- A false positive (FP) is a negative case predicted as positive. You act when you should not have.
- A false negative (FN) is a positive case predicted as negative. You miss something you should have caught.
The metrics you already know each guard against one of these errors.
- Precision is TP / (TP + FP). It falls when false positives rise, so it tracks the cost of acting wrongly.
- Recall is TP / (TP + FN). It falls when false negatives rise, so it tracks the cost of missing real cases.
- Accuracy counts both errors equally and ignores which class they come from. It fits only when the two errors cost about the same and the classes are roughly balanced.
Choosing a metric is therefore a business or safety decision first. The statistics come second.
Mapping scenarios to metrics
The table lists typical scenarios. Read the "costlier error" column first, then the metric it implies.
| Scenario | Positive class | Costlier error | Metric to favor |
|---|---|---|---|
| Spam filter for a work inbox | Spam | FP: a real email lands in spam | Precision |
| Cancer screening test | Disease present | FN: a sick patient is sent home | Recall |
| Credit card fraud alerts | Fraud | FN: stolen money is lost | Recall, within the review team's capacity |
| Automatic account suspension | Abusive account | FP: a legitimate user is locked out | Precision |
| Safety-critical defect detection | Defective part | FN: a faulty part ships | Recall |
| Product recommendations on a page | Relevant item | Both are cheap | Precision, since screen space is limited |
Two details matter here. First, "costlier" depends on what happens after the prediction. A fraud model that feeds an automatic card block has expensive false positives, because customers are angry. The same model feeding a human review queue has cheap false positives, because an analyst glances at the case. Second, the same domain can need different metrics at different pipeline stages. A screening test favors recall. The confirmation test that follows it favors precision.
A worked comparison with explicit costs
Suppose a fraud team reviews flagged transactions. Each false positive costs 1 unit of analyst time. Each false negative costs 50 units in lost money. Two models are tested on the same 1,000 transactions, of which 90 are fraud.
The code below computes the usual metrics and a total cost for each model.
def summarize(name, tp, fp, tn, fn, cost_fp, cost_fn):
total = tp + fp + tn + fn
accuracy = (tp + tn) / total
precision = tp / (tp + fp)
recall = tp / (tp + fn)
cost = fp * cost_fp + fn * cost_fn
print(
f"{name} accuracy={accuracy:.3f} precision={precision:.3f} "
f"recall={recall:.3f} cost={cost}"
)
summarize("A", tp=70, fp=30, tn=880, fn=20, cost_fp=1, cost_fn=50)
summarize("B", tp=80, fp=120, tn=790, fn=10, cost_fp=1, cost_fn=50)
# A accuracy=0.950 precision=0.700 recall=0.778 cost=1030
# B accuracy=0.870 precision=0.400 recall=0.889 cost=620Model A wins on accuracy and on precision. Model B has the lower total cost, because it misses 10 frauds instead of 20, and each miss costs 50 times as much as a false alarm. If you had ranked the models by accuracy, you would have shipped the more expensive one.
Reading the output this way gives a working rule. Rank models by the metric that matches the costly error. Where you can, confirm the ranking with a total-cost calculation like the one above.
From costs to a decision threshold
Costs also tell you where to set the decision threshold. The previous chapter showed that moving the threshold trades precision against recall. Costs tell you which direction to move.
Suppose the model outputs a probability p that a case is positive, and that this probability is well calibrated. Calibrated means that among cases scored 0.2, about 20% really are positive. For one case:
- Predicting positive costs
(1 - p) * cost_fpin expectation, because the prediction is wrong with probability1 - p. - Predicting negative costs
p * cost_fnin expectation.
Predicting positive is cheaper when (1 - p) * cost_fp < p * cost_fn. Solving for p gives a threshold:
cost_fp, cost_fn = 1, 50
threshold = cost_fp / (cost_fp + cost_fn)
print(round(threshold, 4)) # 0.0196With a miss 50 times worse than a false alarm, you should flag any case whose probability is above about 2%. That is far below the default 0.5. When costs are equal, the formula gives 0.5, which is why 0.5 is a reasonable default only for symmetric costs.
Pitfalls when matching metrics to costs
Maximizing one metric alone
Recall alone is trivial to maximize. Predict "positive" for every case and recall is 1.0, while precision collapses to the share of positives in the data. Precision alone is just as easy to game. Flag only the single most confident case, and if it is right, precision is 1.0 while recall is near zero.
So pick a primary metric, then set a floor on the other. A common specification reads: "maximize recall subject to precision of at least 0.30." The floor encodes the capacity or tolerance of whoever handles the false positives.
Ignoring capacity limits
Costs are not always linear. If a review team can inspect only 50 alerts per day, extra false positives cost nothing until the queue overflows, and then each one pushes a real case out of the queue. In that situation, evaluate precision among the top 50 scored cases. A cost-per-error table does not capture this.
Treating costs as constant across cases
A missed $20 fraud and a missed $20,000 fraud are both false negatives, but they do not cost the same. When the cost varies per case, confusion-matrix counts hide it. Weight each error by its actual amount, or use sample_weight in scikit-learn's metric functions, which accept it.
Forgetting that precision depends on prevalence
Recall is computed only over real positives, so it does not change when the share of positives in the data changes. Precision does change, because the false positives come from the negative class, which grows or shrinks relative to the positives.
recall = 0.9 # share of real positives caught
fpr = 0.05 # share of real negatives wrongly flagged
n = 1000
for prevalence in (0.10, 0.01):
positives = n * prevalence
negatives = n - positives
tp = recall * positives
fp = fpr * negatives
print(prevalence, round(tp / (tp + fp), 3))
# 0.1 0.667
# 0.01 0.154The classifier is identical in both runs, with the same recall and the same false positive rate. Yet precision falls from 0.667 to 0.154 when positives become rarer. If you evaluate on a test set with a different class mix than production, your precision estimate will be wrong, even though your recall estimate stays valid. Evaluate on data whose class balance matches deployment, or correct for the difference.
Assuming the costs are known
Sometimes nobody can state the costs. Then report precision and recall together, and avoid presenting one without the other. The F1 score, covered in the next chapter, summarizes both in one number, but it assumes the two errors matter equally. That is itself a cost assumption, and you should make it deliberately.
A checklist for choosing
- Name the positive class and what a positive prediction triggers.
- Write down what a false positive costs and what a false negative costs, in the same unit, even roughly.
- Pick the primary metric that guards the costlier error. Add a floor on the other metric if there is a capacity or tolerance limit.
- Use the cost ratio to set or sanity-check the decision threshold.
- Evaluate on held-out data with the class balance you expect in production.
- Recheck the choice when the downstream process changes, such as moving from human review to automatic action.
Exercise
A hospital uses a model to flag X-rays for urgent radiologist review. A missed case can be fatal. A false alarm costs a radiologist five minutes. Which metric should be the primary one, and what would you do about precision?
Show answer
Recall should be primary, because the costly error is the false negative. Precision should not be ignored, though. Set a floor on it, such as a minimum precision that keeps the review queue manageable, so the model cannot reach high recall by flagging almost every X-ray. Choose a low decision threshold, and confirm that total radiologist workload stays within capacity.
Exercise
A model has recall 0.8 and a false positive rate of 0.10. It is tested on 1,000 cases with 50% positives, then deployed where only 2% of cases are positive. Compute precision in both settings, using the same calculation as the prevalence example, and explain the difference.
Show answer
In the test set, positives = 500 and negatives = 500. TP = 0.8 500 = 400 and FP = 0.1 500 = 50, so precision = 400 / 450 = 0.889. In deployment with n = 1,000, positives = 20 and negatives = 980. TP = 0.8 20 = 16 and FP = 0.1 980 = 98, so precision = 16 / 114 = 0.140. Recall and the false positive rate are the same in both settings, but negatives vastly outnumber positives in deployment, so false positives swamp the true positives and precision falls.