Binary Classifier Threshold & Cost Trade-off Calculator
Arrays & HashingAuto-graded · 15 test casesLast updated
Tests whether candidates can compute F1, confusion matrix counts, and deployment cost for two classifier configurations, then select the cheaper model to deploy.
Binary Classifier Threshold & Cost Trade-off Calculator
You are comparing two versions of a binary classifier on the same held-out validation set. Given the precision, recall, and decision threshold for each version, along with the business costs assigned to each type of error, your job is to compute the key evaluation metrics for both and decide which one to ship.
Your function receives
| Parameter | Type | Meaning |
|---|---|---|
n | int | Total number of examples in the validation set |
positive_rate | float | Fraction of examples that are true positives (e.g. 0.08 for 8 %) |
baseline_precision | float | Precision of the baseline model at its operating threshold |
baseline_recall | float | Recall of the baseline model at its operating threshold |
baseline_threshold | float | The decision threshold used for the baseline model |
enhanced_precision | float | Precision of the enhanced model at its operating threshold |
enhanced_recall | float | Recall of the enhanced model at its operating threshold |
enhanced_threshold | float | The decision threshold used for the enhanced model |
fp_cost | float | Business cost of one false positive |
fn_cost | float | Business cost of one false negative |
What to compute for each model version
Let P = total actual positives = n × positive_rate.
- TP (true positives) =
P × recall - FN (false negatives) =
P − TP - Predicted positives =
TP / precision(assuming precision > 0) - FP (false positives) = predicted positives − TP
- F1 =
2 × precision × recall / (precision + recall)(0 if denominator is 0) - Cost =
fp_cost × FP + fn_cost × FN
Return value
Return a dict with exactly three keys:
"baseline"— metrics dict for the baseline model"enhanced"— metrics dict for the enhanced model"deploy"— the string"enhanced"if the enhanced model has strictly lower total cost, otherwise"baseline"
Each metrics dict must contain these six keys, with all numeric values rounded to 6 decimal places:
{
"threshold": <float>,
"f1": <float>,
"tp": <float>,
"fp": <float>,
"fn": <float>,
"cost": <float>
}
Constraints
1 ≤ n ≤ 10,000,0000.0 < positive_rate < 1.00.0 < baseline_precision ≤ 1.0and0.0 < enhanced_precision ≤ 1.00.0 ≤ baseline_recall ≤ 1.0and0.0 ≤ enhanced_recall ≤ 1.00.0 ≤ baseline_threshold ≤ 1.0and0.0 ≤ enhanced_threshold ≤ 1.00.0 ≤ fp_costand0.0 ≤ fn_cost- If the enhanced model's total cost equals the baseline's total cost, return
"baseline"
Examples
Example 1
Input: compare_classifier_configurations(10000, 0.08, 0.7, 0.45, 0.5, 0.58, 0.66, 0.3, 1, 5)
Output: {'baseline': {'threshold': 0.5, 'f1': 0.547826, 'tp': 360, 'fp': 154.285714, 'fn': 440, 'cost': 2354.285714}, 'enhanced': {'threshold': 0.3, 'f1': 0.617419, 'tp': 528, 'fp': 382.344828, 'fn': 272, 'cost': 1742.344828}, 'deploy': 'enhanced'}
Hints
Hint 1
Start by computing the absolute number of actual positives in the validation set — every other quantity derives from it and the per-model precision/recall pair.
Hint 2
To recover FP from precision and recall alone, think about what 'predicted positives' means: TP / precision gives you that count, and FP is just the leftover.
Hint 3
Round each numeric field to exactly 6 decimal places before storing it, then compare the raw (pre-rounded) costs — or compare the rounded costs consistently — to decide which model to deploy.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The problem is pure arithmetic — no search, no data structures. All you need is a systematic derivation of each confusion-matrix cell from the three quantities you're given: the size of the validation set, the fraction that are true positives, and the precision/recall pair at the operating threshold.
Step-by-step derivation
Given n total examples and positive_rate, the absolute count of actual positives is fixed:
P = n × positive_rate
From precision and recall you can reconstruct every cell of the 2×2 confusion matrix:
TP = P × recall
FN = P − TP (missed positives)
PP = TP / precision (all examples predicted positive)
FP = PP − TP (predicted positive but actually negative)
F1 is the harmonic mean of precision and recall:
F1 = 2 × precision × recall / (precision + recall)
Total business cost is a weighted sum of errors:
cost = fp_cost × FP + fn_cost × FN
Apply this to both models, round every numeric result to 6 decimal places, then compare costs to choose which to deploy. If the enhanced model's cost is strictly less, return "enhanced"; otherwise return "baseline".
Why this is correct
Precision is defined as TP / (TP + FP), so rearranging gives TP + FP = TP / precision, which is exactly the "predicted positives" step above. No approximation is involved — the derivation is algebraically exact given the stated precision and recall values.
Worked example
With n = 10,000, positive_rate = 0.08, fp_cost = 1, fn_cost = 5:
P = 800
Baseline (precision=0.70, recall=0.45, threshold=0.50):
- TP = 800 × 0.45 = 360
- FN = 800 − 360 = 440
- PP = 360 / 0.70 ≈ 514.286
- FP ≈ 154.286
- F1 = 2×0.70×0.45 / 1.15 ≈ 0.547826
- Cost = 154.286 + 5×440 = 2354.286
Enhanced (precision=0.58, recall=0.66, threshold=0.30):
- TP = 800 × 0.66 = 528
- FN = 800 − 528 = 272
- PP = 528 / 0.58 ≈ 910.345
- FP ≈ 382.345
- F1 = 2×0.58×0.66 / 1.24 ≈ 0.617419
- Cost = 382.345 + 5×272 = 1742.345
Enhanced cost < baseline cost → deploy = "enhanced".
The 168 fewer false negatives save 5 cost units each (840 total), while the 228 extra false positives cost only 1 unit each (228 total) — a net saving of roughly 612 units.
Complexity
- Time: O(1) — a fixed number of arithmetic operations regardless of input size.
- Space: O(1) — only a constant number of scalar values are stored.
Reference solution
from typing import Dict
def compare_classifier_configurations(
n: int,
positive_rate: float,
baseline_precision: float,
baseline_recall: float,
baseline_threshold: float,
enhanced_precision: float,
enhanced_recall: float,
enhanced_threshold: float,
fp_cost: float,
fn_cost: float,
) -> Dict[str, object]:
actual_positives = n * positive_rate
def evaluate(precision: float, recall: float, threshold: float) -> Dict[str, float]:
tp = actual_positives * recall
fn = actual_positives - tp
predicted_positives = tp / precision if precision else 0.0
fp = predicted_positives - tp
f1_denom = precision + recall
f1 = (2.0 * precision * recall / f1_denom) if f1_denom else 0.0
cost = fp_cost * fp + fn_cost * fn
return {
"threshold": round(threshold, 6),
"f1": round(f1, 6),
"tp": round(tp, 6),
"fp": round(fp, 6),
"fn": round(fn, 6),
"cost": round(cost, 6),
}
baseline = evaluate(baseline_precision, baseline_recall, baseline_threshold)
enhanced = evaluate(enhanced_precision, enhanced_recall, enhanced_threshold)
deploy = "enhanced" if enhanced["cost"] < baseline["cost"] else "baseline"
return {
"baseline": baseline,
"enhanced": enhanced,
"deploy": deploy,
}