Drift Guardrails
Drift guardrails are the rules that decide when a model in production stops being trusted: thresholds on distribution distances, on prevalence, on performance, with an action attached to each. The rules are easy to write and the actions are where they usually go wrong.
Mechanisms and non-unique monitoring signatures
Covariate drift, label drift and concept drift are usually listed as three things to watch. They are better treated as three things to distinguish, while allowing overlapping signatures and simultaneous changes in real systems.
The simulator draws three standard-normal features and a rare binary label from a logistic model, so about 1% of rows are positive. A model is fitted once on 60,000 training rows and a baseline window Xb, yb is drawn from the same process. Each mechanism then changes one piece of the generator through make: shift moves the inputs, extra_bias moves the intercept of P(Y|X), and coef changes how the inputs relate to the label. ks_stat is the largest gap between two empirical CDFs of one feature, and ece averages the gap between the observed rate and the mean score over ten score bins, weighted by bin size.
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_auc_score
rng = np.random.default_rng(1207)
w = np.array([1.1, -0.7, 0.6])
bias = -5.6
def ks_stat(a, b):
a, b = np.sort(a), np.sort(b)
g = np.concatenate([a, b])
return float(np.max(np.abs(np.searchsorted(a, g, "right") / a.size
- np.searchsorted(b, g, "right") / b.size)))
def ece(y, p, bins=10):
edges = np.linspace(0, 1, bins + 1)
idx = np.clip(np.searchsorted(edges, p, "right") - 1, 0, bins - 1)
return float(sum(abs(y[idx == b].mean() - p[idx == b].mean()) * (idx == b).mean()
for b in range(bins) if (idx == b).sum() > 0))
def make(n, coef=w, shift=0.0, extra_bias=0.0):
X = rng.normal(size=(n, 3)) + shift
p = 1 / (1 + np.exp(-(X @ coef + bias + extra_bias)))
return X, (rng.random(n) < p).astype(int)
Xtr, ytr = make(60000)
model = LogisticRegression(max_iter=5000).fit(Xtr, ytr)
Xb, yb = make(40000)
pb = model.predict_proba(Xb)[:, 1]
Each case is scored with the fixed model and compared with the baseline on five columns: the worst per-feature KS statistic, the positive rate, the difference in positive rate from the baseline, AUC, and ECE.
cases = {
"nothing moved": make(40000),
"covariate: inputs shifted": make(40000, shift=0.45),
"intercept: fraud rate up": make(40000, extra_bias=1.28),
"concept: coefficients moved": make(40000, coef=np.array([0.3, -0.8, 1.5])),
}
print("%-30s %9s %9s %10s %9s %9s"
% ("what changed", "worst KS", "rate", "rate diff", "AUC", "ECE"))
print("%-30s %9s %8.2f%% %10s %9.4f %9.4f"
% ("baseline", "-", yb.mean() * 100, "-", roc_auc_score(yb, pb), ece(yb, pb)))
for name, (Xc, yc) in cases.items():
pc = model.predict_proba(Xc)[:, 1]
worst = max(ks_stat(Xb[:, j], Xc[:, j]) for j in range(3))
print("%-30s %9.4f %8.2f%% %9.2f pt %9.4f %9.4f"
% (name, worst, yc.mean() * 100, (yc.mean() - yb.mean()) * 100,
roc_auc_score(yc, pc), ece(yc, pc)))
what changed worst KS rate rate diff AUC ECE
baseline - 1.07% - 0.8328 0.0004
nothing moved 0.0105 0.93% -0.14 pt 0.8321 0.0012
covariate: inputs shifted 0.1865 1.40% 0.33 pt 0.8306 0.0019
intercept: fraud rate up 0.0058 3.12% 2.05 pt 0.8379 0.0205
concept: coefficients moved 0.0070 1.45% 0.38 pt 0.7935 0.0046
Reading the table by column: only the covariate row moves the input distribution (worst KS 0.1865), and its AUC and ECE barely change. The intercept row leaves KS at 0.0058 while the rate rises by 2.05 points and ECE grows to 0.0205. The concept row leaves KS at 0.0070 and the rate nearly unchanged, yet AUC falls to 0.7935.
The rows illustrate distinct simulated mechanisms, not uniquely identifying signatures. Covariate shift can change AUC through case mix. The intercept example holds P(X) fixed and changes P(Y|X), so it is not pure label shift; multiple metrics guide diagnosis without identifying a unique cause.
A guardrail sheet that watches one column can tell you something changed. It cannot tell you which of the three, and the three do not want the same response.
Diagnose prevalence movement before choosing a repair
Pure label shift assumes P(X|Y) is unchanged while class prevalence changes. For a calibrated old posterior and adequate support, the following prior-odds correction follows from Bayes’ rule, where \(p_{\text{old}}\) is the old model’s score, \(p_{\text{new}}\) is the corrected score, \(\pi_{\text{old}}\) is the positive prevalence the model was fitted on, and \(\pi_{\text{new}}\) is the current positive prevalence. The code’s intercept-shift generator is different: it fixes P(X), changes P(Y|X), and generally changes P(X|Y). Its demonstration therefore uses the known simulation intercept change, not an estimated prevalence-odds correction.
\[ \text{logit}(p_{\text{new}}) = \text{logit}(p_{\text{old}}) + \ln\frac{\pi_{\text{new}}}{1 - \pi_{\text{new}}} - \ln\frac{\pi_{\text{old}}}{1 - \pi_{\text{old}}} \]
The next example continues in the same Python session, reusing make, ece and the fitted model. Restarting the seed regenerates the same training rows, so pi_train is \(\pi_{\text{old}}\), the prevalence the model was fitted on, and pi_now is \(\pi_{\text{new}}\). The score correction adds the known constant 1.28 to the log-odds of every score, and the retrained model is fitted on a new window drawn under the changed intercept.
rng = np.random.default_rng(1207)
Xtr, ytr = make(60000)
pi_train = ytr.mean()
Xc, yc = make(40000, extra_bias=1.28)
p = model.predict_proba(Xc)[:, 1]
pi_now = yc.mean()
logit = np.log(p / (1 - p))
shift = 1.28 # Known simulation intercept change, NOT a label-shift estimate.
fixed = 1 / (1 + np.exp(-(logit + shift)))
Xn, yn = make(40000, extra_bias=1.28)
retrained = LogisticRegression(max_iter=5000).fit(Xn, yn)
pr = retrained.predict_proba(Xc)[:, 1]
print("Training prevalence %.2f%%, current prevalence %.2f%%." % (pi_train * 100, pi_now * 100))
print("%-22s %8s %8s %12s" % ("", "AUC", "ECE", "mean score"))
for name, q in (("as scored", p), ("after the constant", fixed),
("after a full retrain", pr)):
print("%-22s %8.4f %8.4f %11.4f%%" % (name, roc_auc_score(yc, q), ece(yc, q), q.mean() * 100))
Training prevalence 1.03%, current prevalence 3.19%.
AUC ECE mean score
as scored 0.8389 0.0214 1.0428%
after the constant 0.8389 0.0019 3.3608%
after a full retrain 0.8388 0.0017 3.2230%
The constant correction adds the known simulation intercept change of 1.28 to the fitted log-odds. It preserves ranking and may repair most calibration error in this correctly specified example, though fitting error remains. This is an oracle demonstration; in production, estimate a justified correction on calibration data and evaluate on separate labels. A full retrain is another candidate, not automatically necessary or useless.
Prevalence movement triggers diagnosis of population, labeling and calibration changes. Recalibration may suffice for a validated probability distortion; retraining can be justified by decision utility, subgroup performance, changed inputs or ranking degradation. Neither prevalence nor AUC alone determines the action.
A gate in AUC points needs a sample size beside it
The other standard rule is block deployment if AUC drops more than three points. AUC is an estimate, and for a rare event most of its variance comes from a handful of rows.
The next example continues in the same session and reuses make. It retrains the model under a new seed and takes its AUC on 200,000 rows as the reference. Every holdout window is drawn from that same process, so any window that trips the gate is a false block.
rng = np.random.default_rng(88)
Xtr, ytr = make(60000)
model = LogisticRegression(max_iter=5000).fit(Xtr, ytr)
Xr, yr = make(200000)
reference = roc_auc_score(yr, model.predict_proba(Xr)[:, 1])
print("Reference AUC on 200,000 rows: %.4f" % reference)
print("%10s %10s %12s %10s %10s %15s"
% ("holdout n", "eligible", "mean frauds", "mean AUC", "sd of AUC", "trips 3pp rule"))
for n, reps in ((500, 600), (2000, 600), (10000, 400), (50000, 200)):
aucs, pos = [], []
for _ in range(reps):
Xh, yh = make(n)
if yh.sum() < 2 or (len(yh) - yh.sum()) < 2:
continue # fewer than two positives or negatives: insufficient evidence
pos.append(yh.sum())
aucs.append(roc_auc_score(yh, model.predict_proba(Xh)[:, 1]))
aucs = np.array(aucs)
print("%10d %10s %12.1f %10.4f %10.4f %14.1f%%"
% (n, "%d/%d" % (len(aucs), reps), np.mean(pos), aucs.mean(), aucs.std(ddof=1),
(reference - aucs > 0.03).mean() * 100))
Reference AUC on 200,000 rows: 0.8426
holdout n eligible mean frauds mean AUC sd of AUC trips 3pp rule
500 572/600 5.1 0.8400 0.0987 33.9%
2000 600/600 19.4 0.8410 0.0435 24.7%
10000 400/400 96.9 0.8394 0.0188 8.0%
50000 200/200 488.6 0.8400 0.0094 0.0%
The experiment implements the stated one-sided gate: reference AUC minus holdout AUC greater than 0.03. It reports the rejection rate among eligible windows and separately counts windows with fewer than two positives or negatives as insufficient evidence; at 500 rows, 28 of the 600 windows were insufficient and 572 were scored. With about five positives per 500-row window the standard deviation of AUC is 0.0987, so windows from an unchanged process trip the three-point gate 33.9% of the time. At 50,000 rows (about 489 positives) the standard deviation falls to 0.0094 and no window trips. Zero rejections in a finite batch of larger windows would not prove a zero false-block probability.
This is the same defect that fixed PSI and KS shift thresholds have, in a metric that looks less statistical. A guardrail is a comparison against a noise floor whether or not it was written that way, and the version that was not written that way has a floor anyway — it just does not know what it is.
So the minimum a usable drift guardrail states is four things: the metric, the threshold, the window size it is computed over, and the action. State all four together so an operator can interpret the signal and apply the intended response. The set of rules has properties of its own that need joint assessment, including repeated looks, dependence, and combined false-alarm behavior.
References
- Gama, J., Žliobaitė, I., Bifet, A., Pechenizkiy, M., & Bouchachia, A. (2014). A survey on concept drift adaptation. ACM Computing Surveys, 46(4), 1–37.
- scikit-learn documentation,
sklearn.metrics.roc_auc_score. - Lipton et al., Detecting and Correcting for Label Shift. Source
Discover more from Insightful Data Lab
Subscribe to get the latest posts sent to your email.
