Search

Search papers and lessons

Cross-Fitting

Level 4 · 14 min read

Cross-fitting fits the flexible nuisance models on one part of the data and measures the effect on a separate part, then swaps and averages, so the model's overfitting cannot bias the estimate.

On this page

  • Colour = topic mix
  • Probability
  • Discrete math
  • Calculus
  • Inference
  • Learning basics
  • Losses & regularization
  • ReadReady to readNot yet

TL;DR

Cross-fitting is a way to split a dataset so that a flexible machine-learning model can help estimate an effect without biasing it. The data is cut into folds. The model is fitted on all folds but one, and the effect is measured only on the fold that was held out. The roles then rotate, and the results are averaged. Each measurement uses a model that never saw the rows it is measured on.

Builds on: Train, Validation and Test Sets, Nuisance Parameter · Leads to: Cross-Validation, Average Treatment Effect

Where it's used

Measuring the effect of a policy with many controls. An economist wants the effect of a training programme on wages, which is a causal question. Age, education, region and job history all affect both who enrolls and what they earn, so the model must adjust for them. With dozens of controls, a random forest or a lasso can adjust better than a straight line. Cross-fitting lets that adjustment run without distorting the programme effect. The 2016 paper by Chernozhukov and colleagues that introduced the method, "double machine learning", is built around exactly this kind of problem.

Online experiments with many covariates. A company tests a new checkout page on random visitors and records dozens of visitor features. The average difference in purchases is the quantity of interest, and the visitor features are used to tighten it. Cross-fitting lets the company use a flexible model for those features while keeping the reported difference honest.

Scientific data with a flexible background. In a physics or biology measurement, a known smooth background has to be removed before the signal can be read. When the background is estimated from the same data that gives the signal, the signal can absorb part of the background estimate. Splitting the data keeps the two apart.

How it works

Take a tiny sample of four people. For each person we record a treatment DD (a number, such as hours of training), a control XX (a number, such as years of experience) and an outcome YY (a number, such as a wage score). We want the effect of DD on YY. Each step adds one idea.

  1. Split the rows into two folds. Fold A holds people 1 and 2. Fold B holds people 3 and 4. Check: every row is in exactly one fold, and each fold has two rows.

  2. Fit the nuisance models on fold B only. A nuisance model predicts DD from XX, and another predicts YY from XX. Suppose the simplest possible model is used, which predicts the average value in the fitting fold. On fold B, the average treatment is (3+5)/2=4(3 + 5)/2 = 4 and the average outcome is (4+7)/2=5.5(4 + 7)/2 = 5.5. Check: these two numbers were computed without touching fold A.

  3. Measure the residuals on fold A. A residual is what the model did not predict: the actual value minus the prediction. For person 1, the treatment residual is 1−4=−31 - 4 = -3 and the outcome residual is 3−5.5=−2.53 - 5.5 = -2.5. For person 2, they are 2−4=−22 - 4 = -2 and 5−5.5=−0.55 - 5.5 = -0.5. Check: the residuals come from a model that never saw these two people.

  4. Estimate the effect on fold A. Multiply the two residuals for each person and add the products, then divide by the sum of the squared treatment residuals. The products are (−3)(−2.5)=7.5(-3)(-2.5) = 7.5 and (−2)(−0.5)=1(-2)(-0.5) = 1, which add to 8.58.5. The squares are 99 and 44, which add to 1313. So the fold A estimate is 8.5/13=17/26≈0.658.5 / 13 = 17/26 \approx 0.65. Check: the estimate uses only residuals, so the controls have been taken out.

  5. Swap the roles. Fit the nuisance models on fold A (averages Dˉ=1.5\bar D = 1.5 and Yˉ=4\bar Y = 4), and measure the residuals on fold B. The residuals are (1.5,0)(1.5, 0) for person 3 and (3.5,3)(3.5, 3) for person 4, giving an estimate of 10.5/14.5=21/29≈0.7210.5 / 14.5 = 21/29 \approx 0.72. Check: the two folds have each played both roles once.

  6. Average the two estimates. The final answer is (0.65+0.72)/2≈0.69(0.65 + 0.72)/2 \approx 0.69. Check: the average is taken over the estimates, not over the raw rows.

flowchart LR
    S["Full sample"] --> A["Fold A"]
    S --> B["Fold B"]
    B -->|"fit nuisance models"| MA["Models from B"]
    MA -->|"residuals on"| A
    A --> EA["Estimate from A"]
    A -->|"fit nuisance models"| MB["Models from A"]
    MB -->|"residuals on"| B
    B --> EB["Estimate from B"]
    EA --> F["Average"]
    EB --> F

The idea that matters is in step 2 and step 3. The nuisance model was never allowed to see the rows it was judged on, so any error it makes is not a fit to those rows' noise.

The math

Definition: Cross-fitting estimator (DML1)

Let the data be Wi=(Xi,Di,Yi)W_i = (X_i, D_i, Y_i) for i=1,…,Ni = 1, \dots, N. Split the indices into KK folds I1,…,IKI_1, \dots, I_K of equal size n=N/Kn = N / K, and write IkcI_k^c for everything outside fold kk. For each kk, fit the nuisance functions on IkcI_k^c and compute an estimate θˇk\check{\theta}_k on IkI_k. The cross-fitted estimate is the average θ~=1K∑k=1Kθˇk\tilde{\theta} = \frac{1}{K}\sum_{k=1}^{K} \check{\theta}_k.

The setting for the walkthrough is the partially linear model of Chernozhukov and colleagues, a standard model in econometrics. Here YY is the outcome, DD is the treatment, and XX is a vector of controls:

Y=θ0D+g0(X)+U,D=m0(X)+V,Y = \theta_0 D + g_0(X) + U, \qquad D = m_0(X) + V,

with E[U∣X,D]=0\mathbb{E}[U \mid X, D] = 0 and E[V∣X]=0\mathbb{E}[V \mid X] = 0. The number θ0\theta_0 is the effect we want. The functions g0g_0 and m0m_0 are the nuisance parameters: they must be estimated, but we do not report them. In the walkthrough, m0m_0 is the function that predicts DD from XX, and g0g_0 predicts YY from XX with the treatment effect removed.

Step 2 in symbols: fit on the other folds

For fold kk, the nuisance estimates are

m^k=m^((Xi,Di)i∈Ikc),g^k=g^((Xi,Yi)i∈Ikc).\hat{m}_k = \hat{m}\big((X_i, D_i)_{i \in I_k^c}\big), \qquad \hat{g}_k = \hat{g}\big((X_i, Y_i)_{i \in I_k^c}\big).

The subscript kk tells you which data was used. In the example, m^A\hat{m}_A is the average of DD over fold B, which is 4, and g^A\hat{g}_A is the average of YY over fold B, which is 5.5.

Step 3 in symbols: residuals on the held-out fold

The residuals for a row ii in fold kk are

V^i=Di−m^k(Xi),U^i=Yi−g^k(Xi)    (before the effect is removed).\hat{V}_i = D_i - \hat{m}_k(X_i), \qquad \hat{U}_i = Y_i - \hat{g}_k(X_i) \;\; \text{(before the effect is removed)}.

Each residual uses a function fitted without row ii. Person 1 gives V^1=1−4=−3\hat{V}_1 = 1 - 4 = -3 and Y1−g^A(X1)=3−5.5=−2.5Y_1 - \hat{g}_A(X_1) = 3 - 5.5 = -2.5.

Step 4 in symbols: the estimate on one fold

The Neyman orthogonal score for this model is ψ=(Y−θD−g(X)) (D−m(X))\psi = (Y - \theta D - g(X))\,(D - m(X)), which uses the treatment residual in place of the raw treatment. On fold kk, the estimate θˇk\check{\theta}_k solves

1n∑i∈Ik(Yi−g^k(Xi)−θˇkV^i)V^i=0,\frac{1}{n} \sum_{i \in I_k} \big(Y_i - \hat{g}_k(X_i) - \check{\theta}_k \hat{V}_i\big)\hat{V}_i = 0,

which gives

θˇk=∑i∈IkV^i (Yi−g^k(Xi))∑i∈IkV^i 2.\check{\theta}_k = \frac{\sum_{i \in I_k} \hat{V}_i\, (Y_i - \hat{g}_k(X_i))}{\sum_{i \in I_k} \hat{V}_i^{\,2}}.

With the numbers from fold A, the numerator is (−3)(−2.5)+(−2)(−0.5)=8.5(-3)(-2.5) + (-2)(-0.5) = 8.5 and the denominator is 9+4=139 + 4 = 13, so θˇA=8.5/13=17/26\check{\theta}_A = 8.5 / 13 = 17/26.

The property that makes this work is that the score is insensitive to small errors in g^\hat{g} and m^\hat{m}. This is the "orthogonal" part of the name, and it is why a biased nuisance fit does not spoil the estimate to first order.

Step 6 in symbols: the average

θ~=12(θˇA+θˇB)=12(1726+2129)≈0.69.\tilde{\theta} = \frac{1}{2}\left(\check{\theta}_A + \check{\theta}_B\right) = \frac{1}{2}\left(\frac{17}{26} + \frac{21}{29}\right) \approx 0.69.

Why the split matters

Without the split, g^\hat{g} is fitted on the same rows whose residuals are multiplied. A flexible model can then absorb some of the noise UiU_i into g^(Xi)\hat{g}(X_i), which makes the residual Yi−g^(Xi)Y_i - \hat{g}(X_i) smaller than it should be, and the estimate moves toward the wrong value. The Chernozhukov paper calls this "bias due to overfitting" in the nuisance step. In its contrived example, the term that should vanish grows with the sample size when the full sample is used, and two-fold cross-fitting removes the bias. Cross-fitting is the practical way to separate the nuisance and estimation data.

Worked examples

Example
1. Two folds, two rows each

Use the four people in "How it works", with folds A (people 1 and 2) and B (people 3 and 4).

  1. Fit on B: average treatment DˉB=(3+5)/2=4\bar D_B = (3 + 5)/2 = 4, average outcome YˉB=(4+7)/2=5.5\bar Y_B = (4 + 7)/2 = 5.5.
  2. Residuals for person 1: D−DˉB=1−4=−3D - \bar D_B = 1 - 4 = -3, and Y−YˉB=3−5.5=−2.5Y - \bar Y_B = 3 - 5.5 = -2.5.
  3. Residuals for person 2: D−DˉB=2−4=−2D - \bar D_B = 2 - 4 = -2, and Y−YˉB=5−5.5=−0.5Y - \bar Y_B = 5 - 5.5 = -0.5.
  4. Numerator: (−3)(−2.5)+(−2)(−0.5)=7.5+1=8.5(-3)(-2.5) + (-2)(-0.5) = 7.5 + 1 = 8.5.
  5. Denominator: (−3)2+(−2)2=9+4=13(-3)^2 + (-2)^2 = 9 + 4 = 13.
  6. Fold A estimate: 8.5/13=17/26≈0.658.5 / 13 = 17/26 \approx 0.65.
  7. Interpretation: the estimate comes from the two residual pairs only. No row in fold A was used to build the averages it was compared with.
Example
2. The full two-fold run

Use the same sample and swap the roles.

  1. Fit on A: DˉA=(1+2)/2=1.5\bar D_A = (1 + 2)/2 = 1.5, YˉA=(3+5)/2=4\bar Y_A = (3 + 5)/2 = 4.
  2. Residuals for person 3: 3−1.5=1.53 - 1.5 = 1.5 and 4−4=04 - 4 = 0. Residuals for person 4: 5−1.5=3.55 - 1.5 = 3.5 and 7−4=37 - 4 = 3.
  3. Numerator: (1.5)(0)+(3.5)(3)=0+10.5=10.5(1.5)(0) + (3.5)(3) = 0 + 10.5 = 10.5.
  4. Denominator: (1.5)2+(3.5)2=2.25+12.25=14.5(1.5)^2 + (3.5)^2 = 2.25 + 12.25 = 14.5.
  5. Fold B estimate: 10.5/14.5=21/29≈0.7210.5 / 14.5 = 21/29 \approx 0.72.
  6. Average: (0.65+0.72)/2≈0.69(0.65 + 0.72) / 2 \approx 0.69.
  7. Interpretation: the two folds give estimates that are close, which is a sign that the answer does not depend heavily on which person landed in which fold. On a real dataset, practitioners often repeat the random split several times and look at the spread of the answers.
Example
3. A realistic training study with twelve people

A training programme has 12 participants. The treatment DD is hours of training, the outcome YY is a wage score, and the controls XX include age, prior earnings and region. The data is split into K=3K = 3 folds of 4 people each, chosen at random.

  1. For fold 1, the nuisance models are fitted on the 8 people in folds 2 and 3. A random forest predicts DD and YY from XX using only those 8.
  2. Take one person in fold 1 with controls XX where the forest predicts D^=6.0\hat{D} = 6.0 hours and Y^=40.0\hat{Y} = 40.0 points. The person actually trained for 8 hours and scored 46 points.
  3. Treatment residual: 8−6.0=2.08 - 6.0 = 2.0. Outcome residual: 46−40.0=6.046 - 40.0 = 6.0.
  4. This pair enters the sums for fold 1. Its contribution to the numerator is 2.0×6.0=12.02.0 \times 6.0 = 12.0 and to the denominator is 2.02=4.02.0^2 = 4.0, before the other three people in fold 1 are added.
  5. Repeat for folds 2 and 3, each time fitting on the other 8 people, then average the three fold estimates.
  6. Interpretation: the forest that gave Y^=40.0\hat{Y} = 40.0 never saw this person. Any quirk of the forest for people like them cannot leak into the residual that measures the training effect.

Try it: fold 2 gives two residual pairs (V^,Y−g^)=(1,2)(\hat V, Y - \hat g) = (1, 2) and (−2,3)(-2, 3). Compute the fold estimate θˇ2\check{\theta}_2. Answer: numerator 1×2+(−2)(3)=2−6=−41 \times 2 + (-2)(3) = 2 - 6 = -4; denominator 12+(−2)2=51^2 + (-2)^2 = 5; so θˇ2=−4/5=−0.8\check{\theta}_2 = -4 / 5 = -0.8.

Common mistakes and tradeoffs

It is tempting to think that using more data for the nuisance model is always better, and so the nuisance model should be fitted on the whole sample. In fact, the fit then sees the rows it is judged on, and that is the Overfitting bias cross-fitting removes. The trade-off is real: each fold's nuisance model uses only (K−1)/K(K-1)/K of the data, so each one is a little worse. Cross-fitting accepts that loss to keep the estimate honest.

It is tempting to think a single split is enough. In fact, a single random split gives an answer that depends on which rows landed where. Repeating the whole procedure with several random splits and reporting the spread of the answers is a common safeguard.

It is tempting to think cross-fitting is the same as cross-validation. In fact, cross-validation is used to pick a model and score it, whereas cross-fitting produces the final causal estimate. They use the same folds, but the purpose and the quantity averaged are different. Cross-validation is covered in Cross-Validation.

It is tempting to think cross-fitting fixes every bias. In fact, it fixes the bias from overfitting in the nuisance step, but only when the score is orthogonal to the nuisance. A badly chosen score can still be biased, and the nuisance models still have to be reasonably accurate.

Use cross-fitting when a causal or structural parameter has to be estimated with flexible machine-learning nuisance models. Do not use it when a simple linear model is correct and the sample is small, because the split costs efficiency. Alternatives include fitting the nuisance on the full sample with a parametric model and accepting its bias, or using a method that controls overfitting without splitting, such as a bound on the complexity of the nuisance class.

History

Peter Robinson described the partially linear regression model in 1988, with a method that gets a root-n-consistent estimate of the linear part while leaving the other part unspecified. The sample-splitting idea is older: statisticians split data into training and test pieces long before machine learning used the word. What changed in the 2010s is that flexible learners such as random forests and lasso regressions became practical, and naive use of them in causal estimates produced biased answers.

In 2016, Victor Chernozhukov and co-authors posted a paper to arXiv that formalised the fix. They combined an orthogonal score with cross-fitting, and proved that the resulting estimator is root-n-consistent and asymptotically normal even when the nuisance models are learned with machine learning. The method was given the name "double machine learning" in that work, and it has since been used in economics, marketing and medicine. The name "cross-fitting" emphasises the swap of roles between folds.

Recap

  • Split the rows into folds. Fit the nuisance models on all folds except one.
  • Measure the residuals on the held-out fold, so the nuisance models have never seen those rows.
  • Compute the estimate on each fold from residual products, then average the estimates over the folds.
  • The gain is that overfitting in the nuisance models cannot bias the final effect, and the cost is that each nuisance model sees less data.

Read next: Cross-Validation for choosing models with the same folds, Average Treatment Effect for the quantity most often estimated this way, and Random Forest for a common flexible learner used in the nuisance step.

Check yourself

QuestionWhy is the nuisance model fitted only on the folds other than the one being measured?Show answer

If the model were fitted on the rows it is measured on, it would partly fit their noise, so the residuals would be too small and the effect estimate would be biased. Fitting on the other folds means the residuals come from predictions for rows the model never saw.

QuestionFold 1 has residual pairs (V^,U^)=(2,1)(\hat V, \hat U) = (2, 1) and (1,3)(1, 3), where U^\hat U is the outcome residual. Compute θˇ1=∑V^U^/∑V^2\check{\theta}_1 = \sum \hat V \hat U / \sum \hat V^2.Show answer

The numerator is 2×1+1×3=52 \times 1 + 1 \times 3 = 5. The denominator is 22+12=52^2 + 1^2 = 5. So θˇ1=5/5=1\check{\theta}_1 = 5 / 5 = 1.

QuestionTwo folds give estimates 0.40.4 and 0.80.8. What is the cross-fitted estimate?Show answer

It is the average (0.4+0.8)/2=0.6(0.4 + 0.8)/2 = 0.6. Each fold counts equally because the folds are the same size.

QuestionA researcher uses cross-fitting with K=5K = 5 folds on 100 rows. Each nuisance model is fitted on how many rows, and why does that matter?Show answer

Each fold has 100/5=20100 / 5 = 20 rows, so each nuisance model is fitted on 8080 rows. This matters because the nuisance model is weaker with fewer rows, and that loss has to be weighed against the bias the split removes. With very small samples, that loss can be large.

QuestionA team wants the effect of a discount on sales. They have 30 store-level controls and a random forest for the nuisance step. They fit the forest on all 30 stores, then compute residuals on the same 30 stores and report the effect. What is wrong, and what should they do?Show answer

The forest was fitted on the same stores whose residuals are used, so it can absorb some of the sales noise and bias the discount effect. They should split the stores into folds, fit the forest on the other folds, compute residuals on the held-out fold, and average the fold estimates. Repeating the random split a few times would show how stable the answer is.

Sources