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 (a number, such as hours of training), a control (a number, such as years of experience) and an outcome (a number, such as a wage score). We want the effect of on . Each step adds one idea.
-
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.
-
Fit the nuisance models on fold B only. A nuisance model predicts from , and another predicts from . Suppose the simplest possible model is used, which predicts the average value in the fitting fold. On fold B, the average treatment is and the average outcome is . Check: these two numbers were computed without touching fold A.
-
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 and the outcome residual is . For person 2, they are and . Check: the residuals come from a model that never saw these two people.
-
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 and , which add to . The squares are and , which add to . So the fold A estimate is . Check: the estimate uses only residuals, so the controls have been taken out.
-
Swap the roles. Fit the nuisance models on fold A (averages and ), and measure the residuals on fold B. The residuals are for person 3 and for person 4, giving an estimate of . Check: the two folds have each played both roles once.
-
Average the two estimates. The final answer is . 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
Let the data be for . Split the indices into folds of equal size , and write for everything outside fold . For each , fit the nuisance functions on and compute an estimate on . The cross-fitted estimate is the average .
The setting for the walkthrough is the partially linear model of Chernozhukov and colleagues, a standard model in econometrics. Here is the outcome, is the treatment, and is a vector of controls:
with and . The number is the effect we want. The functions and are the nuisance parameters: they must be estimated, but we do not report them. In the walkthrough, is the function that predicts from , and predicts from with the treatment effect removed.
Step 2 in symbols: fit on the other folds
For fold , the nuisance estimates are
The subscript tells you which data was used. In the example, is the average of over fold B, which is 4, and is the average of over fold B, which is 5.5.
Step 3 in symbols: residuals on the held-out fold
The residuals for a row in fold are
Each residual uses a function fitted without row . Person 1 gives and .
Step 4 in symbols: the estimate on one fold
The Neyman orthogonal score for this model is , which uses the treatment residual in place of the raw treatment. On fold , the estimate solves
which gives
With the numbers from fold A, the numerator is and the denominator is , so .
The property that makes this work is that the score is insensitive to small errors in and . 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
Why the split matters
Without the split, is fitted on the same rows whose residuals are multiplied. A flexible model can then absorb some of the noise into , which makes the residual 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
Use the four people in "How it works", with folds A (people 1 and 2) and B (people 3 and 4).
- Fit on B: average treatment , average outcome .
- Residuals for person 1: , and .
- Residuals for person 2: , and .
- Numerator: .
- Denominator: .
- Fold A estimate: .
- 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.
Use the same sample and swap the roles.
- Fit on A: , .
- Residuals for person 3: and . Residuals for person 4: and .
- Numerator: .
- Denominator: .
- Fold B estimate: .
- Average: .
- 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.
A training programme has 12 participants. The treatment is hours of training, the outcome is a wage score, and the controls include age, prior earnings and region. The data is split into folds of 4 people each, chosen at random.
- For fold 1, the nuisance models are fitted on the 8 people in folds 2 and 3. A random forest predicts and from using only those 8.
- Take one person in fold 1 with controls where the forest predicts hours and points. The person actually trained for 8 hours and scored 46 points.
- Treatment residual: . Outcome residual: .
- This pair enters the sums for fold 1. Its contribution to the numerator is and to the denominator is , before the other three people in fold 1 are added.
- Repeat for folds 2 and 3, each time fitting on the other 8 people, then average the three fold estimates.
- Interpretation: the forest that gave 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 and . Compute the fold estimate . Answer: numerator ; denominator ; so .
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 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 and , where is the outcome residual. Compute .Show answer
The numerator is . The denominator is . So .
QuestionTwo folds give estimates and . What is the cross-fitted estimate?Show answer
It is the average . Each fold counts equally because the folds are the same size.
QuestionA researcher uses cross-fitting with folds on 100 rows. Each nuisance model is fitted on how many rows, and why does that matter?Show answer
Each fold has rows, so each nuisance model is fitted on 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.