3.6  Environment and Distribution Shift

Every guarantee in Section 3.5 rested on one quiet assumption: that deployment data are drawn from the exact same distribution as our training and test sets. In the real world, that assumption breaks constantly. Scanners and cameras get upgraded, seasonal flu waves alter disease prevalence overnight, regional slang evolves, and worse yet, deploying a model can itself cause the population to change its behavior. Suppose a bank discovers in historical records that loan applicants wearing Oxfords repay more often than applicants in sneakers, and deploys a classifier that approves Oxfords and rejects sneakers; within weeks, every applicant shows up in Oxfords while the underlying default rate never budges—a textbook example of Goodhart’s law (when a statistical correlate becomes a decision target, it ceases to be a valid correlate).

In this section, we study how to detect and correct distribution shift between a source distribution \(p_S(\mathbf{x}, y)\) (where we have labeled training data) and a target distribution \(p_T(\mathbf{x}, y)\) (where we observe only unlabeled deployment inputs \(\mathbf{x}\)). We organize the three canonical types of passive shift by which factor of the joint distribution remains invariant, derive and implement importance-weighted covariate shift correction and confusion-matrix label shift correction, and close with concept shift and feedback loops.

3.6.1 Types of Distribution Shift

Without some structural assumption connecting \(p_S(\mathbf{x}, y)\) and \(p_T(\mathbf{x}, y)\), adapting to an unlabeled target domain is mathematically impossible: if \(p_T(\mathbf{x}) = p_S(\mathbf{x})\) stays identical while the labels of cats and dogs secretly swap overnight (\(p_T(y = \textrm{cat} \mid \mathbf{x}) = p_S(y = \textrm{dog} \mid \mathbf{x})\)), no algorithm looking only at unlabeled target images can ever detect the swap.

Every joint distribution factors in two ways:

\[p(\mathbf{x}, y) = p(\mathbf{x})\, p(y \mid \mathbf{x}) = p(y)\, p(\mathbf{x} \mid y). \tag{3.6.1}\]

Holding one conditional factor invariant while the marginal drifts defines the three classic regimes of distribution shift (Figure 3.6.1):

Shift regime What changes What stays fixed Natural causal setting
Covariate shift \(p(\mathbf{x})\) \(p(y \mid \mathbf{x})\) features \(\mathbf{x}\) cause label \(y\)
Label shift \(p(y)\) \(p(\mathbf{x} \mid y)\) label \(y\) causes features \(\mathbf{x}\)
Concept shift \(p(y \mid \mathbf{x})\) often \(p(\mathbf{x})\) label definition drifts over time or space
Figure 3.6.1: Three regimes of distribution shift for a 1D input \(x\) and two classes (top: source \(p_S\); bottom: target \(p_T\); dotted line: optimal decision boundary). Covariate shift moves \(p(x)\) while leaving \(p(y \mid x)\) and the true boundary fixed; label shift changes the class priors \(p(y)\) while keeping \(p(x \mid y)\) fixed (shifting the Bayes boundary toward the rarer class); concept shift alters \(p(y \mid x)\) directly.

3.6.1.1 Covariate Shift, Label Shift, and Concept Shift

  1. Covariate shift (\(p(y \mid \mathbf{x})\) invariant): Natural whenever \(\mathbf{x}\) causes \(y\). Suppose we train a cat-versus-dog classifier on real photographs (Figure 3.6.2) and deploy it on cartoons (Figure 3.6.3). A cartoon cat is still a cat (\(p(y \mid \mathbf{x})\) is unchanged), but the marginal input distribution \(p(\mathbf{x})\) has shifted into a new region of pixel space. Even though the true Bayes boundary has not moved, a misspecified or capacity-limited model fitted to source photos will focus its accuracy where \(p_S(\mathbf{x})\) is dense and can fail badly where \(p_T(\mathbf{x})\) lives.
  2. Label shift (\(p(\mathbf{x} \mid y)\) invariant): Natural whenever \(y\) causes \(\mathbf{x}\): for instance, diseases cause symptoms, so during a flu epidemic the prevalence \(p(y)\) of influenza surges while the symptom distribution \(p(\mathbf{x} \mid y)\) of a flu patient stays unchanged. By Bayes’ theorem, \(p(y \mid \mathbf{x}) \propto p(\mathbf{x} \mid y)\,p(y)\), so a shift in class priors shifts the optimal decision boundary. Correcting label shift requires estimating only \(q\) class-prior ratios rather than a high-dimensional density ratio over \(\mathbf{x}\).
  3. Concept shift (\(p(y \mid \mathbf{x})\) changes): The meaning of the label itself changes across geography or time, whether in regional names for soft drinks across U.S. counties (Figure 3.6.4), evolving diagnostic criteria, or seasonal shopping intent.
Figure 3.6.2: Source training images for cat vs. dog classification: photographs (illustrations: Lafeez Hossain / 500px / Getty Images; ilkermetinkursova / iStock / Getty Images Plus; GlobalP / iStock / Getty Images Plus; Musthafa Aboobakuru / 500px / Getty Images).
Figure 3.6.3: Target deployment images for cat vs. dog classification: cartoons (illustrations: SIBAS_minich / iStock / Getty Images Plus; Ghrzuzudu / iStock / Getty Images Plus; id-work / DigitalVision Vectors / Getty Images; Yime / iStock / Getty Images Plus).
Figure 3.6.4: Geographic concept shift: the most common word for a sweetened carbonated beverage (“pop”, “soda”, or “coke”) across U.S. counties (CC-BY: Alan McConchie, PopVsSoda.com).

3.6.2 Covariate Shift Correction

3.6.2.1 Importance Weights

At deployment we want to minimize the target risk \(R_T[f] = \mathbb{E}_{(\mathbf{x}, y) \sim p_T}[l(f(\mathbf{x}), y)]\). Under covariate shift (\(p_T(y \mid \mathbf{x}) = p_S(y \mid \mathbf{x}) = p(y \mid \mathbf{x})\)), whenever the source support covers the target (\(p_S(\mathbf{x}) > 0\) wherever \(p_T(\mathbf{x}) > 0\)), multiplying and dividing by \(p_S(\mathbf{x})\) rewrites the target risk as an expectation over the source distribution (Shimodaira 2000):

\[ \begin{aligned} R_T[f] &= \sum_{y} \int l(f(\mathbf{x}), y)\, p(y \mid \mathbf{x})\, p_T(\mathbf{x}) \;d\mathbf{x} \\ &= \sum_{y} \int l(f(\mathbf{x}), y)\, p(y \mid \mathbf{x})\, p_S(\mathbf{x})\, \frac{p_T(\mathbf{x})}{p_S(\mathbf{x})} \;d\mathbf{x} \\ &= \mathbb{E}_{(\mathbf{x}, y) \sim p_S}\left[\beta(\mathbf{x})\, l(f(\mathbf{x}), y)\right], \end{aligned} \tag{3.6.2}\]

where \(\beta(\mathbf{x}) = p_T(\mathbf{x}) / p_S(\mathbf{x})\) is the importance weight of input \(\mathbf{x}\). Approximating Equation 3.6.2 on our \(n\) labeled source examples gives weighted empirical risk minimization:

\[ \begin{aligned} \mathop{\mathrm{minimize}}_f \; &\frac{1}{n} \sum_{i=1}^n \beta_i\, l(f(\mathbf{x}^{(i)}), y^{(i)}), \\ \textrm{where } &\beta_i = \frac{p_T(\mathbf{x}^{(i)})}{p_S(\mathbf{x}^{(i)})}. \end{aligned} \tag{3.6.3}\]

Notice why support overlap (\(p_S(\mathbf{x}) > 0\) wherever \(p_T(\mathbf{x}) > 0\)) is non-negotiable: if a medical startup collects diseased blood samples from elderly patients and healthy control samples exclusively from 20-year-old university students, no reweighting can ever manufacture healthy elderly controls where \(p_S(\mathbf{x}) = 0\). Even when support overlaps, extreme right-tail ratios (Figure 3.6.5) can cause a handful of points to dominate the sum, collapsing the effective sample size \(n_{\textrm{eff}} = (\sum_i \beta_i)^2 / \sum_i \beta_i^2\); clipping weights at a cap \(\min(\beta_i, c)\) trades a sliver of bias for a much larger \(n_{\textrm{eff}}\).

Figure 3.6.5: Source density \(p_S = \mathcal{N}(0, 1)\), shifted target density \(p_T = \mathcal{N}(2, 1)\) (top), and the resulting importance weight \(\beta(x_1) = p_T(x_1)/p_S(x_1) = e^{2x_1 - 2}\) on a log scale with clipping threshold \(c = 5\) (bottom).

3.6.2.2 Estimating Weights with a Domain Classifier

How can we estimate \(\beta(\mathbf{x}) = p_T(\mathbf{x}) / p_S(\mathbf{x})\) without estimating high-dimensional densities \(p_S\) and \(p_T\) separately? Pool the \(n\) source inputs (assigned domain label \(z = 0\)) with \(m\) unlabeled target inputs (assigned domain label \(z = 1\)) and train a binary logistic regression domain classifier \(P(z = 1 \mid \mathbf{x}) = \sigma(h(\mathbf{x}))\) with logit \(h(\mathbf{x})\). By Bayes’ theorem,

\[ \begin{aligned} \frac{P(z = 1 \mid \mathbf{x})}{P(z = 0 \mid \mathbf{x})} &= \frac{m}{n}\,\frac{p_T(\mathbf{x})}{p_S(\mathbf{x})} = \exp(h(\mathbf{x})), \\ \textrm{so} \quad \beta(\mathbf{x}) &= \frac{n}{m}\exp(h(\mathbf{x})). \end{aligned} \]

Dropping the constant factor \(n/m\) (which scales the objective without moving its minimizer), we simply weight each source example by \(\beta_i = \exp(h(\mathbf{x}^{(i)}))\) (or \(\min(\exp(h(\mathbf{x}^{(i)})), c)\)).

The domain classifier has a second use: it tells us whether there is any shift to correct. If \(p_T(\mathbf{x}) = p_S(\mathbf{x})\), nothing separates source inputs from target inputs, and on pooled inputs that it was not trained on the domain classifier stays at chance (\(1/2\) when \(n = m\)). An accuracy clearly above chance is therefore evidence that the inputs have shifted. This check is known as a classifier two-sample test (Lopez-Paz and Oquab 2017). Label shift has an even cheaper check: run the deployed classifier on the unlabeled target inputs and compare how often it predicts each class with the same frequencies on source validation data. A deployed system should watch both numbers, since a shift has to be noticed before it can be corrected.

3.6.2.3 A Two-Dimensional Example

Let us watch domain-classifier reweighting rescue a linear classifier on a 2D problem where \(p_S = \mathcal{N}(\mathbf{0}, \mathbf{I}_2)\), \(p_T = \mathcal{N}((2, 0)^\top, \mathbf{I}_2)\), and the true label is \(y = \mathbf{1}(x_2 > \frac{1}{2}x_1^2 - 1)\). Because the true parabolic boundary curves upward, the best linear boundary near \((0, 0)\) is nearly horizontal and fails miserably near \((2, 0)\). We generate \(n = 1{,}000\) source and \(1{,}000\) target points, fit the logistic domain classifier \(h(\mathbf{x})\) to estimate \(\beta_i = \exp(h(\mathbf{x}^{(i)}))\), and compare unweighted, importance-weighted, and clipped (\(c = 5\)) logistic regression:

rng = np.random.default_rng()
n = 1000
X_src = rng.normal(0, 1, (n, 2))            # source p_S: centered at (0, 0)
X_tgt = rng.normal(0, 1, (n, 2)) + [2, 0]   # target p_T: centered at (2, 0)
label = lambda X: (X[:, 1] > 0.5 * X[:, 0]**2 - 1).astype(float)
y_src, y_tgt = label(X_src), label(X_tgt)   # one labeling rule for both

Our sole learning primitive is weighted logistic regression trained by gradient descent. By Section 3.1.2.2, the cross-entropy gradient with respect to the logit is the residual \(\sigma(\mathbf{w}^\top \mathbf{x} + b) - y\), so weighting example \(i\) by \(\beta_i\) simply multiplies its residual by \(\beta_i\).

def fit_logreg(X, y, weights=None, lr=0.1, steps=2000):
    """Logistic regression by gradient descent on the weighted mean loss."""
    w, b = np.zeros(X.shape[1]), 0.0
    v = np.ones(len(y)) if weights is None else weights / weights.mean()
    for _ in range(steps):
        r = v * (1 / (1 + np.exp(-(X @ w + b))) - y)   # weighted error
        w -= lr * X.T @ r / len(y)
        b -= lr * r.mean()
    return w, b

First, we pool X_src (domain label \(z = 0\)) and X_tgt (\(z = 1\)) to train our domain classifier (w_h, b_h) via fit_logreg, compare the learned logit \(h(\mathbf{x})\) against the analytical Gaussian log-ratio \(2x_1 - 2\), and inspect the resulting source importance weights beta and their effective sample size ess. The last lines draw fresh source and target inputs and measure how often the domain classifier tells them apart:

z = np.concatenate([np.zeros(n), np.ones(n)])      # domain label: 1 = target
w_h, b_h = fit_logreg(np.concatenate([X_src, X_tgt]), z)
beta = np.exp(X_src @ w_h + b_h)                   # weights of source examples
ess = lambda v: v.sum()**2 / (v**2).sum()          # effective sample size
print(f'learned h(x) = {w_h[0]:.2f} x1 {w_h[1]:+.2f} x2 {b_h:+.2f}'
      f'   (true log-ratio: 2 x1 - 2)')
print(f'largest weight: {beta.max():.0f}; effective sample size: '
      f'{ess(beta):.0f} of {n}, {ess(np.minimum(beta, 5)):.0f} if clipped at 5')
X_out = np.concatenate([rng.normal(0, 1, (n, 2)),          # fresh source and
                        rng.normal(0, 1, (n, 2)) + [2, 0]])  # target inputs
print(f'domain classifier accuracy on held-out inputs: '
      f'{((X_out @ w_h + b_h > 0) == (z == 1)).mean():.2f} (chance: 0.50)')
learned h(x) = 1.98 x1 +0.09 x2 -1.98   (true log-ratio: 2 x1 - 2)
largest weight: 193; effective sample size: 23 of 1000, 221 if clipped at 5
domain classifier accuracy on held-out inputs: 0.84 (chance: 0.50)
learned h(x) = 1.97 x1 +0.01 x2 -1.95   (true log-ratio: 2 x1 - 2)
largest weight: 124; effective sample size: 40 of 1000, 219 if clipped at 5
domain classifier accuracy on held-out inputs: 0.84 (chance: 0.50)
learned h(x) = 2.03 x1 -0.10 x2 -2.01   (true log-ratio: 2 x1 - 2)
largest weight: 91; effective sample size: 54 of 1000, 216 if clipped at 5
domain classifier accuracy on held-out inputs: 0.83 (chance: 0.50)
learned h(x) = 1.99 x1 +0.04 x2 -2.02   (true log-ratio: 2 x1 - 2)
largest weight: 87; effective sample size: 51 of 1000, 218 if clipped at 5
domain classifier accuracy on held-out inputs: 0.85 (chance: 0.50)

On fresh inputs the domain classifier separates source from target with an accuracy of about \(0.84\), against \(0.50\) by chance, so this shift is easy to detect. No classifier can do better here: the two Gaussians differ only in the mean of \(x_1\), the best rule is \(x_1 > 1\), and it is right with probability \(\Phi(1) \approx 0.84\), where \(\Phi\) is the standard normal distribution function.

Next, we train fit_logreg on the labeled source data (X_src, y_src) under three weighting schemes ('unweighted', raw importance weights 'weighted', and clipped weights 'clipped' at \(5\)) and evaluate each classifier’s accuracy on X_tgt and X_src:

acc = lambda wb, X, y: ((X @ wb[0] + wb[1] > 0) == (y == 1)).mean()
models = {name: fit_logreg(X_src, y_src, v) for name, v in [
    ('unweighted', None), ('weighted', beta), ('clipped', np.minimum(beta, 5))]}
for name, wb in models.items():
    print(f'{name:10s}  target accuracy {acc(wb, X_tgt, y_tgt):.3f}'
          f'   source accuracy {acc(wb, X_src, y_src):.3f}')
print(f'always predicting the most common target class: '
      f'{max(y_tgt.mean(), 1 - y_tgt.mean()):.3f}')
unweighted  target accuracy 0.603   source accuracy 0.853
weighted    target accuracy 0.945   source accuracy 0.854
clipped     target accuracy 0.944   source accuracy 0.854
always predicting the most common target class: 0.711
unweighted  target accuracy 0.572   source accuracy 0.858
weighted    target accuracy 0.939   source accuracy 0.837
clipped     target accuracy 0.940   source accuracy 0.850
always predicting the most common target class: 0.704
unweighted  target accuracy 0.494   source accuracy 0.853
weighted    target accuracy 0.947   source accuracy 0.799
clipped     target accuracy 0.953   source accuracy 0.807
always predicting the most common target class: 0.716
unweighted  target accuracy 0.522   source accuracy 0.850
weighted    target accuracy 0.957   source accuracy 0.837
clipped     target accuracy 0.956   source accuracy 0.841
always predicting the most common target class: 0.723

To visualize why reweighting helps so dramatically, we plot the target and source point clouds (scaling each source marker’s area by beta) together with the true parabolic curve \(x_2 = \frac{1}{2}x_1^2 - 1\) and the 'unweighted' and 'weighted' linear decision boundaries:

x1 = np.linspace(-2.5, 4.5, 50)
d2l.set_figsize((4.5, 3.5))
d2l.plt.scatter(*X_tgt.T, s=2, c='C1', label='target')
d2l.plt.scatter(*X_src.T, s=np.clip(beta, 0.2, 20), c='C0',
                label=r'source, area $\propto \beta$')
d2l.plt.plot(x1, 0.5 * x1**2 - 1, 'k--', label='true boundary')
for name, fmt in [('unweighted', 'C2'), ('weighted', 'C3')]:
    (w1, w2), b = models[name]
    d2l.plt.plot(x1, -(w1 * x1 + b) / w2, fmt, label=name)
d2l.plt.xlabel('$x_1$'), d2l.plt.ylabel('$x_2$'), d2l.plt.ylim(-3, 5)
d2l.plt.legend();

Look at the result: on the shifted target cloud, unweighted training scores only \(\sim 50\%\)–\(60\%\) (worse than always guessing the majority class at \(\sim 70\%\)), whereas importance reweighting tilts the linear decision boundary along the right arm of the parabola and boosts target accuracy to \(\sim 95\%\) using zero target labels.

It would be wrong to conclude that we should always reweight under covariate shift. Reweighting helped here because a line cannot follow a parabola: the model is misspecified and has to choose where to be accurate. A model flexible enough to fit \(p(y \mid \mathbf{x})\) wherever the source has data gains nothing, since the weighted and the unweighted objective then share the same minimizer (Shimodaira 2000). Deep networks are close to this regime. Once they are trained to a low training loss, the effect of importance weights fades (Byrd and Lipton 2019), and on the WILDS collection of real-world shifts, methods designed for distribution shift rarely beat plain empirical risk minimization (Koh et al. 2021). In practice, covariate shift is handled mostly by training on broader data and by fine-tuning on a few labeled target examples.

3.6.3 Label Shift Correction

Under label shift (\(p_T(\mathbf{x} \mid y) = p_S(\mathbf{x} \mid y)\) while class proportions \(p_T(y) \neq p_S(y)\) shift), the exact same importance-weighting identity weights each source example of class \(y\) by the class-prior ratio \(\beta(y) = p_T(y) / p_S(y)\). Since \(p_S(y)\) is known from the training labels, the only challenge is estimating the target class proportions \(\mathbf{p}_T \in \mathbb{R}^q\) from unlabeled target data.

3.6.3.1 Estimating Target Proportions via the Confusion Matrix

Let \(\mathbf{C} \in \mathbb{R}^{q \times q}\) be our source classifier’s column-normalized confusion matrix from Section 3.3, with entries \(C_{ij} = P_S(\hat{c} = i \mid y = j)\) estimated on labeled source validation data. Because \(\hat{c}\) is a function of \(\mathbf{x}\) alone and \(p(\mathbf{x} \mid y)\) is invariant under label shift, \(P_T(\hat{c} = i \mid y = j) = C_{ij}\) holds on the target domain as well. By the law of total probability, the vector \(\boldsymbol{\mu} \in \mathbb{R}^q\) of predicted class frequencies on the unlabeled target inputs satisfies (Saerens et al. 2002; Lipton et al. 2018)

\[ \begin{aligned} \mu_i &= \sum_{j=1}^q P(\hat{c} = i \mid y = j)\, p_T(y = j) = \sum_{j=1}^q C_{ij}\, p_T(y = j), \\ \textrm{or} \quad \mathbf{C}\, \mathbf{p}_T &= \boldsymbol{\mu}. \end{aligned} \tag{3.6.4}\]

Whenever \(\mathbf{C}\) is invertible (which is guaranteed if the classifier gets more than half of each class right, \(C_{jj} > 1/2\); see Exercise 3), solving the \(q \times q\) linear system \(\hat{\mathbf{p}}_T = \mathbf{C}^{-1}\boldsymbol{\mu}\) recovers the unknown target class proportions without a single target label. Furthermore, if the source classifier’s probabilities approximate the source posterior \(p_S(y \mid \mathbf{x})\) (a stronger requirement than the top-class calibration of Section 3.4), Bayes’ rule adapts its predictions to the target domain immediately, without retraining, by adding the log-prior shift \(\log(\hat{p}_T(y) / p_S(y))\) to each class’s logit.

Let us test confusion-matrix inversion and Bayes logit adjustment on Fashion-MNIST. We first train d2l.SoftmaxRegression on the balanced training split and collect its validation logits O, labels y, and predicted classes pred = O.argmax(axis=1):

data = d2l.FashionMNIST(batch_size=256)
model = d2l.SoftmaxRegression(num_outputs=10, lr=0.1)
trainer = d2l.Trainer(max_epochs=10)
trainer.board.display = False
trainer.fit(model, data)
O, y = [], []
for X, Y in data.val_dataloader():
    O.append(d2l.numpy(model(X)))
    y.append(d2l.numpy(Y))
O, y = np.concatenate(O), np.concatenate(y)
pred = O.argmax(axis=1)

Next, we randomly partition the \(10{,}000\) validation examples into a \(5{,}000\)-image source validation set src (used to estimate the column-normalized \(10 \times 10\) confusion matrix C) and a target pool pool from which we draw a skewed target set tgt where \(50\%\) of all garments are shirts (\(400\) shirts and \(45\) of each other class), then solve p_hat = np.linalg.solve(C, mu) from the unlabeled target prediction frequencies mu:

perm = rng.permutation(len(y))
src, pool = perm[:5000], perm[5000:]  # Source validation set; target pool
C = np.zeros((10, 10))
np.add.at(C, (pred[src], y[src]), 1)  # The confusion matrix of the source
C /= C.sum(axis=0, keepdims=True)     # Each column sums to 1
counts = np.full(10, 45)
counts[6] = 400                       # Half of the target images are shirts
p_T = counts / counts.sum()
tgt = np.concatenate([rng.choice(pool[y[pool] == j], counts[j], replace=False)
                      for j in range(10)])
mu = np.bincount(pred[tgt], minlength=10) / len(tgt)  # Needs no labels
p_hat = np.linalg.solve(C, mu)
for j, name in enumerate(data.text_labels(range(10))):
    print(f'{name:10s} true {p_T[j]:.2f}, predicted {mu[j]:.3f}, '
          f'estimated {p_hat[j]:.3f}')
T-shirt    true 0.06, predicted 0.133, estimated 0.067
trouser    true 0.06, predicted 0.055, estimated 0.055
pullover   true 0.06, predicted 0.133, estimated 0.091
dress      true 0.06, predicted 0.070, estimated 0.051
coat       true 0.06, predicted 0.082, estimated 0.050
sandal     true 0.06, predicted 0.052, estimated 0.054
shirt      true 0.50, predicted 0.294, estimated 0.468
sneaker    true 0.06, predicted 0.058, estimated 0.055
bag        true 0.06, predicted 0.066, estimated 0.054
ankle boot true 0.06, predicted 0.057, estimated 0.057
T-shirt    true 0.06, predicted 0.133, estimated 0.050
trouser    true 0.06, predicted 0.055, estimated 0.054
pullover   true 0.06, predicted 0.081, estimated 0.043
dress      true 0.06, predicted 0.068, estimated 0.055
coat       true 0.06, predicted 0.161, estimated 0.046
sandal     true 0.06, predicted 0.056, estimated 0.058
shirt      true 0.50, predicted 0.263, estimated 0.537
sneaker    true 0.06, predicted 0.060, estimated 0.059
bag        true 0.06, predicted 0.070, estimated 0.048
ankle boot true 0.06, predicted 0.053, estimated 0.051
T-shirt    true 0.06, predicted 0.153, estimated 0.114
trouser    true 0.06, predicted 0.052, estimated 0.052
pullover   true 0.06, predicted 0.118, estimated 0.048
dress      true 0.06, predicted 0.088, estimated 0.069
coat       true 0.06, predicted 0.129, estimated 0.077
sandal     true 0.06, predicted 0.055, estimated 0.057
shirt      true 0.50, predicted 0.215, estimated 0.411
sneaker    true 0.06, predicted 0.065, estimated 0.062
bag        true 0.06, predicted 0.076, estimated 0.062
ankle boot true 0.06, predicted 0.050, estimated 0.049
T-shirt    true 0.06, predicted 0.112, estimated 0.045
trouser    true 0.06, predicted 0.056, estimated 0.055
pullover   true 0.06, predicted 0.083, estimated 0.023
dress      true 0.06, predicted 0.072, estimated 0.043
coat       true 0.06, predicted 0.125, estimated 0.098
sandal     true 0.06, predicted 0.052, estimated 0.054
shirt      true 0.50, predicted 0.314, estimated 0.515
sneaker    true 0.06, predicted 0.061, estimated 0.059
bag        true 0.06, predicted 0.068, estimated 0.052
ankle boot true 0.06, predicted 0.056, estimated 0.054

Finally, we adapt the classifier’s predictions to the estimated target distribution by adding the log-prior shift log_w = np.log(np.maximum(p_hat, 1e-6) / 0.1) to the target logits O[tgt] and comparing target accuracy before and after adjustment:

log_w = np.log(np.maximum(p_hat, 1e-6) / 0.1)  # log p_T(y) - log p_S(y)
accuracy = lambda O: (O.argmax(axis=1) == y[tgt]).mean()
print(f'target accuracy: {accuracy(O[tgt]):.3f} as trained, '
      f'{accuracy(O[tgt] + log_w):.3f} adapted to the estimated proportions')
target accuracy: 0.706 as trained, 0.816 adapted to the estimated proportions
target accuracy: 0.663 as trained, 0.807 adapted to the estimated proportions
target accuracy: 0.666 as trained, 0.799 adapted to the estimated proportions
target accuracy: 0.725 as trained, 0.807 adapted to the estimated proportions

Even though the raw classifier predicts “shirt” on only \(\sim 20\%\)–\(40\%\) of the target images (spilling the rest into look-alike tops), \(\mathbf{C}^{-1}\boldsymbol{\mu}\) unscrambles that confusion to recover a shirt proportion near the true \(0.50\) (typically within a few hundredths, occasionally off by almost a tenth), and adding \(\log(\hat{\mathbf{p}}_T / 0.1)\) to the logits lifts target accuracy by roughly 5 to 15 percentage points with zero target labels and zero gradient steps.

3.6.4 Concept Shift and Feedback

When concept shift alters \(p(y \mid \mathbf{x})\) itself, historical labels are outdated and no importance reweighting of old data can teach the new labeling rule; continuous monitoring on a trickle of freshly labeled examples and periodic fine-tuning or retraining are essential.

Even more care is needed when a model’s predictions trigger actions that reshape future training data (performative prediction and feedback loops). In predictive policing (Figure 3.6.6), dispatching extra patrols to neighborhoods with high predicted crime causes more incidents to be recorded there; feeding those biased arrest records back into the next training round reinforces the forecast in a runaway loop. Breaking or auditing such loops requires randomized exploration or independent data collection, crossing from passive supervised learning into reinforcement learning (Chapter 14).

Figure 3.6.6: A runaway feedback loop in predictive policing: model predictions steer patrol allocation, patrol presence determines where incidents are observed, and logged incidents become the next training dataset.

3.6.5 Summary

Adapting to an unlabeled deployment environment is impossible without a structural invariance connecting source \(p_S(\mathbf{x}, y)\) and target \(p_T(\mathbf{x}, y)\), factored either as covariate shift (\(p(y \mid \mathbf{x})\) fixed while \(p(\mathbf{x})\) drifts), label shift (\(p(\mathbf{x} \mid y)\) fixed while \(p(y)\) drifts), or concept shift (\(p(y \mid \mathbf{x})\) drifts). Under covariate shift with overlapping support, weighting source losses by \(\beta(\mathbf{x}) = p_T(\mathbf{x})/p_S(\mathbf{x}) \propto \exp(h(\mathbf{x}))\)—estimated by a logistic domain classifier separating source from unlabeled target inputs—aligns misspecified models with the target risk. Under label shift, inverting the source confusion matrix via \(\hat{\mathbf{p}}_T = \mathbf{C}^{-1}\boldsymbol{\mu}\) recovers target class proportions from unlabeled target predictions and adapts calibrated logits directly by adding \(\log(\hat{\mathbf{p}}_T/\mathbf{p}_S)\). When concept shift or decision-driven feedback loops alter the environment itself, passive reweighting is not enough: deployed systems need fresh target labels and independent exploration.

3.6.6 Exercises

  1. Missing support. Suppose that \(p_S(\mathbf{x}) = 0\) on a region \(\mathcal{A}\) of inputs that has probability \(\pi > 0\) under the target, and that \(p_S(\mathbf{x}) > 0\) everywhere else. Define \(\beta(\mathbf{x}) = p_T(\mathbf{x})/p_S(\mathbf{x})\) outside \(\mathcal{A}\).

    1. Show that the weighted source risk \(\mathbb{E}_{(\mathbf{x}, y) \sim p_S}[\beta(\mathbf{x})\, l(f(\mathbf{x}), y)]\) equals the target risk with the contribution of \(\mathcal{A}\) removed. Write the missing term as \(\pi\) times a conditional expectation.
    2. Explain why no choice of weights on source examples can recover the missing term. Construct two models that agree outside \(\mathcal{A}\) and have the same weighted source risk but different target risks.
  2. [code] When reweighting fails. The code of Section 3.6.2 (fit_logreg, X_src, X_tgt, label) places the target mean at \((2, 0)\). Sweep the target mean over \((s, 0)\) for \(s \in \{0, 0.5, 1, 2, 3, 4, 6\}\), keeping \(n = 1000\) and the labeling rule fixed.

    1. For each \(s\), report the accuracy of the domain classifier \(h\) on held-out pooled data, and interpret its growth with \(s\) in terms of how detectable the shift is.
    2. For each \(s\), record the target accuracy of the unweighted, weighted, and clipped (\(c = 5\)) models, and of always predicting the most common target class. Explain why target accuracy alone cannot show whether reweighting worked at large \(s\).
  3. The label shift system. Let \(\mathbf{C}\) be the column-normalized confusion matrix of Equation 3.6.4.

    1. Show that \(\mathbf{C}\) is invertible whenever \(C_{jj} > \frac{1}{2}\) for every class \(j\). Hint: suppose that \(\mathbf{v}^\top \mathbf{C} = \mathbf{0}\) for some \(\mathbf{v} \neq \mathbf{0}\), look at the column \(j\) for which \(|v_j|\) is largest, and use that the column sums to 1.
    2. Give an example of a classifier for which \(\mathbf{C}\) is singular, and describe what \(\boldsymbol{\mu}\) can still reveal about \(\mathbf{p}_T\) in that case.
    3. For two classes, show that an error \(\delta\) in the fraction \(\mu_1\) of positive predictions changes the estimated fraction of positives by \(\delta/(C_{11} - C_{10})\). Using the standard error \(\sqrt{\mu_1 (1 - \mu_1)/m}\) of a fraction estimated from \(m\) target inputs, find how many target inputs the screening example needs for a standard error of 0.01 in the estimated fraction of sick patients. Repeat for a weaker test with \(C_{11} = 0.6\) and \(C_{10} = 0.4\) on the same population.
  4. [code] Learned versus true weights. ● Reuse the sweep of exercise 2 over target means \((s, 0)\).

    1. Derive the true log-ratio \(\log(p_T(\mathbf{x})/p_S(\mathbf{x}))\) as a function of \(s\), and compare it with the learned \(h\) at each \(s\). Explain how the domain classifier can reach high accuracy at large \(s\) while \(h\) is far from the true log-ratio.
    2. Show that under the source the true weights satisfy \(\mathbb{E}[\beta(\mathbf{x})] = 1\) and \(\mathbb{E}[\beta(\mathbf{x})^2] = e^{s^2}\), so that for large \(n\) their effective sample size is about \(n e^{-s^2}\). Evaluate this for \(n = 1000\) and \(s = 1, 2, 3\).
    3. For each \(s\), compute the effective sample size of the true weights and of the learned weights on the actual source sample. Explain why at \(s = 2\) the sample value is usually much larger than \(1000\, e^{-4} \approx 18\), and what this means for judging the reliability of a reweighted fit from the sample alone. Does clipping at \(c = 5\) repair the shortage?
  5. Shift assumptions from unlabeled data. ● In a binary source domain, \(p_S(y = 1) = p_S(y = 0) = \frac{1}{2}\), and the scalar input satisfies \(x \mid y = 1 \sim \mathcal{N}(1, 1)\) and \(x \mid y = 0 \sim \mathcal{N}(-1, 1)\). Hence \(p_S(y = 1 \mid x) = 1/(1 + \exp(-2x))\). Unlabeled target inputs follow the mixture

    \[p_T(x) = 0.8\, \mathcal{N}(x; 1, 1) + 0.2\, \mathcal{N}(x; -1, 1).\]

    1. Assume label shift. Give \(p_T(y = 1)\) and the threshold on \(x\) of the Bayes-optimal target classifier.
    2. Assume instead covariate shift with the same \(p_T(x)\). Compute \(p_T(y = 1)\) numerically and give the Bayes-optimal threshold.
    3. Use your results to explain why no statistic of the unlabeled target sample can decide which of the two corrections of this section applies.

Discussions