Sub topic 6
Choosing reference data and comparison windows
This chapter assumes you can train and evaluate a supervised model, run a hypothesis test, and read basic NumPy code. Every drift score compares two samples. The first is the reference, the data you treat as normal. The second is the current window, the recent production data you want to check. A drift test is only as meaningful as these two choices.
What the reference should represent
The reference answers one question: "What did the model see when it was known to work?" You have three common options.
| Reference | Strength | Weakness |
|---|---|---|
| Training data | Matches what the model learned from | May differ from production on day one because of sampling or preprocessing |
| Validation or early-production data | Reflects real serving conditions | Small, and may already contain problems |
| Rolling previous window | Adapts to slow, expected change | Hides gradual drift, because the baseline moves along with the data |
A fixed reference is the better default. It tells you how far production has moved from the conditions the model was built for. A rolling reference tells you only how much changed between two adjacent periods, so slow drift can pass unnoticed.
Choosing the current window
The current window has a size and a step. The size sets how much data each test sees. The step sets how often you test.
- A small window reacts quickly but gives noisy scores. With few rows, a test can flag chance variation.
- A large window gives stable scores but reacts slowly. A one-day problem may be diluted in a 30-day window.
- Match the window to the traffic and to natural cycles. If demand repeats weekly, use windows of whole weeks or compare the same weekdays.
Worked example: window size changes the result
The same small shift gives very different p-values depending on how many rows you compare. This code applies a KS test with a fixed reference to windows of different sizes.
import numpy as np
from scipy.stats import ks_2samp
rng = np.random.default_rng(0)
reference = rng.normal(loc=0.0, scale=1.0, size=5000)
for n in (100, 1000, 5000):
current = rng.normal(loc=0.1, scale=1.0, size=n)
result = ks_2samp(reference, current)
print(f"n={n:5d} KS={result.statistic:.3f} p-value={result.pvalue:.3f}")The shift is the same in every run, a mean change of 0.1. With 100 rows the test usually sees nothing. With thousands of rows the p-value falls because the test detects even tiny differences. Large windows make p-values hypersensitive, so pair them with an effect-size measure such as the KS statistic or PSI, which later chapters cover.
Common errors
- Reference contaminated by drift. If the baseline already includes an outage or a promotion period, later normal data looks drifted. Inspect the reference before you trust it.
- Reference built differently from production. Training data that was cleaned or resampled will not match raw serving data. Apply the same preprocessing to both.
- Ignoring seasonality. Comparing a holiday week with a quiet week reports drift that is expected.
- Tiny windows. Segments with little traffic produce unstable scores. Merge them or lengthen the window.
- Moving the reference silently. Re-baseline only on purpose, for example after retraining, and record the date.
Exercise
Your model serves a retail store with strong weekly demand cycles. A teammate proposes comparing each day to the previous day, using a rolling reference. Name two problems with this design and one alternative.
Show answer
Day-to-day comparison confuses the weekly cycle with drift (a Monday compared with a Sunday looks shifted), and a rolling reference hides slow drift because the baseline moves with the data. Use a fixed reference, and compare windows of whole weeks, or the same weekday against the same weekday.