TL;DR
An invariant predictor is built from features whose relation to the label is the same in every setting the model will meet. A feature that merely happens to go with the label in one setting can look excellent during training and then fail when the setting changes. Looking for features that behave the same way across several settings, called environments, is a principled way to avoid that failure. The idea is simple to state, but finding such features in practice is hard.
Builds on: Out-of-Distribution Generalization, Confounding Bias, Empirical Risk Minimization, Conditional Probability · Leads to: Invariant Risk Minimization, Causal Inference
Where it's used
Medical imaging across hospitals. A model that reads scans is trained on data from one hospital. Each hospital has its own scanner, its own patients and its own habits, so an artifact tied to one scanner can accidentally track the diagnosis at that site. A model that uses the artifact will look accurate at the training hospital and fail at a new one. Choosing features that predict the diagnosis the same way at every hospital is the invariance idea applied directly.
Image recognition with shortcut backgrounds. Photographs of cows are often taken on grass, and photographs of camels often on sand. A classifier that learns the background learns a shortcut. In a new setting, where a cow stands on a beach, the shortcut gives the wrong answer. The shape of the animal, which relates to the label the same way in every setting, is the invariant cue. This example is a standard illustration in the out-of-distribution literature.
Policy and economics data from several countries. Economists often pool data from many countries or years to estimate a relationship that should hold elsewhere. A variable that tracks the outcome only in the countries that happen to be in the sample cannot be trusted for a new country. Testing whether the relation holds across the sample is the first defence, and it is the logic behind the Causal Inference tradition of looking for stable mechanisms.
How it works
Here is a tiny instance traced from start to finish. There are two environments, e1 (training) and e2 (new). In each one the label Y is 0 or 1, with probability 0.5 each. There are two features:
- C, a noisy measurement of the true cause. In both environments, C equals Y with probability 0.8.
- S, a shortcut. In e1, S equals Y with probability 0.9. In e2, S equals Y with probability only 0.2.
The task: pick the feature that a predictor should use.
-
Check the first feature against the label in each environment. Compute P(Y = 1 | C = 1). The joint probability P(Y = 1, C = 1) is 0.5 × 0.8 = 0.4, and P(C = 1) is 0.5, so the answer is 0.4 / 0.5 = 0.8. Check: the same arithmetic in e2 gives 0.8 too, because the rule is the same in both.
-
Check the same thing for the other value of C. P(Y = 1 | C = 0) is P(Y = 1, C = 0) / P(C = 0) = (0.5 × 0.2) / 0.5 = 0.2. Again the same in e1 and e2. So the rule that connects C to Y does not change between environments. C is invariant.
-
Check the shortcut in each environment. In e1, P(Y = 1, S = 1) = 0.5 × 0.9 = 0.45, and P(S = 1) = 0.5, so P(Y = 1 | S = 1) = 0.9. In e2, P(Y = 1, S = 1) = 0.5 × 0.2 = 0.1, so P(Y = 1 | S = 1) = 0.2. The rule changed, so S is not invariant.
-
Compare the accuracy of the two simple rules in the training environment. The rule "predict Y equals C" is right 80% of the time in e1. The rule "predict Y equals S" is right 90% of the time in e1. Trained on e1 alone, ordinary training picks S, because it looks better.
-
Deploy in e2. The rule "predict Y equals C" is still right 80% of the time. The rule "predict Y equals S" is right only 20% of the time, worse than guessing. The shortcut looked best during training and is the worst choice in the new setting.
-
Decide. The invariant predictor uses C. It is the feature whose relation to the label held across both environments.
flowchart LR A["Several environments with labels"] --> B["For each feature, compare P of label given feature across environments"] B --> C["Keep features whose relation is the same everywhere"] C --> D["Fit one predictor on the kept features"] D --> E["Predictor is reliable in new environments"]
The math
Let each environment e have its own joint distribution of inputs X and label Y, written P^e(X, Y). A feature map Φ is invariant if the conditional distribution P^e(Y | Φ(X)) is the same for every environment e. A predictor that depends on X only through an invariant Φ is an invariant predictor.
Step 1 in symbols: the environments
Each environment e produces data from its own P^e. The environments are the settings the model is expected to face: hospitals, camera locations, years, countries. The model sees a sample from some of them and must work in others, which is the setting of Out-of-Distribution Generalization.
Step 2 in symbols: the conditional rule
The quantity that must not change is the conditional P^e(Y = 1 | Φ(X) = z), the probability of the label given the feature value z. In the walkthrough, Φ(X) = C for the causal feature and Φ(X) = S for the shortcut. For C, this quantity is 0.8 when C = 1 and 0.2 when C = 0, in both environments. For S, it is 0.9 in e1 and 0.2 in e2 when S = 1.
Why this matters: the best predictor under squared error is the conditional expectation E[Y | Φ(X)], see Conditional Expectation. If the conditional rule is the same in every environment, the same predictor is best in every environment. A change in the marginal distribution of Φ(X) can still change average error, so invariance guarantees the rule, not the overall error rate.
Step 3 in symbols: the training objective
A standard way to search for an invariant predictor, the Invariant Risk Minimization idea of Arjovsky and colleagues, learns a feature map Φ and a classifier w on top of it. It minimises the sum of the risks over environments, plus a penalty for a predictor whose best scale changes between environments:
where R^e is the risk, meaning the average loss, in environment e, and λ sets how strongly invariance is enforced. The gradient in the penalty is taken with respect to a dummy scale, and it is zero exactly when a single classifier is already optimal in every environment. The first term keeps the fit good, the second term rejects features whose best classifier differs across environments.
Step 4 in symbols: the walkthrough numbers
For the tiny instance, the test in step 3 of the walkthrough is a direct comparison of conditional probabilities:
Any feature whose conditional probabilities differ across environments fails the invariance test, and the predictor must not use it.
Worked examples
Use the same two environments. Suppose you only look at e1.
- In e1, P(Y = 1 | S = 1) = 0.9 and P(Y = 1 | C = 1) = 0.8.
- Both rules look reasonable, and S looks stronger.
- Now add e2, where P(Y = 1 | S = 1) = 0.2.
- The value changed from 0.9 to 0.2, so S fails the invariance check, and C passes it.
Interpretation: the invariance test needs at least two environments. With one, every feature looks stable, because there is nothing to compare against.
Three hospitals train together. In each, the diagnosis Y has probability 0.5 of being 1. The lesion feature C agrees with Y with probability 0.8 at all three. The scanner artifact S agrees with Y with probabilities 0.9, 0.7 and 0.2 at hospitals H1, H2 and H3.
- The conditional P(Y = 1 | S = 1) equals the agreement probability, because the label is balanced. So it is 0.9 at H1, 0.7 at H2 and 0.2 at H3.
- The values differ across hospitals. The artifact fails the invariance check.
- The conditional P(Y = 1 | C = 1) equals 0.8 at all three. The lesion feature passes.
- A predictor built on C gives the same conditional error at every hospital. A predictor built on S gives an error that depends on which hospital it is used in.
Interpretation: the invariant predictor is the lesion feature. A model trained on pooled hospitals that leans on the artifact will look accurate on average and then fail at H3.
A new hospital, H4, has balanced diagnoses, and its scanner artifact agrees with the diagnosis with probability 0.6. The lesion feature still agrees with the diagnosis with probability 0.8.
- Compute P(Y = 1 | S = 1) at H4. The label is balanced, so this equals the agreement probability, 0.6.
- Compare the four values of P(Y = 1 | S = 1) across the hospitals: 0.9, 0.7, 0.2 and 0.6. They differ, so the artifact is not invariant.
- The lesion feature is the same at all four hospitals, so it remains the candidate for a predictor that works at a new site.
- A model that relies on the artifact does worse at H4 than one that relies on the lesion feature. The artifact is right 60% of the time there, against 80% for the lesion, and at H3 it is right only 20% of the time.
Try it: at a fifth hospital, P(Y = 1) is 0.5 and the artifact agrees with the label with probability 0.2. Is the artifact invariant across the five hospitals? Answer: no. The five values of P(Y = 1 | S = 1) are 0.9, 0.7, 0.2, 0.6 and 0.2. The fifth matches the third, but the values are not all equal, so the artifact is not invariant.
Common mistakes and tradeoffs
It is tempting to think that a feature which is invariant across the environments you have is causal. In fact invariance across a few environments is evidence, not proof. A spurious feature can look stable if every training setting happens to share the same shortcut. The more varied the environments, the stronger the evidence.
It is tempting to think that more data from one environment fixes out-of-distribution failure. In fact a larger sample from the same environment only makes the shortcut more precisely estimated. Invariance comes from variety across environments, not from size within one.
It is tempting to think that invariance and covariate shift are the same thing. In fact covariate shift, see Covariate Shift, assumes the rule from inputs to labels stays fixed while the input distribution changes. Invariance assumes the conditional rule for the chosen feature stays fixed, while the feature's own distribution may change. The two can be combined, and they often are.
It is tempting to think that the penalty in Step 3 always finds the invariant feature. In fact the search is a non-convex optimisation, the guarantees depend on assumptions about the environments, and the method needs environment labels, which are often not available. When environments are unknown, invariance cannot be tested directly.
Use this approach when you have several reasonably different settings with labels and you expect a shortcut to break in a new one. Do not use it when you have only one setting, or when the settings differ in ways the features already capture. Alternatives include collecting more diverse data, data augmentation that breaks the shortcut, and reweighting that matches the new setting. Under Covariate Shift specifically, importance reweighting is the standard fix.
History
The idea that a stable relation across settings is a mark of a real mechanism runs through the causal inference literature. Work by Jonas Peters, Peter Bühlmann and Nicolai Meinshausen in 2016 formalised the search for causal predictors through invariance across environments, which gave the idea a precise statistical form.
Machine learning researchers were slow to take it up, because most learning methods simply minimised average loss on pooled data. In 2019 Martin Arjovsky and colleagues proposed Invariant Risk Minimization, a practical training objective that penalises features whose best classifier changes across environments. The paper made the idea a widely cited reference for out-of-distribution generalisation.
Recap
- A feature is invariant when its conditional relation to the label is the same in every environment.
- Ordinary training on one environment can prefer a shortcut that breaks in a new one.
- Comparing P(Y | feature) across environments is a direct test of invariance.
- Invariance guarantees that the conditional rule is stable, not that every error rate is the same.
- Next, read Invariant Risk Minimization, which turns the test into a training objective, and Causal Inference, which explains why stable relations point toward mechanisms.
Check yourself
QuestionRecall: what does it mean for a feature to be invariant across environments?Show answer
QuestionComputation: in an environment with P(Y = 1) = 0.5, the feature S agrees with Y with probability 0.7. What is P(Y = 1 | S = 1)?Show answer
QuestionWhy: why does ordinary training on one environment often choose a shortcut feature that fails later?Show answer
QuestionApplied: a team trains a model on images from one camera and finds that one background feature is highly predictive. What should they do before deploying it to new cameras?Show answer
Sources
- Causal inference (Wikipedia): https://en.wikipedia.org/wiki/Causal_inference
- Arjovsky et al., Invariant Risk Minimization, arXiv 1907.02893: https://arxiv.org/abs/1907.02893