Sub topic 7
Detecting Drift with PSI and the KS Test
Assume you know how to train and deploy a model, use NumPy, and compare validation metrics. This chapter focuses on detecting changes in numeric input features. Drift detection produces a signal for a retraining policy. It does not, by itself, prove that the model has become inaccurate.
Compare a reference distribution with a current distribution
A reference distribution is a stable sample, usually from the training or validation period. A current distribution contains recent production values. Compare the same feature with the same preprocessing applied to both samples.
Population Stability Index (PSI) divides the reference values into bins and compares the percentage of observations in each bin:
PSI = \sum_i (q_i - p_i)\log(q_i/p_i)Here, \(pi\) is the reference percentage and \(qi\) is the current percentage. A common rule of thumb treats PSI below 0.1 as small change, 0.1 to 0.2 as moderate change, and 0.2 or higher as a warning. These cutoffs are heuristics. Validate them against historical model performance.
The two-sample Kolmogorov-Smirnov (KS) test compares the entire empirical cumulative distribution of two samples. Its statistic is the largest vertical distance between their cumulative distributions. A small p-value suggests that the samples are unlikely to come from the same distribution.
import numpy as np
from scipy.stats import ks_2samp
def psi(reference, current, bins=10):
"""Calculate PSI using quantile bins from the reference sample."""
reference = np.asarray(reference, dtype=float)
current = np.asarray(current, dtype=float)
edges = np.unique(np.quantile(reference, np.linspace(0, 1, bins + 1)))
if len(edges) < 2:
return 0.0
edges[0], edges[-1] = -np.inf, np.inf
reference_pct = np.histogram(reference, bins=edges)[0] / len(reference)
current_pct = np.histogram(current, bins=edges)[0] / len(current)
reference_pct = np.clip(reference_pct, 1e-6, None)
current_pct = np.clip(current_pct, 1e-6, None)
return float(np.sum(
(current_pct - reference_pct)
* np.log(current_pct / reference_pct)
))
rng = np.random.default_rng(0)
reference = rng.normal(0.0, 1.0, 10_000)
current = rng.normal(0.5, 1.0, 10_000)
print("PSI:", round(psi(reference, current), 3))
result = ks_2samp(reference, current)
print("KS statistic:", round(result.statistic, 3))
print("KS p-value:", result.pvalue)Interpret the signal carefully
PSI depends on bin edges. Quantile bins make comparisons reasonably balanced, but different bin choices can produce different values. KS avoids fixed bins, but with very large samples it can flag tiny, unimportant differences. With small samples, it can miss meaningful changes.
Monitor several important features, not just one. Require a minimum sample size and use a sustained signal, such as drift in two consecutive windows. Also compare drift with prediction distributions and, when labels arrive, actual performance.
Exercise
Why should a large KS sample not automatically cause retraining?
Show answer
A large sample can make a tiny distribution difference statistically significant. The difference may have little effect on predictions or business outcomes, so practical impact and model performance must also be checked.