TL;DR
A model learns from examples gathered in one setting and is then used in another. Covariate shift is the case where the inputs change between those settings (the mix of ages, images, sentences or sensor readings), but the correct answer for a given input stays the same. The model's mistakes then land on a different part of the input space from the one it was tuned for. If you know how much more or less likely each input is in the new setting, you can reweight the training examples so that the training loss behaves like the loss on the new setting.
Where you'll see it: a medical model trained on patients from one hospital and deployed in another, a spam filter facing an inbox whose mix of messages has drifted, and off-policy evaluation, where the logged data came from an older decision policy.
Intuition
Same rule, different crowd
Suppose a bank trained a loan model on applications from 2019, when most applicants had salaried jobs. Now most applicants are freelancers. For any particular applicant, the chance of default given their income, credit history and job (a conditional probability) is what it always was. The economy has not changed how a person's finances map to risk. What changed is the crowd that walks in. That is covariate shift. The inputs have a new distribution, but the conditional probability of the label given the input, , stays put.
The word comes from regression. The inputs of a regression are called covariates, so a shift in the covariates is a shift in the inputs.
Why does this hurt? A model is a compromise. It spends its capacity fitting the regions of input space where training examples are dense, because that is where its loss is measured. In regions it rarely saw, it guesses. If the new crowd lives mostly in those regions, the guesses are what the user experiences. The rule is fine. The model was graded on the wrong crowd.
The fix is intuitive too. If freelancers are rare in the 2019 data but common now, each freelancer in the training set should count for more. Give each example a weight equal to how common its kind of input is in the new world divided by how common it was in the old. The weighted training loss then looks like a loss measured on the new world.
- training inputs, N(0, 1)
- deployment inputs, N(1, 1)
What does not change
The whole idea depends on being the same in both settings. If the rule itself changes, the problem is different. A fraud pattern that adapts to your filter is concept drift, and reweighting the inputs does not repair it. A change in how often each label appears, with the inputs for each label unchanged, is label shift, which needs a different correction. Before reaching for covariate-shift methods, check that the labelling rule really is stable.
The math
Setup
Training pairs are drawn from . At deployment, inputs come from and labels still come from . Let be the model and the loss on one example, for instance squared error. What we want to minimise is the expected loss on the deployment inputs:
The plain training loss averages over instead, so it estimates a different number.
A change of data distribution in which the input distribution differs between training and test, , while the conditional distribution of the label does not, for every input that has positive density in both.
The importance weight
Define the importance weight (also called the density ratio)
Write for the average loss at one input. Then
This is the importance sampling identity. Step by step:
- Write the expectation over test inputs as an integral against .
- Replace by . This is an identity wherever .
- Read the last integral as an expectation over training inputs.
The sample version is a weighted average of training losses:
Training with this objective is weighted empirical risk minimisation. For a probabilistic model it is weighted maximum likelihood: maximise .
There is one condition. The weights exist only where the training data has support, so you need wherever . If the deployment crowd lives in a region the training set never visits, no weighting can recover what the model should do there.
Estimating the weight
When the densities are known, as in a simulation where you chose both distributions, you can compute directly. With real data you usually need to estimate it. A standard trick is a domain classifier. Pool an equal number of training and deployment inputs, label them 0 and 1, and train a classifier. Let be its output, the probability that an input came from the deployment set. By Bayes' rule, with equal sample sizes,
So the odds of the classifier are the weight.
Worked examples
First, the weight. Both densities share the constant , which cancels:
The step uses , so the exponent is .
Three training examples, with losses:
| loss | ||
|---|---|---|
| 0.8 | ||
| 0.2 | ||
| 1.0 |
- The naive average loss is .
- The weighted terms are , and .
- Their sum is . Dividing by gives .
The deployment population sits further right, near , where the loss is high. The example at is one point in the training data, but it carries most of the weight, so the corrected estimate of 1.62 is far above the naive 0.67. The naive number would have told you the model is doing fine on the crowd it was trained on.
- w(x) = p_test(x) / p_train(x)
Suppose a review-sentiment model was trained on 2015 reviews and will be used on 2024 reviews. A domain classifier, trained on equal numbers of each year, gives three reviews these outputs:
- Review A: , so . The classifier thinks it looks like 2015 text, so it counts for a quarter of an example.
- Review B: , so . The classifier cannot tell the years apart here, so the review counts as usual.
- Review C: , so . It looks like 2024 text, so it counts nine times.
Weighting by these numbers makes the training loss look more like the 2024 loss. The cost shows up in the effective sample size,
For the three weights of the tiny case, and , so . Three examples carry the information of about 1.5 equal examples, because one weight dominates. Big weights buy bias reduction at the price of variance.
Tradeoffs and failure modes
Covariate-shift correction is the right tool when the labelling rule is stable and the shift is in the inputs. Its main failure mode is extreme weights. The table lists the common situations.
| Situation | What goes wrong | Better choice |
|---|---|---|
| Ratio is moderate and is stable | Reweighting works as described | Importance-weighted training |
| A few weights are huge | Variance explodes and collapses onto a handful of examples | Clip the weights, regularise, or collect target data |
| High-dimensional inputs such as images or text | Density ratios are hard to estimate directly | Domain classifier, or estimate the ratio in a learned feature space |
| No overlap: where | No weight can fix a region the model never saw | Collect data in the new region, then fine-tune |
| The labelling rule itself changes | Weights correct the wrong thing | Concept-drift methods, monitoring, retraining |
| Label frequencies change but inputs given the label do not | This is label shift, not covariate shift | Prior correction |
Two practical points. First, the domain classifier is a test as well as an estimator. If it cannot beat 50% accuracy at telling the two sets apart, the shift is small and the weights are close to 1, so reweighting changes little. Second, keep the domain classifier separate from the model you are training.
When the shift is large, reweighting is often the wrong first move. Fine-tuning on a modest amount of labelled deployment data (Fine-Tuning) or adapting features with Transfer Learning usually works better, because they change what the model learns rather than only how much each example counts.
A common mistake is to call any change in the data covariate shift. Test for it before correcting for it. A shift in the inputs that leaves intact is rare in the wild, and many "shift" problems turn out to be label shift or concept drift.
History
Covariate shift has an older and a newer history. The weighting idea is much older. Importance sampling was used in the late 1940s and early 1950s, when physicists such as Herman Kahn used reweighted random samples to estimate how particles pass through shielding. The trick is the same as in the derivation above: sample from an easy distribution and correct with a ratio.
The statistical picture arrived in 2000. Hidetoshi Shimodaira showed that under covariate shift, predictive inference is improved by weighting the log-likelihood with the density ratio, and his paper gave the term its standard meaning. In the late 2000s, density-ratio estimation became its own research area, and a 2009 book by Quiñonero-Candela, Sugiyama, Schwaighofer and Lawrence collected the work on dataset shift in machine learning.
Deep learning brought a different use of the phrase. In 2015, Ioffe and Szegedy called the drift of layer inputs during training "internal covariate shift" and proposed batch normalization to reduce it. In 2018, Santurkar and colleagues showed that batch normalization helps for other reasons, which left the term's meaning in that setting disputed. The usage is loose, and it is not the same as the precise covariate shift defined here.
Check yourself
QuestionWhat quantity stays the same under covariate shift, and what changes?Show answer
The conditional distribution stays the same. The marginal distribution of the inputs, , changes from to . The model's rule for a given input is still right; it is the mix of inputs that moved.
QuestionTraining inputs are and deployment inputs are . What is the importance weight at a training example ?Show answer
. Here . A training example that far to the right stands in for about 55 typical examples of the deployment population, so one such example dominates the weighted loss.
QuestionWhy does an unweighted training loss estimate the wrong quantity under covariate shift, even when the model is flexible enough to fit the labels perfectly?Show answer
The unweighted loss averages over , but the target is an average over . A flexible model can fit the training data well everywhere it has examples and still be inaccurate where the deployment inputs concentrate, because the training loss gives those regions little weight. Fitting better does not change which regions are measured.
QuestionA colleague proposes importance weights of about for some training points. What is the problem, and what would you check before using them?Show answer
Such a weight means one example would dominate the weighted loss, and the effective sample size would collapse toward one or a few points. The estimate would have huge variance, and the model would overfit those points. Check the effective sample size, look at whether the ratio is estimated from a domain classifier that is overconfident, and consider clipping the weights or collecting more data in the region where the ratio is extreme.
Sources
- Dataset shift (Wikipedia): https://en.wikipedia.org/wiki/Dataset_shift
- Importance sampling (Wikipedia): https://en.wikipedia.org/wiki/Importance_sampling
- Batch Normalization (Ioffe, Szegedy, 2015): https://arxiv.org/abs/1502.03167
- How Does Batch Normalization Help Optimization? (Santurkar et al., 2018): https://arxiv.org/abs/1805.11604