Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.When the world moves under a model
A model learns from examples gathered under some conditions; then the conditions change. Sometimes only the inputs change: a crop classifier trained in spring sees summer images. Sometimes only the mix of answers changes: a fault detector built when faults were common is used when they are rare. And sometimes the meaning changes: the same sensor reading now signals a different state. The first two can often be corrected using unlabelled new data. The third cannot be seen without new labels. The demo shows how much useful data is left when you reweight old examples to match a shifted input distribution.
Earth-observation models break when a new satellite has a different sensor response (covariate shift), when the land-cover mix differs by region (label shift), or when a class definition changes between mapping campaigns (concept shift). Each needs a different fix.
- 1. Covariate shift changes…
- 2. Label shift changes…
- 3. Concept shift can be detected from unlabelled inputs…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Covariate shift (Shimodaira, 2000): p_t(x) ≠ p_s(x), p_t(y given x) = p_s(y given x). Weight each training loss by w(x) = p_t(x)/p_s(x). This matters when the model is misspecified; a correct, well-specified model needs no weights.
- Density-ratio estimation: train a domain classifier to tell target from source; w(x) = P(target given x)/P(source given x) · n_s/n_t. Alternatives: KMM (Huang et al., 2007), KLIEP (Sugiyama et al., 2008).
- Effective sample size: ESS = (Σw)²/Σw². For N(0,1) → N(δ,1) the ESS share is exp(−δ²); large shifts leave few useful points. Clip or flatten (w^λ) weights to trade bias for variance.
- Label shift (prior shift): p_t(y) ≠ p_s(y), p(x given y) fixed. Posterior correction p_t(y given x) ∝ p_s(y given x) · q_t(y)/p_s(y). Estimate q_t without target labels by EM (Saerens et al., 2002), BBSE (Lipton et al., 2018: solve C w = μ with the source confusion matrix) or RLLS.
- Concept shift: p(y given x) changes (new definitions, adversaries, wear). Needs labelled target data: monitoring, retraining, continual learning. Concept drift in streams is detected with DDM, ADWIN and similar error-rate monitors.
- Detecting shift: two-sample tests on inputs or on model outputs, such as MMD (Gretton et al., 2012) or KS tests on predicted probabilities (Rabanser et al., 2019). Benchmarks: WILDS (Koh et al., 2021), ImageNet-C, BREEDS.
- 1. The domain-classifier density ratio is…
- 2. BBSE uses…
- 3. ESS share for a one-standard-deviation Gaussian mean shift:
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Estimating and correcting shift
Exercise 1.
- Logistic regression by Newton’s method, used as a domain classifier.
- Density ratios from the classifier, and importance-weighted least squares.
- BBSE and EM estimates of target class priors.
- The squared MMD between two samples.
Practice.
- Always test for shift first: two-sample tests on inputs, embeddings and predicted probabilities, with a held-out reference window.
- For covariate shift, estimate weights with a calibrated domain classifier, check the ESS and clip weights; evaluate on any labelled target data you have.
- For label shift, estimate priors with EM or BBSE (needs an invertible confusion matrix) and adjust the posteriors; recalibrate first.
- Concept shift needs labels: monitor error rates on a labelled trickle, set drift alarms, retrain.
- Libraries: alibi-detect (MMD, KS, classifier and learned-kernel drift detectors), river (ADWIN, DDM), WILDS datasets.
from alibi_detect.cd import MMDDrift, ClassifierDrift
cd = MMDDrift(x_ref, backend="pytorch", p_val=0.05) # x_ref: reference embeddings
pred = cd.predict(x_new) # permutation test on new embeddings
print(pred["data"]["is_drift"], pred["data"]["p_val"])
# importance weights from a domain classifier (any probabilistic classifier works)
from sklearn.linear_model import LogisticRegression
dom = LogisticRegression(max_iter=1000).fit(np.r_[x_src, x_tgt], np.r_[np.zeros(len(x_src)), np.ones(len(x_tgt))])
p = dom.predict_proba(x_src)[:, 1]; w = p / (1 - p) * len(x_src) / len(x_tgt)- 1. A library with MMD and classifier drift detectors:
- 2. Before reweighting, check…
- 3. Concept shift is handled by…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Why importance weighting works. For any loss ℓ, E_t[ℓ(x, y)] = ∫ p_t(x) p(y given x) ℓ dx dy = E_s[w(x) ℓ(x, y)] when p(y given x) is shared and p_s(x) > 0 wherever p_t(x) > 0. Weighted empirical risk on source data is therefore unbiased for the target risk. Its variance grows with E_s[w²] = 1/ESS share. For Gaussians N(0,1) → N(δ,1), E_s[w²] = exp(δ²).
When weighting is unnecessary. If the model class contains the true regression function, the unweighted least-squares fit is already consistent on every region, and weighting only adds variance (Shimodaira, 2000). Weighting pays off for misspecified models, which must compromise across regions and should compromise where the target data are.
Label-shift identities. With p(x given y) fixed, p_t(y given x) = p_s(y given x) w(y) / Σ_k p_s(k given x) w(k), where w(y) = q_t(y)/p_s(y). For any fixed classifier h, P_t(h = i) = Σ_j P_s(h = i, y = j) w(j), which is the BBSE linear system C w = μ. It is solvable when C is invertible. EM maximises the target likelihood over the priors and is a fixed-point iteration of the posterior correction.
Concept shift is not identifiable from x. Two worlds with the same p(x) and different p(y given x) produce identical unlabelled data, so no test on inputs alone can tell them apart.
- 1. Importance weighting is unbiased if…
- 2. BBSE requires…
- 3. For a well-specified model, weighting…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.What does each type of shift do to a model, and which corrections work without target labels?
History.
- H. Shimodaira (2000) covariate shift and importance-weighted likelihood; M. Saerens, P. Latinne and C. Decaestecker (2002) EM for new priors.
- J. Quiñonero-Candela et al. (2009) “Dataset Shift in Machine Learning”; J. G. Moreno-Torres et al. (2012) taxonomy.
- Z. Lipton, Y.-X. Wang and A. Smola (2018) BBSE; S. Rabanser, S. Günnemann and Z. Lipton (2019) “Failing loudly”; P. W. Koh et al. (2021) WILDS.
Exercise 2. One experiment per type of shift.
- Covariate: y = sin 2x + x + N(0, 0.3²). Source x ~ N(0, 1), target x ~ N(1.5, 0.6²), 500 points each. A straight-line model (misspecified) is fitted with no weights, true weights, weights from a quadratic logistic domain classifier, or those weights clipped at their 95th percentile.
- Label: three unit Gaussian classes; source prior uniform, target (0.7, 0.2, 0.1); Bayes classifier for the source; 3000 points each.
- Concept: source label 1[x1 + x2 > 0], target 1[x1 − x2 > 0], same N(0, I) inputs; MMD permutation test (150 against 150, 200 permutations) on the inputs, and on a covariate-shifted sample for comparison.
| covariate shift | target MSE | ESS share |
|---|---|---|
| unweighted | 1.057 | 1 |
| true weights | 0.251 | 0.200 |
| domain-classifier weights | 0.254 | 0.178 |
| clipped weights | 0.258 | 0.229 |
| trained on target data | 0.250 |
| label shift | estimated target prior | target accuracy |
|---|---|---|
| source prior (no correction) | (0.333, 0.333, 0.333) | 0.627 |
| BBSE | (0.749, 0.193, 0.059) | 0.759 |
| EM | (0.687, 0.207, 0.106) | 0.764 |
| true prior (oracle) | (0.7, 0.2, 0.1) | 0.763 |
| concept shift | value |
|---|---|
| accuracy on source / target | 1.00 / 0.487 |
| MMD p-value, inputs under concept shift | 0.667 |
| MMD p-value, inputs under covariate shift | 0.005 |
- Covariate shift hurt the misspecified model fourfold. Weights estimated without target labels recovered almost all of it. The price was variance: only 18% of the sample was effectively used, and clipping raised that to 23% with a small loss.
- Label shift was fixed by priors alone. EM’s estimate was closer to the truth than BBSE’s here, and both matched the oracle’s accuracy within half a point.
- Concept shift was invisible. Accuracy fell to chance while the input test found nothing; the same test flagged a covariate shift at the smallest possible p-value.
- Seeds 1 and 2: unweighted MSE 1.019 and 0.787 against 0.233 and 0.209 with estimated weights; EM accuracy 0.776 and 0.781 (uncorrected 0.648 and 0.628). The concept-shift MMD p-values were 0.254 and 0.035. On seed 2 the test raised a false alarm at the 5% level, as it will 5% of the time when nothing has changed.
Distribution shift in AI, robotics and aerospace
- Satellite constellations: a model trained on one sensor generation meets another; domain-classifier weights and MMD monitors on embeddings flag the change before accuracy reports arrive.
- Fault detection: fault prevalence changes with fleet age; prior correction with EM keeps alarm probabilities meaningful.
- Sim-to-real robotics: covariate shift between simulation and reality is handled by randomisation, reweighting and fine-tuning on real data.
References
- H. Shimodaira (2000). “Improving predictive inference under covariate shift by weighting the log-likelihood function.” J. Statistical Planning and Inference.
- M. Saerens, P. Latinne and C. Decaestecker (2002). “Adjusting the outputs of a classifier to new a priori probabilities.” Neural Computation.
- Z. C. Lipton, Y.-X. Wang and A. J. Smola (2018). “Detecting and correcting for label shift with black box predictors.” ICML.
- S. Rabanser, S. Günnemann and Z. C. Lipton (2019). “Failing loudly: an empirical study of methods for detecting dataset shift.” NeurIPS.
- P. W. Koh et al. (2021). “WILDS: a benchmark of in-the-wild distribution shifts.” ICML.