Cost-Minimizing Fraud Decision Threshold

Quick Overview

Implement a function that chooses the decision threshold for a fraud classifier so that the weighted cost of false positives and false negatives is as low as possible. It tests confusion-matrix counting under an inclusive threshold rule, tie-breaking, input validation and an efficient sweep over candidate thresholds.

Cost-Minimizing Fraud Decision Threshold

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `find_optimal_threshold(y_true, y_probs, cost_fp, cost_fn)`. A sample is flagged as fraud when `y_prob >= threshold`. Return the candidate threshold minimizing `cost_fp * FP + cost_fn * FN` as a Python `float`. For example: ```python y_true = [0, 0, 1, 1, 0, 1] y_probs = [0.1, 0.4, 0.35, 0.8, 0.65, 0.9] find_optimal_threshold(y_true, y_probs, cost_fp=1, cost_fn=5) # Output: 0.35 (FP=2, FN=0, cost=2) ``` ```hint Work the example by hand List the seven candidates for the example and write down FP and FN at each one. Watch how the counts change as the threshold rises, and think about what sorting each class's scores would let you look up directly. ``` ```hint Ties and the inclusive rule A score equal to the threshold is flagged. Make sure your counts treat that case the same way, and check how you choose among several candidates that share the minimal cost. ``` ### Constraints & Assumptions - Candidate thresholds are the sorted unique values of `y_probs` plus `np.inf` (flags nothing). Tied scores share one threshold. - If several candidates reach the minimal cost, return the largest. - Inputs may be lists or NumPy arrays: 1-D, equal length, non-empty, `y_true` contains only 0/1. - Costs must be non-negative. - Any violation of the above input rules raises `ValueError`. ### What a Strong Answer Covers - Input validation that accepts lists or NumPy arrays and raises `ValueError` for every input rule in the constraints. - Why the unique scores plus `np.inf` reach every cost a threshold can produce. - FP and FN counts that follow the inclusive `>=` rule when a score equals the threshold. - The tie-break toward the largest threshold and a Python `float` return value. ### Follow-up Questions - What is the time complexity, and how do you avoid an O(n²) sweep? - If the probabilities are well calibrated, what threshold would theory predict? - How would you handle a cost that depends on each transaction's amount?

Overview: Implement a function that chooses the decision threshold for a fraud classifier so that the weighted cost of false positives and false negatives is as low as possible. It tests confusion-matrix counting under an inclusive threshold rule, tie-breaking, input validation and an efficient sweep over candidate thresholds.

General logo
General
Nov 24, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement find_optimal_threshold(y_true, y_probs, cost_fp, cost_fn). A sample is flagged as fraud when y_prob >= threshold. Return the candidate threshold minimizing cost_fp * FP + cost_fn * FN as a Python float.

For example:

y_true  = [0, 0, 1, 1, 0, 1]
y_probs = [0.1, 0.4, 0.35, 0.8, 0.65, 0.9]
find_optimal_threshold(y_true, y_probs, cost_fp=1, cost_fn=5)
# Output: 0.35   (FP=2, FN=0, cost=2)

Constraints & Assumptions

  • Candidate thresholds are the sorted unique values of y_probs plus np.inf (flags nothing). Tied scores share one threshold.
  • If several candidates reach the minimal cost, return the largest.
  • Inputs may be lists or NumPy arrays: 1-D, equal length, non-empty, y_true contains only 0/1.
  • Costs must be non-negative.
  • Any violation of the above input rules raises ValueError .

What a Strong Answer Covers Guidance

  • Input validation that accepts lists or NumPy arrays and raises ValueError for every input rule in the constraints.
  • Why the unique scores plus np.inf reach every cost a threshold can produce.
  • FP and FN counts that follow the inclusive >= rule when a score equals the threshold.
  • The tie-break toward the largest threshold and a Python float return value.

Follow-up Questions Guidance

  • What is the time complexity, and how do you avoid an O(n²) sweep?
  • If the probabilities are well calibrated, what threshold would theory predict?
  • How would you handle a cost that depends on each transaction's amount?