Out of Distribution Generalization
02 October 2021 · 5 min read
First we start with the question: what does it mean for data to be out of distribution (OOD)? In traditional statistical inference we concern ourselves with the generalization gap, defined as the difference in expected error of our algorithm on the training set vs. the test set. In classical approaches, the test set is assumed to be sampled from the same distribution — i.e., both train and test samples are drawn i.i.d. from the true data distribution \(p(x, y)\).
If we consider generalization in the OOD setting, things get a bit more complicated. Imagine that your test set is not sampled from the same data-generating distribution as your training set. This introduces a whole new set of challenges that we need to address to describe generalization in the context of out-of-distribution data.
Alas, we cannot just go about as machine learning researchers without making any assumptions about the nature of the data-generating distribution. Without some shared structure between training and test distributions, the problem is hopeless — an adversary could make \(p_\text{test}(y \mid x)\) arbitrary. In order to provide results for OOD generalization, some structural assumptions are necessary.
Environments and Distributional Robustness
When talking about OOD generalization, we are going to talk about environments a lot. In each environment \(e \in \mathcal{E}\), the data-generating distribution manifests itself in a particular form \(p^e(x, y)\), shifting the expected error (risk) \(R^e(f) = \E_{(x, y) \sim p^e}[\ell(f(x), y)]\) accordingly. In fact, the set of environments \(\mathcal{E}\) can be (and most often is) specified as the distribution family considered for analysis purposes.
The concept of distributional robustness quantifies the maximum OOD risk that is obtained across a large (sometimes infinite) set of environments. In mathematical terms, we define the OOD risk as:
\[\begin{equation} R^{\text{OOD}}(f) = \max_{e \in \mathcal{E}} R^e(f) \end{equation}\]where \(f\) is our hypothesis, i.e., the predictor/model. Intuitively, the goal of this objective is to minimize the worst-case error across all environments (test data distributions). Notice one important thing right here: any meaningful theorem for OOD generalization must restrict the family \(\mathcal{E}\) from which test environments can be drawn.
Why should we use a \(\max\) and not a prior over environments? A caveat with regards to this objective is that it is dominated by the worst-case environments, so it cannot distinguish between two predictors that have different performance across all other environments. An interesting area of research is finding Pareto-optimal predictors that balance performance across environments while still controlling worst-case risk.
Why Empirical Risk Minimization Fails: A Linear SEM Example
A common assumption is that we have access to data from multiple training environments \(\mathcal{E}_\text{tr} \subset \mathcal{E}\). Consider a concrete linear Gaussian structural equation model (SEM) across environments \(e \in \mathcal{E}\):
\[\begin{aligned} X_1 &\sim \gN(0, 1) \\ Y &= X_1 + \epsilon_Y, \qquad \epsilon_Y \sim \gN(0, 1) \\ X_2^e &= \gamma_e Y + \epsilon_{X_2}, \qquad \epsilon_{X_2} \sim \gN(0, \sigma_e^2) \end{aligned}\]Here \(X_1\) is a causal parent of \(Y\) whose mechanism \(p(Y \mid X_1)\) is invariant across all environments, whereas \(X_2\) is an anti-causal / spurious feature whose relationship with \(Y\) (controlled by \(\gamma_e\) and \(\sigma_e^2\)) varies across environments.
Suppose we train a linear predictor \(f(x_1, x_2) = w_1 x_1 + w_2 x_2\) using Empirical Risk Minimization (ERM) on pooled data from training environments where \(\gamma_e \approx 1\) and \(\sigma_e^2\) is small. Because \(X_2\) is strongly correlated with \(Y\) in training, ERM will assign a large weight \(w_2 > 0\) to \(X_2\) to reduce training error. Now imagine a test environment where the spurious correlation flips (\(\gamma_{e_\text{test}} = -2\)) or blows up: the ERM predictor fails catastrophically. In contrast, the causal predictor \(f^*(x_1, x_2) = x_1\) (with \(w_1 = 1, w_2 = 0\)) achieves the same bounded risk \(\E[\epsilon_Y^2] = 1\) in every environment \(e \in \mathcal{E}\), regardless of how \(\gamma_e\) or \(\sigma_e^2\) varies.
More generally, why can’t we just pool datasets drawn from \(x, y \sim p(x, y \mid e) p(e)\) and run ERM? While pooling yields a predictor that is optimal in terms of average performance under \(p(e)\), we can construct environments with low probability under \(p(e)\) that carry huge errors — contributing little to the average training risk, yet leading to disastrous predictions when encountered at test time.
When is ERM the best thing we can do? When the environments themselves are meta-i.i.d. (no structured shift) and we have no structural knowledge distinguishing invariant mechanisms from spurious ones.
Robust Optimization
Robust optimization (such as GroupDRO) attempts to minimize the worst-case risk across environments, often with environment-specific baselines \(r_e\):
\[\begin{equation} R^{\text{rob}}(f) = \max_{e \in \mathcal{E}_\text{tr}} \big( R^e(f) - r_e \big) \end{equation}\]Here \(r_e\) is a baseline variable set for each environment. Intuitively, we want \(r_e\) to be large when we have a very stochastic environment where even the Bayes-optimal predictor cannot achieve low error (for example, setting \(r_e\) proportional to the target noise variance). Without \(r_e\), the \(\max\) would simply chase the noisiest training environment rather than the hardest learnable one.
By Sion’s minimax theorem, optimizing the worst-case over a finite set of environments is equivalent to minimizing a weighted sum \(\sum_{e} \lambda_e R^e(f)\) for adversarial weights \(\lambda \in \Delta^{\lvert \mathcal{E}_\text{tr} \rvert - 1}\). However, methods based on robust optimization over training risks still struggle when spurious correlations are present in all training environments: any convex combination of training environments will still reward using the spurious feature \(X_2\), because it helps on every training environment!
Invariant Prediction & IRM
An alternative is invariant prediction: finding a feature mapping \(\Phi(x)\) such that the predictive distribution \(p^e(y \mid \Phi(x))\) is invariant across all environments \(e \in \mathcal{E}\).
Invariant Risk Minimization (IRM) (Arjovsky et al., 2019) formalizes this as a bi-level optimization problem: we seek a data representation \(\Phi: \mathcal{X} \to \mathcal{H}\) and a single classifier \(w: \mathcal{H} \to \mathcal{Y}\) that is simultaneously optimal across every training environment:
\[\min_{\Phi,\, w} \sum_{e \in \mathcal{E}_\text{tr}} R^e(w \circ \Phi) \qquad \text{s.t.} \qquad w \in \arg\min_{\bar{w}} R^e(\bar{w} \circ \Phi) \quad \forall e \in \mathcal{E}_\text{tr}.\]In our linear SEM example, \(\Phi(x_1, x_2) = x_1\) satisfies this constraint because \(\E^e[Y \mid X_1] = X_1\) in every environment, so \(w = 1\) is simultaneously optimal everywhere. Any representation \(\Phi\) that leaks \(X_2\) will have different optimal linear predictors across environments with different \(\gamma_e\), violating the constraint.
Connections to Causality
Causality has often been defined as having the central property of invariance under intervention — also known as the Independent Causal Mechanisms (ICM) principle: the conditional distribution of each variable given its causal parents remains invariant when other mechanisms in the system are altered.
The concept of environments used in the OOD generalization literature is intimately connected to the concept of interventions in causality. By intervening, we change the mechanism generating a subset of variables (for example, changing \(\gamma_e\) on \(X_2\) across environments), which induces an interventional structural equation model while leaving the causal mechanism \(p(Y \mid \text{Pa}(Y))\) untouched.
In certain settings, invariance, causality, and OOD generalization are mathematically equivalent. As Martin Arjovsky notes in his thesis, even when recovering a full causal graph over high-dimensional raw inputs (like pixels) is out of reach, searching for representations that induce invariant predictors gives us a practical handle on building models that generalize beyond their training distribution.