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 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 changes while the conditional distribution of the answer given the input, , stays the same.
- label shift
- A dataset shift in which the answer distribution changes while the conditional distribution of the input given the answer, , 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 , changes. The input distribution can stay the same.
- population stability index (PSI)
- Split the values into bins, and let and 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.
- two-sample Kolmogorov-Smirnov test
- A test that uses the largest vertical gap D between the empirical cumulative distribution functions and 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.
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).
| Change | What changes | What stays | Example |
|---|---|---|---|
| The inputs change | P(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 changes | P(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 changes | P(y | x) | The inputs may stay the same | Fraudsters 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).
P(x). Stays: P(y | x). Both groups slide sideways, and the boundary still separates them.P(y). Stays: P(x | y). Only the lower group grows, from 7 points to 16, and the points sit in the same places.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.
- The input distribution. Compare each input with training (PSI, KS test). It points straight at which input moved. But with many inputs you get more false alarms, and it also fires for inputs the model barely uses.
- The prediction distribution. Compare the model's scores or class shares with those at training time (on the validation data). Even with hundreds of inputs, it is one line to watch. A change in the label mix shows up here. One paper that compared many detection methods (see sources) reports that a two-sample test after reducing the data with a pre-trained classifier worked best.
- Confidence. Watch the average top probability, or the share of low-confidence predictions. It often drops when the model sees unfamiliar inputs. But it only means something if the probabilities match how often the model is right (calibration), and a neural network can be very confident on unfamiliar inputs.
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
- PSI:
PSI = Σ (qᵢ − pᵢ)·ln(qᵢ ÷ pᵢ). Split the values into bins and compare the share in bin i at training time (pᵢ) and in production (qᵢ). It is never below 0, and swapping p and q gives the same value. For a category input, each category is a bin. - KL divergence:
D_KL(Q ‖ P) = Σ qᵢ·ln(qᵢ ÷ pᵢ). The same KL as on the cross-entropy page. It has a direction:Q ‖ PandP ‖ Qgive different values. PSI is the sum of both directions. - KS test: find the biggest vertical gap D between the two samples' cumulative share curves, and get the chance of a gap that big if both came from the same distribution (the p-value). No bins needed, but it handles one number input at a time (hypothesis testing).
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.
- 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. - 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. - 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. - 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).
- Differences. −0.2, 0, 0.2. They add up to 0.
- 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. - Products.
−0.2 × −0.511 = 0.102, 0,0.2 × 0.693 = 0.139. All 0 or more. - Add up.
PSI = 0.102 + 0 + 0.139 = 0.241. - Check with KL.
D_KL(Q ‖ P) = 0.3 × (−0.511) + 0 + 0.4 × 0.693 = 0.124andD_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.
| Bin | p | q | q − p | q ÷ p | ln(q ÷ p) | Part |
|---|---|---|---|---|---|---|
| 1 | 0.5 | 0.3 | −0.2 | 0.6 | −0.511 | 0.102 |
| 2 | 0.3 | 0.3 | 0 | 1 | 0 | 0 |
| 3 | 0.2 | 0.4 | 0.2 | 2 | 0.693 | 0.139 |
| Sum | 1 | 1 | 0 | 0.241 |
7. Properties worth knowing
| Property | In plain words | In the experiment |
|---|---|---|
| Never below 0, and 0 only when equal | Each bin's part is 0 or more, so nothing cancels | Section 5 step ③, the middle row |
| Symmetric, as the sum of KL both ways | Swapping p and q gives the same value. KL depends on direction | 0.124 + 0.117 in section 6 |
| Not 0 even for the same distribution | With a limited sample, chance differences remain as noise | 0.016 at mean 0, spread 1 |
| Grows even when the mean stays | It also catches changes in spread or shape | 0.658 at spread 2 |
| Depends on how you bin | Different 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 ones | Bins 1 and 10 |
8. Alert thresholds and false alarms
- Measure the noise first. Compute PSI between two periods you know are the same (for example, the training data split in two). That is the noise. With the same 10 bins and 1,000 values per sample as this page, changing only the seed over 20 pairs gives 0.007 to 0.036. Put the threshold well above that. Fixed thresholds are often quoted, but the noise depends on the number of bins and the sample size, so they do not carry over as is.
- With big samples, tests fire on tiny differences. The KS test's p-value gets small for smaller and smaller differences as the sample grows. Statistically different is not the same as harmful to the model. Look at the size (D, PSI) along with the p-value.
- Many inputs pile up chance alerts. Test 100 inputs, each at the 5% level, and about 5 will fire on average even when nothing changed. Make each test stricter by the number of inputs (Bonferroni correction), or watch the single prediction distribution first.
- Do not fire on one crossing. Fire when the threshold is crossed several times in a row. A short window (how much data you measure at once) is noisy, and a long one is slow.
- When it fires, look for a bug first. Does it line up with a release? Did the share of missing values, the units, or the category names change?
9. How to respond: retrain, adjust the threshold, roll back
- Roll back. If things changed right after you shipped a new model or new preprocessing, the cause may be your own bug, not the data. Go back to the previous version and fix the cause. For that, the previous version must be ready to switch back on (section 11).
- Adjust the threshold. If only the label mix changed, recalibrate the probabilities to the new mix, or move the cutoff for calling something positive. It is cheaper and faster than retraining. Estimate the new mix once a few labels arrive. There are also ways to estimate it from the model's predictions without labels (BBSE in the sources).
- Retrain. If the link changed, or inputs the model never saw become common, retrain with recent data. The retrained model must still pass a recent test set split by time before it goes live (data splitting). Retraining on a fixed schedule is simple, but it costs money even when nothing changed and reacts late to sudden changes.
10. When to use what
| Kind of change | What to watch, and why it helps | Downside | What to do |
|---|---|---|---|
| The inputs change | PSI and KS per input. Points to which input moved | If inputs move within a range the model knows, performance holds and the alert is false. Many inputs pile up chance alerts | Check performance once labels arrive. If unfamiliar inputs grew, retrain with recent data |
| The label mix changes | The prediction distribution (class shares, average score). One line, no labels needed | Probabilities stop matching the new mix. Watching accuracy alone finds it late | Estimate the new mix and recalibrate, or move the cutoff (threshold) |
| The link between input and label changes | Performance once labels arrive. The most certain signal | May not show in the input or prediction distributions, and you learn it only as fast as labels arrive | Retrain 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 names | Mistake it for drift and retrain, and the model learns the broken input | Roll 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.
| Record | What | Why |
|---|---|---|
| Seeds | The random seeds used for weight setup, shuffling, and splitting | To get the same result again from the same data and settings. GPU math can differ slightly even with the same seed |
| Data version | Which data from which period went into training, validation, and test, what was filtered out, and a fingerprint (hash) of the files | Different data makes a different model from the same code |
| Settings | Hyperparameters, preprocessing values (the mean and standard deviation used for normalization), library versions, code commit | Preprocessing that differs from training looks like drift by itself |
| Model version | The weight file and its label, and for each prediction in the logs, which version made it | For rollback, and to see whether an alert lines up with a version change |
| Training distribution | For each input, the bin edges and shares p, plus the distribution of prediction scores | PSI needs this p. Change the edges and the value changes |
Where it is used
- Scoring models. In loan review or fraud detection, labels come months later, so you watch the input and score distributions.
- Recommendations and search. Seasons, events, and new products change the inputs often. Watch the prediction distribution and the click rate together.
- Demand forecasting. When people suddenly behave differently, the link between input and label changes. Decide how often to retrain on recent data.
- Image and audio models. A new camera or microphone changes the spread of brightness or noise. Watch predictions and confidence instead of raw pixels.
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 here | Textbook term |
|---|---|
| Data drift | dataset shift, distribution shift |
| The inputs change | covariate shift |
| The label mix changes | label shift, prior probability shift |
| The link between input and label changes | concept drift, concept shift |
| Monitoring | model monitoring |
| Training distribution p, production distribution q | reference distribution, current distribution |
| Bin | bin, interval |
| PSI | population stability index |
| KL | Kullback-Leibler divergence |
| KL both ways, added | Jeffreys divergence, symmetric KL |
| Cumulative share curve | empirical CDF |
| KS test | Kolmogorov-Smirnov test, a two-sample test |
| Confidence | maximum softmax probability |
| False alarm | false positive |
| Stricter thresholds for many tests | multiple comparison correction, Bonferroni correction |
| Cutoff | decision threshold |
| Labels come late | label delay |
| Retrain, roll back | retraining, rollback |
| Seed, data version, model version | random seed, versioning |
Sources
- Stephan Rabanser, Stephan Günnemann, Zachary C. Lipton, Failing Loudly: An Empirical Study of Methods for Detecting Dataset Shift, arXiv:1810.11953. Machine learning systems tend to fail silently, and a comparison of detection methods (two-sample tests after reducing the data with a pre-trained classifier).
- Zachary C. Lipton, Yu-Xiang Wang, Alexander J. Smola, Detecting and Correcting for Label Shift with Black Box Predictors, arXiv:1802.03916. The definition of label shift (
P(y)changes,P(x | y)stays), and BBSE, which estimates the new label mix and corrects a classifier without labels. - Jose G. Moreno-Torres et al., A unifying view on dataset shift in classification, Pattern Recognition. The types of shift: covariate shift, prior probability shift, concept shift.
- SciPy documentation: scipy.stats.ks_2samp (the two-sample Kolmogorov-Smirnov test).