Sub topic 7
Detecting Data and Prediction Drift
Assume you know binary classification metrics, train/validation/test splits, basic Python, pandas, and scikit-learn. Also assume your production system stores each prediction with its input features, score, model version, and prediction time.
When labels arrive late, you cannot immediately measure accuracy or recall. Drift detection gives you an earlier warning by checking whether production data or predictions differ from a trusted reference period.
What drift means
Data drift is a change in the distribution of model inputs. For example, a fraud model may receive transactions from a new country or a different mix of device types.
Prediction drift is a change in the distribution of model outputs. For example, the model may produce far more high-risk scores than it did during validation.
The reference distribution usually comes from a training, validation, or recent stable production cohort. Compare it with a current time window, such as the last day or week.
Drift is a monitoring signal, not a performance metric. A feature can change while the model remains accurate. Conversely, accuracy can fall without obvious marginal feature drift if the relationship between features and labels changes.
Choosing drift tests
Use tests that match the type of data and the question you are asking.
| Data or question | Useful comparison | Important limitation |
|---|---|---|
| Continuous feature or score | Kolmogorov-Smirnov test, quantile comparison | Sensitive to sample size |
| Categorical feature | Category frequency comparison, chi-square test | Rare categories can make results unstable |
| Business distribution | Population Stability Index (PSI) | Depends on bin definitions |
| Many features together | Classifier-based drift detection | Can detect a difference without explaining its cause |
The Kolmogorov-Smirnov test compares the entire empirical distributions of two continuous variables. A small p-value suggests that the distributions differ. With very large samples, tiny changes can produce small p-values, so also measure the size of the change.
PSI compares the proportions in fixed bins:
PSI = \sum_i (p_i-q_i)\log(p_i/q_i)Here, \(pi\) is the reference proportion and \(qi\) is the current proportion. Use bins defined from the reference data. Recomputing bins for every window can hide movement.
A simple score-drift check
import numpy as np
from scipy.stats import ks_2samp
reference_scores = np.array([0.03, 0.08, 0.12, 0.20, 0.31, 0.45, 0.52, 0.70])
current_scores = np.array([0.10, 0.18, 0.22, 0.35, 0.48, 0.61, 0.72, 0.90])
result = ks_2samp(reference_scores, current_scores)
print(f"KS statistic: {result.statistic:.3f}")
print(f"p-value: {result.pvalue:.4f}")
if result.pvalue < 0.05:
print("Investigate prediction drift.")The test assumes independent samples. Production records may be correlated, such as repeated transactions from one customer, so interpret the result carefully. Set minimum sample sizes and avoid alerting on one noisy window.
A practical alert often combines:
- a statistical test result;
- an effect-size threshold;
- persistence across multiple windows;
- affected traffic volume;
- important features or score ranges involved.
Detecting multivariate drift
A model can depend on interactions that univariate checks miss. To detect broader change, label reference rows as domain 0 and current rows as domain 1. Train a classifier to distinguish the two groups. If it performs much better than random guessing, the current population is distinguishable from the reference population.
This method helps detect joint changes, but it does not identify the cause automatically. Inspect feature importance, data quality checks, and recent deployment or policy changes. A classifier may detect a harmless timestamp or logging change instead of a meaningful population shift.
Exercise
A production score distribution has shifted, but the model's matured recall is unchanged. Should you disable the model immediately?
Show answer
No. Investigate the cause and keep the drift alert separate from the performance metric. The shift may reflect a real population change that the model handles correctly.