Skip to main content
DARKPC.NET

Tune a Decision Threshold With Validation Data

Compare three decision thresholds against a fixed false-alert budget, then evaluate the chosen rule on independent records.

Difficulty Intermediate

Prerequisites

Use Python 3 and a text editor. The synthetic scores represent outputs from an already trained model. A higher score indicates stronger evidence for class 1. The exercise does not assume the scores are calibrated probabilities.

Choose a rule on validation data

validation = [(0.92, 1), (0.78, 0), (0.71, 1), (0.63, 1),
              (0.55, 0), (0.42, 0), (0.37, 1), (0.19, 0)]
thresholds = [0.5, 0.7, 0.8]

def counts(rows, threshold):
    tp = sum(label == 1 and score >= threshold for score, label in rows)
    fp = sum(label == 0 and score >= threshold for score, label in rows)
    return tp, fp

eligible = []
for threshold in thresholds:
    tp, fp = counts(validation, threshold)
    print(threshold, tp, fp)
    if fp <= 1:
        eligible.append((tp, -fp, threshold))
if not eligible:
    raise SystemExit("No tested threshold fits the budget.")
chosen = max(eligible)[2]
print("Chosen", chosen)

test = [(0.73, 1), (0.70, 0), (0.69, 1), (0.42, 0)]
print("Independent test", counts(test, chosen))

Check the decision

At 0.50 the validation set has two false alerts, so that option fails the budget. At 0.70 there are two correct alerts and one false alert. The 0.80 threshold misses one extra positive. The example picks 0.70. Equality triggers an alert, which matters for the test example at exactly 0.70.

Troubleshooting and practice

Record that rule and its validation criteria before opening the test set. If no threshold fits the budget, report the result and revisit the requirements or model on training and validation data. Testing threshold after threshold on the same held-out set weakens its independence. Repeat with a validation budget of zero. Explain the expected trade-off, and keep sensitive case records out of threshold reports.

References and related reading