Skip to content
Français

Drift in many features, and what to do about it

  • Lesson 13
  • 35 min
  • Module 3: Statistical process control and distribution drift

Where we are

We can test one feature for drift. Larkfield has many features, and a test on each of them every day gives false alarms by chance. In this lesson, we control those false alarms, rank drift by its size, and separate drift in the inputs from a drop in a model's performance.

Lesson 13 of 38 · Anomaly Detection: Find What Is UnusualDrift in many features, and what to do about it
Course overview

What you will be able to do

Check many features every day without drowning in false alarms, and decide what a drift alarm means for a detector. Eight tests a day at a 5% limit give a false alarm on about one day in three, even when nothing changed.

NotebookDo this lesson in a notebookColab · Kaggle · GitHub

The notebook has every code cell, task and check of this lesson. It does Bonferroni and Benjamini-Hochberg by hand, 248 tests on a month with nothing new, and a per-feature drift report. Then it runs the report every day for six weeks. Its setup cells install the packages and download the payments (about 7 MB). About 35 minutes.

Colab and Kaggle open your own copy. They need a free Google or Kaggle account. Stuck? See the solution notebook.

Before you startWhat to know and to have readyAbout 35 minutes

What you need to know

What you need on your computer

Nothing. The notebook runs in Colab, Kaggle or VS Code.

Tested with Python 3.12.13 in Jupyter on macOS, with pandas 3.1.1, NumPy 2.5.4 and SciPy 1.18.1. Colab and Kaggle were not tested.

A smoke alarm that goes off by mistake once a year is a good alarm. Now put one in each of the 100 rooms of a hotel. Each one is just as good. But together, they give the hotel a false alarm about every three or four days. The night manager soon stops running to every alarm.

Nothing is wrong with any single alarm. The problem is the number of them. Many checks, each with a small chance of a false alarm, give many false alarms together.

Larkfield checks eight payment features every day. They are the amount, the hour, the device type, the merchant category, the account type, the region, the country and the currency. Each check is a test from the last lesson, with a limit of p < 0.05. A test with that limit says “changed” by chance in 1 case of 20 when nothing changed.

The notebook checks each day of August against all of July: 31 days × 8 features = 248 tests. If nothing changed, chance alone gives about 5% of 248, so about 12 tests below 0.05. Before you read the output, predict: how many will you see?

248 tests: p < 0.05 in 62, Bonferroni 33, FDR (BH) 45
feature
country 21
currency 19
merchant_category 5
hour 4
region 4
device_type 4
amount 3
account_type 2

62 tests are below 0.05, five times more than chance. So many alarms are a problem: nobody can check 62 alarms a month. But they are not all false either. Most are on country and currency. On a day with a burst of planted fraud from abroad, these two features really move. A test cannot tell a fraud burst from a change of normal. It only tells you that the day is different.

Two methods control the false alarms of many tests.

The Bonferroni correction divides the limit by the number of tests. With 10 tests a day, each test must be below 0.05 / 10 = 0.005. Then the chance of even one false alarm in the day stays below 5%. It is simple and strict. It also misses many real changes, because each test must be very sure.

The Benjamini-Hochberg (BH) procedure controls the false discovery rate (FDR): among the tests that you keep as “changed”, at most 5% are false on average. You accept a few false alarms in exchange for finding more real changes. The procedure has three steps:

  1. Sort the p-values from the smallest to the largest, and number them: rank 1, 2, 3 …
  2. Give each one a limit of 0.05 × rank / number of tests.
  3. Find the largest rank whose p-value is under its limit. Keep that test and every test with a smaller rank.

Here are ten p-values of one day (constructed). Before you read the last columns, predict: how many does each method keep?

Feature p Rank p < 0.05 Bonferroni: p < 0.005 BH limit BH keeps
A 0.001 1 yes yes 0.005 yes
B 0.008 2 yes no 0.010 yes
C 0.012 3 yes no 0.015 yes
D 0.030 4 yes no 0.020 no
E 0.041 5 yes no 0.025 no
F 0.120 6 no no 0.030 no
G to J 0.25 to 0.90 7 to 10 no no 0.035 to 0.050 no

The plain limit keeps 5, Bonferroni keeps 1 and BH keeps 3. C, at 0.012, is under its BH limit of 0.015, so BH keeps A, B and C. D, at 0.030, is above its limit of 0.020, and so is every test after it.

  • p-values, sorted
  • BH limit
  • Bonferroni limit, 0.005
  • Plain limit, 0.05
The ten sorted p-values against three limits. BH keeps every test up to the last dot under its rising line: ranks 1 to 3. Bonferroni keeps only rank 1; the plain limit keeps ranks 1 to 5.
Text description of the chart

The chart shows ten dots for the sorted p-values, from 0.001 at rank 1 to 0.120 at rank 6. The dots for ranks 7 to 10 (0.25 to 0.90) are above the top of the chart. Three dashed lines show the limits. The plain limit is flat at 0.05: the first five dots are under it. The Bonferroni limit is flat at 0.005: only the first dot is under it. The BH limit rises from 0.005 at rank 1 to 0.05 at rank 10. The dots at ranks 1, 2 and 3 are under it, and the dot at rank 4 (0.030) is above it.

On Larkfield’s 248 tests, Bonferroni keeps 33 and BH keeps 45. Both are better than 62. But both still keep tens of tests, because, as in the last lesson, the samples are large.

With big samples, almost every feature is “significant”, so a correction of the p-values is not enough. The answer of the last lesson applies here too: rank the features by the size of their change. The notebook builds a drift report by hand. For each feature, it gives the test, the p-value, the BH decision and the PSI. It also gives a size label: small below 0.1, moderate to 0.25, large above. This is for the window after the new region (2025-09-15 to 2025-10-14) against August:

feature test p PSI BH size
region chi-square 0 1.3018 True large
country chi-square 1.12e-52 0.0123 True small
amount KS 7.24e-29 0.0068 True small
currency chi-square 3.64e-28 0.0064 True small
device_type chi-square 3.34e-33 0.0055 True small
hour KS 6.97e-09 0.0027 True small
account_type chi-square 1.54e-14 0.0022 True small
merchant_category chi-square 0.0418 0.0005 True small

BH keeps all eight features. With about 55,000 payments in each window, every change is “significant”. By size, only one change is large: the region mix.

The PSI of each payment feature after the new region, against August. One change is large (region, 1.30); the next largest, country, is 0.0123.
Text description of the chart

A column chart with eight columns, sorted from the largest PSI to the smallest. The region column is tall, at 1.3018. The other seven columns are so short that they look flat. They are country 0.0123, amount 0.0068, currency 0.0064, device 0.0055, hour 0.0027, account 0.0022 and category 0.0005.

The report tells you where to look first: the region. The region change is planned, so it needs one note, not an investigation.

The notebook runs the report on every day from 2025-09-01 to 2025-10-14, against August:

44 days x 8 features = 352 checks
p < 0.05: 212; FDR (BH): 191; PSI 0.1 or more: 56
feature
region 41
country 13
currency 2

By p-value, with BH, 191 of 352 checks say “changed”. By size, 56 checks have a PSI of 0.1 or more. 41 of them are the planned new region, on almost every day. Ranked by size, the report has 15 checks left to look at in six weeks, and not 191.

The country feature passed a PSI of 0.1 on 13 days. What happened on those days? The notebook answers with the course’s answer key. The key is packed in data/sealed-key.txt, and compare.planted_payments() unpacks it. You can unpack it yourself: it is there to score your own work, not to keep a secret.

payments abroad planted payments abroad
country PSI 0.1 or more
False 17.9 13.9
True 52.0 48.8

On the 13 days with country drift, there were 48.8 planted fraud payments abroad on average, against 13.9 on the other days. The drift check found the days of the fraud bursts. It did not find which payments were fraud, which is the job of a detector. And it could not tell whether the change was fraud or a new kind of normal customer. Only a person who looks can.

So a drift alarm is a reason to look, not a proof that something is wrong. It can mean a planned change (the new region), a real problem (a fraud burst), or a broken feed.

People often say “the model drifted” for three different things. They have different causes and different fixes.

  • Input drift: the data that comes in looks different. Larkfield’s region mix changed with the new region. This lesson’s tests measure it.
  • Prediction drift: the detector’s outputs changed. Its scores move, or the number of payments above its threshold each day goes up or down.
  • Lower performance: the detector is worse at its job. Fewer of its alerts are real fraud, or more fraud gets through. You can see it only with labels, and labels come late: a chargeback can take weeks.
A flow from top to bottom. Input drift: the data looks different, for example the new region with a region PSI of 1.30. A question: did the detector's scores or alerts per day change? If no: write it down and keep watching. If yes: prediction drift, the scores moved. A second question: are the results worse, measured by reviewed alerts and missed fraud? If no: normal moved and the detector copes, so update the reference window. If yes: lower performance, so retrain or fix the detector.
Input drift is a reason to check the detector's outputs; a change in the outputs is a reason to check its results. Only worse results call for retraining or a fix.
Text description of the diagram

The diagram starts at the top with input drift: the data looks different, as with the new region (region PSI 1.30). An arrow leads to a question: did the detector’s scores or alerts per day change? If not, you write the change down and keep watching. If they changed, that is prediction drift, and a second question follows: are the results worse? You measure this with the reviewed alerts and the missed fraud. If the results are not worse, normal moved and the detector copes, so you update the reference window. If the results are worse, the detector’s performance dropped, and you retrain it or fix it.

Each step down the diagram needs more evidence and more time. Input drift is visible on the first day. Prediction drift is visible on the same day, if you log the scores. Lower performance is visible only when the labels arrive. The MLOps course explains these three in Data and concept drift, and how to work with late labels in Delayed labels.

What you find Example What to do
A planned change of the inputs the new region, region PSI 1.30 Write it down. Add the new data to the next reference window.
A large, unplanned change a feature that drops to zero after a release Check the data feed first: a broken feed is the most common cause.
A burst on a few days country drift on fraud days Look at the payments of those days. It may be what your detectors must find.
Worse results fewer real frauds among the reviewed alerts Retrain on a recent, checked reference window, or fix the features.

Open-source tools build this kind of per-feature drift report: Evidently and NannyML. NannyML can also estimate a model’s performance before the labels arrive. We did not run them here: Evidently adds many packages, and it did not work with this course’s version of pandas. The report in this lesson is about 30 lines of pandas and SciPy, and every number in it is one that you can explain.

Guided practice: the drift report in the notebook

Section titled “Guided practice: the drift report in the notebook”
  1. Open the notebook from the box Do this lesson in a notebook at the top of this page.
  2. Run the setup cells. The data cell ends with the Ready: line.
  3. In section 1, write your prediction for the ten p-values. Then run the cell. BH keeps 3.
  4. Run the helpers cell and the data cell. You see the payments of July and August.
  5. In section 2, write your prediction, then run the 248 tests. You see 62, 33 and 45.
  6. In section 3, run the report. region is first, with a large PSI.
  7. In section 4, run the daily report. You see 191 checks kept by BH and 56 with a PSI of 0.1 or more.
  8. In section 5, run the cell with the answer key. You see 48.8 planted payments abroad on the country-drift days.
  9. In section 6, write the three answers and run each check cell. Each prints Check passed.

Change one thing: the false discovery rate q. With q = 0.10, you accept that up to 10% of the kept tests are false. Before you compute it, predict: how many of the ten p-values of section 1 does BH keep now? In the notebook, use the limit 0.10 × rank / 10, and put the number in bh_10.

Show the answer
bh_10 = int((tiny["rank"] <= tiny["rank"][tiny.p <= 0.10 * tiny["rank"] / m].max()).sum())
Check passed: bh_10 is 5.

BH now keeps 5 tests: A to E. The limits double (0.01, 0.02, 0.03, 0.04, 0.05 …), so D (0.030, limit 0.04) and E (0.041, limit 0.05) are under them. A looser q finds more changes and accepts more false ones. Choose q, like every limit in this module, from the cost of a false alarm and the cost of a miss.

Common mistake

Retrain on every drift alarm

Symptom: Tomás wants to connect the drift report to the training job: “when a feature drifts, retrain the detector on the last 30 days”. This is a constructed case: we did not run it. With the daily report above, the detector would retrain on almost every day of the six weeks.

Cause: a drift alarm says that the inputs changed, not that the detector got worse. Some of the drift was the planned new region, which the detector may handle well. Some of it was the fraud bursts: the country drift days had 48.8 planted fraud payments abroad on average. Retraining on those days would teach the detector that fraud from abroad is normal.

Diagnose: follow the diagram. Did the scores or the alerts per day change? Are the reviewed results worse? If you cannot answer the second question, you do not yet know that retraining helps.

Fix: retrain on a reference window that a person checked, after a drop in performance or a planned, lasting change. Keep the days of known incidents out of it. Log every retraining with its reason.

Consequence in AI work: a detector that retrains on every change learns its own blind spots. It accepts the anomalies that it should find. The control limits from every day, in Control charts: is this variation normal?, made the same mistake.

Check your understanding

Explain in your own words why 248 tests with a limit of 0.05 give alarms even when nothing changed, and how Bonferroni and Benjamini-Hochberg differ.

Show the answer

When nothing changed, each test says “changed” by chance in about 1 case of 20. So 248 tests give about 12 false alarms by chance alone. Bonferroni divides the limit by the number of tests (0.05 / 248), so that even one false alarm in the whole set is unlikely. It is strict, and it misses many real changes. Benjamini-Hochberg controls the share of false alarms among the tests it keeps (the false discovery rate). It sorts the p-values and keeps every test up to the largest rank under 0.05 × rank / number of tests. It keeps more tests than Bonferroni: 45 against 33 on Larkfield’s 248.

Check your understanding

Your drift report after the new region keeps all eight features by BH, with p-values from 0.0418 down to 0. What do you look at instead, and what does it tell you here?

Show the answer

With about 55,000 payments per window, every feature is “significant”, so the p-values do not rank anything. Look at the size of each change, the PSI. Only the region is large (1.30); the next is the country at 0.0123, and the rest are below 0.007. So there is one change to explain: the planned new region. Write it down, and add the new region to the next reference window.

Check your understanding

Omar asks: the inputs drifted, so is the detector wrong now? Explain input drift, prediction drift and lower performance, and what evidence each one needs.

Show the answer

Input drift means the data looks different; you see it with tests like PSI on the first day. Prediction drift means the detector’s scores or alerts per day changed; you see it if you log the scores. Lower performance means the detector is worse at its job: fewer real frauds among its alerts, or more missed fraud. You see it only with labels, which come late. Input drift alone does not prove that the detector is wrong. It is a reason to check its outputs, and then its results.

Recap

  • Many tests give false alarms by chance: 62 of 248 tests below 0.05, where chance alone gives about 12. Bonferroni kept 33, and Benjamini-Hochberg (false discovery rate) kept 45.
  • With big samples, rank drift by its size. In six weeks, 191 checks said “changed” by p-value, and 56 by a PSI of 0.1 or more. 41 of those 56 were the planned region.
  • A drift alarm is a reason to look. Country drift led to the days of planted fraud bursts.
  • Input drift, prediction drift and lower performance are three different things. Only lower performance, measured with labels, calls for retraining.

You can now check many features and decide what a drift alarm means. But auditors also check the amounts themselves: which digits they start with, and how many sit just below a limit. Next, you test Benford’s law and the other business checks, and you see where they do not apply.

Next: Benford's law and other business checks