KatInterview

Binary Classifier Threshold & Cost Trade-off Calculator

Arrays & HashingAuto-graded · 15 test cases
MediumAuto-gradedArrays & Hashingmachine-learningclassificationevaluation-metrics

Last 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

ParameterTypeMeaning
nintTotal number of examples in the validation set
positive_ratefloatFraction of examples that are true positives (e.g. 0.08 for 8 %)
baseline_precisionfloatPrecision of the baseline model at its operating threshold
baseline_recallfloatRecall of the baseline model at its operating threshold
baseline_thresholdfloatThe decision threshold used for the baseline model
enhanced_precisionfloatPrecision of the enhanced model at its operating threshold
enhanced_recallfloatRecall of the enhanced model at its operating threshold
enhanced_thresholdfloatThe decision threshold used for the enhanced model
fp_costfloatBusiness cost of one false positive
fn_costfloatBusiness cost of one false negative

What to compute for each model version

Let P = total actual positives = n × positive_rate.

  1. TP (true positives) = P × recall
  2. FN (false negatives) = P − TP
  3. Predicted positives = TP / precision (assuming precision > 0)
  4. FP (false positives) = predicted positives − TP
  5. F1 = 2 × precision × recall / (precision + recall) (0 if denominator is 0)
  6. 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,000
  • 0.0 < positive_rate < 1.0
  • 0.0 < baseline_precision ≤ 1.0 and 0.0 < enhanced_precision ≤ 1.0
  • 0.0 ≤ baseline_recall ≤ 1.0 and 0.0 ≤ enhanced_recall ≤ 1.0
  • 0.0 ≤ baseline_threshold ≤ 1.0 and 0.0 ≤ enhanced_threshold ≤ 1.0
  • 0.0 ≤ fp_cost and 0.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.