Sam Babichenko

Reading notes / Conformal prediction and distribution shift

Conformal Prediction Under Covariate Shift

Tibshirani et al., 2019

Context. Every conformal guarantee rests on exchangeability of training and test points, and the most common way that fails is covariate shift: the test inputs come from a different P~X\tilde P_X while PY∣XP_{Y|X} is unchanged. Ordinary split conformal then miscovers in either direction; on a tilted version of the airfoil data it covered 82% at a 90% target. This paper corrects distribution-free prediction intervals for covariate shift with a finite-sample guarantee, and in doing so finds the general condition behind conformal prediction. Given the unordered set of n+1n+1 data points, exchangeability says each is equally likely to be the test point; under covariate shift the test point is point ii with probability proportional to w(xi)=dP~X/dPX(xi)w(x_i)=d\tilde P_X/dP_X(x_i). So the rank argument goes through with the uniform law replaced by these weights, and the test point's own weight is placed on +∞+\infty, because its score is the one value the threshold may not use.

Setting. Training data (Xi,Yi)∼iidPX×PY∣X(X_i,Y_i)\overset{\rm iid}{\sim}P_X\times P_{Y|X}, i≤ni\le n, and an independent test point (Xn+1,Yn+1)∼P~X×PY∣X(X_{n+1},Y_{n+1})\sim\tilde P_X\times P_{Y|X}, with P~X≪PX\tilde P_X\ll P_X and w=dP~X/dPXw=d\tilde P_X/dP_X known up to a constant. A score S((x,y),Z)S((x,y),Z) treats the multiset ZZ as unordered, e.g. ∣y−μ^(x)∣|y-\hat\mu(x)| with μ^\hat\mu fit symmetrically on Z∪{(x,y)}Z\cup\{(x,y)\}. For trial (x,y)(x,y) let Vi(x,y)=S(Zi,Z−i∪{(x,y)})V_i^{(x,y)}=S(Z_i,Z_{-i}\cup\{(x,y)\}) and Vn+1(x,y)=S((x,y),Z1:n)V_{n+1}^{(x,y)}=S((x,y),Z_{1:n}). Quantiles are of distributions on R∪{∞}\mathbb R\cup\{\infty\}, Quantile(β;F)=inf⁡{z:PF(Z≤z)≥β}\mathrm{Quantile}(\beta;F)=\inf\{z:\mathbb P_F(Z\le z)\ge\beta\}.

Lemma 1 and Theorem 1 (the unweighted case). If V1,…,Vn+1V_1,\dots,V_{n+1} are exchangeable, then P{Vn+1≤Quantile(β; 1n+1∑i=1nδVi+1n+1δ∞)}≥β,\mathbb P\Big\{V_{n+1}\le\mathrm{Quantile}\Big(\beta;\ \tfrac1{n+1}\textstyle\sum_{i=1}^n\delta_{V_i}+\tfrac1{n+1}\delta_\infty\Big)\Big\}\ge\beta, with upper bound β+1/(n+1)\beta+1/(n+1) when ties have probability zero, so the band of yy with Vn+1(x,y)≤Quantile(1−α;V1:n(x,y)∪{∞})V_{n+1}^{(x,y)}\le\mathrm{Quantile}(1-\alpha;V^{(x,y)}_{1:n}\cup\{\infty\}) covers with probability at least 1−α1-\alpha.

Corollary 1 (covariate shift). With the weights piw(x)=w(Xi)∑j=1nw(Xj)+w(x), i≤n,pn+1w(x)=w(x)∑j=1nw(Xj)+w(x),p_i^w(x)=\frac{w(X_i)}{\sum_{j=1}^nw(X_j)+w(x)},\ i\le n,\qquad p_{n+1}^w(x)=\frac{w(x)}{\sum_{j=1}^nw(X_j)+w(x)}, the band C^n(x)={y:Vn+1(x,y)≤Quantile(1−α;∑i=1npiw(x)δVi(x,y)+pn+1w(x)δ∞)}\hat C_n(x)=\{y:V_{n+1}^{(x,y)}\le\mathrm{Quantile}(1-\alpha;\sum_{i=1}^np_i^w(x)\delta_{V_i^{(x,y)}}+p_{n+1}^w(x)\delta_\infty)\} satisfies P{Yn+1∈C^n(Xn+1)}≥1−α\mathbb P\{Y_{n+1}\in\hat C_n(X_{n+1})\}\ge1-\alpha. For split conformal, with μ0\mu_0 fit on independent data, this is μ0(x)±Quantile(1−α;∑ipiw(x)δ∣Yi−μ0(Xi)∣+pn+1w(x)δ∞)\mu_0(x)\pm\mathrm{Quantile}(1-\alpha;\sum_ip_i^w(x)\delta_{|Y_i-\mu_0(X_i)|}+p_{n+1}^w(x)\delta_\infty), valid conditionally on the fitting data.

Theorem 2 (weighted exchangeability). Z1,…,Zn+1Z_1,\dots,Z_{n+1} are weighted exchangeable with weight functions w1,…,wn+1w_1,\dots,w_{n+1} if their joint density factors as f(z1,…,zn+1)=∏iwi(zi) g(z1,…,zn+1)f(z_1,\dots,z_{n+1})=\prod_iw_i(z_i)\,g(z_1,\dots,z_{n+1}) with gg symmetric; independent draws Zi∼PiZ_i\sim P_i with Pi≪P1P_i\ll P_1 are an example, with wi=dPi/dP1w_i=dP_i/dP_1. For such data the conformal band built with piw(z1,…,zn+1)=∑σ:σ(n+1)=i∏j=1n+1wj(zσ(j))∑σ∏j=1n+1wj(zσ(j))p_i^w(z_1,\dots,z_{n+1})=\frac{\sum_{\sigma:\sigma(n+1)=i}\prod_{j=1}^{n+1}w_j(z_{\sigma(j)})}{\sum_\sigma\prod_{j=1}^{n+1}w_j(z_{\sigma(j)})} in place of 1/(n+1)1/(n+1), and the test point's weight on ∞\infty, has coverage at least 1−α1-\alpha. No upper bound is claimed.

Step 1

A point mass at infinity stands in for the unknown test score. The threshold should not use the test score, and in the split case should not depend on yy at all. Raising the support points of a discrete distribution that lie above its β\beta quantile leaves that quantile unchanged, so Vn+1≤Quantile(β;V1:n∪{∞})  ⟺  Vn+1≤Quantile(β;V1:(n+1)),V_{n+1}\le\mathrm{Quantile}\big(\beta;V_{1:n}\cup\{\infty\}\big)\iff V_{n+1}\le\mathrm{Quantile}\big(\beta;V_{1:(n+1)}\big), and the right side says Vn+1V_{n+1} is among the ⌈β(n+1)⌉\lceil\beta(n+1)\rceil smallest of n+1n+1 exchangeable values, which has probability at least β\beta. The point at ∞\infty takes the place of the test score in the ranking, the same device as the inflated quantile in conformalized quantile regression.

Step 2

Condition on the unordered sample, and exchangeability says which point is the test point. The paper reproves Lemma 1 in a form that generalizes. Condition on the event EvE_v that the multiset of scores is {v1,…,vn+1}\{v_1,\dots,v_{n+1}\}; permutation invariance of the density gives P{Vn+1=vi∣Ev}=∑σ:σ(n+1)=if(vσ)∑σf(vσ)=1n+1.\mathbb P\{V_{n+1}=v_i\mid E_v\}=\frac{\sum_{\sigma:\sigma(n+1)=i}f(v_\sigma)}{\sum_\sigma f(v_\sigma)}=\frac1{n+1}. Given the multiset, Vn+1V_{n+1} is a uniform draw from it, so it falls below the β\beta quantile of that law with probability at least β\beta; marginalize and apply Step 1. Exchangeability is used once, to compute the probability that each observed value is the test value.

Step 3

Under weighted exchangeability that probability is the weight piwp_i^w. Condition instead on the multiset of data points EzE_z, so that the scores vi=S(zi,z−i)v_i=S(z_i,z_{-i}) are fixed. With f=∏jwj⋅gf=\prod_jw_j\cdot g and gg symmetric, gg cancels: P{Zn+1=zi∣Ez}=∑σ:σ(n+1)=i∏jwj(zσ(j))∑σ∏jwj(zσ(j))=piw.\mathbb P\{Z_{n+1}=z_i\mid E_z\}=\frac{\sum_{\sigma:\sigma(n+1)=i}\prod_jw_j(z_{\sigma(j)})}{\sum_\sigma\prod_jw_j(z_{\sigma(j)})}=p_i^w . So Vn+1∣Ez∼∑ipiwδviV_{n+1}\mid E_z\sim\sum_ip_i^w\delta_{v_i}, and Step 1 with the test point's weight moved to ∞\infty gives Theorem 2. The upper bound does not carry over: the largest jump of the conditional CDF is max⁡ipiw\max_ip_i^w, which can be large, where in the unweighted case it is 1/(n+1)1/(n+1).

Step 4

Under covariate shift the weights reduce to normalized likelihood ratios. The data are weighted exchangeable with wi≡1w_i\equiv1 for i≤ni\le n and wn+1(x,y)=w(x)w_{n+1}(x,y)=w(x). Only the last slot carries a weight, so each permutation's product is ww at the point it sends there, and the sum collapses to piw=w(xi)∑j=1n+1w(xj).p_i^w=\frac{w(x_i)}{\sum_{j=1}^{n+1}w(x_j)} . The normalizing constant of ww cancels, and the test weight w(Xn+1)w(X_{n+1}) is computable because Xn+1X_{n+1} is observed. That weight goes on ∞\infty: when the test point sits where training data are rare, pn+1wp_{n+1}^w is large, and once it exceeds α\alpha the band is all of R\mathbb R. The infinite band is how the method reports extrapolation, and the mass at ∞\infty cannot be dropped. In a small check of mine (not in the paper: n=20n=20, training X∼N(0,1)X\sim N(0,1), test X∼N(1,1)X\sim N(1,1), noise sd ∣X∣|X|, absolute-residual scores, 90% target), the weighted band covered 97.3% and was infinite 40% of the time, unweighted split conformal covered 82.3%, and the weighted quantile with the test mass dropped and the training weights renormalized covered 81.6%, no better than ignoring the shift; at n=200n=200 the three were 91.3%, 80.9% and 88.9%, with 0.5% infinite.

Step 5

The airfoil experiment confirms the correction and shows its cost. The airfoil data (N=1503N=1503, d=5d=5) were split into fitting, calibration and test parts over 5000 trials, and a shifted test set was drawn by an exponential tilt of two covariates. Unweighted split conformal covered 90.2% unshifted and 82.2% shifted; with the true weights the weighted band covered 90.8%, with coverage more dispersed across trials. The dispersion is the loss of effective sample size, n^=∥w(X1:n)∥12/∥w(X1:n)∥22\hat n=\|w(X_{1:n})\|_1^2/\|w(X_{1:n})\|_2^2: unweighted split conformal calibrated on only n^\hat n points reproduced it. In practice the ratio is estimated from unlabeled test covariates by a classifier of test against training points, whose odds w^(x)=p^(x)/(1−p^(x))\hat w(x)=\hat p(x)/(1-\hat p(x)) are proportional to the density ratio; logistic regression and random forests both gave 91.0% average coverage, the forest with more variable intervals. The guarantee is proved for the true ww; with estimated weights it is shown only empirically, on one low-dimensional dataset.

Step 6

The same lemma applies beyond covariate shift. Any factorization that gives the law of which point is the test point gives weights. If only the law of a low-dimensional ZZ in Z→X→YZ\to X\to Y shifts, the weight is p~Z(z)/pZ(z)\tilde p_Z(z)/p_Z(z), avoiding a density ratio in high-dimensional XX; a second site that shares only covariates and the frequency of a binary attribute gets w(x)=∑zπB(z)πA(z)PA(Z=z∣X=x)w(x)=\sum_z\frac{\pi_B(z)}{\pi_A(z)}\mathbb P_A(Z=z\mid X=x); and kernel weights w(x)∝K((x−x0)/h)w(x)\propto K((x-x_0)/h) give locally averaged conditional coverage around one chosen center x0x_0, rebuilt per center, consistent with the impossibility of exact conditional coverage.

Consequences and limits. Conformal validity rests on knowing the law of which point is the test point, with identical distributions as one case; any setting where that law can be computed gets finite-sample prediction intervals by the same rank argument, and later work on drift and nonexchangeable conformal prediction builds on this. The cost is a smaller effective sample size, and the band reports extrapolation as infinite width instead of hiding it. In credit, where the applicant mix shifts with marketing and approval cutoffs and default labels arrive months late, the method needs exactly what is available, current unlabeled applicants and old labeled ones, but only if default given the features is stable over that lag and a loosened cutoff has not admitted applicants outside the labeled population's support. It does not help when PY∣XP_{Y|X} itself moves (concept shift, as when fraud adapts to the model) or under label shift. Limits: PY∣XP_{Y|X} must be invariant and P~X≪PX\tilde P_X\ll P_X; errors in an estimated ratio carry over to coverage; without an upper bound the bands can be very conservative when weights are uneven; and the guarantee is marginal over the test distribution.

Terms

Covariate shift
train PX×PY∣XP_X\times P_{Y|X}, test P~X×PY∣X\tilde P_X\times P_{Y|X}; only the input law changes
w(x)=dP~X/dPXw(x)=d\tilde P_X/dP_X
likelihood ratio of test to training covariates; needed only up to a constant
piw(x)p_i^w(x)
w(Xi)/(∑jw(Xj)+w(x))w(X_i)/(\sum_jw(X_j)+w(x)); probability that point ii is the test point given the unordered sample
Point mass at ∞\infty
the test point's weight, placed on +∞+\infty; makes the threshold free of the unknown test score
Quantile lemma
Vn+1≤V_{n+1}\le quantile of V1:n∪{∞}V_{1:n}\cup\{\infty\} iff ≤\le quantile of V1:(n+1)V_{1:(n+1)}
Weighted exchangeability
density ∏iwi(zi) g(z)\prod_iw_i(z_i)\,g(z) with gg symmetric; includes independent non-identical draws
No upper bound
largest jump of the conditional CDF is max⁡ipiw\max_ip_i^w, not 1/(n+1)1/(n+1)
Effective sample size
n^=∥w∥12/∥w∥22\hat n=\|w\|_1^2/\|w\|_2^2; explains the extra spread of weighted coverage
Classifier weights
w^=p^/(1−p^)\hat w=\hat p/(1-\hat p) from a test-versus-training classifier on unlabeled covariates
Infinite band
when pn+1w(x)>αp_{n+1}^w(x)\gt\alpha, C^n(x)=R\hat C_n(x)=\mathbb R: the test point is where training data are sparse

Quiz

  1. The weighted method has no upper bound on coverage, unlike ordinary conformal. Why?
  2. In a small simulation with n=20n=20, dropping the point mass at infinity and renormalizing the training weights covered about 82% at a 90% target. What went wrong?
  3. A credit model sees its applicant mix shift after a marketing change. Which condition decides whether weighted conformal restores coverage?

Sources