Sub topic 2
Automated Retraining Pipelines and Their Triggers
This chapter assumes you know the train, validate, test, and deploy cycle, can write Python with NumPy, and have served a model in batch or online mode.
Detecting drift with PSI and the KS test
Drift is a change in the distribution of a feature between the training data and production data. The Population Stability Index (PSI) bins a feature using quantiles of the reference data and sums the weighted log ratio of the bin shares. A common reading is below 0.1 for stable, 0.1 to 0.2 for moderate change, and above 0.2 for significant drift. The two-sample Kolmogorov-Smirnov (KS) test compares the two empirical distributions and returns a p-value. Large samples make the KS test flag very small differences, so use PSI for the size of the change and KS as a second check.
import numpy as np
from scipy.stats import ks_2samp
def psi(reference, current, bins=10):
reference = np.asarray(reference, dtype=float)
current = np.asarray(current, dtype=float)
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.0, 1.0, 10_000)
current = rng.normal(0.5, 1.0, 10_000)
print("PSI:", round(psi(reference, current), 3))
print("KS p-value:", ks_2samp(reference, current).pvalue)Data drift and data volume triggers
A data drift trigger fires when the PSI of one or more features crosses a threshold. A data volume trigger fires when enough new rows have arrived. Volume matters because a PSI computed from a few hundred rows is noisy and can cross the threshold by chance.
Combining signals into a trigger policy
Use a policy that combines signals instead of a single signal. A typical policy sets a PSI threshold, a minimum row count, a maximum model age, and a cooldown. The cooldown prevents retraining loops, where each new model triggers another run.
def should_retrain(age_days, psi_value, new_rows):
if age_days < 2:
return False, "cooldown"
if age_days >= 30:
return True, "max_model_age"
if psi_value >= 0.2 and new_rows >= 50_000:
return True, "drift_with_enough_data"
return False, "no_trigger"Stages of an automated retraining pipeline
Each stage produces an artifact that the next stage reads.
Validating data before training
Check the data before training starts. Fail the run on any error so a corrupted batch cannot become a model.
| Check | Example rule |
|---|---|
| Schema | Column names and types match the training contract |
| Nulls | Null rate below 2 percent for each feature |
| Range | Age between 0 and 120 |
| Volume | At least 50,000 new rows |
Exercise
A feature has PSI 0.31, but only 400 new rows arrived this week. Should the policy above retrain, and why?
Show answer
No. The policy needs at least 50,000 new rows before drift can trigger a run. Log the drift for review and wait for more data.