Sub topic 8
Data Drift and Data Volume Triggers
Assume you know how to train, validate, and deploy a model, and that you are familiar with Python, NumPy, batch or online serving, and basic version control. This chapter shows how changes in production data can signal that retraining is needed.
Why data triggers matter
A model learns patterns from its training data. Production data can later change because of new customers, seasonal behavior, product changes, sensor failures, or changes in data collection. A model may become less accurate even when its code has not changed.
A data drift trigger starts retraining when the distribution of one or more input features differs from the training reference. A data volume trigger starts retraining after enough new examples arrive. Volume matters because a small drift measurement can be noisy, while a large amount of new data may contain a meaningful new pattern.
| Trigger | Signal | Typical response |
|---|---|---|
| Data drift | Feature distribution differs from reference | Check data, then consider retraining |
| Data volume | Enough new labeled or unlabeled rows arrived | Retrain on a refreshed data window |
| Both | Drift is detected and enough rows exist | Start an automated training run |
Measuring drift
Population Stability Index (PSI) compares the percentage of observations in matching bins. Larger values mean the current distribution differs more from the reference distribution. The two-sample Kolmogorov-Smirnov (KS) test compares the cumulative distributions without requiring fixed bins.
import numpy as np
from scipy.stats import ks_2samp
def psi(reference, current, bins=10):
edges = np.unique(np.quantile(reference, np.linspace(0, 1, bins + 1)))
edges[0], edges[-1] = -np.inf, np.inf
ref_pct = np.histogram(reference, bins=edges)[0] / len(reference)
cur_pct = np.histogram(current, bins=edges)[0] / len(current)
ref_pct = np.clip(ref_pct, 1e-6, None)
cur_pct = np.clip(cur_pct, 1e-6, None)
return float(np.sum((cur_pct - ref_pct) * np.log(cur_pct / ref_pct)))
rng = np.random.default_rng(0)
reference = rng.normal(0, 1, 10_000)
current = rng.normal(0.5, 1, 10_000)
print("PSI:", round(psi(reference, current), 3))
ks = ks_2samp(reference, current)
print("KS statistic:", round(ks.statistic, 3))
print("KS p-value:", ks.pvalue)A threshold is a policy choice, not a universal law. For example, you might investigate PSI values at or above 0.2. Always check sample size, missing values, and feature-specific behavior before treating a statistic as a retraining command.
Combining drift with volume
A practical policy avoids retraining on every small fluctuation:
Use a stable reference dataset, such as the training or validation population. Store the reference version and the time window used for each measurement. Monitor individual features as well as an aggregate signal, because one important feature can drift while the average remains normal.
Exercise
A feature has PSI 0.25, but only 500 new rows are available and the policy requires 50,000 rows. Should retraining start?
Show answer
No. The drift signal passes its threshold, but the minimum data-volume condition does not.