Sub topic 10
Reweighting Labeled Data Under Covariate Shift
Prerequisites and the core problem
Assume you know binary classification metrics, train/validation/test splits, basic Python, pandas, and scikit-learn. You also have a prediction log containing feature values and predictions.
Suppose your production feature distribution has changed since the model was evaluated. For example, a fraud model may now receive more international transactions than it saw during validation. Labels for these new transactions will arrive weeks later. You still want to estimate current accuracy using older rows whose labels are already available.
Covariate shift is the case where the feature distribution changes but the relationship between features and labels remains stable:
P_{\text{new}}(x) \ne P_{\text{ref}}(x)but
P_{\text{new}}(y \mid x) = P_{\text{ref}}(y \mid x)Here, the reference data has labels, and the new data represents the production population you want to measure.
Why ordinary averages can be wrong
An ordinary metric gives every reference row the same influence. That estimates performance on the reference distribution:
E_{\text{ref}}[\text{correct}]You instead want performance on the new distribution:
E_{\text{new}}[\text{correct}]Importance weighting changes each reference row's influence. A row receives a large weight when its feature values are common in production but rare in the reference data. The ideal weight is:
w(x) = \frac{P_{\text{new}}(x)}{P_{\text{ref}}(x)}Then estimate accuracy as a weighted average:
\widehat{\text{accuracy}} =
\frac{\sum_i w(x_i) \cdot 1[\hat{y}_i = y_i]}
{\sum_i w(x_i)}A domain classifier provides a practical estimate of this ratio. It learns to distinguish reference rows from new production rows.
| Domain-classifier result | Interpretation | Monitoring concern |
|---|---|---|
| Cannot distinguish the groups | Little detectable feature shift | Reweighting may have little effect |
| Distinguishes them moderately | Some covariate shift | Check weighted metrics and overlap |
| Almost perfectly distinguishes them | Poor overlap | Estimates may be unstable or unreliable |
Estimating weights with a domain classifier
Give reference rows domain label 0 and new rows domain label 1. If the classifier estimates \(P(D=1 \mid x)\), the density ratio can be estimated as:
\frac{P_{\text{new}}(x)}{P_{\text{ref}}(x)}
\approx
\frac{P(D=1 \mid x)}{P(D=0 \mid x)}
\cdot
\frac{n_{\text{ref}}}{n_{\text{new}}}The sample-size correction matters when the two datasets have different sizes.
import numpy as np
from sklearn.linear_model import LogisticRegression
def covariate_shift_weights(X_ref, X_new, clip=20.0):
"""Estimate weights for labeled reference rows."""
X = np.vstack([X_ref, X_new])
domain = np.r_[np.zeros(len(X_ref)), np.ones(len(X_new))]
classifier = LogisticRegression(max_iter=1000)
classifier.fit(X, domain)
p_new = classifier.predict_proba(X_ref)[:, 1]
p_ref = 1.0 - p_new
ratio = (p_new / np.maximum(p_ref, 1e-8))
ratio *= len(X_ref) / len(X_new)
return np.clip(ratio, 0.0, clip)
# y_ref and y_pred_ref must contain labels and predictions for reference rows.
# weights = covariate_shift_weights(X_ref, X_new)
# correct = (y_ref == y_pred_ref).astype(float)
# estimated_accuracy = np.average(correct, weights=weights)Clipping limits the effect of rows with extreme weights. Without clipping, a few poorly supported rows can dominate the estimate. Record the clipping threshold and the effective sample size:
n_{\text{eff}} =
\frac{(\sum_i w_i)^2}{\sum_i w_i^2}A small effective sample size means the estimate contains little independent information, even when the raw reference dataset is large.
Assumptions and failure modes
Importance weighting does not repair every distribution change. It relies on three conditions:
- The important features are present in both datasets.
- New rows have adequate overlap with reference rows.
- The conditional relationship \(P(y \mid x)\) has not changed.
If the fraud pattern changes, labels become noisier, or the model's behavior changes for reasons not represented in the features, covariate weighting can produce a precise-looking but incorrect estimate.
Use this estimate as an early signal. Compare it with metrics from matured cohorts when labels arrive. Monitor weight distributions, domain-classifier performance, clipping frequency, and effective sample size. A large disagreement between the weighted estimate and later ground truth is evidence that the covariate-shift assumptions do not hold.
> callout warning > Reweighting estimates performance only for the part of the new population represented by the labeled reference data. It cannot infer performance in regions with no reference examples.
Exercise
A weighted accuracy estimate is based on 10,000 labeled reference rows, but its effective sample size is 180. Should you treat it like an estimate based on 10,000 independent rows?
Show answer
No. Extreme weights make the estimate depend on a small number of rows. Report the effective sample size and treat the estimate as uncertain.