Tutorials › Statistical Learning (Graduate) › Diagnosing Shift Mechanisms From Source And Target Samples

Robustness and Distribution Shift · Tutorial 977 of 989

Diagnosing Shift Mechanisms From Source And Target Samples

Use two-sample comparisons and prediction-based checks to assess plausible shift mechanisms, while recognizing the limits of unlabeled target data.

Advanced 12 min read

What You'll Learn

  • Distinguish covariate shift, label shift, and conditional shift.
  • Use a domain classifier to compare source and target covariate distributions.
  • Interpret target prediction proportions using a source confusion matrix.
  • Check how uncertainty and classifier choice affect shift diagnostics.
  • Explain why unlabeled target data cannot generally rule out conditional shift.

What Can a Shift Diagnostic Establish?

A source sample with labels and a target sample without labels provide different kinds of evidence. The source data reveal the joint relationship between covariates and outcomes in the source population. The target covariates reveal how the input distribution may have changed. A fixed prediction rule applied to those covariates gives a further observable summary, but it does not reveal the target outcomes themselves.

The central task is therefore not to name a shift mechanism from one statistic. It is to ask which observable consequences follow from each mechanism, check those consequences against the available samples, and keep distinct mechanisms that the observations cannot distinguish.

Definition: Write \(P_S\) and \(P_T\) for the source and target distributions of \((X,Y)\), with covariate marginals \(P_S^X\) and \(P_T^X\). Covariate shift means \(P_T(Y\mid X)=P_S(Y\mid X)\), while \(P_T^X\) may differ from \(P_S^X\). Label shift means \(P_T(X\mid Y)=P_S(X\mid Y)\), while the class probabilities \(P_T(Y)\) may differ from \(P_S(Y)\). Conditional shift refers to a change in \(P(Y\mid X)\).

These definitions describe population distributions, not sample patterns. In practice, a diagnostic can support or contradict implications of a shift assumption; it rarely proves the assumption from finite data. In particular, finding changed covariate distributions is evidence against identical source and target covariates, but it does not decide whether the conditional distribution of \(Y\) given \(X\) stayed fixed.

Comparing Source and Target Covariates

A two-sample diagnostic asks whether the observed source covariates could plausibly have come from the same distribution as the target covariates. One flexible approach is to combine the samples, label each observation by its domain (source or target), and fit a classifier to predict that domain. Evaluation must use held-out observations: a classifier that memorizes its training data does not provide a useful comparison.

For an ideal domain classifier, performance has a precise population interpretation. Let \(p\) and \(q\) be densities of \(P_S^X\) and \(P_T^X\) with respect to a common dominating measure. Suppose source and target are equally likely before seeing \(X\). The classifier predicts the source for covariates with the larger source density and the target otherwise. Its accuracy is related to the total variation distance, defined here as \(\operatorname{TV}(P_S^X,P_T^X)=\frac12\int |p-q|\).

Proposition: With equal source and target sampling probabilities, the best possible domain-classification accuracy is \(\frac12(1+\operatorname{TV}(P_S^X,P_T^X))\). Thus, population accuracy is \(1/2\) when the covariate distributions agree and exceeds \(1/2\) when they differ.

To verify the formula, at each covariate value the best decision selects the domain with the larger density. The probability of a correct decision is therefore:

$$ \frac12\int \max\{p(x),q(x)\}\,dx =\frac12\left(1+\frac12\int |p(x)-q(x)|\,dx\right) =\frac12\bigl(1+\operatorname{TV}(P_S^X,P_T^X)\bigr). $$

The first equality uses the equal domain probabilities. The second follows from \(\max\{a,b\}=(a+b+|a-b|)/2\) and the fact that each density integrates to one. This result concerns the best possible classifier. A practical classifier may perform near chance because it is poorly specified, underfit, or trained on too few observations, even when the distributions differ. Conversely, estimated performance needs uncertainty assessment and a held-out evaluation before it is interpreted as evidence.

Worked Example: A Domain Classifier for Normal Covariates

Suppose the source covariate has distribution \(N(0,1)\), the target covariate has distribution \(N(1,1)\), and the two domains are equally likely. Their densities are equal at \(x=1/2\). Since the target distribution is shifted to the right, the optimal rule predicts source for \(x<1/2\) and target for \(x>1/2\).

Under the source distribution, the probability of correctly predicting source is \(P_S(X<1/2)=\Phi(1/2)\). Under the target distribution, the probability of correctly predicting target is \(P_T(X>1/2)=\Phi(1/2)\), by symmetry of the standard normal distribution. Thus the accuracy is \(\Phi(1/2)\), approximately \(0.6915\), rather than \(0.5\).

The proposition gives \(\operatorname{TV}=2(0.6915)-1=0.383\) (approximately). This establishes that the covariate marginals differ in this population example. It does not establish whether the conditional distribution of \(Y\) given \(X\) is unchanged.

Other useful comparisons include prespecified summaries, feature-wise distributions, and two-sample tests. A domain classifier can detect differences involving combinations of covariates that individual summaries miss, but its result depends on the classifier class and the available sample size. A weak domain classifier is not proof of equality, and a detected difference is not itself proof of covariate shift: it only concerns \(P_S^X\) versus \(P_T^X\).

Using Target Predictions to Check Label Shift

Label shift has an observable implication when a prediction rule is fixed and applied to both domains. Let \(g(X)\) be a classifier with classes \(1,\ldots,K\), and define its source confusion probabilities by \(C_{a y}=P_S(g(X)=a\mid Y=y)\). This convention places predicted classes in rows and true classes in columns, so each column sums to one. If class-conditional distributions are unchanged, the same probabilities \(C_{a y}\) apply in the target population.

Let \(\pi_T\) be the vector of target class probabilities, with entries \(\pi_{T,y}=P_T(Y=y)\). Let \(q_T\) be the vector of target predicted-class proportions, with entries \(q_{T,a}=P_T(g(X)=a)\). Under label shift, conditioning on the true class gives:

$$ q_{T,a} =\sum_{y=1}^{K}P_T(g(X)=a\mid Y=y)P_T(Y=y) =\sum_{y=1}^{K}C_{a y}\pi_{T,y}, \qquad q_T=C\pi_T. $$

This is a diagnostic relationship, not an automatic confirmation of label shift. The source labels can be used to estimate \(C\); target covariates can be passed through the same fixed \(g\) to estimate \(q_T\). If the relationship cannot be reconciled with a plausible target class-probability vector, label shift is contradicted for the assumed stable confusion probabilities. If it fits, label shift remains plausible, but other changes can produce the same predicted proportions.

Worked Example: Recovering Target Class Proportions

Consider two classes and a fixed classifier with source confusion matrix \(C=\begin{pmatrix}0.9&0.2\\0.1&0.8\end{pmatrix}\). The first column says that among true class 1 cases, 90% are predicted as class 1; the second says that among true class 2 cases, 20% are predicted as class 1. Suppose the target sample has predicted-class proportions \(q_T=(0.725,0.275)\).

Under label shift, write \(\pi_{T,1}=u\) and \(\pi_{T,2}=1-u\). The predicted class 1 proportion must satisfy \(0.725=0.9u+0.2(1-u)=0.2+0.7u\). Therefore \(u=(0.725-0.2)/0.7=0.75\), and the implied target proportions are \(\pi_T=(0.75,0.25)\).

Checking the second predicted-class proportion gives \(0.1(0.75)+0.8(0.25)=0.075+0.200=0.275\), as observed. The proportions are internally consistent with the label-shift equation. In an actual analysis, the confusion probabilities and target proportions are estimated, so sampling uncertainty must be included; this exact arithmetic alone does not verify the assumption.

The diagnostic depends on the classifier being the same fixed function in both domains and on the source confusion probabilities being informative about the target. If the classifier is retrained using target data, the comparison no longer uses the same \(g\). If \(C\) is nearly singular, substantially different class proportions can produce similar prediction proportions, so the inferred proportions are unstable. Hard predictions also discard information: score-based summaries can be useful, but their own class-conditional stability must be justified rather than presumed.

Why Unlabeled Data Cannot Rule Out Conditional Shift

The covariate comparison and the prediction-proportion check use only observable quantities: source \((X,Y)\), target \(X\), and predictions computed from \(X\). None directly observes target \(Y\). This creates a fundamental limit, not merely a weakness of a particular test.

Proposition: Given a source joint distribution and a target covariate marginal, the target conditional distribution \(P_T(Y\mid X)\) is not generally identifiable from these observations. In particular, two target joint distributions can have the same observed target covariate marginal and different conditional distributions.

Proof. Fix any target covariate distribution \(Q_X\). Choose two different conditional distributions \(K_1(Y\mid X)\) and \(K_2(Y\mid X)\). Define target joint distributions by \(Q_1(dx,dy)=Q_X(dx)K_1(dy\mid x)\) and \(Q_2(dx,dy)=Q_X(dx)K_2(dy\mid x)\). Both produce exactly the same target covariate marginal \(Q_X\). The source distribution is also identical under the two constructions. Thus the distribution of all observed data—labeled source observations and unlabeled target covariates—is the same, even though the target conditionals differ. Any diagnostic based only on those observations has the same sampling distribution in both cases, so it cannot generally distinguish them. \(\square\)

Worked Example: Unchanged Observations, Changed Conditional

Let \(X\) take values 0 and 1, each with probability \(1/2\) in both source and target, and let the fixed classifier be \(g(X)=X\). In the source, suppose \(P_S(Y=1\mid X=0)=0.2\) and \(P_S(Y=1\mid X=1)=0.8\). The source class probability is \(P_S(Y=1)=0.5(0.2)+0.5(0.8)=0.5\).

Now consider a target population with \(P_T(Y=1\mid X=0)=0.8\) and \(P_T(Y=1\mid X=1)=0.2\). The target covariate distribution is unchanged, and \(P_T(Y=1)=0.5(0.8)+0.5(0.2)=0.5\), but the conditional distribution has changed at both covariate values.

Because \(g(X)=X\), the target predicted class 1 proportion is still \(P_T(g(X)=1)=P_T(X=1)=0.5\), exactly the same as in the source. A covariate comparison and this prediction-proportion check would both see no marginal difference. Yet the target conditional has changed. This example demonstrates why passing these checks cannot rule out conditional shift.

Interpreting the Diagnostics Together

A disciplined interpretation separates observable findings from assumptions about outcomes. The following table summarizes what the checks can and cannot say.

Observable checkWhat a difference can indicateWhat it cannot establish by itself
Source versus target covariate comparisonThe covariate marginals differ, if the evidence is reliable.Whether \(P(Y\mid X)\) is stable or whether label shift holds.
Target prediction proportions versus \(C\pi_T\)An incompatibility can contradict label shift with the assumed stable \(C\).That label shift is true when the proportions are compatible.
Agreement across several diagnosticsThe observations are consistent with a proposed shift model.That no unobserved conditional change occurred.

Use uncertainty estimates for all sample-based comparisons. For the domain classifier, choose evaluation data not used to fit it and report uncertainty in its held-out performance. For the label-shift equation, account for uncertainty in both the estimated source confusion matrix and the target prediction proportions. Also examine whether conclusions are sensitive to the chosen representation and prediction rule. A result based on one classifier is a result about that classifier’s summaries, not a complete comparison of distributions.

Common Mistake

Wrong reasoning: “The source and target covariate distributions look similar, and the target prediction proportions match the label-shift equation; therefore the target conditional distribution must be unchanged.”

Correction: Both checks concern observable covariates or predictions. Different target conditionals can produce the same covariate distribution and prediction proportions, as the discrete example shows. These checks can expose contradictions and support a shift assumption, but unlabeled target data alone cannot generally verify \(P_T(Y\mid X)=P_S(Y\mid X)\). That requires additional assumptions, target labels, or other information that links target outcomes to observed quantities.

Key takeaway: Covariate comparisons diagnose changes in \(P(X)\); prediction proportions test implications of label shift. Neither can generally rule out conditional shift when target outcomes are unobserved.

Check Your Understanding

Use the distinctions between observable evidence and shift assumptions to answer these questions.

  1. With equal source and target sampling probabilities, what does an optimal domain-classification accuracy above \(1/2\) imply about the covariate marginals?
  2. Why does a target prediction-proportion vector compatible with \(q_T=C\pi_T\) not prove label shift?
  3. In the two-class worked example, verify the implied target predicted-class 1 proportion using \(\pi_T=(0.75,0.25)\).
  4. What information is missing when only source labels and target covariates are observed?
  5. Can matching source and target covariate distributions rule out conditional shift? Explain why or why not.
Clinical Biostats