🧮 Brain Teaser

The Cross-Validation That Peeked: Screening Before the Folds

You have a binary classification problem with N=50N = 50 samples and p=5000p = 5000 features. The data are pure noise:

  • Labels y1,…,y50y_1,\dots,y_{50} are i.i.d. uniform on {−1,+1}\{-1,+1\}.
  • Features xijx_{ij} are i.i.d. N(0,1)\mathcal N(0,1) and independent of the labels.

An analyst runs Procedure W:

  1. Using all 50 samples, compute the sample correlation between each feature and the label. Keep the 100 features with the largest ∣corr∣|\text{corr}|.
  2. Using only these 100 features, estimate the error of a 1-nearest-neighbour classifier with 5-fold cross-validation.

A colleague runs Procedure R: inside each CV fold, redo step 1 using only the training folds. Then fit 1-NN on the training folds and evaluate on the held-out fold.

Questions.

(a) What is the true test error of any classifier on this problem?

(b) Prove that the CV error estimate of Procedure R has expectation exactly 1/21/2.

(c) Procedure W typically reports an error of only a few percent. Explain why, and give a back-of-the-envelope reason the effect is so large.

(d) Suppose instead that, before cross-validation, the analyst keeps the 100 features with the largest sample variance across all 50 samples. Does this pre-screening bias the CV estimate?

cross-validationfeature selectionselection biasdata leakagehigh-dimensional

Answer: The Cross-Validation That Peeked: Screening Before the Folds

Key Idea / Intuition

Cross-validation is honest only if the held-out labels play no role in building the model that predicts them. In Procedure W, the screening step already looked at every label, including the ones later "held out." It chose features that happen to line up with those labels. So the held-out points are not fresh: their labels are partly encoded in the selected features themselves. With 5000 candidate features, there are always 100 that line up remarkably well by chance, and averaging over 100 such weak alignments produces a strong fake signal. Screening that never looks at the labels, such as screening by variance, is harmless.


Formal Proof / Solution

(a) True error is 1/21/2

Since XX is independent of YY and P(Y=1)=1/2P(Y=1)=1/2, any classifier gg satisfies P(g(X)≠Y)=E[P(Y≠g(X)∣X)]=E[1/2]=1/2.P(g(X)\neq Y) = \mathbb E\big[P(Y\neq g(X)\mid X)\big] = \mathbb E[1/2] = 1/2 . No method can beat a coin flip, so the Bayes error is 1/21/2.

(b) Procedure R is exactly unbiased

Fix a fold FF and a held-out index i∈Fi\in F. In Procedure R, the prediction y^i\hat y_i is a deterministic function of:

  • the training data {(xk,yk):k∉F}\{(x_k,y_k): k\notin F\}, which drives both the screening and the 1-NN fit, and
  • the held-out feature vector xix_i.

By assumption, yiy_i is independent of all of these. Write ZZ for the collection of these inputs. Then P(y^i≠yi)=E[P(yi≠y^i(Z)∣Z)]=E[1/2]=12.P(\hat y_i \neq y_i) = \mathbb E\big[P(y_i \neq \hat y_i(Z)\mid Z)\big] = \mathbb E[1/2] = \tfrac12 . The CV estimate is the average of the indicators 1{y^i≠yi}\mathbf 1\{\hat y_i\neq y_i\} over all ii. By linearity of expectation, its expectation is exactly 1/21/2. ■\blacksquare

The argument uses nothing about 1-NN, the number of features, or the number of folds. It needs only one thing: the held-out label was never touched.

(c) Why Procedure W reports a tiny error

Where the logic breaks. In W, the selected feature set SS depends on all of y1,…,y50y_1,\dots,y_{50}. So the prediction y^i\hat y_i depends on yiy_i through SS, and the argument in (b) collapses.

How big the effect is. Under the null, the sample correlation rjr_j of a feature with the labels is approximately N(0,1/N)\mathcal N(0,1/N). Keeping the top 100 of 5000 means keeping the top 2%2\% in absolute value. That corresponds to ∣z∣≳2.33|z|\gtrsim 2.33, so ∣rj∣≳2.3350≈0.33.|r_j| \gtrsim \frac{2.33}{\sqrt{50}} \approx 0.33 . The average over the selected features is a bit larger. A typical value is rj2≈0.13r_j^2\approx 0.13.

What this means for the data. Let y~\tilde y be the standardized label vector. For each selected feature, decompose the 50-vector x⋅jx_{\cdot j} as x⋅j=rj y~+residual,residual variance≈1−rj2.x_{\cdot j} = r_j\,\tilde y + \text{residual}, \qquad \text{residual variance} \approx 1-r_j^2 . This decomposition holds on all 50 coordinates, including the samples later held out, because the correlation was computed on all of them. Each selected coordinate therefore carries a shift of about ±rj\pm r_j that depends on the class.

Effect on nearest-neighbour distances. In the 100-dimensional selected space:

  • Same-class pairs have squared distance ≈2⋅100 (1−r2ˉ)≈174\approx 2\cdot 100\,(1-\bar{r^2}) \approx 174.
  • Opposite-class pairs have an extra ≈4∑jrj2≈52\approx 4\sum_j r_j^2 \approx 52, giving about 226226.
  • The fluctuation of a squared distance is roughly 100⋅8(1−r2ˉ)2≈25\sqrt{100\cdot 8(1-\bar{r^2})^2}\approx 25.

So opposite-class points sit about two standard deviations farther away. Taking the minimum over about 20 candidates of each class makes the nearest neighbour usually same-class.

The result is a reported error of a few percent. Hastie–Tibshirani–Friedman's simulation of exactly this setup gives an average CV error of about 3%3\%, while the truth is 50%50\%.

The moral. Each selected feature is only a weak, chance alignment with the labels, about 0.330.33 correlation. But the selection picked 100 of them out of 5000, and they all point at the same label vector. Summing 100 weak alignments yields a strong fake signal, and the fake signal includes the "test" points.

(d) Variance screening is fine

Sample variances σ^j2\hat\sigma_j^2 are functions of the features only. Let ZiZ_i denote everything used to produce y^i\hat y_i: all 50 feature vectors (used for screening), plus the training labels. Then yiy_i is still independent of ZiZ_i, and the argument of (b) applies verbatim, giving expectation 1/21/2.

General rule: unsupervised preprocessing (variance filters, PCA on XX, standardization) may be done before CV. Any step that uses the labels must be repeated inside every fold.

Source: Hastie, Tibshirani & Friedman, The Elements of Statistical Learning (2nd ed.), Section 7.10.2 'The Wrong and Right Way to Do Cross-validation'

Type: ML/StatsSource: Hastie, Tibshirani & Friedman, The Elements of Statistical Learning (2nd ed.), Section 7.10.2 'The Wrong and Right Way to Do Cross-validation'Edit on GitHub ↗