Optimizing importance weighting in the presence of sub-population shifts

## Abstract

Abstract A distribution shift between the training and test data can severely harm performance of machine learning models. Importance weighting addresses this issue by assigning different weights to data points during training. We argue that existing heuristics for determining the weights are suboptimal, as they neglect the increase of the variance of the estimated model due to the finite sample size of the training data. We interpret the optimal weights in terms of a bias-variance trade-off, and propose a bi-level optimization procedure in which the weights and model parameters are optimized simultaneously. We apply this optimization to existing importance weighting techniques for last-layer retraining of deep neural networks in the presence of sub-population shifts and show empirically that optimizing weights significantly improves generalization performance.

## 1 Introduction

Machine learning (ML) models typically are trained to minimize the average loss on training data, with the implicit aim to generalize well to unseen test data. A distribution shift between the training and test data can severely harm generalization performance (Alcorn et al., ; Quiñonero-Candela et al., ). Distribution shifts come in many forms; a prime example is a so-called sub-population shift, where the population is dissected in groups and the distribution shift is entirely due to a change in occurrence frequencies of the different groups (Yang et al., ). In ML applications, e.g., in the medical field (Jabbour et al., ), such shifts can negatively impact the average accuracy on test data and/or the accuracy on specific groups. The latter has implications for algorithmic fairness (Shankar et al., ; Buolamwini & Gebru, ; Wilson et al., ). Furthermore, sub-population shifts are also relevant regarding spurious correlations (Gururangan et al., ; Geirhos et al., ; DeGrave et al., ), where the frequency of occurrence of certain combinations of the variable of interest (e.g., a medical condition) and non-causally related attributes (e.g., the sex of the patient) might differ between the training and test distribution.

Importance weighting applied to ML models treats different observations in the training data differently, for example, by using a weighted loss or a weighted sampler (Kahn & Marshall, ). It has long been used to address distribution shifts and other issues, including domain adaptation, covariate shift and class imbalance; see Kimura & Hino () for an overview. In the context of training deep neural networks (DNNs), it was used in a variety of ways to tackle sub-population shifts, e.g., Sagawa et al. (); Liu et al. (); Idrissi et al. (); Kirichenko et al. ().

This paper addresses the question of how to weigh each observation in an importance weighting scheme that aims to mitigate the effects of a distribution shift. Typically, the weights are chosen based on a heuristic argument. For example, in group-weighted empirical risk minimization (GW-ERM) they are chosen such that the weighted training loss is unbiased for the expected loss with respect to the test distribution. We argue that such a choice is usually not optimal: for a limited training sample it does not lead to estimated model parameters that minimize the expected loss with respect to the test distribution. Because of the limited size of the sample, heuristically chosen weights tend to put too much weight on certain observations (usually the minority groups). They thereby introduce significant variance, due to a strong dependence of the trained model on the particulars of the training data. We illustrate this bias-variance trade-off for importance weighting analytically with an example of a linear regression model in the presence of a sub-population shift.

Secondly, we introduce an estimation procedure for the optimal weights via a bi-level optimization problem, where the weights and the model parameters are optimized simultaneously. In the context of last-layer retraining of DNNs in the presence of sub-population shifts, we show empirically, for benchmark vision and natural language processing datasets, that existing state-of-the-art importance weighting methods (see Section 2) can be improved significantly by optimizing for the weights. We find that both the weighted average and worst-group accuracy generally improve, and that optimized weights increase robustness against the choice of hyperparameters for training. We show that the effect of optimizing weights is larger when the limited size of the training sample becomes pressing, namely in the case of a small total sample size or when the minority groups are small.

We emphasize that the proposed procedure for optimizing weights should not be positioned as a new importance weighting method. Instead, it should be seen as an addition to existing methods that typically improves their generalization performance.

## 2 Related work

In this section we give a brief overview of importance weighting techniques, the issue of sub-population shift, and how existing methods aim to address it.

Importance weighting for distribution shifts in deep learning: importance weighting is a classical tool to estimate a model for a test distribution that differs from the distribution of the training sample (Kahn & Marshall, ; Shimodaira, ; Koller & Friedman, ). When applied to training DNNs, importance weighting only has a noticeable effect in the presence of sufficient regularization (Byrd & Lipton, ; Xu et al., ). In our applications we focus on the use of importance weighting for last-layer retraining, as this has been argued to be sufficient for dealing with sub-population shifts (Kirichenko et al., ; LaBonte et al., ; Chen et al., ).

Sub-population shifts: it has been widely documented that ML models frequently face deteriorating generalization performance due to sub-population shifts (Cai et al., ; Koh et al., ; Yang et al., ). There exist different possible definitions of the groups for which the distribution shift occurs. In the case of a spurious correlation, groups are commonly defined by a combination of a label and a non-causally related attribute (Joshi et al., ; Makar et al., ). Alternatively, groups can be defined exclusively via an attribute (Martinez et al., ) or, in the case of class imbalance, exclusively via a label (Liu et al., ). While our experiments consider spurious correlations, the methodology also applies to alternative group definitions. Generalizing to groups which do not occur in the training data (Santurkar et al., ) is beyond the scope of this paper.

Existing approaches of importance weighting for training DNNs in the presence of a sub-population shift: a key method is group-weighted empirical risk minimization (GW-ERM), which uses a weighted loss and where the weights are determined by the occurrence frequencies of the groups in the training and test distribution. The weighted sampler analogue subsamples from large groups (SUBG) to create a novel dataset (Sagawa et al., ). Both methods are strong baselines for addressing sub-population shifts (Idrissi et al., ).
An alternative approach is to minimize the worst-group loss via group distributional robust optimization (GDRO, Sagawa et al. ()). While these methods require group annotations for all training data, there exist other methods that require limited group annotations in the form of a validation set. An example is Just Train Twice (JTT), where the errors of the model are used to identify data points that need to be upweighted (Liu et al., ). Other methods in this realm incorporate errors of a secondary model in their estimation procedure (Nam et al., ; Zhang et al., ; Qiu et al., ) or estimate group attributes (e.g., Nam et al. (); Han & Zou (); Pezeshki et al. ()). These methods typically require a small validation set for determining hyperparameters. Kirichenko et al. () argue that instead one should use this validation set for the estimation of an ensemble of models via SUBG, a method denoted as deep feature reweighting (DFR).

Estimation of optimal weights: a commonly noted downside of importance weighting is that it reduces the effective sample size available for model training (Martino et al., ; Schwartz & Stanovsky, ). In light of this, Cui et al. () and Wen et al. () suggest that the weight given to a particular label should be based on the effective sample size. Similar to our approach, Ren et al. () aim to learn weights for neural networks given a validation set.

## 3 Problem Setting

Consider random variables $(y,\bm{x}),$ where $y\in\mathcal{Y}$ is the target variable and $\bm{x}\in\mathcal{X}$ represents the input features. Suppose we are dealing with a family of models $\left\{f_{\bm{\theta}}:\mathcal{X}\rightarrow\mathcal{Y}\,|\,\bm{\theta}\in
\Theta\right\}$ and we aim to find a model that minimizes $\mathbb{E}_{(y,\bm{x})\sim\mathcal{P}_{\mathrm{te}}}[\mathcal{L}(y,f_{\bm{
\theta}}(\bm{x})],$ where $\mathcal{L}(\cdot,\cdot)$ is some pre-defined loss function and $\mathcal{P}_{\mathrm{te}}$ is a test distribution of interest. Moreover, we have access to a dataset $D_{\mathrm{tr}}^{n}=\{y_{{i}},\bm{x}_{i}\}^{n}_{i=1},$ which is an i.i.d. sample drawn from a training distribution $\mathcal{P}_{\mathrm{tr}}$ that is different from the test distribution. This distribution shift can be problematic, as the minimum of an empirical loss for the training data does typically not minimize the expected loss with respect to the test distribution.

One method to address this issue is by estimating the model parameters using a weighted loss,

$$ $\displaystyle\hat{\bm{\theta}}_{n}(\bm{w}):=\operatorname*{arg\,min}_{\bm{ \theta}\in\Theta}\mathcal{L}_{\mathrm{train}}^{n}(\bm{\theta},\bm{w}),\qquad \text{where}\quad\mathcal{L}_{\mathrm{train}}^{n}(\bm{\theta},\bm{w}):=\frac{1 }{n}\sum^{n}_{i=1}w_{i}\,\mathcal{L}(y_{{i}},f_{\bm{\theta}}(\bm{x}_{i})).$ (1) $$

If there is no unique minimizer, which is often the case for DNNs, the $\operatorname*{arg\,min}$ should be interpreted as drawing randomly one element from the set of minimizers. A common choice for the weights $\bm{w}=(w_{1},w_{2},\ldots w_{n})^{\prime}\in\mathbb{R}^{n}$ are the likelihood ratios $\bm{r}=(r_{1},r_{2},\ldots r_{n})^{\prime},$ where $r_{i}=r(y_{i},\bm{x}_{i})$ and $r(y,\bm{x}):=\frac{p_{\mathrm{te}}(y,\bm{x})}{p_{\mathrm{tr}}(y,\bm{x})}$ is the (assumed to be known) ratio of the probability density functions (pdfs) associated with the test and training distribution respectively. This is due to a key result in importance weighting that, assuming $p_{\mathrm{tr}}(y,\bm{x})>0$ when $p_{\mathrm{te}}(y,\bm{x})\neq 0$,

$$ $\mathbb{E}_{(y,\bm{x})\sim\mathcal{P}_{\mathrm{tr}}}[r(y,\bm{x})\mathcal{L}(y, f_{\bm{\theta}}(\bm{x}))]=\mathbb{E}_{(y,\bm{x})\sim\mathcal{P}_{\mathrm{te}}} [\mathcal{L}(y,f_{\bm{\theta}}(\bm{x}))].$ (2) $$

If we choose the weights $\bm{w}$ to be equal to the likelihood ratios $\bm{r},$ then by the law of large numbers $\mathcal{L}_{\mathrm{train}}^{n}(\bm{\theta},\bm{r})$ converges in the limit of $n$ to infinity to the expectations in Equation 2. As a consequence, $\hat{\bm{\theta}}_{n}(\bm{r})$ minimizes for sample size tending to infinity the expected loss with respect to the test distribution, $\mathbb{E}_{(y,\bm{x})\sim\mathcal{P}_{\mathrm{te}}}[\mathcal{L}(y,f_{\bm{
\theta}}(\bm{x})].$ However, in practice one works with a finite amount of training data and the expectation might be poorly approximated by its sample analogue. Therefore, the optimal choice $\bm{w}^{*}_{n}$ for the weights should be defined as the one that leads to a “best” parameter estimate $\hat{\bm{\theta}}_{n}(\bm{w}),$ in the sense that it minimizes the expected loss with respect to the test distribution,

$$ $\bm{w}^{*}_{n}:=\operatorname*{arg\,min}_{\bm{w}}\mathbb{E}_{D_{\mathrm{tr}}^{ n}\sim\mathcal{P}_{\mathrm{tr}}}\!\left[\mathbb{E}_{(y,\bm{x})\sim\mathcal{P}_ {\mathrm{te}}}[\mathcal{L}(y,f_{\hat{\bm{\theta}}_{n}(\bm{w})}(\bm{x}))|D_{ \mathrm{tr}}^{n}]\right].$ (3) $$

Note that by marginalizing over $D_{\mathrm{tr}}^{n}$ the optimal choice for $\bm{w}$ is not determined by the actual realization of the training data. In practice, with access to only a single dataset $D_{\mathrm{tr}}^{n},$ the estimate of the optimal choice will be optimal given that specific training set, although one might be able to alleviate this dependence by using a cross-validation or bootstrapping scheme.

The first main claim of this paper is that the optimal weights $\bm{w}^{*}_{n}$ are typically not equal to the weight choice based on the likelihood ratio, $(\bm{w}^{*}_{n})_{i}\neq r(y_{i},\bm{x}_{i}).$ Although the likelihood ratio makes the estimated parameters asymptotically unbiased under certain conditions (Shimodaira ()), it neglects the variance that arises due to a limited size of the training set. More concretely, putting more weight on certain training data points could lead to overfitting and does not necessarily lead to better generalization, not even on the test distribution for which the weights are supposed to be a correction.

The second main contribution of this paper is the introduction of a new estimation procedure for the optimal weights $\bm{w}^{*}_{n}.$ To address the expectation with respect to $\mathcal{P}_{\mathrm{te}}$ in Equation 3, we make use of Equation 2 and write the optimal weights as

$$ $\bm{w}^{*}_{n}=\operatorname*{arg\,min}_{\bm{w}}\mathbb{E}_{D_{\mathrm{tr}}^{n }\sim\mathcal{P}_{\mathrm{tr}}}\!\left[\mathbb{E}_{(y,\bm{x})\sim\mathcal{P}_{ \mathrm{tr}}}[r(y,\bm{x})\mathcal{L}(y,f_{\hat{\bm{\theta}}_{n}(\bm{w})}(\bm{x }))|D_{\mathrm{tr}}^{n}]\right].$ (4) $$

We propose to estimate the inner expectation using a randomly drawn subset of size $n_{\mathrm{val}}$ from the available training data. We call this subset the validation set. Let $n^{\prime}=n-n_{\mathrm{val}}$ be the remaining number of data points. The result is a bi-level optimization problem. The weights are estimated via

$$ $\hat{\bm{w}}^{*}_{n}=\operatorname*{arg\,min}_{\bm{w}}\mathcal{L}_{\mathrm{val }}(\hat{\bm{\theta}}_{n^{\prime}}(\bm{w}),\bm{r}),$ (5) $$

where the validation loss is defined analogously to the training loss in Equation 1. The model parameters are estimated by minimizing this training loss, using the $n^{\prime}$ data points that are not in the validation set. The creation of the validation set has the advantage of avoiding possible overfitting of $\bm{w}$, but comes at the cost of having fewer data points for estimating $\bm{\theta}$.

### 3.1 Optimizing weights for sub-population shifts

In what follows, we make the previous general claims concrete by focusing on one specific type of distribution shift that has been studied extensively in the machine learning literature, namely sub-population shift. Each observation carries an additional random variable $g\in\mathcal{G},$ which labels a total number of $|\mathcal{G}|=G$ subgroups that dissect the population. We note that $g$ can depend on $y$ and $\bm{x},$ but this is not necessarily the case. For ease of notation, we assume that $g$ can take the values $1,2,\ldots,G.$ The distribution shift arises from a difference between the marginal training and test distributions of $g,$ while the conditional pdf $p(\bm{x},y|g)$ remains unchanged:

$$ $\displaystyle p_{\mathrm{tr}}(y,g,\bm{x})=p_{\mathrm{tr}}(g)p(y,\bm{x}|g), \quad p_{\mathrm{te}}(y,g,\bm{x})=p_{\mathrm{te}}(g)p(y,\bm{x}|g),$ (6) $$

where $p_{\mathrm{tr}}(g)\neq p_{\mathrm{te}}(g).$ Unless otherwise stated, for the training set defined in Section 3 we presume access to the subgroup labels $\{g_{i}\}^{n}_{i=1}.$

Since the distribution shift is entirely due to a change in the marginal distribution of $g,$ it is natural to constrain the weights $w_{i}$ to be determined by the group labels $g_{i}.$ It is convenient to define group weights $p_{g},$ where $g\in\mathcal{G},$ such that $w_{i}=p_{g_{i}}/p_{\mathrm{tr}}(g_{i}).$ Estimating the model parameters can then be seen as a function of the group weights, i.e., $\hat{\bm{\theta}}_{n}(\bm{p}),$ where $\bm{p}=(p_{1},p_{2},\ldots,p_{G})^{\prime}\in\mathbb{R}_{+}^{G}.$ Instead of optimal weights $\bm{w}^{*}_{n},$ one now aims to find optimal group weights $\bm{p}^{*}_{n}.$ Assuming that the number of groups is much smaller than the size of the training set, this heavily reduces the dimension of the bi-level optimization problem defined in Equation 5.

## 4 Theoretical analysis of linear regression

The optimal weights, as defined in Equation 3, can be analytically derived for the case of linear regression with a simple data-generating process (DGP) and sub-population shift. This analysis provides insight into the nature of optimized weights as a bias-variance trade-off and the conditions under which optimization of the weights becomes relevant.

We consider a DGP $y=\bm{x}^{\prime}\bm{\beta}^{g}+\varepsilon,$ where $y\in\mathbb{R}$ is the target variable, $\bm{x}=(1,\tilde{\bm{x}}^{\prime})^{\prime}$ represents the intercept and the feature vector $\tilde{\bm{x}}\in\mathbb{R}^{d},$ and $\varepsilon\in\mathbb{R}$ is the noise. We take $\tilde{\bm{x}}\sim\mathcal{N}(\bm{0},\bm{I}_{d})$ and the noise to have zero mean, variance $\sigma^{2}$ and to be uncorrelated with $\bm{x}.$ The population consists of two groups labeled by $g\in\{0,1\}.$ Group membership determines the value of the intercept, $\bm{\beta}^{g}=(a_{g},\bm{\beta}^{\prime})^{\prime}$ for $g=0,1.$ Note that the slope coefficients are common among the two groups. The sub-population shift between training and test data is defined by a change in the marginal distribution of the group label, $p_{\mathrm{tr}}:=\mathbb{P}_{\mathrm{tr}}(g=1)\neq\mathbb{P}_{\mathrm{te}}(g=1
)=:p_{\mathrm{te}}.$

Suppose we have a training sample $D_{\mathrm{tr}}^{n}=\left\{y_{i},\bm{x}_{i},g_{i}\right\}_{i=1}^{n}$ of size $n$ and we use the weighted least squares (WLS) estimator

$$ $\hat{\bm{\beta}}_{n}(p)=(\bm{X}^{\prime}\bm{W}\bm{X})^{-1}\bm{X}^{\prime}\bm{W }\bm{y},\quad\text{with }\bm{W}=\mathrm{diag}(w_{1},...,w_{n}),\hskip 2.84544 ptw_{i}=\begin{cases}\frac{p}{p_{\mathrm{tr}}}&g_{i}=1,\\ \frac{1-p}{1-p_{\mathrm{tr}}}&g_{i}=0.\end{cases}$ (7) $$

Here, $\bm{X}\in\mathbb{R}^{n\times(d+1)}$ is the design matrix of input features and $\bm{y}\in\mathbb{R}^{n}$ contains the target variables. The weights $w_{i}$ are parameterized by $p\in[0,1].$ If we choose the weights to be the likelihood ratio between the test and training distribution, the WLS estimator asymptotically minimizes the expected quadratic loss with respect to the test distribution. This choice of the weights corresponds to $p=p_{\mathrm{te}}.$

For large but finite $n$, the approximate expected loss is given by (see Appendix B for details)

$$ $\mathbb{E}_{D_{\mathrm{tr}}^{n}\sim\mathcal{P}_{\mathrm{tr}}}\!\left[\mathbb{E }_{(y,\bm{x})\sim\mathcal{P}_{\mathrm{te}}}\left[(y-\bm{x}^{\prime}\hat{\bm{ \beta}}_{n}(\bm{p}))^{2}\middle|D_{\mathrm{tr}}^{n}\right]\right]\approx \mathcal{B}^{2}(p_{\mathrm{te}},p,a_{1},a_{0})+\mathcal{V}(\sigma^{2},p,p_{ \mathrm{tr}},n,d)+\sigma^{2},$ (8a) where the result is decomposed in the following bias and variance term, respectively, $\displaystyle\mathcal{B}^{2}(p_{\mathrm{te}},p,a_{1},a_{0})$ $\displaystyle=\left[p_{\mathrm{te}}(1-p)^{2}+(1-p_{\mathrm{te}})p^{2}\right](a _{1}-a_{0})^{2},$ (8b) $\displaystyle\mathcal{V}(\sigma^{2},p,p_{\mathrm{tr}},n,d)$ $\displaystyle=\sigma^{2}\left[\frac{p^{2}}{p_{\mathrm{tr}}}+\frac{(1-p)^{2}}{1 -p_{\mathrm{tr}}}\right]\frac{d+1}{n}.$ (8c) $$

Both terms are quadratic with respect to $p.$ The bias term is minimized for $p=p_{\mathrm{te}},$ provided that $a_{0}\neq a_{1},$ whereas the variance is minimized for $p=p_{\mathrm{tr}}.$ The minimum of the expected loss is at

$$ $\displaystyle p^{*}_{n}=\frac{p_{\mathrm{te}}+\eta\,p_{\mathrm{tr}}}{1+\eta}, \quad\text{where }\eta=\frac{\sigma^{2}(d+1)}{n(a_{1}-a_{0})^{2}p_{\mathrm{tr} }(1-p_{\mathrm{tr}})}.$ (9) $$

This is the optimal trade-off between minimizing the bias and variance. In the limit of $n\rightarrow\infty$ the variance disappears and the optimal $p$ is indeed equal to the heuristic choice $p_{\mathrm{te}}.$ However, for finite $n$ it can differ considerably. This is illustrated in Figure 1. Its impact on the expected loss is shown in Figure 5 in Appendix B.

Figure: (a) For $p_{\mathrm{tr}}=0.9$ and varying $d.$
Refer to caption: extracted/5936978/plots/section_5/optimal_p_vs_n_varying_d.png

## 5 Optimizing weights of existing importance weighting techniques for sub-population shifts

Before continuing, we note that the setup described in Section 3, when applied to sub-population shifts, largely corresponds to GW-ERM. However, as emphasized in Section 1, the methodology proposed in this paper also applies to other importance weighting methods. They differ from GW-ERM in terms of, e.g., the use of a weighted sampler instead of a weighted loss (SUBG, DFR), the overall objective (GDRO) or the inference of groups (JTT). For all these cases, optimal weights can be defined and subsequently found through a bi-level optimization problem. In what follows, we discuss the details of optimizing weights for the different existing importance weighting techniques.

### 5.1 Group-Weighted Empirical Risk Minimization

The optimal group weights $\bm{p}^{*}_{n}$ are estimated by solving a bi-level optimization problem that for GW-ERM consists of parameter estimation as in Equation 1, with the difference that the estimates are a function of $\bm{p}$ instead of $\bm{w},$ and group weights optimization

$$ $\hat{\bm{p}}^{*}_{n}=\operatorname*{arg\,min}_{\bm{p}\in\mathcal{P}_{G}} \mathcal{L}_{\mathrm{val}}(\hat{\bm{\theta}}_{n^{\prime}}(\bm{p}),\bm{r}),$ (10) $$

where the likelihood ratios $\bm{r}$ are given by $r_{i}=\frac{p_{\mathrm{te}}(g_{i})}{p_{\mathrm{tr}}(g_{i})}.$ Also note that the group weights are restricted to the $G$-dimensional probability simplex $\mathcal{P}_{G}.$ This normalization conveniently prevents modifying the effective learning rate of the optimization procedure (Ren et al., ).

We solve the bi-level optimization problem iteratively. We find $\hat{\bm{p}}^{*}_{n}$ in Equation 10 by means of exponentiated gradient descent (Nemirovsky & Yudin, ; Kivinen & Warmuth, ), where the parameter estimates $\hat{\bm{\theta}}_{n^{\prime}}(\bm{p})$ are kept fixed. After each weight update, we re-calibrate the parameter estimates of Equation 1. Although convergence to the optimal group weights is not guaranteed due to potential non-convexity, gradient-based optimization of hyperparameters has previously been applied successfully (Pedregosa, ). Details of the optimization procedure can be found in Algorithm 1.

A step in this algorithm that requires extra attention is the computation of the gradient of the validation loss with respect to the group weights, commonly referred to as the hyper-gradient. For this, we use the chain rule

$$ $\nabla_{\bm{p}}\mathcal{L}_{\mathrm{val}}(\hat{\bm{\theta}}_{n^{\prime}}(\bm{p }),\bm{r})=\nabla_{\hat{\bm{\theta}}_{n^{\prime}}(\bm{p})}\mathcal{L}_{\mathrm {val}}(\hat{\bm{\theta}}_{n^{\prime}}(\bm{p}),\bm{r})\cdot\nabla_{\bm{p}}\hat{ \bm{\theta}}_{n^{\prime}}(\bm{p})$ $$

and note that the first factor can typically be computed in a straightforward manner. For the gradient of the parameters with respect to the weights, we use the implicit function theorem. The idea of using the implicit function theorem for gradient-based optimization of hyperparameters was originally introduced by Bengio (). For the details of calculating the hyper-gradient, see Appendix A.

Figure: Algorithm 1 Estimation of the optimal weights $\hat{\bm{p}}^{*}_{n}$ for GW-ERM

### 5.2 Subsampling Large Groups

In the case of SUBG (Sagawa et al., ), the weighted loss in Equation 1 is replaced by a training loss on a subset of the data created by subsampling without replacement. Let $\bm{s}=\{s^{{g}}_{{i}}|g\in\mathcal{G},\hskip 2.84544pti=1,...,n_{g}\},$ where $s^{{g}}_{{i}}\in\{0,1\}$ is a random variable denoting whether data point $i$ from group $g$ is included in the sample. The weights $v_{g}\in[0,1]$ represent the fraction of data points to be subsampled, such that a total of $\lceil v_{g}n_{g}\rceil$ observations of group $g$ is drawn randomly and included in the training loss

$$ $\mathcal{L}_{\mathrm{train}}^{\text{SUBG},n}(\bm{\theta},\bm{v}):=\frac{1}{m} \sum_{g\in\mathcal{G}}\sum^{n_{g}}_{i=1}s^{{g}}_{{i}}\,\mathcal{L}(y^{{g}}_{{i }},f_{\theta}(\bm{x}^{{g}}_{{i}})),$ (11) $$

where $y^{{g}}_{{i}},\bm{x}^{{g}}_{{i}}$ are observations belonging to group $g;$ $m=\sum_{g\in\mathcal{G}}\lceil v_{g}n_{g}\rceil$ and $\bm{v}=(v_{1},v_{2},\ldots,v_{G})^{\prime}.$ This training loss is not differentiable with respect to $\bm{v},$ which poses a problem for the bi-level optimization procedure. This is resolved by using parameter estimates obtained by averaging over the random subsampling process,

$$ $\hat{\bm{\theta}}^{\text{SUBG}}_{n}(\bm{v}):=\operatorname*{arg\,min}_{\bm{ \theta}\in\Theta}\mathbb{E}_{\bm{s}}\left[\mathcal{L}_{\mathrm{train}}^{\text{ SUBG},n}(\bm{\theta},\bm{v})\right]\approx\operatorname*{arg\,min}_{\bm{\theta }\in\Theta}\frac{1}{m}\sum_{g\in\mathcal{G}}\sum^{n_{g}}_{i=1}v_{g}\,\mathcal{ L}(y^{{g}}_{{i}},f_{\theta}(\bm{x}^{{g}}_{{i}})),$ (12) $$

where the approximation $\lceil v_{g}n_{g}\rceil/n_{g}\approx v_{g}$ was used. The optimized weights are still estimated with a weighted validation loss, $\hat{\bm{v}}^{*}_{n}=\operatorname*{arg\,min}_{\bm{v}\in\mathcal{V}_{G}}
\mathcal{L}_{\mathrm{val}}(\hat{\bm{\theta}}^{\text{SUBG}}_{n^{\prime}}(\bm{v}
),\bm{r}),$ where $\mathcal{V}_{G}=\left\{\bm{v}\in[0,1]^{G}\,|\,\exists g\in\mathcal{G}:v_{g}=1
\right\}.$ Note that the weights $\bm{v}$ are not required to be normalized, but to avoid unnecessary loss of training data at least one of them must be equal to one. Apart from this and some minor differences in the computation of the hyper-gradient (see Appendix A), the bi-level optimization procedure for SUBG is similar to the one for GW-ERM as described in Algorithm 1.

A closely related method is DFR Kirichenko et al. (), in which an ensemble of models trained on subsampled groups is used to estimate the model parameters by averaging Equation 12. Typically, this is done on a validation set. Per model, the hyper-gradient is the same as for SUBG.

### 5.3 Group Distributional Robust Optimization

Instead of minimizing the overall expected loss with respect to a test distribution, GDRO aims to minimize the largest expected loss per group. This means that there is no separate test distribution. The associated training procedure minimizes a weighted sum of expected losses per group, where the loss weights $\bm{q}=(q_{1},q_{2},\ldots,q_{G})^{\prime}$ are updated during training and larger weights are given to groups with a larger expected loss (Oren et al., ; Sagawa et al., ). We apply the framework of optimized importance weighting to the GDRO objective. As in Section 5.1, we work with group weights $\bm{p}.$ The model parameters are estimated via a weighted loss as in Equation 1, whereas the optimal weights are found by minimizing

$$ $\bm{p}^{*}_{n}=\operatorname*{arg\,min}_{\bm{p}\in\mathcal{P}_{G}}\mathbb{E}_{ D_{\mathrm{tr}}^{n}\sim\mathcal{P}_{\mathrm{tr}}}\left[\sup_{\bm{q}\in\mathcal {P}_{G}}\sum^{G}_{g=1}q_{g}\,\mathbb{E}_{(y,\bm{x}|g)\sim\mathcal{P}_{\mathrm{ tr}}}\left[\mathcal{L}(y,f_{\hat{\bm{\theta}}_{n}(\bm{p})}(\bm{x}))\,\middle| \,D_{\mathrm{tr}}\right]\right],$ (13) $$

where (recall that) $\mathcal{P}_{G}$ is the $G$-dimensional probability simplex. The inner expectation of Equation 13 is estimated with a validation set that is withheld from the data used to train the model parameters. The bi-level optimization procedure now consists of updating three quantities per update step: the model parameters $\bm{\theta},$ the group weights $\bm{p}$ and the loss weights $\bm{q}.$ See Algorithm 2 in Appendix D for details.

In the standard GDRO setting, the coefficients of the logistic regression are trained to minimize worst-group loss. This makes GDRO prone to overfitting (Sagawa et al., ). We, instead, optimize the weights to minimize worst-group loss, whereas the model parameters are trained via Equation 1. In typical cases, because of the (much) lower dimension of the group weights, overfitting is less likely to be a problem, giving optimized weights an edge over standard GDRO.

### 5.4 Group inference methods

Acquiring group labels is often costly. Several methods (see Section 2) resolve this by estimating group membership. Methods in this realm, such as Just Train Twice (Liu et al., ), require a validation set with the ground-truth group labels to tune hyperparameters. A key hyperparameter of JTT is the extent to which misclassified observations should be upweighted, which is optimized via a grid search on a validation set. We replace this single upweighting factor by multiple group weights, leading to higher flexibility. Furthermore, we optimize them via gradient descent instead of grid search. Like JTT, we use estimated group labels instead of ground-truth group labels, and aim to find weights that minimize the worst-group loss.

## 6 Experiments

The goal of this section is to verify empirically that existing state-of-the-art methods for addressing sub-population shifts benefit from using optimized group weights. We use benchmark classification datasets for studying sub-population shifts: two from computer vision (Waterbirds, CelebA) and one from natural language processing (MultiNLI). Groups are defined via a combination of the target label and a spurious attribute. The training and validation set are randomly split. For Waterbirds, this is different from previous implementations, where the validation set is balanced in terms of the groups (Sagawa et al., ). We deviate from this, since it is more realistic that the training and validation sets are drawn from the same distribution. The distribution shift is established by measuring the generalization performance on a test set for which all groups have an equal weight. For details on the datasets, including group sizes, see Appendix E.1.

We start by finetuning a pre-trained DNN on the respective datasets, a ResNet50 model (He et al., ) for Waterbirds and CelebA, and a BERT model (Devlin et al., ) for MultiNLI. For five existing importance weighting methods that address sub-population shifts (GW-ERM, SUBG, DFR, GDRO, JTT), we then retrain the last layer of the DNN, both with the standard choice of weights prescribed by the method and the optimized weights as proposed in this paper. We focus on last-layer retraining, which amounts to training a logistic regression on the last-layer embeddings of the finetuned DNN, because of computational feasibility and because it has been shown to be sufficient for addressing sub-population shifts (Izmailov et al., ; Kirichenko et al., ). For each importance weighting method with standard weights choice, we use a grid search to optimize the respective hyperparameters, including an L1 penalty for sufficient regularization. To get a fair comparison, we use the same hyperparameters for optimized weights. This possibly gives an advantage to the standard choice of weights.

We repeat the above procedure, starting from a pre-trained DNN, five times for each dataset, importance weighting method and weights choice approach (standard vs. optimized). For each run, we use the test set to compute two metrics for generalization performance: weighted average accuracy, which is the equal-weight average of the accuracy per group, and worst-group accuracy. We compare the difference in generalization performance between the standard weights choice and optimized weights via a one-sided $t$-test of paired samples. See Appendix E for details.

Improving existing importance weighting methods: Figure 2 compares the expected generalization performance of the standard weights choice and optimized weights. For a correct interpretation of the results, recall that for GW-ERM, SUBG and DFR, the objective of optimized weights is the weighted average accuracy, whereas for GDRO and JTT this is the worst-group accuracy. We observe that our approach consistently improves on the objective for which the weights are optimized, and is at least as good as standard weights with the exception of JTT on CelebA. Out of the 15 cases, we observe 12 times an increase that is statistically significant at the 10% level, and 8 times at the 5% level. Regarding the size of the improvements, in 5 cases there is an increase greater than 1%, with a maximum of 5.61%. To explain the case of JTT on CelebA, we note that even on the validation set optimizing weights did not improve the worst-group accuracy. This indicates that in this case our bi-level optimization procedure was unable to find better weights than the grid search of JTT.

Figure: Figure 2: Estimated generalization performance (averaged over 5 runs) of standard weights choice (squares) versus optimized weights (triangles). Error bars reflect the paired-sample 90% confidence interval of the difference.
Refer to caption: extracted/5936978/plots/section6/main_plot.png

Furthermore, we find that optimizing weights often also improves the metric for which the weights were not optimized (worst-group accuracy for GW-ERM, SUBG and DFR; weighted average accuracy for GDRO and JTT). The only exception is SUBG on CelebA. In 10 cases, there is an improvement greater than 1%, with a maximum of 4.03%. This result was not necessarily expected, since weighted average accuracy and group robustness are often opposed objectives (Agarwal et al., ; Martinez et al., ). See appendix C for numerical details of the results.

Role of the dataset size: it can be observed in Figure 2 that the improvement due to optimized weights is less pronounced for CelebA and MultiNLI than for Waterbirds. One possible explanation is that the Waterbirds dataset is much smaller, leading to (very) small minority groups (see Appendix E.1). We have argued in Sections 3 and 4 that it is in particular this situation where the effect of optimized weights is expected to be large.

We test this for GW-ERM on CelebA in Figure 3, where we show the effect of optimized weights using only fractions of the original training and validation set (while keeping the ratio of the sizes the same). The improvement due to optimizing weights peaks when around 10% of the original data is used. Note that for smaller fractions of the data (5%) the effect decreases again. This is likely due to the validation set becoming (very) small, increasing the chance of overfitting on this data.

Figure: Figure 3: Estimated generalization performance (averaged over 5 runs) of standard weights choice (squares) versus optimized weights (triangles) for GW-ERM as a function of the sizes of the training and validation set. Error bars reflect the paired-sample 90% confidence interval of the difference.
Refer to caption: extracted/5936978/plots/section6/n_fraction_CelebA.png

Robustness to other hyperparameters: an additional benefit of optimizing weights is an improved robustness to the choice of the L1 penalty. We illustrate this in Figure 4 for GW-ERM and the Waterbirds dataset. Both generalization metrics vary less with optimized weights. We believe this is because optimized weights improve on the bias-variance trade-off, thereby compensating for suboptimal regularization choices in the hyperparameters.

Figure: Figure 4: Estimated generalization performance (averaged over 5 runs) of standard weights choice (squares) versus optimized weights (triangles) for GW-ERM as a function of the L1 regularization parameter. Error bars reflect the paired-sample 90% confidence interval of the difference.
Refer to caption: extracted/5936978/plots/section6/penalty_strength.png

## 7 Conclusion and Discussion

This paper has shown that the standard choice of weights for importance weighting in the presence of sub-population shifts is often not optimal, due to the finite sample size of the training data. We have provided an estimation procedure for the optimal weights, which generally improves existing importance weighting methods in the case of last-layer retraining of DNNs.

Although last-layer retraining is known to be sufficient for addressing sub-population shifts (Izmailov et al., ; Kirichenko et al., ), it might be that retraining the whole DNN is the preferred option. In that case computational costs can become an issue, as calculating the hyper-gradient requires refitting the model on the training data and computing the inverted Hessian of the training loss with respect to the model parameters. To alleviate this, one might resort to several existing approaches to reduce computational costs for refitting the model (Maclaurin et al., ) or calculating the inverted Hessian (Lorraine et al., ).

The optimization properties of our method are another potential topic for future research. Although, with the exception of JTT on CelebA in Figure 2, the bi-level optimization procedure seems to converge well, formal convergence guarantees or the conditions under which the optimization in Equation 10 is convex were not investigated here.

The application to more challenging data-generating processes and models could lead to additional benefits of our approach.
One example is the case in which the test distribution of interest is unknown (Sugiyama et al., ). Another example is concept bottleneck models, which are known to suffer from spurious correlations (Margeloiu et al., ; Heidemann et al., ). The existence of many concepts, and thus groups, leads to a severe reduction in the effective sample size. This is problematic for existing importance weighting techniques and gives our approach of optimizing weights the potential to lead to big improvements. We leave this for future work.

## Appendix A Calculation of the hyper-gradient

The goal of this section is to provide further details on the calculation of the hyper-gradient, which is the gradient of the validation loss with respect to our hyperparameter $\bm{p},$ introduced in Section 5.1. This section should be read in conjunction with Section 5. Recall that the chain rule gives

$$ $\displaystyle\frac{\partial\mathcal{L}_{\mathrm{val}}(\hat{\bm{\theta}}_{n^{ \prime}}(\bm{p}),\bm{r})}{\partial\bm{p}}=\frac{\partial\mathcal{L}_{\mathrm{ val}}(\hat{\bm{\theta}}_{n^{\prime}}(\bm{p}),\bm{r})}{\partial\hat{\bm{\theta} }_{n^{\prime}}(\bm{p})}\frac{\partial\hat{\bm{\theta}}_{n^{\prime}}(\bm{p})}{ \partial\bm{p}}.$ (14) $$

Suppose we have a function $h(\bm{\theta},\bm{p})$ which is twice differentiable, which will play the role of an (expected) training loss. Assume that the gradient of $h(\bm{\theta},\bm{p})$ with respect to $\bm{\theta}$ is zero at the point $\hat{\bm{\theta}}_{n^{\prime}}(\bm{p})$. We can then apply the implicit function theorem to this gradient at the point $\bm{\theta}=\hat{\bm{\theta}}_{n^{\prime}}(\bm{p}),$ and write

$$ $\displaystyle\frac{\partial\hat{\bm{\theta}}_{n^{\prime}}(\bm{p})}{\partial\bm {p}}=-\left[\left.\frac{\partial^{2}h(\bm{\theta},\bm{p})}{\partial\bm{\theta} \partial\bm{\theta}^{T}}\right|_{\bm{\theta}=\hat{\bm{\theta}}_{n^{\prime}}( \bm{p})}\right]^{-1}\left.\frac{\partial^{2}h(\bm{\theta},\bm{p})}{\partial\bm {\theta}\partial\bm{p}^{T}}\right|_{\bm{\theta}=\hat{\bm{\theta}}_{n^{\prime}} (\bm{p})}.$ (15) $$

The gradient in Equation 15 depends on the form of $h$, and thus the estimation procedure of the parameters. Here, we discuss two such estimation procedures: (1) a group-weighted loss, and (2) subsampling per group without replacement.

Group-weighted loss: for simplicity of notation, assume that the group label $g$ takes values $1,2,\ldots,G.$ The group-weighted training loss is defined as

$$ $h(\bm{\theta},\bm{p})=\frac{1}{n}\left(\sum_{g=1}^{G}\frac{p_{g}}{p_{\mathrm{ tr},g}}\sum^{n_{g}}_{i=1}\mathcal{L}(y^{{g}}_{{i}},f_{\theta}(\bm{x}^{{g}}_{{i }}))\right).$ (16) $$

The partial derivative of $h(\bm{\theta},\bm{p})$ with respect to $p_{g}$ becomes a novel (weighted) loss function,

$$ $\frac{\partial h(\bm{\theta},\bm{p})}{\partial p_{g}}=\frac{1}{n}\left(\frac{1 }{p_{\mathrm{tr},g}}\sum^{n_{g}}_{i=1}\mathcal{L}(y^{{g}}_{{i}},f_{\theta}(\bm {x}^{{g}}_{{i}}))-\frac{1}{(1-\sum^{G-1}_{g^{\prime}=1}p_{\mathrm{tr},g^{ \prime}})}\sum^{n_{G}}_{i=1}\mathcal{L}(y^{{G}}_{{i}},f_{\theta}(\bm{x}^{{G}}_ {{i}}))\right),$ (17) $$

where we used that $p_{G}=1-\sum_{g^{\prime}=1}^{G}p_{g^{\prime}}$ due to normalization. Then, in order to determine $\left.\frac{\partial^{2}h(\bm{\theta},\bm{p})}{\partial\bm{\theta}\partial\bm{
p}^{T}}\right|_{\bm{\theta}=\hat{\bm{\theta}}_{n^{\prime}}(\bm{p})}$, we can calculate the derivative with respect to the estimated parameters for the novel loss function in Equation 17 for each $p_{g}$.

Subsampling large groups: here, we are interested in the hyper-gradient for the hyperparameter $\bm{v}$ instead of $\bm{p}$, as defined in
Section 5.2. The parameters $\hat{\bm{\theta}}_{n^{\prime}}(\bm{v})$ are not determined via a weighted loss, but rather by minimizing the training loss on a subsample of the data. However, the expected training loss can be rewritten as a weighted loss. To mimic the procedure of subsampling without replacement, we say that for each $g=1,\ldots,G$ the random variables $s^{{g}}_{{i}}$ are identically distributed $\mathrm{Bern}(v_{g})$ random variable with constraint

$$ $\displaystyle\sum^{n_{g}}_{i=1}s^{{g}}_{{i}}=m_{g},\hskip 2.84544pt\text{where }\hskip 2.84544ptm_{g}=\lceil v_{g}n_{g}\rceil.$ (18) $$

Recall from the main text that $0\leq v_{g}\leq 1$, the fraction of samples used from the training data in our subsample. The expected training loss is then

$$ $\displaystyle\mathbb{E}_{\bm{s}}\left[\mathcal{L}_{\mathrm{train}}^{\text{SUBG },n}(\bm{\theta},\bm{v})\right]$ $\displaystyle=\mathbb{E}_{\bm{s}}\left[\frac{1}{m}\sum^{G}_{g=1}\sum^{n_{g}}_{ i=1}s^{{g}}_{{i}}\mathcal{L}(y^{{g}}_{{i}},f_{\theta}(\bm{x}^{{g}}_{{i}}))\right]$ (19) $\displaystyle=\frac{1}{m}\sum^{G}_{g=1}\sum^{n_{g}}_{i=1}\mathcal{L}(y^{{g}}_{ {i}},f_{\theta}(\bm{x}^{{g}}_{{i}}))\frac{\lceil v_{g}n_{g}\rceil}{n_{g}}$ (20) $$

where $m=\sum^{G}_{g=1}m_{g}$ is the total number of samples in our subsample. By fixing the value of each $m_{g}$ beforehand, $m$ is not stochastic. As explained in Section 5.2, we approximate this expected loss via
$\tilde{h}(\bm{\theta},\bm{v})=\frac{1}{m}\sum_{g=1}^{G}\sum^{n_{g}}_{i=1}v_{g}
\,\mathcal{L}(y^{{g}}_{{i}},f_{\theta}(\bm{x}^{{g}}_{{i}}))$. The partial derivative of $\tilde{h}(\bm{\theta},\bm{v})$ with respect to $v_{g}$ again becomes a novel loss function

$$ $\displaystyle\frac{\partial\tilde{h}(\bm{\theta},\bm{v})}{\partial v_{g}}= \frac{1}{m}\sum^{G}_{g=1}\sum^{n_{g}}_{i=1}\mathcal{L}(y^{{g}}_{{i}},f_{\theta }(\bm{x}^{{g}}_{{i}})).$ (21) $$

Note the difference with Equation 17, because the $\bm{v}$ are not normalized. To determine $\left.\frac{\partial^{2}\tilde{h}(\bm{\theta},\bm{v})}{\partial\bm{\theta}
\partial\bm{v}^{T}}\right|_{\bm{\theta}=\hat{\bm{\theta}}_{n^{\prime}}(\bm{v})}$, we can (again) calculate the derivative with respect to the estimated parameters for the novel loss function in Equation 21 for each $v_{g}$. In addition to the change in the calculation of the hyper-gradient, the only change to Algorithm 1 is that we do not normalize the weights $\bm{v}$.

## Appendix B Theoretical analysis for linear regression

The goal of this section is to derive the approximate expected loss given the data-generating process described in Section 4.
It consists of four parts.

- •
In Section B.1 we give a description how $\mathbb{E}_{D_{\mathrm{tr}}^{n}\sim\mathcal{P}_{\mathrm{tr}}}\!\left[\mathbb{E
}_{(y,\bm{x})\sim\mathcal{P}_{\mathrm{te}}}[(y-\bm{x}^{T}\hat{\bm{\beta}}(\bm{
w}))^{2}|D_{\mathrm{tr}}]\right]$ can be decomposed into a bias and variance term in the case of linear regression, and data stemming from $G$ groups.
- •
In Section B.2 we provide the approximations of the OLS coefficients for large but finite $n$, given the data-generating process described in Section 4.
- •
In Section B.3, combining the results of the previous two sections, we derive the (approximate) bias-variance decomposition for our data-generating process.
- •
Finally, in Section B.4 we provide a numerical comparison between simulations and our approximation.

### B.1 The bias-variance tradeoff

For clarity, throughout the next three sections we will use the subscript $\mathrm{tr}$ to indicate if random variables are drawn from the training distribution, and the subscript $\mathrm{te}$ that they are drawn from the test distribution. We use the mean-squared error as the loss function, and use a linear regression where the coefficients depend on our weights.

$$ $\displaystyle\mathbb{E}_{D_{\mathrm{tr}}^{n}\sim\mathcal{P}_{\mathrm{tr}}}\! \left[\mathbb{E}_{y_{\mathrm{te}},\bm{x}_{\mathrm{te}}}[(y_{\mathrm{te}}-f_{ \hat{\bm{\theta}}_{n}(\bm{w})}(\bm{x}))^{2}|D_{\mathrm{tr}}]\right]$ $\displaystyle=\mathbb{E}_{D_{\mathrm{tr}}^{n}\sim\mathcal{P}_{\mathrm{tr}}}\! \left[\mathbb{E}_{y_{\mathrm{te}},\bm{x}_{\mathrm{te}}}[(y_{\mathrm{te}}-\bm{x }_{\mathrm{te}}^{T}\hat{\bm{\beta}}(\bm{w}))^{2}|D_{\mathrm{tr}}]\right].$ $$

The following only requires the assumptions stated in Section 4 regarding the independence of $\bm{x}_{\mathrm{te}}$ from $\epsilon_{\mathrm{te}}$. The expected mean squared error over a test distribution, conditional on the data coming from group $g$, can be written as

$$ $\displaystyle\mathbb{E}_{D_{\mathrm{tr}}^{n}\sim\mathcal{P}_{\mathrm{tr}}} \left[\mathbb{E}_{\bm{x}_{\mathrm{te}},y_{\mathrm{te}}}[(y_{\mathrm{te}}-\bm{x }_{\mathrm{te}}^{T}\hat{\bm{\beta}}(\bm{w}))^{2}|g,D_{\mathrm{tr}}]\right]$ $\displaystyle=\mathbb{E}_{D_{\mathrm{tr}}^{n}\sim\mathcal{P}_{\mathrm{tr}}} \left[\mathbb{E}_{\bm{x}_{\mathrm{te}},y_{\mathrm{te}}}[y_{\mathrm{te}}^{2}-2y _{\mathrm{te}}\bm{x}_{\mathrm{te}}^{T}\hat{\bm{\beta}}(\bm{w})+(\bm{x}_{ \mathrm{te}}^{T}\hat{\bm{\beta}}(\bm{w}))^{2}|g,D_{\mathrm{tr}}]\right]$ $\displaystyle=\mathbb{E}_{y_{\mathrm{te}}}[y_{\mathrm{te}}^{2}|g]-2\mathbb{E}_ {D_{\mathrm{tr}}^{n}\sim\mathcal{P}_{\mathrm{tr}}}\left[\mathbb{E}_{\bm{x}_{ \mathrm{te}},y_{\mathrm{te}}}\left[y_{\mathrm{te}}\bm{x}_{\mathrm{te}}^{T}\hat {\bm{\beta}}(\bm{w})|g,D_{\mathrm{tr}}\right]\right]$ $\displaystyle+\mathbb{E}_{D_{\mathrm{tr}}^{n}\sim\mathcal{P}_{\mathrm{tr}}} \left[\mathbb{E}_{p(\bm{x}_{\mathrm{te}})}\left[(\bm{x}_{\mathrm{te}}^{T}\hat{ \bm{\beta}}(\bm{w}))^{2}|g,D_{\mathrm{tr}}\right]\right].$ $$

For each group $g$, the following holds:

$$ $\displaystyle\mathbb{E}_{y_{\mathrm{te}}}[y_{\mathrm{te}}^{2}|g]=\mathbb{E}_{ \bm{x}_{\mathrm{te}}}[(\bm{x}_{\mathrm{te}}^{T}\bm{\beta}^{g}+\epsilon_{ \mathrm{te}})^{2}|g]=\mathbb{E}_{\bm{x}_{\mathrm{te}}}[(\bm{x}_{\mathrm{te}}^{ T}\bm{\beta}^{g})^{2}|g]+\sigma^{2},$ $\displaystyle\mathbb{E}_{D_{\mathrm{tr}}^{n}\sim\mathcal{P}_{\mathrm{tr}}} \left[\mathbb{E}_{\bm{x}_{\mathrm{te}},y_{\mathrm{te}}}\left[y\bm{x}_{\mathrm{ te}}^{T}\hat{\bm{\beta}}(\bm{w})|g,D_{\mathrm{tr}}\right]\right]$ $\displaystyle=\mathbb{E}_{D_{\mathrm{tr}}^{n}\sim\mathcal{P}_{\mathrm{tr}}} \left[\mathbb{E}_{\bm{x}_{\mathrm{te}},y_{\mathrm{te}}}\left[(\bm{x}_{\mathrm{ te}}^{T}\bm{\beta}^{g}+\epsilon_{\mathrm{te}})\bm{x}_{\mathrm{te}}^{T}\hat{\bm {\beta}}(\bm{w})|g,D_{\mathrm{tr}}\right]\right]$ $\displaystyle=\mathbb{E}_{\bm{x}_{\mathrm{te}}}[\bm{x}_{\mathrm{te}}^{T}\bm{ \beta}^{g}|g]\mathbb{E}_{D_{\mathrm{tr}}^{n}\sim\mathcal{P}_{\mathrm{tr}}} \left[\mathbb{E}_{\bm{x}_{\mathrm{te}}}\left[\bm{x}_{\mathrm{te}}^{T}\hat{\bm{ \beta}}(\bm{w})|g,D_{\mathrm{tr}}\right]\right],$ $\displaystyle\mathbb{E}_{D_{\mathrm{tr}}^{n}\sim\mathcal{P}_{\mathrm{tr}}} \left[\mathbb{E}_{\bm{x}_{\mathrm{te}},y_{\mathrm{te}}}\left[(\bm{x}_{\mathrm{ te}}^{T}\hat{\bm{\beta}}(\bm{w}))^{2}|g,D_{\mathrm{tr}}\right]\right]$ $\displaystyle=\mathbb{E}_{D_{\mathrm{tr}}^{n}\sim\mathcal{P}_{\mathrm{tr}}} \left[\mathrm{Var}\left(\bm{x}_{\mathrm{te}}^{T}\hat{\bm{\beta}}(\bm{w})|g,D_{ \mathrm{tr}}\right)+\mathbb{E}_{\bm{x}_{\mathrm{te}}}\left[\bm{x}_{\mathrm{te} }^{T}\hat{\bm{\beta}}(\bm{w})|g,D_{\mathrm{tr}}\right]^{2}|D_{\mathrm{tr}} \right].$ $$

We can then write the expected loss conditional on $g$ as:

$$ $\displaystyle\mathbb{E}_{D_{\mathrm{tr}}^{n}\sim\mathcal{P}_{\mathrm{tr}}} \left[\mathbb{E}_{\bm{x}_{\mathrm{te}},y_{\mathrm{te}}}[(y_{\mathrm{te}}-\bm{x }_{\mathrm{te}}^{T}\hat{\bm{\beta}}(\bm{w}))^{2}|g,D_{\mathrm{tr}}]\right]$ $\displaystyle=\mathbb{E}_{\bm{x}_{\mathrm{te}}}[(\bm{x}_{\mathrm{te}}^{T}\bm{ \beta}^{g})^{2}|g]-2\mathbb{E}_{\bm{x}_{\mathrm{te}}}[\bm{x}_{\mathrm{te}}^{T} \bm{\beta}^{g}|g]\mathbb{E}_{D_{\mathrm{tr}}^{n}\sim\mathcal{P}_{\mathrm{tr}}} \left[\mathbb{E}_{\bm{x}_{\mathrm{te}}}\left[\bm{x}_{\mathrm{te}}^{T}\hat{\bm{ \beta}}(\bm{w})|g,D_{\mathrm{tr}}\right]\right]$ $\displaystyle+\mathbb{E}_{D_{\mathrm{tr}}^{n}\sim\mathcal{P}_{\mathrm{tr}}} \left[\mathbb{E}_{\bm{x}_{\mathrm{te}}}\left[\bm{x}_{\mathrm{te}}^{T}\hat{\bm{ \beta}}(\bm{w})|g,D_{\mathrm{tr}}\right]^{2}|D_{\mathrm{tr}}\right]$ $\displaystyle+\mathbb{E}_{D_{\mathrm{tr}}^{n}\sim\mathcal{P}_{\mathrm{tr}}} \left[\mathrm{Var}\left(\bm{x}_{\mathrm{te}}^{T}\hat{\bm{\beta}}(\bm{w})|g,D_{ \mathrm{tr}}\right)\right|D_{\mathrm{tr}}]+\sigma^{2}.$ $$

Suppose we write this expected loss conditional on both $\bm{x}_{\mathrm{te}}$ and $g$.
Then, it can be written as:

$$ $\displaystyle\mathbb{E}_{D_{\mathrm{tr}}^{n}\sim\mathcal{P}_{\mathrm{tr}}} \left[\mathbb{E}_{y_{\mathrm{te}}}[(y_{\mathrm{te}}-\bm{x}_{\mathrm{te}}^{T} \hat{\bm{\beta}}(\bm{w}))^{2}|\bm{x}_{\mathrm{te}},g]\right]$ $\displaystyle=(\bm{x}_{\mathrm{te}}^{T}\bm{\beta}^{g}-\bm{x}_{\mathrm{te}}^{T} \mathbb{E}_{D_{\mathrm{tr}}^{n}\sim\mathcal{P}_{\mathrm{tr}}}\left[\hat{\bm{ \beta}}(\bm{w})\right])^{2}+\bm{x}_{\mathrm{te}}^{T}\mathbb{E}_{D_{\mathrm{tr} }^{n}\sim\mathcal{P}_{\mathrm{tr}}}\left[\mathrm{Var}(\hat{\bm{\beta}}(\bm{w}) |D_{\mathrm{tr}})\right]\bm{x}_{\mathrm{te}}+\sigma^{2}_{g}.$ $$

To further specify the expected loss, we will use the data-generating process specified in Section 4. Most notably, we will use that the $\bm{x}_{\mathrm{te}}$ does not change with the group $g$, that there are only two groups, and our definition of the coefficients per group.

By the law of total probability, the expected loss conditional on only $\bm{x}_{\mathrm{te}}$ can be written as a sum of of the expected losses conditional on $\bm{x}_{\mathrm{te}},g$.

$$ $\displaystyle\mathbb{E}_{D_{\mathrm{tr}}^{n}\sim\mathcal{P}_{\mathrm{tr}}} \left[\mathbb{E}_{y_{\mathrm{te}},g_{\mathrm{te}}}[(y_{\mathrm{te}}-\bm{x}_{ \mathrm{te}}^{T}\hat{\bm{\beta}}(\bm{w}))^{2}|\bm{x}_{\mathrm{te}}]\right]$ $\displaystyle=\sum^{G}_{g=1}p(g_{\mathrm{te}}=g)\mathbb{E}_{D_{\mathrm{tr}}^{n }\sim\mathcal{P}_{\mathrm{tr}}}\left[\mathbb{E}_{y_{\mathrm{te}}}[(y_{\mathrm{ te}}-\bm{x}_{\mathrm{te}}^{T}\hat{\bm{\beta}}(\bm{w}))^{2}|\bm{x}_{\mathrm{te} },g]\right]$ $\displaystyle=p_{\mathrm{te}}((\bm{x}_{\mathrm{te}}^{T}\bm{\beta}^{1}-\bm{x}_{ \mathrm{te}}^{T}\mathbb{E}_{D_{\mathrm{tr}}^{n}\sim\mathcal{P}_{\mathrm{tr}}}[ \hat{\bm{\beta}}(\bm{w})])^{2}+(1-p_{\mathrm{te}})((\bm{x}_{\mathrm{te}}^{T} \bm{\beta}^{0}-\bm{x}_{\mathrm{te}}^{T}\mathbb{E}_{D_{\mathrm{tr}}^{n}\sim \mathcal{P}_{\mathrm{tr}}}[\hat{\bm{\beta}}(\bm{w})])^{2}$ $\displaystyle+\bm{x}_{\mathrm{te}}^{T}\mathbb{E}_{D_{\mathrm{tr}}^{n}\sim \mathcal{P}_{\mathrm{tr}}}\left[\mathrm{Var}(\hat{\bm{\beta}}(\bm{w})|D_{ \mathrm{tr}})\right]\bm{x}_{\mathrm{te}}+\sigma^{2}.$ (22) $$

### B.2 The asymptotic bias and variance of the coefficients

The goal of this subsection is to define the expectation and variance of the WLS estimator as defined per equation 7.
Because we have fixed the amount of observations per group, we can state the following

$$ $\displaystyle\frac{n_{1}}{n}$ $\displaystyle=\frac{np_{\mathrm{tr}}}{n}=p_{\mathrm{tr}},\quad,\frac{n_{0}}{n} =\frac{n(1-p_{\mathrm{tr}})}{n}=(1-p_{\mathrm{tr}}),$ (23) $\displaystyle w_{1}\frac{n_{1}}{n}$ $\displaystyle=\frac{p}{p_{\mathrm{tr}}}p_{\mathrm{tr}}=p,\quad w_{0}\frac{n_{0 }}{n}=\frac{(1-p)}{(1-p_{\mathrm{tr}})}(1-p_{\mathrm{tr}})=(1-p).$ (24) $$

This means we can write

$$ $\displaystyle\bm{X}_{\mathrm{tr}}^{T}\bm{W}\bm{X}_{\mathrm{tr}}=w_{1}\bm{X}_{ \mathrm{tr},1}^{T}\bm{X}_{\mathrm{tr},1}+w_{0}\bm{X}_{\mathrm{tr},0}^{T}\bm{X} _{\mathrm{tr},0},\quad\bm{X}_{\mathrm{tr}}^{T}\bm{W}\bm{y}_{\mathrm{tr}}=w_{1} \bm{X}_{\mathrm{tr},1}^{T}\bm{y}_{1}+w_{0}\bm{X}_{\mathrm{tr},0}^{T}\bm{y}_{0},$ (25) $$

and similarly $\bm{X}_{\mathrm{tr}}^{T}\bm{W}\bm{W}\bm{X}_{\mathrm{tr}}=w_{1}^{2}\bm{X}_{
\mathrm{tr},1}^{T}\bm{X}_{\mathrm{tr},1}+w_{0}^{2}\bm{X}_{\mathrm{tr},0}^{T}
\bm{X}_{\mathrm{tr},0}$, where $\bm{X}_{\mathrm{tr},1}\in\mathbb{R}^{n_{1}\times d},\bm{X}_{\mathrm{tr},0}\in
\mathbb{R}^{n_{0}\times d}$ are matrices where for the observations holds that $g_{i}=1,g_{i}=0$ respectively, and similarly $\bm{y}_{1}\in\mathbb{R}^{n_{1}},\bm{y}_{0}\in\mathbb{R}^{n_{0}}$.

In the following analysis we will use that

$$ $\displaystyle\left(\frac{1}{n}(w_{1}\bm{X}_{\mathrm{tr},1}^{T}\bm{X}_{\mathrm{ tr},1}+w_{0}\bm{X}_{\mathrm{tr},0}^{T}\bm{X}_{\mathrm{tr},0})\right)^{-1}$ $\displaystyle=\left(\frac{1}{n}(w_{1}\frac{n_{1}}{n_{1}}\bm{X}_{\mathrm{tr},1} ^{T}\bm{X}_{\mathrm{tr},1}+w_{0}\frac{n_{0}}{n_{0}}\bm{X}_{\mathrm{tr},0}^{T} \bm{X}_{\mathrm{tr},0})\right)^{-1}$ $\displaystyle=\left(p\frac{1}{n_{1}}\bm{X}_{\mathrm{tr},1}^{T}\bm{X}_{\mathrm{ tr},1}+(1-p)\frac{1}{n_{0}}\bm{X}_{\mathrm{tr},0}^{T}\bm{X}_{\mathrm{tr},0} \right)^{-1}.$ (26) $$

And similarly,

$$ $\displaystyle\frac{1}{n}\left(w_{1}^{2}\bm{X}_{\mathrm{tr},1}^{T}\bm{X}_{ \mathrm{tr},1}+w_{0}^{2}\bm{X}_{\mathrm{tr},0}^{T}\bm{X}_{\mathrm{tr},0}\right)$ $\displaystyle=\frac{1}{n}\left(w_{1}^{2}\frac{n_{1}}{n_{1}}\bm{X}_{\mathrm{tr} ,1}^{T}\bm{X}_{\mathrm{tr},1}+w_{0}^{2}\frac{n_{0}}{n_{0}}\bm{X}_{\mathrm{tr}, 0}^{T}\bm{X}_{\mathrm{tr},0}\right)$ $\displaystyle=\left(\frac{p^{2}}{p_{\mathrm{tr}}}\frac{1}{n_{1}}\bm{X}_{ \mathrm{tr},1}^{T}\bm{X}_{\mathrm{tr},1}+\frac{(1-p)^{2}}{(1-p_{\mathrm{tr}})} \frac{1}{n_{0}}\bm{X}_{\mathrm{tr},0}^{T}\bm{X}_{\mathrm{tr},0}\right).$ (27) $$

Finally, we will use that

$$ $\displaystyle\frac{1}{n}(w_{1}\bm{X}_{\mathrm{tr},1}^{T}\bm{y}_{1}+w_{0}\bm{X} _{\mathrm{tr},0}^{T}\bm{y}_{0})$ $\displaystyle=\frac{1}{n}(w_{1}\frac{n_{1}}{n_{1}}\bm{X}_{\mathrm{tr},1}^{T} \bm{y}_{1}+w_{0}\frac{n_{0}}{n_{0}}\bm{X}_{\mathrm{tr},0}^{T}\bm{y}_{0})$ $\displaystyle=p\frac{1}{n_{1}}\bm{X}_{\mathrm{tr},1}^{T}\bm{y}_{1}+(1-p)\frac{ 1}{n_{0}}\bm{X}_{\mathrm{tr},0}^{T}\bm{y}_{0}.$ (28) $$

Using Equation 26 and Equation 28, the WLS estimator can be re-written as

$$ $\displaystyle(\bm{X}_{\mathrm{tr}}^{T}\bm{W}\bm{X}_{\mathrm{tr}})^{-1}\bm{X}_{ \mathrm{tr}}^{T}\bm{W}\bm{y}_{\mathrm{tr}}$ $\displaystyle=(\frac{1}{n}\bm{X}_{\mathrm{tr}}^{T}\bm{W}\bm{X}_{\mathrm{tr}})^ {-1}\frac{1}{n}\bm{X}_{\mathrm{tr}}^{T}\bm{W}\bm{y}_{\mathrm{tr}}$ $\displaystyle=\left(\frac{1}{n}\bm{X}_{\mathrm{tr}}^{T}\bm{W}\bm{X}_{\mathrm{ tr}}\right)^{-1}\left(p\frac{1}{n_{1}}\bm{X}_{\mathrm{tr},1}^{T}\bm{y}_{1}+(1- p)\frac{1}{n_{0}}\bm{X}_{\mathrm{tr},0}^{T}\bm{y}_{0}\right).$ $$

The expectation of this estimator, conditional on $\bm{g},\bm{X}$ becomes

$$ $\displaystyle\mathbb{E}[\hat{\bm{\beta}}(p)|\bm{g}_{\mathrm{tr}},\bm{X}_{ \mathrm{tr}}]$ $\displaystyle=\mathbb{E}\left[(\frac{1}{n}\bm{X}_{\mathrm{tr}}^{T}\bm{W}\bm{X} _{\mathrm{tr}})^{-1}\frac{1}{n}\bm{X}_{\mathrm{tr}}^{T}\bm{W}\bm{y}_{\mathrm{ tr}}|\bm{g}_{\mathrm{tr}},\bm{X}_{\mathrm{tr}}\right]$ $\displaystyle=\mathbb{E}\left[\left(\frac{1}{n}\bm{X}_{\mathrm{tr}}^{T}\bm{W} \bm{X}_{\mathrm{tr}}\right)^{-1}\left(p\frac{1}{n_{1}}\bm{X}_{\mathrm{tr},1}^{ T}\bm{y}_{1}+(1-p)\frac{1}{n_{0}}\bm{X}_{\mathrm{tr},0}^{T}\bm{y}_{0}\right)| \bm{g}_{\mathrm{tr}},\bm{X}_{\mathrm{tr}}\right]$ $\displaystyle=\left(\frac{1}{n}\bm{X}_{\mathrm{tr}}^{T}\bm{W}\bm{X}_{\mathrm{ tr}}\right)^{-1}\left(p\frac{1}{n_{1}}\bm{X}_{\mathrm{tr},1}^{T}\mathbb{E}[\bm {y}_{1}|\bm{X}]+(1-p)\frac{1}{n_{0}}\bm{X}_{\mathrm{tr},0}^{T}\mathbb{E}[\bm{y }_{0}|\bm{X}]\right)$ $\displaystyle=\left(\frac{1}{n}\bm{X}_{\mathrm{tr}}^{T}\bm{W}\bm{X}_{\mathrm{ tr}}\right)^{-1}\left(p\frac{1}{n_{1}}\bm{X}_{\mathrm{tr},1}^{T}\bm{X}_{ \mathrm{tr},1}\bm{\beta}^{1}+(1-p)\frac{1}{n_{0}}\bm{X}_{\mathrm{tr},0}^{T}\bm {X}_{\mathrm{tr},0}\bm{\beta}^{0}\right).$ $$

Given our DGP we can write the variance of $\bm{y}_{\mathrm{tr}}$ conditional on $\bm{g}_{\mathrm{tr}},\bm{X}_{\mathrm{tr}}$ as

$$ $\displaystyle\mathrm{Var}(\bm{y}_{\mathrm{tr}}|\bm{g}_{\mathrm{tr}},\bm{X}_{ \mathrm{tr}})$ $\displaystyle=\sigma^{2}\bm{I}.$ (29) $$

We can then define the variance of the WLS estimator conditional on $\bm{g},\bm{X}$:

$$ $\displaystyle\mathrm{Var}(\hat{\bm{\beta}}(p)|\bm{g}_{\mathrm{tr}},\bm{X}_{ \mathrm{tr}})$ $\displaystyle=\mathrm{Var}((\bm{X}_{\mathrm{tr}}^{T}\bm{W}\bm{X}_{\mathrm{tr}} )^{-1}\bm{X}_{\mathrm{tr}}^{T}\bm{W}\bm{y}_{\mathrm{tr}}|\bm{g}_{\mathrm{tr}}, \bm{X}_{\mathrm{tr}})$ $\displaystyle=(\bm{X}_{\mathrm{tr}}^{T}\bm{W}\bm{X}_{\mathrm{tr}})^{-1}\bm{X}_ {\mathrm{tr}}^{T}\bm{W}\mathrm{Var}(\bm{y}_{\mathrm{tr}}|\bm{g}_{\mathrm{tr}}, \bm{X}_{\mathrm{tr}})\bm{W}\bm{X}_{\mathrm{tr}}(\bm{X}_{\mathrm{tr}}^{T}\bm{W} \bm{X}_{\mathrm{tr}})^{-1}$ $\displaystyle=\sigma^{2}(\bm{X}_{\mathrm{tr}}^{T}\bm{W}\bm{X}_{\mathrm{tr}})^{ -1}\bm{X}_{\mathrm{tr}}^{T}\bm{W}\bm{W}\bm{X}_{\mathrm{tr}}(\bm{X}_{\mathrm{tr }}^{T}\bm{W}\bm{X}_{\mathrm{tr}})^{-1}$ (30) $$

Similar to the data-generating process in Section 4, we assume here that each $\bm{x}$ follows the same distribution regardless of the group. Given this, we can state $\mathbb{E}[\frac{1}{n_{1}}\bm{X}_{\mathrm{tr},1}^{T}\bm{X}_{\mathrm{tr},1}]=
\mathbb{E}[\frac{1}{n_{0}}\bm{X}_{\mathrm{tr},0}^{T}\bm{X}_{\mathrm{tr},0}]=
\bm{\Sigma}$, e.g. they have the same expectation.
Assuming $\bm{\Sigma}$ is a positive definite matrix with finite elements, $\frac{1}{n_{1}}\bm{X}_{1}^{T}\bm{X}_{1},\frac{1}{n_{0}}\bm{X}_{0}^{T}\bm{X}_{0}$ converge in probability to it, e.g.

$$ $\displaystyle\underset{n_{1}\rightarrow\infty}{\mathrm{plim}}\frac{1}{n_{1}} \bm{X}_{1}^{T}\bm{X}_{1}=\underset{n_{0}\rightarrow\infty}{\mathrm{plim}}\frac {1}{n_{0}}\bm{X}_{0}^{T}\bm{X}_{0}=\bm{\Sigma}.$ (31) $$

Since we assume each of our observations of $\bm{x}$ are identically and independently distributed (IID), we can write

$$ $\displaystyle\frac{1}{n_{1}}\bm{X}_{\mathrm{tr},1}^{T}\bm{X}_{\mathrm{tr},1}= \frac{1}{n_{0}}\bm{X}_{\mathrm{tr},1}^{T}\bm{X}_{\mathrm{tr},1}=\bm{\Sigma}+ \mathcal{O}_{p}(\frac{1}{\sqrt{n}}).$ (32) $$

See Equation 14.4-7 of Bishop et al. () for a general version of this result.
We can thus write

$$ $\displaystyle\left(p\frac{1}{n_{1}}\bm{X}_{\mathrm{tr},1}^{T}\bm{X}_{\mathrm{ tr},1}+(1-p)\frac{1}{n_{0}}\bm{X}_{\mathrm{tr},0}^{T}\bm{X}_{\mathrm{tr},0} \right)=p\bm{\Sigma}+(1-p)\bm{\Sigma}+\mathcal{O}_{p}(\frac{1}{\sqrt{n}})=\bm{ \Sigma}+\mathcal{O}_{p}(\frac{1}{\sqrt{n}}),$ $$

and also,

$$ $\displaystyle\left(p\frac{1}{n_{1}}\bm{X}_{\mathrm{tr},1}^{T}\bm{X}_{\mathrm{ tr},1}+(1-p)\frac{1}{n_{0}}\bm{X}_{\mathrm{tr},0}^{T}\bm{X}_{\mathrm{tr},0} \right)^{-1}$ $\displaystyle=\left(\bm{\Sigma}+\mathcal{O}_{p}(\frac{1}{\sqrt{n}})\right)^{-1}$ $\displaystyle=\bm{\Sigma}^{-1}-\bm{\Sigma}^{-1}\mathcal{O}_{p}(\frac{1}{\sqrt{ n}})\bm{\Sigma}^{-1}$ $\displaystyle=\bm{\Sigma}^{-1}-\mathcal{O}_{p}(\frac{1}{\sqrt{n}}).$ $$

The $\mathcal{O}_{p}(\frac{1}{\sqrt{n}})$ indicates $\frac{1}{n_{1}}\bm{X}_{\mathrm{tr},1}^{T}\bm{X}_{\mathrm{tr},1},\frac{1}{n_{0}
}\bm{X}_{\mathrm{tr},1}^{T}\bm{X}_{\mathrm{tr},1}$ are bounded in probability (see Vaart () Section 2.2). Thus, for a large $n$, we can approximate $\mathbb{E}\left[\left(\bm{X}_{\mathrm{tr}}^{T}\bm{W}\bm{X}_{\mathrm{tr}}\right
)^{-1}\right]$ using $\bm{\Sigma}^{-1}$.

We use the following approximation

$$ $\displaystyle\mathbb{E}_{D_{\mathrm{tr}}^{n}\sim\mathcal{P}_{\mathrm{tr}}}[ \hat{\bm{\beta}}(\bm{w})$ (33) $\displaystyle=\mathbb{E}_{\bm{g}_{\mathrm{tr}},\bm{X}_{\mathrm{tr}}}\left[ \mathbb{E}\left[\left(\bm{X}_{\mathrm{tr}}^{T}\bm{W}\bm{X}_{\mathrm{tr}}\right )^{-1}\left(p\frac{1}{n_{1}}\bm{X}_{\mathrm{tr},1}^{T}\bm{X}_{\mathrm{tr},1} \bm{\beta}^{1}+(1-p)\frac{1}{n_{0}}\bm{X}_{\mathrm{tr},0}^{T}\bm{X}_{\mathrm{ tr},0}\bm{\beta}^{0}\right)\right]\right]$ $\displaystyle=\bm{\Sigma}^{-1}(p\bm{\Sigma}\bm{\beta}^{1}+(1-p)\bm{\Sigma}\bm{ \beta}^{0})+\mathcal{O}_{p}(\frac{1}{\sqrt{n}})$ $\displaystyle\approx p\bm{\beta}^{1}+(1-p)\bm{\beta}^{0}.$ (34) $$

Similarly, we can define an approximation for the conditional variance. First, we define

$$ $\displaystyle\underset{n\rightarrow\infty}{\mathrm{plim}}\frac{1}{n}\bm{X}_{ \mathrm{tr}}^{T}\bm{W}\bm{W}\bm{X}_{\mathrm{tr}}=\left(\frac{p^{2}}{p_{\mathrm {tr}}}+\frac{(1-p)^{2}}{(1-p_{\mathrm{tr}})})\bm{\Sigma}\right).$ (35) $$

We can now state

$$ $\displaystyle\mathbb{E}_{D_{\mathrm{tr}}^{n}\sim\mathcal{P}_{\mathrm{tr}}} \left[\mathrm{Var}(\hat{\bm{\beta}}(\bm{w})|D_{\mathrm{tr}})\right]$ $\displaystyle=\mathbb{E}_{\bm{g}_{\mathrm{tr}},\bm{X}_{\mathrm{tr}}}\left[ \sigma^{2}(\bm{X}_{\mathrm{tr}}^{T}\bm{W}\bm{X}_{\mathrm{tr}})^{-1}\bm{X}_{ \mathrm{tr}}^{T}\bm{W}\bm{W}\bm{X}_{\mathrm{tr}}(\bm{X}_{\mathrm{tr}}^{T}\bm{W }\bm{X}_{\mathrm{tr}})^{-1}\right]$ $\displaystyle\approx\sigma^{2}\left(\frac{p^{2}}{p_{\mathrm{tr}}}+\frac{(1-p)^ {2}}{(1-p_{\mathrm{tr}})}\right)\bm{\Sigma}^{-1}.$ (36) $$

### B.3 The expected loss

If we substitute in our approximations derived in Equation 36, Equation 34 in Equation B.1, we get

$$ $\displaystyle\mathbb{E}_{D_{\mathrm{tr}}^{n}\sim\mathcal{P}_{\mathrm{tr}}} \left[\mathbb{E}_{p_{\mathrm{te}}(y)}[(y-\bm{x}_{\mathrm{te}}^{T}\hat{\bm{ \beta}}(\bm{w}))^{2}|\bm{x}_{\mathrm{te}}]\right]$ $\displaystyle\approx\left(\sum^{G}_{g=1}p_{\mathrm{te},g}((\bm{x}_{\mathrm{te} }^{T}\bm{\beta}^{g}-\bm{x}_{\mathrm{te}}^{T}(p\bm{\beta}^{1}+(1-p)\bm{\beta}^{ 0}))^{2}+\sigma^{2}_{g}\right)$ $\displaystyle+\bm{x}_{\mathrm{te}}^{T}\frac{1}{n}\sigma^{2}\left(\frac{p^{2}}{ p_{\mathrm{tr},1}}+\frac{(1-p)^{2}}{(1-p_{\mathrm{tr},1})}\right)\bm{\Sigma}^{ -1}\bm{x}_{\mathrm{te}}.$ $$

Now, we will define the expected loss unconditional of $\bm{x}_{\mathrm{te}}$. We will start to generally define the squared bias

$$ $\displaystyle\mathbb{E}[(\bm{x}_{\mathrm{te}}^{T}\bm{\beta}^{g}-\bm{x}_{ \mathrm{te}}^{T}\left(p\bm{\beta}^{1}+(1-p)\bm{\beta}^{0}\right)])^{2}]$ $\displaystyle=\mathbb{E}[(\bm{x}_{\mathrm{te}}^{T}\bm{\beta}_{d}^{g})^{2}],$ $$

where $\bm{\beta}_{d}^{g}$ is the difference between the coefficients in the data-generating process of group $g$ and estimated coefficients. Note that in general

$$ $\displaystyle(\bm{x}_{\mathrm{te}}^{T}\bm{\beta}_{d}^{g})^{2}$ $\displaystyle=(\sum^{p}_{j=1}x_{j}\beta_{d,j}^{g})(\sum^{p}_{j=1}x_{j}\beta_{d ,j}^{g})=\mathrm{Tr}(\bm{x}_{\mathrm{te}}\bm{x}_{\mathrm{te}}^{T}\bm{\beta}_{d }^{g}\bm{\beta}_{d}^{{g}^{T}}),$ $\displaystyle\mathbb{E}[(\bm{x}_{\mathrm{te}}^{T}\bm{\beta}_{d}^{g})^{2}]$ $\displaystyle=\mathrm{Tr}(\mathbb{E}[\bm{x}_{\mathrm{te}}\bm{x}_{\mathrm{te}}^ {T}]\mathbb{E}[\bm{\beta}_{d}^{g}\bm{\beta}_{d}^{{g}^{T}}]),$ (Since they are independent) $\displaystyle\mathbb{E}[\bm{x}_{\mathrm{te}}\bm{x}_{\mathrm{te}}^{T}]$ $\displaystyle=\begin{pmatrix}1&\bm{0}^{T}\\ \bm{0}&\bm{\Sigma}.\end{pmatrix}.$ $$

Now, the challenge is to define $\bm{\beta}_{d}^{g}$. To do so, we define the difference between the coefficients as $\bm{\beta}_{d}=(\bm{\beta}^{1}-\bm{\beta}^{0})$. We can then define

$$ $\displaystyle(p\bm{\beta}^{1}+(1-p)\bm{\beta}^{0})$ $\displaystyle=p(\bm{\beta}^{1}-\bm{\beta}^{0})+\bm{\beta}^{0},$ $\displaystyle\bm{\beta}_{d}^{(1)}$ $\displaystyle=\bm{\beta}^{1}-p(\bm{\beta}^{1}-\bm{\beta}^{0})-\bm{\beta}^{0}=( 1-p)(\bm{\beta}^{1}-\bm{\beta}^{0}),$ $\displaystyle\bm{\beta}_{d}^{(0)}$ $\displaystyle=\bm{\beta}^{0}-p(\bm{\beta}^{1}-\bm{\beta}^{0})-\bm{\beta}^{0}=- p(\bm{\beta}^{1}-\bm{\beta}^{0}),$ $\displaystyle\bm{\beta}_{d}^{(1)}\bm{\beta}_{d}^{{(1)}^{T}}$ $\displaystyle=(1-p)^{2}\bm{\beta}_{d}\bm{\beta}_{d}^{T}$ $\displaystyle\bm{\beta}_{d}^{(0)}\bm{\beta}_{d}^{{(0)}^{T}}$ $\displaystyle=p^{2}\bm{\beta}_{d}\bm{\beta}_{d}^{T}.$ $$

Because the difference in the coefficients is only in the intercepts, we can write

$$ $\displaystyle\mathbb{E}[(\bm{x}_{\mathrm{te}}^{T}\bm{\beta}_{d}^{(1)})^{2}]$ $\displaystyle=(1-p)^{2}(a_{1}-a_{0})^{2},$ $\displaystyle\mathbb{E}[(\bm{x}_{\mathrm{te}}^{T}\bm{\beta}_{d}^{(0)})^{2}]$ $\displaystyle=p^{2}(a_{1}-a_{0})^{2}.$ $$

Suppose that we define $\bm{\Sigma}^{-1}=\frac{1}{\gamma}\bm{I}$. Note our DGP as defined in Section 4 uses $\gamma=1$. We can now write

$$ $\displaystyle\mathbb{E}[\bm{x}_{\mathrm{te}}^{T}\frac{\sigma^{2}}{n}(\frac{p^{ 2}}{p_{\mathrm{tr}}}+\frac{(1-p)^{2}}{(1-p_{\mathrm{tr}})})\bm{\Sigma}^{-1}\bm {x}_{\mathrm{te}}]$ $\displaystyle=\sigma^{2}\left(\frac{p^{2}}{p_{\mathrm{tr}}}+\frac{(1-p)^{2}}{( 1-p_{\mathrm{tr}})}\right)\frac{1}{n}\mathbb{E}[\bm{x}_{\mathrm{te}}^{T}\begin {pmatrix}1&\bm{0}^{T}\\ \bm{0}&\frac{1}{\gamma}\bm{I}\end{pmatrix}\bm{x}_{\mathrm{te}}]$ $\displaystyle=\sigma^{2}\left(\frac{p^{2}}{p_{\mathrm{tr}}}+\frac{(1-p)^{2}}{( 1-p_{\mathrm{tr}})}\right)\frac{d+1}{n}.$ $$

Now that we have defined the bias and variance terms, we can combine these with the noise term $\sigma^{2}$ into the expected loss.

$$ $\displaystyle\mathcal{B}^{2}(p_{\mathrm{te}},p,a_{1},a_{0})$ $\displaystyle=p_{\mathrm{te}}(1-p)^{2}(a_{1}-a_{0})^{2}+(1-p_{\mathrm{te}})p^{ 2}(a_{1}-a_{0})^{2},$ $\displaystyle\mathcal{V}(\sigma^{2},p,p_{\mathrm{tr}},n,d)$ $\displaystyle=\sigma^{2}(\frac{p^{2}}{p_{\mathrm{tr}}}+\frac{(1-p)^{2}}{(1-p_{ \mathrm{tr}})})\frac{d+1}{n},$ $\displaystyle\mathbb{E}_{D_{\mathrm{tr}}^{n}\sim\mathcal{P}_{\mathrm{tr}}}\! \left[\mathbb{E}_{(y,\bm{x})\sim\mathcal{P}_{\mathrm{te}}}[(y-\bm{x}_{\mathrm{ te}}^{T}\hat{\bm{\beta}}(\bm{w}))^{2}|D_{\mathrm{tr}}]\right]$ $\displaystyle\approx\mathcal{B}^{2}(p_{\mathrm{te}},p,a_{1},a_{0})+\mathcal{V} (\sigma^{2},p,p_{\mathrm{tr}},n,d)+\sigma^{2}.$ (37) $$

We then take the derivative of $p$ with respect to the bias,

$$ $\displaystyle\frac{\partial\mathcal{B}^{2}(p_{\mathrm{te}},p,a_{1},a_{0})}{ \partial p}$ $\displaystyle=2(a_{1}-a_{0})^{2}(p-p_{\mathrm{te}}),$ $\displaystyle 2(a_{1}-a_{0})^{2}(p-p_{\mathrm{te}})$ $\displaystyle=0\implies p=p_{\mathrm{te}}.$ $$

In the case that $a_{1}\neq a_{0}$, the bias is uniquely minimized when $p=p_{\mathrm{te}}$. If we take the derivative of $p$ with respect to the variance, it is uniquely minimized at $p=p_{\mathrm{tr}}$ if $n(p_{\mathrm{tr}}-1)p_{\mathrm{tr}}\neq 0$.

$$ $\displaystyle\frac{\partial\mathcal{V}(\sigma^{2},p,p_{\mathrm{tr}},n,d)}{ \partial p}=-\frac{2(d+1)\sigma^{2}(p-p_{\mathrm{tr}})}{n(p_{\mathrm{tr}}-1)p_ {\mathrm{tr}}},$ (38) $\displaystyle n(p_{\mathrm{tr}}-1)p_{\mathrm{tr}}\neq 0,\frac{2(d+1)\sigma^{2} (p-p_{\mathrm{tr}})}{n(p_{\mathrm{tr}}-1)p_{\mathrm{tr}}}=0\implies p=p_{ \mathrm{tr}}.$ (39) $$

For the variance, if $n\rightarrow\infty$, $\mathcal{V}(\sigma^{2},p,p_{\mathrm{tr}},n,d)\rightarrow 0$. Hence, if our sample is large enough, the optimal $p$ will be $p$ for which the bias is minimal. The $p^{*}_{n}$ as per Equation 9 is derived by taking the derivative of the approximate expected loss with respect to $p$, and setting it to 0

$$ $\displaystyle n(p_{\mathrm{tr}}-1)p_{\mathrm{tr}}\neq 0,\quad 2(a_{1}-a_{0})^{ 2}(p-p_{\mathrm{te}})-\frac{2(d+1)\sigma^{2}(p-p_{\mathrm{tr}})}{n(p_{\mathrm{ tr}}-1)p_{\mathrm{tr}}}=0,$ (40) $\displaystyle\implies p=\frac{p_{\mathrm{te}}+\eta\,p_{\mathrm{tr}}}{1+\eta}, \quad\text{where }\eta=\frac{\sigma^{2}(d+1)}{n(a_{1}-a_{0})^{2}p_{\mathrm{tr} }(1-p_{\mathrm{tr}})}.$ (41) $$

### B.4 Numerical comparison

To illustrate the validity of the approximation used in the previous section, we compare it to the actual mean squared error via simulation. We simulate the mean squared error for the given DGP for the following values: $a_{1}=1,a_{0}=0,\sigma^{2}=1,\gamma=1,p_{\mathrm{tr}}=0.9,p_{\mathrm{te}}=0.5$. We compare this to our approximation of the expected loss for several values of $n,d$. In addition, we plot the bias and variance. For 1,000 simulations, and several values of $n$, $d$, the results are shown in Figure 5.

We can observe again in this figure that as $n$ decreases or $d$ increases, the variance becomes a greater share of the expected loss. This variance can be reduced by selecting a $p$ closer to the original $p_{\mathrm{tr}}$. However, this this subsequently leads to an increase in the bias. The optimal $p$ provides a trade-off between these two objectives.

Figure: (a) $n=1.000,d=10$
Refer to caption: extracted/5936978/plots/d10_n1000_nsims1000.png

## Appendix C Full set of results

**Table 1: Results for Waterbirds dataset of comparison between optimized and standard choice of weights: we report the average over 5 runs, with the standard error in parentheses. The ${}^{*}/^{**}$ indicate statistical significance at a 10%/5% significance level respectively for a paired sample one-sided $t-$test.**
| Method | Weighted Average Accuracy (%) | Worst Group Accuracy (%) |
| --- | --- | --- |
| GW-ERM | 90.863 (0.351) | 82.556 (1.196) |
| +Optimized weights | 91.998 (0.092) | 85.435 (0.507) |
| Difference | 1.134 (0.287)^∗∗ | 2.879 (0.878)^∗∗ |
| SUBG | 90.892 (0.382) | 81.908 (1.503) |
| +Optimized weights | 91.709 (0.167) | 85.497 (0.543) |
| Difference | 0.817 (0.269)^∗∗ | 3.589 (1.268)^∗∗ |
| DFR | 90.629 (0.207) | 82.333 (0.735) |
| +Optimized weights | 91.039 (0.225) | 84.028 (0.978) |
| Difference | 0.410 (0.212)^∗ | 1.695 (0.706)^∗∗ |
| GDRO | 91.523 (0.188) | 84.306 (0.445) |
| +Optimized weights | 92.595 (0.139) | 88.481 (0.551) |
| Difference | 1.072 (0.258)^∗∗ | 4.175 (0.393)^∗∗ |
| JTT | 88.542 (0.326) | 79.319 (0.566) |
| +Optimized weights | 91.408 (0.396) | 84.925 (2.202) |
| Difference | 2.866 (0.466)^∗∗ | 5.606 (1.986)^∗∗ |

**Table 2: Results for CelebA dataset of comparison between optimized and standard choice of weights: we report the average over 5 runs, with the standard error in parentheses. The ${}^{*}/^{**}$ indicate statistical significance at a 10%/5% significance level respectively for a paired sample one-sided $t-$test.**
| Method | Weighted Average Accuracy (%) | Worst Group Accuracy (%) |
| --- | --- | --- |
| GW-ERM | 90.471 (0.120) | 85.667 (0.539) |
| +Optimized weights | 90.700 (0.062) | 87.000 (0.136) |
| Difference | 0.229 (0.114)^∗ | 1.333 (0.487)^∗∗ |
| SUBG | 89.141 (0.111) | 87.598 (0.284) |
| +Optimized weights | 89.238 (0.162) | 83.333 (0.731) |
| Difference | 0.097 (0.185) | -4.265 (0.734) |
| DFR | 88.320 (0.195) | 81.000 (0.920) |
| +Optimized weights | 88.299 (0.176) | 81.171 (1.698) |
| Difference | -0.021 (0.092) | 0.171 (1.118) |
| GDRO | 89.113 (0.173) | 85.333 (0.648) |
| +Optimized weights | 90.712 (0.083) | 89.667 (0.222) |
| Difference | 1.599 (0.203)^∗∗ | 4.333 (0.727)^∗∗ |
| JTT | 88.496 (0.121) | 79.222 (0.716) |
| +Optimized weights | 88.243 (0.165) | 75.556 (0.680) |
| Difference | -0.253 (0.139) | -3.667 (0.372) |

**Table 3: Results for MultiNLI dataset of comparison between optimized and standard choice of weights: we report the average over 5 runs, with the standard error in parentheses. The ${}^{*}/^{**}$ indicate statistical significance at a 10%/5% significance level respectively for a paired sample one-sided $t-$test.**
| Method | Weighted Average Accuracy (%) | Worst Group Accuracy (%) |
| --- | --- | --- |
| GW-ERM | 83.967 (0.192) | 72.087 (0.779) |
| +Optimized weights | 84.315 (0.238) | 76.114 (0.660) |
| Difference | 0.349 (0.106)^∗∗ | 4.027 (0.185)^∗∗ |
| SUBG | 84.100 (0.233) | 73.058 (1.085) |
| +Optimized weights | 84.287 (0.169) | 74.163 (0.798) |
| Difference | 0.187 (0.092)^∗ | 1.105 (0.621)^∗ |
| DFR | 84.281 (0.109) | 78.207 (0.313) |
| +Optimized weights | 84.654 (0.216) | 79.740 (0.130) |
| Difference | 0.373 (0.162)^∗∗ | 1.533 (0.367)^∗∗ |
| GDRO | 84.452 (0.208) | 75.654 (0.585) |
| +Optimized weights | 84.400 (0.190) | 76.350 (0.472) |
| Difference | -0.051 (0.085) | 0.696 (0.338)^∗ |
| JTT | 84.071 (0.205) | 72.427 (0.680) |
| +Optimized weights | 84.374 (0.257) | 76.226 (0.698) |
| Difference | 0.303 (0.081)^∗∗ | 3.799 (0.211)^∗∗ |

## Appendix D Optimizing the weights for the worst-group loss

Here we present the algorithm for GDRO with optimized weights. It should be read in conjunction with Section 5.3.

Figure: Algorithm 2 Estimation of the optimal weights $\hat{\bm{p}}^{*}_{n}$ for GDRO

## Appendix E Experimental Details

### E.1 Datasets

An overview of the datasets, their group definitions and sizes, as well as the train/validation split used in the experiments, can be found in Table 4.

**Table 4: Overview of all datasets and their respective group sizes.**
|  | Target variable | Counts (training set) | Percentage | Dataset size |  |  |  |
| --- | --- | --- | --- | --- | --- | --- | --- |
| Waterbirds |  | Water | Land |  |  | Train | Val |
| Water bird | 1,057 | 56 | 22.04% | 1.17% | 4,795 | 1,199 |  |
| Land bird | 184 | 3,498 | 3.84% | 72.95% |  |  |  |
| CelebA |  | Female | Male |  | Train | Val |  |
| Blonde | 22,880 | 1,387 | 14.06% | 0.85% | 162,770 | 19,867 |  |
| Not blonde | 71,629 | 66,874 | 44.01% | 41.08% |  |  |  |
| MultiNLI |  | No negation | Negation |  |  | Train | Val |
| Entailment + Neutral | 24,362 | 638 | 48.72% | 1.28% | 50,000 | 20,000 |  |
| Contradiction | 20,930 | 4,070 | 41.86% | 8.14% |  |  |  |

Waterbirds: this dataset from Sagawa et al. () is a combination of the Places dataset (Zhou et al., ) and the CUB dataset (Welinder et al., ). A ‘water background’ is set by selecting an image from the lake and ocean categories in the places dataset, and the ‘land background’ is set based on the broadleaf and bamboo forest categories. A waterbird/land is then pasted in front of the background. The goal is to predict whether or not a picture contains a waterbird or landbird.

As explained in Section 6, we deviate from Sagawa et al. () in that our validation set is not balanced for all groups. This setup of Sagawa et al. () gives an advantage to methods that only fit on the validation set, such as DFR.

Figure: Figure 6: Examples of the different images in the Waterbirds dataset.
Refer to caption: extracted/5936978/plots/appendix/waterbirds_dataset_ICLR25.png

CelebA: this dataset contains images of celebrity faces (Liu et al., ).
The goal is to predict whether or not a picture contains a celebrity with blonde or non-blonde hair. This is spuriously correlated with the sex of the celebrity.

Figure: Figure 7: Examples of the different images in the CelebA dataset.
Refer to caption: extracted/5936978/plots/appendix/celebA_dataset_ICLR25.png

MultiNLI: the MultiNLI dataset (Williams et al., ) contains pairs of hypotheses and premises. The sentences are pasted together with a [SEP] token in between. The original dataset contains three label types: negation, entailment, and neutral. We follow a procedure that is similar to Kumar et al. () and Holstege et al. () and create a smaller binary version of the dataset (50.000 samples in training, 20.000 in validation, 30.000 in test), with only negations ($y=0$) or entailment or neutral ($y=1$). This version of the dataset is class balanced, similar to the original MultiNLI dataset.
It has been reported that the negation label is spuriously correlated with negation words like nobody, no, never and nothing (Gururangan et al., ). A binary label is created to denote the presence of these words in the hypothesis of a given hypothesis-premise pair.

### E.2 Training of DNNs

Models: For the Waterbirds and CelebA dataset, we use the Resnet50 architecture implemented in the torchvision package: torchvision.models.resnet50(pretrained=True). More details on the model can be found in the original paper from He et al. ().
For Waterbirds, this means using a learning rate of $10^{-3},$ a weight decay of $10^{-3}$, a batch size of 32, and for 100 epochs without early stopping. For CelebA, this means using a learning rate of $10^{-3}$, a weight decay of $10^{-4}$, a batch size of 128, and for 50 epochs without early stopping. We use stochastic gradient descent (SGD) with a momentum parameter of 0.9. For simplicity, we perform no data augmentation for both datasets.

For the MultiNLI dataset, we use the base BERT model implemented in the transformers package (Wolf et al., ): BertModel.from_pretrained("bert-base-uncased"). The model was pre-trained on BookCorpus, a dataset consisting of 11,038 unpublished books, as well as the English Wikipedia (excluding lists, tables and headers). More details on the model can be found in the original paper: Devlin et al. (). For finetuning the BERT model on MultiNLI, we use the AdamW optimizer (Loshchilov & Hutter, ) with the standard settings in Pytorch. When finetuning, we use the hyperparameters of Izmailov et al. (), training for 10 epochs with a batch size of 16, a learning rate of $10^{-5}$, and a weight decay of $10^{-4}$, and linear learning rate decay.

### E.3 Implementation details of existing methods

For all methods, unless otherwise mentioned, we use a logistic regression with the sklearn.LogisticRegression class with the Liblinear solver to optimize the logistic regression, with a tolerance of $10^{-4}$.

In each case, we demean the training data and scale it such that each column has a variance of 1. We use the estimated mean and variances of the training data to apply the same transformations to the validation and test data.

For each run, the hyperparameters are selected based on the worst-group accuracy on the respective validation set. For the L1 penalty, we select it from the following values: $0.1,1.0,3.3,10.0,33.33,100,300,500$. In the case that an multiple L1 penalties lead to the same worst-group accuracy, we select the highest one.

GW-ERM and SUBG: we only optimize the L1 penalty.

GDRO: We follow the implementation of Sagawa et al. (). We optimize two hyperparameters: the L1 penalty, and the hyperparameter $C$ used in adapted worst-group loss from Sagawa et al. (), with the possible values of $C$ being $0,1,2,3$. Instead of the Liblinear solver, we use gradient descent with a learning rate of $10^{-5}$. After observing that the standard options for the L1 penalty often led to poor performance, we add the values $0.01,0.05,0.2$ to the possible L1 penalties.

DFR: We follow the implementation of Kirichenko et al. (). We use the validation set of each dataset for fitting the model. For hyperparameter selection, we split this validation set in half, and use one half for selecting the L1 penalty. After this, the entire validation set is used to fit the model. Parameters are estimated by averaging over 10 logistic regression models.

JTT: similar to Liu et al. () we train a DNN, of which the errors are used to identify the groups. For Waterbirds and CelebA, we train a Resnet50 model using the hyperparameters chosen by Liu et al. (). In the case of Waterbirds, this means a learning rate of $10^{-5}$ and a weight decay of 1. In the case of CelebA this means a learning rate of $10^{-5}$ and a weight decay of 0.1. In the case of MultiNLI, we train a BERT model with the same hyperparameters that were used for finetuning. In all cases, we use early stopping.

After getting the errors from the respective model, we define the groups accordingly and retrain the last layer on the embeddings that are used for all other methods. This is in order to ensure a fair comparison between methods. We optimize over both the L1 penalty and the upweighting factor $\lambda_{JTT}$. For the latter hyperparameter, we select from the values $1,2,5,10,25$.

### E.4 Implementation details of optimizing the weights

Following the notation in Algorithm 1. We use a learning rate $\eta$ of 0.1 for each dataset and method, except in two cases: optimizing the weights for DFR on Waterbirds and MultiNLI, where we use a learning rate of 0.01 and 0.2 respectively. This was done after observing that the optimization procedure did not converge to a lower validation loss for the original choice of the learning rate. In the case of Waterbirds, we run Algorithm 1 for $T=200$ steps, and for $T=100$ in the case of CelebA and MultiNLI.

For each dataset and method, we use a momentum parameter of $\gamma=0.5,$ after observing this helped with escaping local minima.

In the case of optimizing the worst-group loss, we use $\eta_{\bm{q}}=0.1$, following Sagawa et al. (), and use Algorithm 2 while using GW-ERM as our method for determining the parameters.

In the case of optimizing the weights of SUBG and DFR, we set $v_{g}=1$ for the group $g$ with the smallest number of samples. We then optimize the other fractions of groups.

## Peer Reviews

confidence: 2
contribution: 3
initial_rating: 6
presentation: 3
questions: see weakness section
soundness: 3
strengths: 1. The bi-level optimization of importance weights is novel, addressing the bias-variance trade-off more effectively than heuristic-based weighting.
2. The analysis using a linear regression model provides clear insights into the bias-variance tradeoff, making the proposed method more convincing.
3. The experiment section is comprehensive, covering multiple datasets.
4. The proposed method is useful in practice.
summary: The paper addresses the challenge of distribution shifts between training and test data, specifically sub-population shifts. The authors propose a bi-level optimization framework to optimize importance weights while accounting for the finite sample size, thereby improving generalization. The method is validated on benchmark datasets from vision and NLP domains, demonstrating superior performance over traditional importance weighting methods.
weaknesses: 1. The computational overhead of the proposed methods should be discussed. Since we only need to train on the last layer, I think the computational overhead is not large?
2. The proposed method need to separate the training dataset into val dataset and remaining train dataset. What's the tradeoff here? What's the best way to split? If the training dataset is limited, will the proposed method work?

---

confidence: 2
contribution: 3
initial_rating: 6
presentation: 3
questions: * Can the authors clarify the overall outline of the training procedure? Did they follow the last-layer retraining protocol as described in Izamailov et al. (2022)? If so, additional details about this protocol would be beneficial.

* For the Group Weighted ERM procedure, it is mentioned that the likelihood ratio between the test and training distributions is required to initiate the proposed algorithm. However, in most cases, access to the test distribution is not available. How is this ratio computed for real-world datasets, such as Waterbirds? (I may have missed a key concept here.)

* Can the authors provide qualitative insights on how they believe the performance of their algorithms may vary with an increase in model scale, particularly for architectures like ViTs and SWIN?

* Can the authors include an example with large-scale datasets, such as ImageNet-1K, or a medical imaging benchmark (e.g., ISIC or MedMNIST) to enhance the impact of the proposed algorithms?

## Post-Rebuttal
I have increased my score and recommend acceptance
soundness: 3
strengths: The paper is well-written and systematically defines all terms and equations with attention to detail. It is mathematically well-supported and contains relevant experiments on standard benchmarks.
summary: In this work, the authors develop a bi-level optimization algorithm for estimating importance weights (group weights) on top of existing deep learning algorithms, such as Group Weighted ERM and GDRO, to enhance generalization under sub-population shifts. They utilize a standard linear model to illustrate the necessity of this optimization in the presence of limited data and provide mathematical proofs to support their claims. Empirical demonstrations with deep networks using existing training strategies are presented, showing performance improvements on standard datasets.
weaknesses: Refer to Questions

---

confidence: 3
contribution: 2
initial_rating: 5
presentation: 2
questions: - How important are the $p_0$ and $r$ in Algorithm 1 line 278 and line 282? Currently these parameters depend on test dataset directly. And thus weaken the usefulness of the algorithm 1. Is it possible to set $p_0$ and $r$ by assuming a test dataset $p_{te}(g_i) = p_{te}(g_j) , \forall i,j $? If it is possible, how does it affect the experiments performances?  That is my main concern.
soundness: 2
strengths: - The arguement that "the conventional importance weighting $p_{te}(x,y)/ p_{tr}(x,y)$ could be sub-optimal" is interesting. 

- This paper estimates model parameters and importance weights iteratively on train and iid-validation datasets. That is interesting, because it reduces the overfitting of training dataset. Different from conventional cross-validation and hyper-parameter search, this approach find the importance weights by optimization.
summary: This work studies importance weighting of sub-population shifts. Conventional naive importance reweighting is $p_{te}(x,y)/ p_{tr}(x,y)$. This work argues that this conventional reweighting is sub-optimal under a limited training data size. Thus this work proposes to uses a learned importance reweighting coefficient. Empirical results show improved generalization performance across benchmarks like image and NLP datasets.
weaknesses: - Experiments shows the good performance of the proposed method. However, Algorithm 1 (line 278 $p_0$ and line 282 $r$) requires the access of test dataset as parameter initialization. This could be problematic. I understand that some explicit reweighting methods, such as JTT, also contains some heuristic & data-dependent importance weights searching space. These heuristic hyperparameter searching space could also depends on test dataset.  However, group distributional robust optimization (GDRO) links to importance weighting in an implicit way [1], and doesn't contain any hints from test dataset. Thus, the comparison between the proposed method and GDRO is prolematic and potentionally unfair. 

- minor typos: line 112 [L(y, fθ (x)] --> [L(y, fθ (x))]

 
[1] Zhang, J., Lopez-Paz, D., & Bottou, L. (2022, June). Rich feature construction for the optimization-generalization dilemma. In International Conference on Machine Learning (pp. 26397-26411). PMLR.

---

confidence: 2
contribution: 3
initial_rating: 8
presentation: 4
questions: I’ll focus specifically on the empirical experiments. I deem them overall sound, but I have a few questions to be sure. The authors describe fine-tuning a network 5 times to build a sample and perform their t-test. I wanted to confirm how that network was initialized (Appendix suggests you had the same base network that you then fine-tuned 5 different times?). It was then from this set of 5 fine-tuned networks that you then retrained the last layer using each algorithm’s original importance weight scheme and then compared to the paired performance when replacing with your optimal weights? What was controlled in this experiment in terms of random seeds? Were seeds controlling batch ordering varied between runs? Were seeds being varied between the pairs of optimal vs original weights?

The appendix describes some hyperparameters that had to be found for the optimal weight algorithm (E.4). Can the authors provide some additional detail about how these hyper parameters are found?
soundness: 4
strengths: To me the paper is a clear acceptance: it is well written, organized, to the point. You present an intriguing argument that the naïve choice of importance weights used in a variety of prior algorithms can be improved upon, present a practical answer of how to define and find these optimal weights, then show that this definition brings benefit over a controlled baseline. The topic clearly has potential for high impact as distribution shifts are pretty ubiquitous.
summary: The authors present a bi-level optimization strategy to estimate optimal importance weights in the presence of distribution shifts. The authors use a linear regression model to demonstrate how the naïve but often used ratio of likelihoods is not necessarily an optimal choice for importance weights (figure 1.) The authors proceed to introduce their bi-level optimization strategy that estimates optimal weights given a validation set sampled from the training set.

The authors emphasize that the strategy can be used in conjunction with existing methods and write how they implement their strategy specific to each of five existing methods for fine-tuning models to manage distribution shift. The authors empirically test their claim using a benchmark of three datasets for each of the 5 modified methods and show how in almost all cases the performance on a held-out test dataset is improved using their bi-level optimization strategy.
weaknesses: I think the paper could be improved with some additional discussion about the practical implementation of these strategies. For example, how much additional time is spent on your specific improvements compared to the naïve/heruristic weights? Roughly how does this time scale with the validation dataset fraction (1 – n’/n)? What additional hyperparameter tuning is done to find optimal weights (Appendix E.4)? Would a user need to repeat this hyperparameter tuning for their own model?

The impact of the paper could be heightened if the authors elected to demonstrate their technique improving upon a real-world challenge; for example, the authors identify in their introduction that importance weighting technique could be used to improve outcomes for algorithmic fairness.

Meta-review: This paper develops a novel way to assign importance weights to data to deal with sub-population shift.  The authors argue that existing methods to deal with distribution shift between training and test data underperform because they don't take into account the sample size of the training data.  The authors propose a bi-level optimization scheme where they jointly optimize the importance weights and model parameters.  

The reviewers unanimously recommended accept for the paper (8, 8, 6, 6) albeit with relatively low confidence (3, 2, 2, 2).  They found the paper well written, sound, empirically comprehensive and relevant to the community.  The reviewers noted as weaknesses mostly requests for additional details.  One reviewer noted that the method requires access to the test dataset / distribution to estimate importance weights, which is potentially an issue since this is not always available during training.  

Given the unanimous recommendation of the reviewers, and not much cited as weaknesses, there doesn't seem to be any reason to override the reviewers.  Thus the recommendation is to accept.

Decision: Accept (Poster)