Lab: a distance score is not an alarm threshold
The clustering chapter's second lab. A round-number cutoff on an anomaly score flags far more normal rows than it sounds like; calibrating on held-out normal rows gives the false-alarm rate you asked for.
What runs in CodeSplash
- UCL061K-nearest-neighbor distance detector Runs in CodeSplash
- UCL048Isolation Forest Runs in CodeSplash
- EVA027Split conformal prediction and coverage audit Runs in CodeSplash
- Isolation forest Verified in Python and R
- Nearest-neighbor outlier scores Verified in Python and R
You will be able to
- Tell an anomaly score from a calibrated alarm rule
- Compute a split-conformal threshold from held-out normal rows
- Check the deployed false-alarm rate on fresh normal data
- Explain why calibrating on training rows sets the cutoff too low
The question
ReadAn anomaly scoreAnomaly scoreHow unusual a row is compared with the rest; a ranking for review, not proof of a problem. ranks rows from ordinary to unusual. A colleague proposes to alarm whenever a row's distance from normal is above 2. Is that a 5% false-alarm rate, a 1% rate, or something else? The lab answers with data where every row is normal, so every alarm is a false alarm, and then builds an alarm whose rate is set on purpose.
Explore it
ExploreAExplorable · A score is not an alarm
How do you turn an anomaly score into alarms with a known false-alarm rate?
What share of new normal rows will raise an alarm (in %)?
Alarm rule
Calibrate on
1 means new normal rows look like the calibration rows.
- No alarm
- Alarm
- Calibration rows (dashed)
- New normal rows
- Planted anomalies
The cutoff comes from the calibration scores, not from the score's own scale. If calibration rows look like new normal rows, the share of normal rows past the cutoff stays near α.
Commit your prediction above to see the result
Make your prediction to see the result.
Move the threshold and watch the false-alarm rate on fresh normal rows. Switch the rule to the calibrated one and choose α. Then press Break it: the calibration scores come from the training rows, which sit closer to the center than new rows do, so the cutoff lands too low.
Calibrate it yourself
ExploreThe notebook measures what the round-number cutoff really does, then computes the split-conformal threshold, the k-th smallest held-out score with k = ⌈(n + 1)(1 − α)⌉, and checks the deployed rate.
Notebook exercise · ~12 min
A distance score is not an alarm threshold
Turn an anomaly score into an alarm with a chosen false-alarm rate, using held-out normal rows, and see what a round-number cutoff really does.
- What "a distance of 2" means
A colleague proposes to alarm when the score is above 2. Store the share of the normal
testrows that would raise an alarm innaive_far.Show a solution
Pythonnaive_far = np.mean(s_test > 2)Rnaive_far <- mean(s_test > 2) - A threshold for 5% false alarms
Use the held-out calibration scores
s_cal. Sort them and take the k-th smallest, with k = ⌈(n + 1) × 0.95⌉ and n the number of calibration rows. Store it inthr. (This split-conformal rule guarantees at most 5% false alarms on new normal rows, on average.)Show a solution
Pythonthr = np.sort(s_cal)[math.ceil((len(s_cal) + 1) * 0.95) - 1]Rthr <- sort(s_cal)[ceiling((length(s_cal) + 1) * 0.95)] - The false-alarm rate after deployment
Store the share of the normal
testrows whose score is abovethrinfar. Compare it withnaive_far.Show a solution
Pythonfar = np.mean(s_test > thr)Rfar <- mean(s_test > thr)
In the app this opens as a Python or R notebook with the sample data, and its checks mark your progress.
In CodeSplash
In the appDiscover's Spot anomalies flags rows by a share or by a score cutoff. Neither is a calibrated false-alarm rate: a share flags that many rows whatever the data, and a cutoff means what the score's scale means. The walkthrough runs both on a sample and has you read what each one flags.
Check: the lab
CheckpointCheck yourself
In three dimensions, a distance from the center above 2 flags about a quarter of normal rows. What does that show?
Show the answer
Answer: a
The same cutoff means different rates in different dimensions and data. Only calibration on normal rows ties a cutoff to a rate.
Check yourself
Why must the calibration scores come from held-out normal rows rather than the training rows?
Show the answer
Answer: a
The guarantee needs calibration rows that are exchangeable with future normal rows, which rows used to fit the detector are not.
Check yourself
The calibrated rule targets α = 0.05. On fresh normal rows the measured rate is a little below 0.05. Is that a problem?
Show the answer
Answer: b
Split conformal promises a rate of at most α, averaged over calibration samples. A single test sample can land below or slightly above it.
Recap
Recap- A score ranks rows; an alarm needs a rule whose error rate you chose.
- Calibrate on held-out normal rows: k = ⌈(n + 1)(1 − α)⌉ gives a rate of at most α.
- Calibrating on training rows sets the cutoff too low.
- Check false alarms on fresh normal rows, and by subgroup, before deploying.
Concepts in this lesson