Machine learningLoading progress…

Monitoring and drift

Construct a leakage-resistant pipeline and select an evaluation procedure that reflects deployment.

A model keeps meeting new data

After launch the world moves: prices change, a sensor is replaced, a new customer group arrives. Drift means the data the model sees differs from the data it learned from. There are three things to watch:

  • Inputs: are feature distributions still like training? Available immediately, no labels needed.
  • Outputs: is the share of positive predictions or the score distribution stable?
  • Outcomes: is accuracy holding? This needs labels, which often arrive weeks late.

An input shift is a warning, not a measured error. A feature may move without hurting the model, and a model can degrade while every input histogram looks the same. Treat drift alerts as a reason to look, and keep measuring outcomes when labels arrive.

Two ways to compare a window with training

PSI (population stability index) cuts the training values into bins (here deciles), compares the share of the window in each bin with the training share, and sums (actual - expected) * ln(actual / expected). It is 0 when the shares match and grows with the shift. KS distance is the largest gap between the two cumulative distribution functions: 0 for identical samples, 1 for completely separate ones.

Both are noisy with small samples. With 100 values and 10 bins, even unchanged data gives PSI around 0.1 on an average day, so an alert line of 0.03 would fire on all 60 of 60 days. Common rules of thumb put worry somewhere around 0.1 to 0.25, but they are conventions, not laws; tune against your own data volume.

Window (days pooled)1Alert threshold0.03
Calm (one bad-sensor day, day 20)0.60day 0day 59

60 alert days, all false.

Step drift (mean +1 from day 30)0.60day 0day 59

30 false alarm days before the drift; first real alert 0 days after it started.

Gradual drift (from day 25)0.60day 0day 59

25 false alarm days before the drift; first real alert 0 days after it started.

Blue: the statistic for the last 1 day of data against the training reference (100 values per day). Red line: your alert threshold. Orange dashes: when drift really begins.

Three 60-day streams of one feature, 100 values a day. Move the window and the alert line. Red dots are alerts.

Predict first. The PSI of a feature crosses your alert line. Does that prove the model's accuracy has dropped?

Tuning an alert is a trade

A low line or a short window reacts quickly but raises false alarms (the one bad-sensor day, day 20, is a spike that vanishes when averaged with its neighbours). A high line or long window is quiet but slow, or misses a slow creep entirely. With a 3-day window and a line of 0.20 the step drift on day 30 is caught 1 day after it starts and the calm stream stays silent; the slow creep from day 25 takes 9 days to be caught. That is a choice about cost: how bad is a late alert, how bad is a false one?

Chapter context

Practical performance depends on the data boundary around training as much as on the estimator. Every learned preprocessing step is part of the model and must respect the same evaluation split.

Chapter reference: math, worked example & code

This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.

Math and notation

Treat the fitted object as preprocessing composed with prediction: model(x) = estimator(transform(x)). Fit both using the training data only. For an imbalanced binary task, report counts as well as rates so denominators remain visible.

Worked example

Training values [0,2] have mean 1. A test value is 100. If the mean is incorrectly fitted on all three values, it becomes 34. The test set has now changed the transformation applied to training examples. A separate 90-negative/10-positive dataset gives 90% accuracy but zero recall for always-negative predictions.

Reference code

The following example uses the Python standard library and does not call a remote service.

train, test = [0,2], [100]
assert sum(train)/len(train) == 1
assert sum(train+test)/len(train+test) == 34
assert 90/100 == .9
assert 0/10 == 0

Python examples are provided for study and local execution; this page does not execute Python.

Common mistakes

An excellent test score can reflect leakage. More hyperparameter trials can overfit a fixed validation set. Monitoring confidence alone does not measure real outcome quality.