{'fraction_non_alphanumeric': 0.04088422297796836, 'fraction_numerical': 0.007048154172211543, 'mean_word_length': 4.5847784200385355, 'pattern_counts': {'":': 0, '<': 1, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Counterfactual Invariance to Spurious Correlations: Why and How to Pass Stress Tests

Informally, a 'spurious correlation' is the dependence of a model on some aspect of the input data that an analyst thinks shouldn't matter. In machine learning, these have a know-it-when-you-see-it character; e.g., changing the gender of a sentence's subject changes a sentiment predictor's output. To check for spurious correlations, we can 'stress test' models by perturbing irrelevant parts of input data and seeing if model predictions change. In this paper, we study stress testing using the tools of causal inference. We introduce counterfactual invariance as a formalization of the requirement that changing irrelevant parts of the input shouldn't change model predictions. We connect counterfactual invariance to out-of-domain model performance, and provide practical schemes for learning (approximately) counterfactual invariant predictors (without access to counterfactual examples). It turns out that both the means and implications of counterfactual invariance depend fundamentally on the true underlying causal structure of the data -- in particular, whether the label causes the features or the features cause the label. Distinct causal structures require distinct regularization schemes to induce counterfactual invariance. Similarly, counterfactual invariance implies different domain shift guarantees depending on the underlying causal structure. This theory is supported by empirical results on text classification.


Introduction
Our focus in this paper is the sort of spurious correlations revealed by "poke it and see what happens" testing procedures for machine-learning models. For example, we might test a sentiment analysis tool by changing one proper noun for another ("tasty Mexican food" to "tasty Indian food"), with the expectation that the predicted sentiment should not change. This kind of perturbative stress testing is increasingly popular: it is straightforward to understand and offers a natural way to test the behavior of models against the expectations of practitioners [Rib+20; Wu+19; Nai+18].
Intuitively, models that pass such stress tests are preferable to those that do not. However, fundamental questions about the use and meaning of perturbative stress tests remain open.
For instance, what is the connection between passing stress tests and model performance on prediction? Eliminating predictor dependence on a spurious correlation should help with domain shifts that affect the spurious correlation-but how do we make this precise? And, how should we develop models that pass stress tests when our ability to generate perturbed examples is limited? For example, automatically perturbing the sentiment of a document in a general fashion is difficult.
The ad hoc nature of stress testing makes it difficult to give general answers to such questions. In this paper, we will use the tools of causal inference to formalize what it means for models

Causal Structure
Counterfactual invariance is a condition on how the predicted label behaves under interventions on parts of the input data. However, intuitions about stress testing are based on how the true label behaves under interventions on parts of the input data. We will see that the true causal structure fundamentally affects both the implications of counterfactual invariance, and the techniques we use to achieve it. To study this phenomenon, we'll use two causal structures that are commonly encountered in applications; see Figure 1.

Prediction in the Causal Direction
We begin with the case where X is a cause of Y . Example 2.1. We want to automatically classify the quality of product reviews. Each review has a number of "helpful" votes Y (from site users). We predict Y using the text of the product review X . However, we find interventions on the sentiment Z of the text change our prediction; changing "Great shoes!" to "Bad shoes!" changes the prediction.
In the examples in this paper, the covariate X is text data. Usually, the causal relationship between the text and Y and Z will be complex-e.g., the relationships may depend on abstract, unlabeled, parts of the text such as topic, writing quality, or tone. In principle, we could enumerate all such latent variables, construct a causal graph capturing the relationships between these variables and Y, Z, and use this causal structure to study counterfactual invariance. For instance, if we think that topic causally influences the helpfulness Y , but is Figure 1: Causal models for the data generating process. We decompose the observed covariate X into latent parts defined by their causal relationships with Z and Y . Solid arrows denote causal relationships, while dashed lines denote non-causal associations. The differences between these causal structures will turn out to be key for understanding counterfactual invariance. not influenced by sentiment Z, then we could build a counterfactually invariant predictor by extracting the topic and predicting Y using topic alone. However, exhaustively articulating all possible such variables is a herculean task.
Instead, notice that the only thing that's relevant about these latent variables is their causal relationship with Y and Z. Accordingly, we'll decompose the observed variable X into parts defined by their causal relationships with Y and Z. We remain agnostic to the semantic interpretation of these parts. Namely, we define X ⊥ Z as the part of X that is not causally influenced by Z (but may influence Y ), X ⊥ Y as the part that does not causally influence Y (but may be influenced by Z), and X Y ∧Z is the remaining part that is both influenced by Z and that influences Y . The causal structure is shown in Figure 1a.
We see there are two paths that lead to Y and Z being associated. The first is when Z affects X Y ∧Z which, in turn, affects Y . For example, a very enthusiastic reviewer might write a longer, more detailed review, which will in turn be more helpful. The second is when a common cause or selection effect in the data generating process induces an association between Z and Y , which we denote with a dashed arrow. For example, if books tend to get more positive reviews, and also people who buy books are more likely to flag reviews as helpful, then the product type would be a common cause of sentiment and helpfulness.

Prediction in the Anti-Causal Direction
We also consider the case where Y causes X .

Example 2.2.
We want to predict the star rating Y of movie reviews from the text X . However, we find that predictions are influenced by the movie genre Z; e.g., changing "Adam Sandler" (a comedy actor) to "Hugh Grant" (a romance actor) changes the predictions. Figure 1b shows the causal structure. Here, the observed X is influenced by both Y and Z. Again, we decompose X into parts defined by their causal relationship with Z and Y . Here, Z (and thus X ⊥ Y ) can be associated with Y through two paths. First, if X Y ∧Z is non-trivial, then conditioning on it causes a dependence between Z and Y (because X Y ∧Z is a collider). For example, if Adam Sandler tends to appear in good comedy movies but bad movies of other genres then seeing "Sandler" in the text induces a dependency between sentiment and genre. Second, Z and Y may be associated due to a common cause, or due to selection effects in the data collection protocol-this is represented by the dashed line between Z and Y . For example, fans of romantic comedies may tend to give higher reviews (to all films) than fans of horror movies.

Non-Causal Associations
Frequently, a predictor trained to predict Y from X will rely on X ⊥ Y , even though there is no causal connection between Y and X ⊥ Y , and therefore will fail counterfactual invariance. The reason is that X ⊥ Y serves as a proxy for Z, and Z is predictive of Y due to the non-causal (dashed line) association.
There are two mechanisms that can induce such associations. First, Y and Z may be confounded: they are both influenced by an unobserved common cause U. For example, people who review books may be more upbeat than people who review clothing. This leads to positive sentiments and high helpfulness votes for books, creating an association between sentiment and helpfulness. Second, Y and Z may be subject to selection: there is some condition (event) S that depends on Y and Z, such that a data point from the population is included in the sample only if S = 1 holds. For example, our training data might only include movies with at least 100 reviews. If only excellent horror movies have so many reviews (but most rom-coms get that many), then this selection would induce an association between genre and score. Formally, the dashed-line causal graphs mean our sample is distributed according to P(X , Y, Z) = P(X , Y, Z, u | S = 1)dP(u) where Y, Z are caused by U and are causes of S, and (X , Y, Z) are causally related according to the graph.
In addition to the non-causal dashed-line relationship, there is also dependency induced by between Y and Z by X Y ∧Z . Whether or not each of these dependencies is "spurious" is a problem-specific judgement that must be made by each analyst based on their particular use case. E.g., using genre to predict sentiment may or may not be reasonable, depending on the actual application in mind. However, there is a special case that captures a common intuition for purely spurious association.

Definition 2.3. We say that the association between Y and Z is purely spurious
That is, if the dashed-line association did not exist (removed by conditioning on Z) then the part of X that is not influenced by Z would suffice to estimate Y .

Observable Signatures of Counterfactually Invariant Predictors
We now consider the question of how to achieve counterfactual invariance in practice. The challenge is that counterfactual invariance is defined by the behavior of the predictor on counterfactual data that is never actually observed. This makes checking counterfactual invariance impossible. Instead, we'll derive a signature of counterfactual invariance that actually can be measured-and enforced-using ordinary datasets where Z (or a proxy) is measured. For example, the star rating of a review as a proxy for sentiment, or genre labels in the movie review case.
Intuitively, a predictor f is counterfactually invariant if it depends only on X ⊥ Z , the part of X that is not affected by Z. To formalize this, we need to show that such a X ⊥ Z is well defined: Lemma 3.1. Let X ⊥ Z be a X -measurable random variable such that, for all measurable functions f , we have that f is counterfactually invariant if and only if f (X ) is X ⊥ Z -measurable. If Z is discrete 1 then such a X ⊥ Z exists. Accordingly, we'd like to construct a predictor that is a function of X ⊥ Z only (i.e., is X ⊥ Z measurable). The key insight is that we can use the causal graphs to read off a set of 1 In fact, it suffices that all potential outcomes {Y (z)} z are jointly measurable with respect to a single wellbehaved sigma algebra; discrete Z is sufficient but not necessary. conditional independence relationships that are satisfied by Z, X ⊥ Z , Y . Critically, these conditional independence relationships are testable from the observed data. Thus, they provide a signature of counterfactual invariance: Theorem 3.2. If f is a counterfactually invariant predictor: 2. Under the causal-direction graph, if Y and Z are not subject to selection (but possibly confounded), f (X ) ⊥ ⊥ Z.
3. Under the causal-direction graph, if the association is purely spurious, Y ⊥ ⊥ X | X ⊥ Z , Z, and Y and Z are not confounded (but possibly selected), f (X ) ⊥ ⊥ Z | Y . Remark 3.3 (Connection to Fairness). This result has an interesting interpretation when Z is a protected attribute (e.g., sex or race) that we'd like to be fair with respect to. There are many ways of formalizing fairness, which are usually mutually incompatible. In the fairness setting, counterfactual invariance is equivalent to counterfactual fairness [Kus+17; Gar+19], the condition f (X ) ⊥ ⊥ Z is equivalent to demographic parity, and the condition f (X ) ⊥ ⊥ Z | Y is equivalent to equalized odds [Meh+19]. Theorem 3.2 then says that counterfactual fairness implies either demographic parity or equalized odds, depending on the true underlying causal structure of the problem. Hence, the relationship between disparate fairness notions is clarified by taking the underlying causal structure into account. This also suggests we can take counterfactual fairness as the fundamental notion, then use demographic parity in the causal-confounding case and equalized odds otherwise. However, we leave the (ethical) question of whether this is a sound strategy to future work.
Causal Regularization Without access to counterfactual examples, we cannot directly enforce counterfactual invariance. However, we can require a trained model to satisfy the counterfactual invariance signature of Theorem 3.2. The hope is that enforcing the signature will lead the model to be counterfactually invariant. To do this, we regularize the model to satisfy the appropriate conditional independence condition. For simplicity of exposition, we restrict to binary Y and Z. The (infinite data) regularization terms are Maximum mean discrepancy (MMD) is a metric on probability measures. 2 The marginal independence condition is equivalent to (3.1) equal 0, and the conditional independence is equivalent to (3.2) equal 0. In practice, we can estimate the MMD with finite data samples [Gre+12]. When training with stochastic gradient descent, we compute the penalty on each minibatch.
The procedure is then: if the data has causal-direction structure and the Y ↔ Z association is due to confounding, add the marginal regularization term to the the training objective. If the data has anti-causal structure, or the association is due to selection, add the conditional regularization term instead. In this way, we regularize towards models that satisfy the counterfactual invariance signature.
A key point is that the regularizer we must use depends on the true causal structure. The conditional and marginal independence conditions are generally incompatible. Enforcing the condition that is mismatched to the true underlying causal structure may fail to encourage counterfactual invariance, or may throw away more information than is required.

Gap to Counterfactual Invariance
The conditional independence signature of Theorem 3.2 is necessary but not sufficient for counterfactual invariance. This is for two reasons. First, counterfactual invariance applies to individual datapoint realizations, but the signature is distributional. In particular, the invariance P for all z, z would also imply the conditional independence signature. But, this invariance is weaker than counterfactual invariance, since it doesn't require access to counterfactual realizations. Second, f (X ) ⊥ ⊥ Z does not imply, in general, that Z is not a cause of f (X ). This (unusual) behavior can happen if, e.g., there are levels of Z that we do not observe in the training data, or there are variables omitted from the causal graph that are a common cause of Z and X .
Unfortunately, the gap between the signature and counterfactual invariance is a fundamental restriction of using observational data. The conditional independence signature is in some sense the closest proxy for counterfactual invariance we can hope for. In section 5, we'll see that enforcing the signature does a good job of enforcing counterfactual invariance in practice.

Performance Out of Domain
Counterfactual invariance is an intuitively desirable property for a predictor to have. However, it's not immediately clear how it relates to model performance as measured by, e.g., accuracy. Intuitively, eliminating predictor dependence on a spurious Z may help with domain shift, where the data distribution in the target domain differs from the distribution of the training data. We now turn to formalizing this idea.
First, we must articulate the set of domain shifts to be considered. In our setting, the natural thing is to hold the causal relationships fixed across domains, but to allow the non-causal ("spurious") dependence between Y and Z to vary. Demanding that the causal relationships stay fixed reflects the requirement that the causal structure describes the dynamics of an underlying real-world process-e.g., the author's sentiment is always a cause (not an effect) of the text in all domains. On the other hand, the dependency between Y and Z induced by either confounding or selection can vary without changing the underlying causal structure. For confounding, the distribution of the confounder may differ between domains-e.g., books are rare in training, but common in deployment. For selection, the selection criterion may differ between domains-e.g., we include only frequently reviewed movies in training, but make predictions for all movies in deployment.
We want to capture spurious domain shifts by considering domain shifts induced by selection or confounding. However, there is an additional nuance. Changes to the marginal distribution of Y will affect the risk of a predictor, even in the absence of any spurious association between Y and Z. Therefore, we restrict to shifts that preserve the marginal distribution of Y .
Definition 4.1. We say that distributions P, Q are causally compatible if both obey the same causal graph, P(Y ) = Q(Y ), and there is a confounder U and/or selection conditions S,S such that P = P(X , Y, Z | U, S = 1)dP(U) and Q = P(X , Y, Z | U,S = 1)dQ(U) for somẽ P(U),Q(U).
We can now connect counterfactual invariance and robustness to domain shift. 2. the data obeys the causal-direction graph, there is no confounding (but possibly selection), and the association is purely spurious, Y ⊥ ⊥ X | X ⊥ Z , Z, or 3. the data obeys the causal-direction graph, there is no selection (but possibly confounding), the association is purely spurious and the causal effect of X ⊥ Z on Y is additive, i.e., the true data generating process is for some functions g,g.
Then, the training domain counterfactually invariant risk minimizer is also the target domain counterfactually invariant risk minimizer, Remark 4.3. The causal case with confounding requires an additional assumption (additive structure) because, e.g., an interaction between confounder and X ⊥ Z can yield a case where X ⊥ Z and Y have a different relationship in each domain (whence, out-of-domain learning is impossible).
This result gives a recipe for finding a good predictor in the target domain even without access to any target domain examples at training time. Namely, find the counterfactually invariant risk minimizer in the training domain. In practice, we can use the regularization scheme of section 3 to (approximately) achieve this. We'll see in section 5 that this works well in practice.
Optimality Theorem 4.2 begs the question: if the only thing we know about the target setting is that it's causally compatible with the training data, is the best predictor the counterfactually invariant predictor with lowest training risk? A natural way to formalize this question is to study the predictor with the best performance in the worst case target distribution. We define = {Q : Q causally compatible with P} and the -minimax predictor The question is then: what's the relationship between the counterfactually invariant risk minimizer and the minimax predictor?
Theorem 4.4. The counterfactually invariant risk minimizer is not -minimax in general. However, under the conditions of Theorem 4.2, if the association is purely spurious, X Y ∧Z ⊥ ⊥ Y | X ⊥ Z , Z, and P(Z, Y ) satisfies overlap, then the two predictors are the same. By overlap we mean that P(Z, Y ) is a discrete distribution such that for all (z, y), if P(z, y) > 0 then there is some y = y such that also P(z, y ) > 0.
Conceptually, Theorem 4.4 just says that the counterfactually invariant predictor excludes X Y ∧Z , even when this information is useful in every domain. In the purely spurious case, X Y ∧Z carries no useful information, so counterfactual invariance is optimal.

Experiments
The main claims of the paper are: 1. Stress test violations can be reduced by (conditional) independence regularization.
2. This reduction will improve out-of-domain prediction performance.
3. To get the full effect, the imposed penalty must match the causal structure of the data.

Setup
To assess these claims, we'll examine the behavior of predictors trained with the marginal or conditinal regularization on multiple text datasets that have either causal or anti-causal structure. We expect to see that marginal regularization improves stress test and out-of-domain performance on data with causal-confounded structure, and conditional regularization improves these on data with anti-causal structure. For each experiment, we use BERT [Dev+19] finetuned to predict a label Y from the text as our base model. We train multiple causally-regularized models on the each dataset. The training varies by whether we use the conditional or marginal penalty, and by the strength of the regularization term. That is, we train identical architectures using CrossEntropy + λ · Regularizer as the objective function, where we vary λ and take Regularizer as either the marginal penalty, (3.1), or conditional penalty, (3.2). We compare these models' predictions on data with causal and anti-causal structure.
See supplement for experimental details.

Robustness to Stress Tests
First, we examine whether enforcing the causal regularization actually helps to enforce counterfactual invariance. We create counterfactual (stress test) examples by perturbing the input data and compare the prediction on these. We build the experimental datasets using Amazon reviews from the product category "Clothing, Shoes, and Jewelry" [NLM19].
Synthetic To study the relationship between counterfactual invariance and the distributional signature of Theorem 3.2, we construct a synthetic confound. For each review, we draw a Bernoulli random Z, and then perturb the text X so that the common words "the" and "a" carry information about Z: for example, we replace "the" with the token "thexxxxx" when Z = 1. We take Y to be the review score, and subsample so Y is balanced. This data has anti-causal structure: the text X is written to explain the score Y . Further, we expect that the Y, Z association is purely spurious, because "the" and "a" carry little information about the label.
We train the models on data where P(Y = Z) = 0.3. We then create perturbed stress-test datasets by changing each example X i (z) to the counterfactual X i (1 − z) (using the synthetic model). By measuring the performance of each model on the perturbed data, we can test whether the distributional properties enforced by the regularizers result in counterfactual invariance at the instance level. Figure 2 shows that conditional regularization (matching the anti-causal structure) reduces checklist failures, as measured by the frequency that the predicted label changes due to perturbation as well as the mean absolute difference in predictive probabilities that is induced by perturbation.
Natural To study the relationship in real data, we use the review data in a different way. We now take Z to be the score, binarized as Z ∈ {1 or 2 stars, 4 or 5 stars}. We use this Z as a proxy for sentiment, and consider problems where sentiment should (plausibly) not have a causal effect on Y . For the causal prediction problem, we take Y to be the helpfulness score of the review (binarized as described below). This is causal because readers decide whether the review is helpful based on the text. For the anti-causal prediction problem, we take Y to be whether "Clothing" is included as a category tag for the product under review (e.g., boots typically do not have this tag). This is anti-causal because the product category affects the text.
We control the strength of the spurious association between Y and Z. In the anti-causal case, this is done by selection: we randomly subset the data to enforce a target level of dependence between Y and Z. The causal-direction case with confounding is more complicated. To manipulate confounding strength, we binarize the number of helpfulness votes V in a manner determined by the target level of association. We take where T Z is a Z-dependent threshold, chosen to induce a target association. We choose P(Y = 0 | Z = 0) = P(Y = 1 | Z = 1) = 0.3. We balance Z by subsampling, which also balances Y . Now, we create stress test perturbations of these datasets by randomly changing adjectives in the examples. Using predefined lists of postive sentiment adjectives and negative sentiment adjectives, we swap any adjective that shows up on a list with a randomly sampled adjective from the other list. This preserves basic sentence structure, and thus creates a limited set of counterfactual pairs that differ on sentiment.
Results for differences in predicted probabilities between original and perturbed data are shown in Figure 3. Each point is a trained model, which vary in measured MMD on the test data and on sensitivity to perturbations. Recall that the conditional independence signature of Theorem 3.2 are necessary but not sufficient for counterfactual invariance, so it's not certain that regularizing to reduce the MMD will reduce perturbation sensitivity. Happily, we see that regularizing to reduce the MMD that matches the causal structure does indeed reduce sensitivity to perturbations.
Notice that regularizing the causally mismatched MMD can have strange effects. Regularizing marginal MMD in the anti-causal case actually makes the model more sensitive to perturbations!

Domain Shift
Next, we study the effect of causal regularization on model performance under domain shift.

Natural Product Review
We again use the natural Amazon review data described above. For both the causal and anti-causal data, we create multiple test sets with variable spurious correlation strength. This is done in the manner described above, varying P(  each regularization type and regularization strength, and measure the accuracy on each test domain. The results are shown in Figure 4. First, the unregularized predictors do indeed learn to rely on the spurious association between sentiment and the label. The accuracy of these predictors decays dramatically as the spurious assocation moves from negative (0.3) to positive-in the causal case, the unregularized predictor is worse than chance in the 0.8 domain.
Following section 3, the regularization that matches the underlying causal structure should yield a predictor that is (approximately) counterfactually invariant. Following Theorem 4.2, we expect that good performance of a counterfactually-invariant predictor in the training domain should imply good performance in each of the other domains. Indeed, we see that this is so. Models that are regularized to have small values of the appropriate MMD do indeed have better out-of-domain performance. Such models have somewhat worse in-domain performance, because they no longer exploit the spurious correlation.

MNLI Data
For an additional test on naturally-occurring confounds, we use the multigenre natural language inference (MNLI) dataset [WNB18]. Instances are concatenations of two sentences, and the label describes the semantic relationship between them, Y ∈ {contradiction, entailment, neutral}. There is a well-known confound in this dataset: examples where the second sentence contain a negation word (e.g., "not") are much more likely to be labeled as contradictions [Gur+18]. Following Sagawa et al. [Sag+20], we set Z to indicate whether one of a small set of negation words is present. Although Z is derived from the text X , it can be viewed as a proxy for a latent variable indicating whether the author intended to use negation in the text. This is an anti-causal prediction problem: the annotators were instructed to write text to reflect the desired label [WNB18].
Following Sagawa et al. [Sag+20], we divide the MNLI data into groups by (Y, Z) and compute the "worst group accuracy" across all such groups. Because this is an anti-causal problem, we predict that the conditional MMD is a more appropriate penalty than the marginal MMD. As shown in Figure 5, this prediction holds: conditional MMD regularization dramatically improves performance on the worst group, while only lightly impacting the overall accuracy across groups.

Related work
Several papers draw a connection between causality and domain shifts [SS18; SCS19; Arj+20; Mei18; PBM16; RC+18; Zha+13; Sch+12]. Typically, this work considers a prediction setting where the covariates X include both causes and effects of Y , and it is unknown which is which. The goal is to learn to predict Y using only its causal parents. Zhang et al. [Zha+13] considers anti-causal domain shift induced by changing P(Y ) and proposes a data reweighting scheme. By contrast, counterfactual invariance is an example-wise notion of invariance, not an invariance across environments. In particular, learning a counterfactually invariant predictor only requires access to data from a single environment. Theorem 4.2 establishes that counterfactual invariance does imply a certain domain generalization guarantee over causally-compatible domains. Note though that the notion of causal compatibility is not generally the same as class of domain shifts previously considered. For example, we have invariant performance in the anti-causal setting, but this is ruled out by Arjovsky et al. [Arj+20].
A related body of work focuses on "causal representation learning" [Bes+19; Loc+20; Sch+21; Arj+20]. Our approach follows this tradition, but focuses on splitting X into components defined by their causal relationships with the label Y and an additional covariate Z. Rather than attempting to infer the causal relationship between X and Y , we show that domain knowledge of this relationship is essential for obtaining counterfactually-invariant predictors. The role of causal vs anti-causal data generation in semi-supervised learning and transfer learning has also been studied [Sch+21;Sch+12]. In this paper we focus on a different implication of the causal vs anti-causal distinction.
Another line of work considers the case where either the counterfactuals X (z), X (z ) are observed for at least some data points, or where it's assumed that there's enough structural knowledge to (learn to) generate counterfactual examples [RPH21; Wu+21; Gar+19; Mit+20; WZ19; KCC20; KHL20; TAH20]. Kusner et al. [Kus+17] and Garg et al. [Gar+19] in particular examine a notion of counterfactual fairness that can be seen as equivalent to counterfactual invariance. In these papers, approximate counterfactuals are produced by direct manipulation of the features (e.g., change male to female names), generative models (e.g., style transfer of images), or crowdsourcing. Then, these counterfactuals can either be used as additional training data or the predictor can be regularized such that it cannot distinguish between X i (z) and X i (z ). This strategy can be viewed as enforcing counterfactual invariance directly; an advantage is that it avoids the necessary-but-notsufficient nuance of Theorem 3.2. However, counterfactual examples can be difficult to obtain for language data in many realistic problem domains, and it may be difficult to learn to generalize from such examples [HLB20].
The marginal and conditional independencies of Theorem 3.2 have appeared in other contexts. As discussed in remark 3.3, if we think of Z as a protected attribute and f as a 'fair' classifier, then counterfactual invariance is counterfactual fairness, the marginal independence is demographic parity, and the conditional independence is equalized odds [Meh+19]. In another setting, one approach to domain adaptation is to seek representations φ such that either φ(X ) [e.g., MBS13; Bak+13; Gan+16;Tze+14]  That is, we arrive at the same conditional independence criterion. However, it's essentially a coincidence that this matches the criterion we'd use if we'd observed Z-the two variables have totally different causal meanings and interpretations. For example, in the review data case, we might take Z to be review score and E to be the website the review is collected from. The review quality doesn't need to be counterfactually invariant to the website, and indeed we wouldn't expect it to be! Finally, this work can be viewed as part of a recent line of work using counterfactuals to examine the connection between example-wise robustness (stress testing) and distributionallevel robustness (domain shift) [e.g., TAH20; RPH21; Kau+21]. Teney et al. [TAH20] use near counterfactuals with different labels in a modified training objective, and show improved performance out-of-domain empirically. Robey et al. [RPH21] use image models to generate counterfactuals, and incorporate the model into training, showing out-of-domain improvement. They assume a somewhat different causal structure-articulating the precise connection is an interesting future direction. Kaushik et al. [Kau+21] study a linear-Gaussian model and argue that out-of-domain performance can be used as a signal to distinguish 'causal' and 'spurious' features.

Discussion
We used the tools of causal inference to formalize and study perturbative stress tests. A main insight of the paper is that counterfactual desiderata can be linked to observationally-testable conditional independence criteria. This requires consideration of the true underlying causal structure of the data. Done correctly, the link yields a simple procedure for enforcing the counterfactual desiderata, and mitigating the effects of domain shift.
The main limitation of the paper is the restrictive causal structures we consider. In particular, we require that X ⊥ Z , the part of X not causally affected by Z, is also statistically independent of Z in the observed data. However, in practice these may be dependent due to a common cause. In this case, the procedure here will be overly conservative, throwing away more information than required. Additionally, it is not obvious how to apply the ideas described here to more complicated causal situations, which can occur in structured prediction (e.g., question answering). Extending the ideas to handle richer causal structures is an important direction for future work. The work described here can provide a template for this research program.

Acknowledgments
Thanks to Kevin Murphy, Been Kim, Molly Roberts, Justin Grimmer, and Brandon Stewart for feedback on an earlier version.
for some functions g,g.
Then, the training domain counterfactually invariant risk minimizer is also the target domain counterfactually invariant risk minimizer, f * = argmin f ∈ invar Q [L(Y, f (X ))].
Proof. First, since counterfactual invariance implies X ⊥ Z -measurable, It is well-known that under squared error or cross entropy loss the minimizer is By the same argument, the counterfactually invariant risk minimizer in the We begin with the anti-causal case. We have that To that end, from the anti-causal direction graph we have that X ⊥ Z ⊥ ⊥ S, U | Y . Then, where the first and third lines are causal compatibility, and the second line is from The causal-direction case with no confounding follows essentially the same argument.
For the causal-direction case without selection, The first line is the assumed additivity. The second line follows because P [ξ | X ⊥ Z ] = 0 for all causally compatible distributions (P(ξ, X ⊥ Z ) doesn't change), and U ⊥ ⊥ X ⊥ Z . Taking an expectation over X ⊥ Z , we have P [Y ] = P [g(X ⊥ Z )] + P [g(U)]. By the same token, , since changes to the confounder don't change the distribution of X ⊥ Z (that is, X ⊥ Z ⊥ ⊥ U). And, by assumption, , as required.

Theorem 4.4. The counterfactually invariant risk minimizer is not -minimax in general.
However, under the conditions of Theorem 4.2, if the association is purely spurious, X Y ∧Z ⊥ ⊥ Y | X ⊥ Z , Z, and P(Z, Y ) satisfies overlap, then the two predictors are the same. By overlap we mean that P(Z, Y ) is a discrete distribution such that for all (z, y), if P(z, y) > 0 then there is some y = y such that also P(z, y ) > 0.
Proof. The reason that the predictors are not the same in general is that the counterfactually invariant predictor will always exclude information in X Y ∧Z , even when this information is helpful for predicting Y in all target settings. For example, consider the case where Y, Z are binary, X = X Y ∧Z and, in the anti-causal direction, X Y ∧Z = AND(Y, Z). With cross-entropy loss, the counterfactually invariant predictor is just the constant [Y ], but the decision rule that uses f (X ) = 1 if X = 1 is always better. In the causal case, consider X Y ∧Z = Z and Y = X Y ∧Z .
Informally, the second claim follows because-in the absence of X Y ∧Z information-any predictor f that's better than the counterfactually invariant predictor when Y and Z are positively correlated will be worse when Y and Z are negatively correlated.
To formalize this, we begin by considering the case where Y is binary and X = X ⊥ Y . So, in particular, the counterfactually invariant predictor is just some constant c. Let f be any predictor that uses the information in X ⊥ Y . Our goal is to show that Q [L( f (X ⊥ Y ), Y )] > Q [L(c, Y )] for at least one test distribution (so that f is not minimax). To that end, let P be any distribution where f (X ⊥ Y ) has lower risk than c (this must exist, or we're done). Then, define A = {(z, y) : P [L( f (X ⊥ Y ), y) | z] < L(c, y)}. In words: A is the collection of z, y points where f did better than the constant predictor. Since f is better than the constant predictor overall, we must have P(A) > 0. Now, define A c = {(z, 1 − y) : (z, y) ∈ A}. That is, the set constructed by flipping the label for every instance where f did better. By the overlap assumption, P(A c ) > 0. By construction, f is worse than c on A c . Further, S = 1 A is a random variable that has the causal structure required by a selection variable (it's a child of Y and Z and nothing else). So, the distribution Q defined by selection on S is causally compatible with P and satisfies Q [L( f (X ⊥ Y ), Y )] > Q [L(c, Y )], as required. To relax the requirement that X = X ⊥ Y , just repeat the same argument conditional on each value of X ⊥ Z . To relax the condition that Y is binary, swap the flipped label 1 − y for any label y with worse risk.

B.1 Model
All experiments use BERT as the base predictor. We use bert_en_uncased_L-12_H-768_A-12 from TensorFlow Hub and do not modify any parameters. Following standard practice, predictions are made using a linear map from the representation layer. We use CrossEntropy loss as the training objective. We train with vanilla stochastic gradient descent, batch size 1024, and learning rate 1e − 5 × 1024. We use patience 10 early stopping on validation risk. Each model was trained using 2 Tensor Processing Units.
For the MMD regularizer, we use the estimator of Gretton et al. [Gre+12] with the Gaussian RBF kernel. We set kernel bandwidth to 10.0. We compute the MMD on (log f 0 (x), . . . , log f k (x)), where f j (x) is the model estimate of P(Y = k | x). (Note: this is log, not logit-the later has an extra, irrelevant, degree of freedom). We use log-spaced regularization coefficients between 0 and 128.

B.2 Data
We don't do any pre-processing on the MNLI data.
The Amazon review data is from [NLM19].

B.2.1 Inducing Dependence Between Y and Z in Amazon Product Reviews
To produce the causal data with P(Y = 1 | Z =

B.2.2 Synthetic Counterfactuals in Product Review Data
We select 10 5 product reviews from the Amazon "clothing, shoes, and jewelery" dataset, and assign Y = 1 if the review is 4 or 5 stars, and Y = 0 otherwise. For each review, we use only the first twenty tokens of text. We then assign Z as a Bernoulli random variable with P(Z = 1) = 1 2 . When Z = 1, we replace the tokens "and" and "the" with "andxxxxx" and "thexxxxx" respectively; for Z = 0 we use the suffix "yyyyy" instead. Counterfactuals can then be produced by swapping the suffixes. To induce a dependency between Y and Z, we randomly resample so as to achieve γ = 0.3 and P(Y = 1) = 1 2 , using the same procedure that was used on the anti-causal model of "natural" product reviews. After selection there are 13, 315 training instances and 3, 699 test instances.