Sub topic 14
Detecting Data Drift and Concept Drift
Prerequisites and working model
This chapter assumes you know basic Python, can train and evaluate a supervised model, and understand a deployed web service. You should also know that a model receives input features and produces predictions.
Data drift is a change in the distribution of input features between training and production. For example, a fraud model trained on transactions averaging $50 may later receive transactions averaging $200.
Concept drift is a change in the relationship between features and the target. The inputs may look similar, but their meaning changes. For example, customers who previously made a repayment after receiving a reminder may stop doing so after a policy change.
Data drift is observable from inputs alone. Concept drift usually requires delayed labels, because you must compare predictions with later outcomes.
Detecting data drift with a statistical test
A common first check compares a reference sample from training with a recent production sample. The two-sample Kolmogorov–Smirnov (KS) test measures the largest difference between their cumulative distributions.
Its p-value estimates how surprising the observed difference would be if both samples came from the same distribution. A small p-value is evidence of a difference. It is not proof that the model has become inaccurate.
import numpy as np
from scipy.stats import ks_2samp
rng = np.random.default_rng(0)
reference = rng.normal(0.0, 1.0, 5000)
live_same = rng.normal(0.0, 1.0, 5000)
live_shifted = rng.normal(0.5, 1.0, 5000)
for name, live in [("same", live_same), ("shifted", live_shifted)]:
result = ks_2samp(reference, live)
drifted = result.pvalue < 0.01
print(
name,
"statistic=", round(result.statistic, 3),
"p_value=", round(result.pvalue, 5),
"drift=",
drifted,
)The threshold of 0.01 is a policy choice. With many features or frequent checks, false alarms become more likely. Record the feature name, time window, sample sizes, test, threshold, and result. Also inspect practical size: a tiny change can be statistically significant with a very large sample.
Detecting concept drift
When labels arrive, calculate production performance by time window and compare it with a trusted baseline. Track metrics such as accuracy, precision, recall, calibration, or business loss. Compare the same population and evaluation rules. A lower score can indicate concept drift, but it can also result from label delays, a broken feature, changed traffic, or an incorrect evaluation join.
| Signal | What changed | Typical next check |
|---|---|---|
| Feature distribution | Inputs differ from training data | Inspect the source and pipeline |
| Prediction distribution | Model outputs changed | Check traffic mix and calibration |
| Labeled performance | Outcomes no longer match predictions | Check labels, segments, and concept drift |
Deciding whether to retrain
Drift should trigger investigation, not automatic retraining by default. First verify data quality and determine whether the change is temporary, expected, or harmful. Segment metrics by geography, customer type, or device when aggregate metrics hide local failures.
Retrain when recent, representative data is available and validation shows that a candidate model improves the required metrics without violating fairness, latency, or safety constraints. Keep the previous model available for rollback.
Exercise
A feature's KS test reports p = 0.001, but production accuracy is unchanged. What should you do first?
Show answer
Investigate whether the feature shift is expected, caused by a pipeline issue, or irrelevant to model performance. Do not retrain solely because the p-value is small.