Quality Monitoring › Drift Detection

Michael BrenndoerferFebruary 14, 20268 min read
Language AI HandbookAll chapters
Part 65 of 67Production Systems
Chapter 498Quality Monitoring › Drift Detection
Reading section 4 of 10 · Drift Detection · 8 minSection 4 of 10 · 8 min

Book contents

Section 4 of 10 · 8 min read

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.

D=sup⁡x∣Fref(x)−Fcurr(x)∣D = \sup_x |F_{\text{ref}}(x) - F_{\text{curr}}(x)|

where:

  • Fref(x)F_{\text{ref}}(x): empirical cumulative distribution function of the reference window, giving the fraction of reference values at or below xx
  • Fcurr(x)F_{\text{curr}}(x): empirical CDF of the current window
  • sup⁡x\sup_x: supremum over all values xx, meaning the largest absolute gap between the two CDFs at any point

A large DD 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 DD and the sample sizes: the same DD 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:

PSI=∑i=1B(pcurr,i−pref,i)⋅ln⁡(pcurr,ipref,i)\text{PSI} = \sum_{i=1}^{B} \left(p_{\text{curr},i} - p_{\text{ref},i}\right) \cdot \ln\left(\frac{p_{\text{curr},i}}{p_{\text{ref},i}}\right)

where:

  • pref,ip_{\text{ref},i}: fraction of reference samples falling in bin ii
  • pcurr,ip_{\text{curr},i}: fraction of current samples falling in bin ii
  • BB: 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:

St+=max⁡(0, St−1++(xt−μ0−k))St−=max⁡(0, St−1−+(μ0−xt−k))\begin{aligned} S_t^+ &= \max(0,\ S_{t-1}^+ + (x_t - \mu_0 - k)) \\ S_t^- &= \max(0,\ S_{t-1}^- + (\mu_0 - x_t - k)) \end{aligned}

where:

  • xtx_t: observed metric value at time tt
  • μ0\mu_0: target (baseline) mean, estimated from the reference window
  • kk: 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
  • St+S_t^+, St−S_t^-: 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 hh, which controls the false alarm rate. The relationship between kk, hh, and detection performance is well-characterized: for a given kk, increasing hh 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 kk to half the minimum shift of interest and tune hh using simulation to achieve an acceptable false alarm rate.

CUSUM is optimal for detecting shifts of a specific magnitude equal to 2k2k. For shifts smaller than 2k2k, it accumulates evidence slowly. For shifts larger than 2k2k, it alarms very quickly. This means setting kk 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:

mt=xt−xˉt−δMt=max⁡(0, Mt−1+mt)\begin{aligned} m_t &= x_t - \bar{x}_t - \delta \\ M_t &= \max(0,\ M_{t-1} + m_t) \end{aligned}

where xˉt\bar{x}_t is the running mean up to time tt, δ\delta is a small allowed deviation that prevents accumulation from noise, and MtM_t is the cumulative statistic. An alarm fires when MtM_t 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.