KatInterview

Cost-Aware Fraud Model Comparison

Arrays & HashingAuto-graded · 13 test cases
HardAuto-gradedArrays & Hashingclassificationcost-sensitive-learningconfusion-matrix

Last updated

Compares classification models using business costs, derives optimal decision thresholds, and identifies the most economical model.

You are building a fraud detection system for an online marketplace. Each order must either be flagged for manual review or allowed to proceed automatically.

You have evaluated two candidate models on a validation dataset. For each model, you know the confusion matrix at a fixed operating point: true positives (TP), false positives (FP), false negatives (FN), and true negatives (TN).

The business incurs these costs:

  • Review cost: paid for every flagged order (legitimate or fraudulent)
  • Friction cost: additional cost when a legitimate order is incorrectly flagged
  • Missed fraud cost: loss incurred when a fraudulent order is not caught

Your task is to compute standard classification metrics, determine the cost-optimal probability threshold for flagging orders, calculate the expected cost per order for each model, and identify which model minimizes cost.

Write a function that takes:

  • models: a list of confusion matrices, where each matrix is [TP, FP, FN, TN]
  • costs: a dictionary with keys "review", "friction", and "missed_fraud" (all positive floats)
  • base_rate: the true prevalence of fraud in the population (between 0 and 1)

Return a dictionary containing:

  • "models": a list of dictionaries, one per input model, each with:
    • "name": "A" for the first model, "B" for the second, or the zero-based index as a string for others
    • "precision": TP / (TP + FP), or 0.0 if no positive predictions
    • "recall": TP / (TP + FN), or 0.0 if no actual positives (also equals TPR)
    • "f1": harmonic mean of precision and recall, or 0.0 if both are zero
    • "fpr": FP / (FP + TN), or 0.0 if no actual negatives
    • "tpr": same as recall
    • "expected_cost_per_order": total cost divided by total orders
  • "base_rate": the input base rate (echoed back)
  • "cost_optimal_probability_threshold": the breakeven probability above which flagging is cheaper than passing, computed as (review + friction) / (missed_fraud + friction)
  • "lowest_cost_model": "A", "B", or index string of the model with minimum expected cost per order, or None if no models provided

Constraints

  • 1 ≤ number of models ≤ 100
  • All confusion matrix entries are non-negative integers
  • TP + FP + FN + TN ≥ 1 for each model
  • All costs are positive floats
  • 0 < base_rate < 1
  • Return floating-point values with full precision (do not round)

Examples

Example 1

Input: compare_fraud_models([[490, 4900, 210, 94400], [560, 8400, 140, 90900]], {'review': 3, 'friction': 1, 'missed_fraud': 120}, 0.007)

Output: {'models': [{'name': 'A', 'precision': 0.09090909090909091, 'recall': 0.7, 'f1': 0.16091954022988506, 'fpr': 0.04934541792547835, 'tpr': 0.7, 'expected_cost_per_order': 0.4627}, {'name': 'B', 'precision': 0.0625, 'recall': 0.8, 'f1': 0.11594202898550725, 'fpr': 0.08459214501510574, 'tpr': 0.8, 'expected_cost_per_order': 0.5208}], 'base_rate': 0.007, 'cost_optimal_probability_threshold': 0.03305785123966942, 'lowest_cost_model': 'A'}

Hints

Hint 1

The expected cost per order is the sum of (flagged orders × review cost) + (false positives × friction cost) + (false negatives × missed fraud cost), all divided by total orders.

Hint 2

The cost-optimal threshold comes from comparing the cost of flagging versus passing an order with probability p of being fraud. Set them equal and solve for p.

Hint 3

A model with higher recall is not necessarily better—compare the models strictly on expected cost per order under the given cost structure.