Skip to content

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.

Advanced ~25 min6 steps

What runs in CodeSplash

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

Read first

01

The question

Read

An 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.

02

Explore it

Explore

AExplorable · 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 %)?

30
How sure?

Alarm rule

0.05
0.60

Calibrate on

5
200
200
0.05
1.00

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 first; the figure appears when you commit.

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.

03

Calibrate it yourself

Explore

The 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.

  1. What "a distance of 2" means

    A colleague proposes to alarm when the score is above 2. Store the share of the normal test rows that would raise an alarm in naive_far.

    Show a solution
    Python
    naive_far = np.mean(s_test > 2)
    R
    naive_far <- mean(s_test > 2)
  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 in thr. (This split-conformal rule guarantees at most 5% false alarms on new normal rows, on average.)

    Show a solution
    Python
    thr = np.sort(s_cal)[math.ceil((len(s_cal) + 1) * 0.95) - 1]
    R
    thr <- sort(s_cal)[ceiling((length(s_cal) + 1) * 0.95)]
  3. The false-alarm rate after deployment

    Store the share of the normal test rows whose score is above thr in far. Compare it with naive_far.

    Show a solution
    Python
    far = np.mean(s_test > thr)
    R
    far <- 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.

04

In CodeSplash

In the app

Discover'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.

05

Check: the lab

Checkpoint

Check 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.

06

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