Sub topic 2
Choosing Reference Data and Comparison Windows
Assume basic Python, NumPy, supervised model evaluation, hypothesis tests, and model deployment. This chapter uses those ideas to compare production data with a trusted baseline.
Choose reference data and comparison windows
Reference data is the data that represents expected behavior. It usually comes from a recent training or validation period, after removing known bad records. Do not use the entire historical dataset automatically. Old data may represent a different product, policy, or customer population.
A comparison window is the new data being tested. Choose its size from traffic and decision speed. A daily window may detect problems quickly, but it can be noisy. A weekly window is more stable. Compare matching periods when behavior is seasonal, such as Monday traffic with earlier Monday traffic.
Keep the windows separate from training decisions. Log the feature values, prediction, score, model version, timestamp, and relevant slice identifiers. Store the reference definition so that an alert can be reproduced.
Test one feature at a time: KS and chi-square
For a numeric feature, the two-sample Kolmogorov-Smirnov test compares the full distributions. Its statistic measures the largest gap between their cumulative distributions. For a categorical feature, the chi-square test compares category counts.
import numpy as np
from scipy.stats import ks_2samp, chi2_contingency
reference_amount = np.array([10, 12, 13, 15, 16, 18])
current_amount = np.array([11, 14, 17, 20, 21, 25])
ks = ks_2samp(reference_amount, current_amount)
print(f"KS={ks.statistic:.3f}, p={ks.pvalue:.3f}")
counts = np.array([[50, 30, 20], [40, 30, 30]])
chi2, p, dof, expected = chi2_contingency(counts)
print(f"chi2={chi2:.2f}, p={p:.3f}")A small p-value suggests a difference under the test assumptions. Large samples can make tiny changes significant, so inspect the effect size and practical impact. When testing many features, account for multiple comparisons or alerts will become noisy.
Score drift with the Population Stability Index
The Population Stability Index (PSI) divides both windows into the same bins and calculates:
PSI = sum((current_percent - reference_percent) * ln(current_percent / reference_percent))
Use bins created from the reference data, not independently from each window. Add a small epsilon when a bin has zero observations. PSI is a monitoring score, not a universal hypothesis test. Set thresholds using historical, labeled behavior.
Watch prediction and score distributions
Track the predicted class and the model's score or probability. Prediction drift can reveal changed inputs, routing bugs, or altered class balance before labels arrive. Examine overall distributions and important slices. A stable average score can hide a damaging change in one customer group.
Track model performance once labels arrive
When ground-truth labels become available, join them to predictions by a stable prediction ID. Calculate metrics such as accuracy, precision, recall, calibration, or business loss over the same cohorts used for drift checks. Preserve the prediction-time window, because late labels can otherwise mix different model versions.
Exercise
Which baseline should you use to compare a seasonal Monday production window?
Show answer
Use a reference window with comparable Monday behavior, such as recent Mondays, and verify that its data quality and model version are appropriate.