Quality Monitoring › Drift Detection
Language AI HandbookAll chapters Quality Monitoring › Drift DetectionOne section · Sections in this chapter4 / 10 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Having metrics is necessary but not sufficient. You also need a principled method to decide when a metric has changed enough to warrant concern. This is the problem of drift detection, and it is harder than it looks. The naive solution of alerting whenever a metric crosses a fixed threshold fails in practice because metrics fluctuate naturally. Setting the threshold too tight generates constant false alarms. Setting it too loose misses real degradation. Drift detection provides a statistical framework for making this decision correctly.
Statistical Foundations
The core challenge is distinguishing real change from natural variability. Metrics fluctuate even when nothing is wrong, due to random sampling, diurnal traffic patterns, variation in user intent, and the intrinsic stochasticity of model outputs with nonzero temperature. A naive threshold like "alert when BLEU drops below 0.45" will fire constantly on noise. Statistical drift detection applies hypothesis testing to ask whether recent observations are consistent with the baseline distribution, or whether they represent a real shift.
The two-sample problem frames drift detection as a question with a clear statistical formulation: given a reference window of metric values and a current window of metric values, test whether they are drawn from the same distribution. Several tests are appropriate depending on what you are measuring and what kind of shift you are trying to detect.
For univariate continuous metrics like BLEU scores, the Kolmogorov-Smirnov (KS) test computes the maximum absolute difference between the empirical cumulative distribution functions of the two samples. It is distribution-free, meaning it does not assume the underlying distribution is Gaussian or any other parametric form, and it is sensitive to differences in both the location (mean shift) and the shape (variance or skewness changes) of the distributions.
where:
- : empirical cumulative distribution function of the reference window, giving the fraction of reference values at or below
- : empirical CDF of the current window
- : supremum over all values , meaning the largest absolute gap between the two CDFs at any point
A large statistic paired with a small p-value rejects the null hypothesis that both samples were drawn from the same distribution. The p-value depends on both and the sample sizes: the same carries more statistical evidence when computed from larger windows, so a monitoring system that uses larger windows has lower detection latency at the same false alarm rate.
The KS test is effective but has two practical limitations worth understanding. First, it operates on fixed windows, so it cannot raise an alarm until a full current window has accumulated. If your window size is 50 observations and drift begins at observation 1, you will not detect it until observation 50 at the earliest. Second, it is equally sensitive to shifts in location, scale, and shape, which means you cannot tell from the test alone whether the distribution has shifted down (quality degradation), become more variable (increased inconsistency), or both.
For monitoring input or output feature distributions, the Population Stability Index (PSI) is widely used in production ML systems, particularly in financial services where it originated. It quantifies how much a feature distribution has shifted by comparing binned proportions between reference and current windows:
where:
- : fraction of reference samples falling in bin
- : fraction of current samples falling in bin
- : number of bins
This formula has a form that should look familiar if you have encountered KL divergence: it is the sum of the difference in proportions times the log ratio of proportions, which is closely related to a symmetrized KL divergence. Each term measures how much a particular region of the distribution has changed. Bins where current frequency has increased contribute positive terms; bins where current frequency has decreased contribute positive terms too, because the logarithm flips sign with the fraction. The result is always non-negative and grows with the magnitude of the shift.
By convention, PSI below 0.1 indicates no significant shift, PSI between 0.1 and 0.25 indicates moderate shift worth investigating, and PSI above 0.25 indicates major shift requiring action. These thresholds come from empirical experience in credit risk modeling and are reasonable starting points, but your specific use case may warrant different thresholds depending on the natural variability of your metrics.
For high-dimensional features like embedding vectors, Maximum Mean Discrepancy (MMD) provides a kernel-based test that works in arbitrary dimensions. The intuition is that if two distributions are identical, the mean of any function computed over samples from each distribution should be the same. MMD finds the function (in a reproducing kernel Hilbert space) that maximizes the discrepancy between the two means, yielding a test statistic that is sensitive to differences in mean, variance, and higher-order moments simultaneously. For output embedding monitoring, MMD can detect shifts in the semantic distribution of outputs even when the marginal statistics of individual dimensions look similar.
Sequential Detection Algorithms
Statistical tests applied to fixed windows introduce a latency problem: you must wait for a full window of data before testing. If your window is 100 observations and drift starts at observation 1, you do not detect it for at least 100 observations. In high-traffic systems where 100 observations arrive within seconds, this is acceptable. In low-traffic systems where 100 observations take days to accumulate, this is a serious problem. Sequential detection algorithms process observations one at a time and raise an alarm as soon as evidence for drift is strong enough, without waiting for a fixed window to fill.
CUSUM (Cumulative Sum Control Chart) is a sequential hypothesis test that accumulates evidence for a shift in the mean of a process. The key insight is that if the process is at its expected level, the cumulative sum of deviations from the expected level should fluctuate near zero. If the process shifts, the cumulative sum will trend monotonically in one direction, and the accumulated evidence will eventually exceed a threshold. CUSUM maintains two running statistics, one for upward shifts and one for downward shifts:
where:
- : observed metric value at time
- : target (baseline) mean, estimated from the reference window
- : allowance parameter, typically set to half the minimum shift magnitude you want to detect; it prevents the cumulative sum from growing due to small random fluctuations that are not operationally significant
- , : cumulative sums for upward and downward deviations respectively, reset to zero when they go negative to prevent memory of past normal behavior from slowing alarm response
An alarm fires when either sum exceeds a threshold , which controls the false alarm rate. The relationship between , , and detection performance is well-characterized: for a given , increasing reduces false alarms (longer average run length under no-change conditions) at the cost of slower detection (longer average run length until alarm when change occurs). Practitioners typically tune to half the minimum shift of interest and tune using simulation to achieve an acceptable false alarm rate.
CUSUM is optimal for detecting shifts of a specific magnitude equal to . For shifts smaller than , it accumulates evidence slowly. For shifts larger than , it alarms very quickly. This means setting well requires knowing approximately what size of degradation you care about detecting, which in turn requires understanding your metric's natural variability and the minimum quality drop that would trigger a response.
ADWIN (Adaptive Windowing) uses an adaptive sliding window approach that automatically expands when data is stationary and contracts when drift is detected. It maintains a window of recent observations and continuously tests whether any partition of the window shows a statistically significant difference between the two halves. When a significant difference is found, the older half of the window is discarded, effectively forgetting the pre-drift data and resetting the baseline. ADWIN requires no prior knowledge of the expected drift magnitude and adapts naturally to gradual drift, making it suitable for use cases where you expect the rate of change to vary over time.
Page-Hinkley test detects persistent changes in the mean and is particularly useful for slow, gradual degradation that CUSUM might miss if the shift is smaller than the allowance parameter:
where is the running mean up to time , is a small allowed deviation that prevents accumulation from noise, and is the cumulative statistic. An alarm fires when exceeds a threshold. Unlike CUSUM, the Page-Hinkley test uses the running mean rather than a fixed target, which makes it more adaptive but also slower to detect sudden shifts.
The choice among these algorithms depends on what kind of drift you expect and what your detection latency requirements are. CUSUM is preferred for detecting abrupt shifts of known magnitude. ADWIN is preferred when the drift magnitude is unknown or variable. The KS test on fixed windows is preferred when you want a nonparametric test that is sensitive to shape changes as well as location shifts.