Data drift and monitoring models in production

A model learns from the data of the day it was trained. If the data coming in after launch looks different, the model quietly starts making mistakes, without a single line of code changing. Shift and widen the production data, watch PSI grow, and then build that formula one step at a time.

Short answer. A deployed model can get worse while its code stays the same, because the incoming data stops looking like the training data. The inputs, the share of each label, or the link between inputs and labels can change. Labels arrive late, so you first watch by comparing the input and prediction distributions with the training ones. A common yardstick is PSI = Σ (qᵢ − pᵢ)·ln(qᵢ ÷ pᵢ). When an alert fires, rule out a bug first, then retrain, adjust the threshold, or roll back to the previous version.

The 1,000 training values (green fill) stay fixed. The 1,000 production values (blue outline) move with the sliders: the mean shifts them, the spread (standard deviation) widens them. There are 10 bins. The 8 inner bins are 0.5 wide, and the two end bins (1 and 10) hold everything below −2 and everything from 2 up. The darker the orange behind a bin, the bigger its part of PSI. The middle row is each bin's part, and the bottom bar is their sum, PSI (0 to 1, with an arrowhead past 1). The dotted line is the PSI between two separate samples of the same distribution. The values are made-up normal data from a fixed-seed random generator, so they are the same every time you open the page. This is a scaled-down toy. In real monitoring there are many inputs, and you run this for each one and for the prediction scores too.

Try this. ① At the start (mean 0.5, spread 1), PSI is 0.277. The biggest part is bin 10 (2 and up) with 0.042, but the left bins (0.025, 0.04, 0.041, 0.04) and the right bins (0.033, 0.038) add almost as much. Bins that shrank and bins that grew both count. ② Set mean 0 and spread 1. It is the same distribution as training, yet PSI is 0.016, not 0. That much is noise. ③ Move the mean to 0.1 and PSI is 0.031. At 0.2 it is 0.052. Small changes are hard to tell apart from noise. ④ Keep the mean at 0 and widen the spread to 2. The production mean stays at −0.04, but PSI is 0.658. Narrow it to 0.5 and PSI is 0.919. Watching only the mean would miss this. ⑤ Move the mean to 1 and PSI becomes 1.033, so an arrowhead appears at the end of the bar.

Definitions

The text below explains these with an experiment that moves the distribution of one input. These are the textbook definitions. x is the input, y is the answer, and we compare the distribution at training time with the distribution in production.

dataset shift
The joint distribution P(x,y) of the training data differs from the joint distribution of the data that comes in during production. It is also called drift or distribution shift.
covariate shift
A dataset shift in which the input distribution P(x) changes while the conditional distribution of the answer given the input, P(y∣x), stays the same.
label shift
A dataset shift in which the answer distribution P(y) changes while the conditional distribution of the input given the answer, P(x∣y), stays the same. It is also called prior probability shift.
concept drift
A dataset shift in which the relation between input and answer, that is, the conditional distribution P(y∣x), changes. The input distribution can stay the same.
population stability index (PSI)
Split the values into bins, and let pi and qi be the shares in bin i at training time and in production. PSI is defined below. It equals the sum of the KL divergence in both directions, so it is never negative and stays the same if p and q are swapped.

PSI=∑i(qi−pi)lnqipi=DKL(Q∥P)+DKL(P∥Q)

two-sample Kolmogorov-Smirnov test
A test that uses the largest vertical gap D between the empirical cumulative distribution functions F^1 and F^2 of two samples as the test statistic, to test the null hypothesis that the two samples come from the same distribution. It handles one numeric input at a time.

D=supx|F^1(x)−F^2(x)|

1. Why a deployed model gets worse

A model learns a rule from its training data: for this kind of input, this kind of answer. That rule rests on one assumption: future data comes from the same place as the training data. Test scores are measured under the same assumption (data splitting).

After launch, the assumption can break. Seasons change, new kinds of users arrive, an upstream program switches units, and people change how they act because of the model. The code and weights stay the same, the performance drops, and no error message appears. When the data in production is distributed differently from the training data, we call it data drift. Trying to notice it before the labels arrive is monitoring.

2. Three kinds of change

x is the input and y is the label (the right answer). P(x) is how the inputs are spread out, P(y) is the share of each label, and P(y | x) is what the label is for a given input (the link the model learns).

ChangeWhat changesWhat staysExample
The inputs changeP(x)P(y | x)An ad campaign brings in younger users, so the age input gets younger. What each age group buys stays the same
The label mix changesP(y)P(x | y)In flu season, a larger share of patients have the flu. The symptoms of a flu patient stay the same
The link between input and label changesP(y | x)The inputs may stay the sameFraudsters change tactics, so a transaction that used to look normal is now fraud

When the label mix changes, the inputs change with it (when the inputs come from the label), and the probabilities the model gives no longer match the new mix. In practice the three come mixed together. The three types are a frame for guessing the cause and picking a response. The experiment above shows the first kind, with one input. The figure below compares all three, before and after (green ● and blue ■ are the two groups with different labels, and the gray line is the model's boundary).

Before After
The inputs change. Changes: P(x). Stays: P(y | x). Both groups slide sideways, and the boundary still separates them.
Before After
The label mix changes. Changes: P(y). Stays: P(x | y). Only the lower group grows, from 7 points to 16, and the points sit in the same places.
Before After
The link between input and label changes. Changes: P(y | x). The inputs may stay the same. The points stay put, but the true boundary turns (orange dashes), so the old boundary (gray) now gets the three × points wrong.

3. What to watch when labels are late or never come

Labels come late. You learn whether a loan was repaid months later, and you may never learn why someone skipped a recommendation. Until then, you can watch three things.

When the link itself changes, the input and prediction distributions may look the same, so you only find out when labels arrive. That is why you also set up a way to get some labels sooner, such as having people label a small sample first.

4. How to measure: PSI, KL, and the KS test

With very fine bins and endless samples, take two normal distributions with the same spread whose means are d standard deviations apart: PSI gets close to d². At the experiment's start (mean 0.5), that is 0.25. The experiment shows 0.277 because it has only 10 bins and only 1,000 values.

5. How the formula is built

Using the same data as the experiment above, watch PSI come together one step at a time. Move the sliders and this chart changes too.

  1. Difference of shares: qᵢ − pᵢ. For each bin, subtract the training share from the production share. Why: to see which bins grew or shrank, you compare first. But both sets of shares add up to 1, so the differences add up to 0. Differences alone cannot measure how big the change is.
  2. Log of the ratio: ln(qᵢ ÷ pᵢ). Positive if production has more, negative if it has less. Why: a difference counts points, a ratio counts times. A rare bin that triples looks small as a difference but big as a ratio. And its sign always matches the difference.
  3. Multiply: (qᵢ − pᵢ)·ln(qᵢ ÷ pᵢ) ≥ 0. Why: two numbers with the same sign multiply to 0 or more, and the result is 0 only where the shares are equal. Now nothing cancels when you add.
  4. Add up: PSI = Σ (qᵢ − pᵢ)·ln(qᵢ ÷ pᵢ). Add every bin's part. You still know which bins added the most, so you can point to what changed.

Try this. With the starting data (mean 0.5, spread 1), bin 10 has the biggest part. ① 0.059 − 0.02 = 0.039, and the differences over all ten bins add up to 0. ② ln(0.059 ÷ 0.02) = ln 2.95 = 1.08. Check that every bar points the same way as in step 1. ③ 0.039 × 1.08 = 0.042. Every bar now points up. ④ The total is 0.277, and bin 10 is 15% of it. Set the mean to 0 in the first experiment and look at step 4: PSI 0.016 remains.

6. Work it out by hand

A tiny example with three bins. Training shares p = (0.5, 0.3, 0.2), production shares q = (0.3, 0.3, 0.4).

  1. Differences. −0.2, 0, 0.2. They add up to 0.
  2. Ratios and logs. The ratios are 0.6, 1, 2. The logs are ln 0.6 = −0.511, ln 1 = 0, ln 2 = 0.693.
  3. Products. −0.2 × −0.511 = 0.102, 0, 0.2 × 0.693 = 0.139. All 0 or more.
  4. Add up. PSI = 0.102 + 0 + 0.139 = 0.241.
  5. Check with KL. D_KL(Q ‖ P) = 0.3 × (−0.511) + 0 + 0.4 × 0.693 = 0.124 and D_KL(P ‖ Q) = 0.5 × 0.511 + 0 + 0.2 × (−0.693) = 0.117. The two directions differ, and their sum 0.241 is the PSI.
Binpqq − pq ÷ pln(q ÷ p)Part
10.50.3−0.20.6−0.5110.102
20.30.30100
30.20.40.220.6930.139
Sum1100.241

7. Properties worth knowing

PropertyIn plain wordsIn the experiment
Never below 0, and 0 only when equalEach bin's part is 0 or more, so nothing cancelsSection 5 step ③, the middle row
Symmetric, as the sum of KL both waysSwapping p and q gives the same value. KL depends on direction0.124 + 0.117 in section 6
Not 0 even for the same distributionWith a limited sample, chance differences remain as noise0.016 at mean 0, spread 1
Grows even when the mean staysIt also catches changes in spread or shape0.658 at spread 2
Depends on how you binDifferent bin counts or edges give different numbers. A bin that is empty on one side grows without limit, so you replace 0 with a tiny number. Keep the bin edges and always measure with the same onesBins 1 and 10

8. Alert thresholds and false alarms

9. How to respond: retrain, adjust the threshold, roll back

10. When to use what

Kind of changeWhat to watch, and why it helpsDownsideWhat to do
The inputs changePSI and KS per input. Points to which input movedIf inputs move within a range the model knows, performance holds and the alert is false. Many inputs pile up chance alertsCheck performance once labels arrive. If unfamiliar inputs grew, retrain with recent data
The label mix changesThe prediction distribution (class shares, average score). One line, no labels neededProbabilities stop matching the new mix. Watching accuracy alone finds it lateEstimate the new mix and recalibrate, or move the cutoff (threshold)
The link between input and label changesPerformance once labels arrive. The most certain signalMay not show in the input or prediction distributions, and you learn it only as fast as labels arriveRetrain with recent labeled data. Set up a way to get labels sooner
Sudden change right after a release (a bug)A step change that lines up with the release, missing-value share, category namesMistake it for drift and retrain, and the model learns the broken inputRoll back to the previous version and fix the cause

11. What to record so experiments can be reproduced

To roll back and retrain, you must be able to build that model again. To monitor, you need the training distribution. Record these with every model version.

RecordWhatWhy
SeedsThe random seeds used for weight setup, shuffling, and splittingTo get the same result again from the same data and settings. GPU math can differ slightly even with the same seed
Data versionWhich data from which period went into training, validation, and test, what was filtered out, and a fingerprint (hash) of the filesDifferent data makes a different model from the same code
SettingsHyperparameters, preprocessing values (the mean and standard deviation used for normalization), library versions, code commitPreprocessing that differs from training looks like drift by itself
Model versionThe weight file and its label, and for each prediction in the logs, which version made itFor rollback, and to see whether an alert lines up with a version change
Training distributionFor each input, the bin edges and shares p, plus the distribution of prediction scoresPSI needs this p. Change the edges and the value changes

Where it is used

Questions

Should I retrain as soon as I see drift?

No. The inputs can move and the model can still do fine, as long as they stay in a range the model knows well. First check whether the change lines up with a release, which would point to a bug. When labels arrive, check real performance. Then retrain. A retrained model still has to pass a test built from recent data before it goes live.

What PSI value is dangerous?

There is no single number that fits every case. The PSI you get when nothing changed (the noise) depends on the number of bins and the sample size. Measure PSI between two periods you know are the same, see how big the noise is, and set the threshold well above it. Then adjust it by checking whether past alerts came with real drops in performance.

How is KL different from PSI?

PSI is KL measured in both directions and added together. KL gives different values for Q‖P and P‖Q, so you must say which side is the reference. PSI is the same either way. In the worked example, 0.124 + 0.117 = 0.241 is the PSI. Both can grow without limit when a bin has a share of 0 on one side, so you replace 0 with a tiny number.

Words

Plain words used hereTextbook term
Data driftdataset shift, distribution shift
The inputs changecovariate shift
The label mix changeslabel shift, prior probability shift
The link between input and label changesconcept drift, concept shift
Monitoringmodel monitoring
Training distribution p, production distribution qreference distribution, current distribution
Binbin, interval
PSIpopulation stability index
KLKullback-Leibler divergence
KL both ways, addedJeffreys divergence, symmetric KL
Cumulative share curveempirical CDF
KS testKolmogorov-Smirnov test, a two-sample test
Confidencemaximum softmax probability
False alarmfalse positive
Stricter thresholds for many testsmultiple comparison correction, Bonferroni correction
Cutoffdecision threshold
Labels come latelabel delay
Retrain, roll backretraining, rollback
Seed, data version, model versionrandom seed, versioning

Sources

← QuantizationTokenization →