Sub topic 13
Detecting multivariate drift with a domain classifier
This chapter assumes you can train a scikit-learn classifier, read a ROC AUC score, and have already run per-feature tests such as KS and chi-square on a reference window and a current window.
Why per-feature tests are not enough
Univariate tests look at one column at a time. They miss shifts in the relationship between columns. Suppose age and income each keep the same distribution, but their correlation flips. Every per-feature test passes, yet the joint distribution has changed, and a model that relies on the interaction may behave differently.
A second problem is volume. With 200 features, running 200 tests produces many alerts by chance alone. A single joint score is easier to track.
The idea
A domain classifier is a model trained to tell reference rows from current rows.
- Label every reference row 0 and every current row 1.
- Stack the rows into one dataset.
- Train a classifier and measure cross-validated ROC AUC.
If the two windows come from the same distribution, the classifier cannot do better than guessing, so AUC is near 0.5. If the joint distribution has shifted, the classifier finds the difference and AUC rises toward 1.0.
A worked example
The code below shifts only the first of three features by 0.5 standard deviations and measures how well a random forest separates the windows.
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
rng = np.random.default_rng(0)
reference = rng.normal(0, 1, size=(2000, 3))
current = rng.normal(0, 1, size=(2000, 3))
current[:, 0] += 0.5 # shift one feature
X = np.vstack([reference, current])
y = np.concatenate([np.zeros(len(reference)), np.ones(len(current))])
clf = RandomForestClassifier(n_estimators=100, random_state=0)
auc = cross_val_score(clf, X, y, cv=5, scoring="roc_auc").mean()
print(f"Domain classifier ROC AUC: {auc:.3f}")The AUC comes out clearly above 0.5, because one feature carries the shift. Rerun it without the += 0.5 line and the score falls to about 0.5.
Finding which features moved
A high AUC tells you that something changed, not what. Fit the classifier on all the data and read clf.feature_importances_. Features with the largest importance are the ones that best separate the windows. Then run KS or chi-square on those features to describe the change.
Common errors
- Scoring on training rows. Always use cross-validation or a held-out split. A forest can memorize rows, so training AUC is near 1.0 even with no drift.
- Unequal window sizes. If one window is much larger, the classes are imbalanced. ROC AUC handles imbalance better than accuracy, but sample both windows to similar sizes for stable results.
- Leaking identifiers. Columns such as a timestamp, row ID, or batch number separate the windows by construction. Drop them before training.
- Treating any AUC above 0.5 as an alert. Random variation moves AUC a little. Estimate the noise by comparing two disjoint slices of the reference window, and set the threshold above that level.
- Large samples flag trivial shifts. With enough rows, the classifier detects differences too small to affect the model. Link the result to performance, as covered in the chapter on drift versus performance degradation.
Trade-offs
| Aspect | Univariate tests | Domain classifier |
|---|---|---|
| Detects correlation changes | No | Yes |
| Names the shifted feature | Yes | Indirectly, through importances |
| Cost | Low | Trains a model per check |
| Tuning | Test threshold | Model choice and AUC threshold |
Use the classifier as a complement to per-feature tests, not a replacement, and run it less often, such as daily instead of hourly.
Exercise
A domain classifier on your logged features gives a cross-validated ROC AUC of 0.97, but all per-feature KS tests pass. Name two possible explanations and one check for each.
Show answer
First, the correlation or interaction between features changed while each marginal distribution stayed the same. Check by comparing correlation matrices between the windows. Second, a leaking column such as a timestamp or batch ID separates the windows by construction. Check the feature importances and drop any identifier-like column, then retrain the classifier.