Tutorials › Statistical Learning (Graduate) › Sample Splitting and Cross-Fitting for Stacking

Ensemble Risk and Diversity · Tutorial 880 of 989

Sample Splitting and Cross-Fitting for Stacking

Use held-out member predictions to estimate stacking weights, and distinguish valid risk comparisons from optimistic or mismatched ones.

Advanced 13 min read

What You'll Learn

  • Define stacking weights using predictions made on observations excluded from member-model fitting
  • Derive a finite-candidate risk bound for weights chosen on an independent validation split
  • Construct out-of-fold predictions and identify their corresponding cross-fitted risk target
  • Prove a risk comparison for pooled cross-fitting without assuming fold scores are independent
  • State the extra transfer condition needed to compare cross-fitted weights with full-data refits
  • Recognize why reusing training predictions or treating cross-validation scores as independent can mislead

Why the Predictions Used to Fit Weights Matter

Stacking combines predictions from several fitted models by choosing weights to minimize a loss. The weights are themselves estimated from data, so the predictions used to estimate them matter: if each model's predictions were made on observations it had already fitted, those predictions can be unusually favorable. The resulting weights may favor members whose apparent accuracy reflects training fit rather than predictive performance.

A direct remedy is to separate the observations used to fit the member models from those used to estimate the weights. Cross-fitting uses the same principle repeatedly: each observation contributes a prediction from a member model trained without that observation. In both cases, the comparison supported by the data must match the models and risk target actually evaluated.

Definition: Let \(Z=(X,Y)\) be a new observation from the target distribution, and let \(\ell(y,a)\) be a prediction loss. For \(J\) member predictors \(g_1,\ldots,g_J\) and a weight vector \(w=(w_1,\ldots,w_J)\), define the stacked prediction \(g_w(x)=\sum_{j=1}^J w_jg_j(x)\). In convex stacking, \(w\) belongs to the simplex \(\mathcal W=\{w_j\geq 0,\ \sum_{j=1}^Jw_j=1\}\). Its risk is \(R(w)=E[\ell(Y,g_w(X))]\). When the member predictors were fitted from data, this risk is conditional on those fitted predictors.

In a sample split, one part of the data fits the members and a separate validation part estimates the weights. Conditional on the fitting data, the member predictions are fixed, and the validation observations are independent draws from the target distribution. Cross-fitting instead partitions the observations into folds; the prediction for an observation is made by a member fitted without that observation's fold.

Independent Validation: A Risk Comparison

The validation criterion is useful because, for any fixed weight vector, it estimates the risk of the already-fitted members. But choosing weights by minimizing that criterion makes the chosen validation score optimistic. A risk comparison must account for this search. The following finite-candidate result gives one precise set of sufficient conditions.

Theorem (finite-class validation bound): Suppose the member predictors are fitted using training data \(D_{\mathrm{tr}}\), and \(Z_1,\ldots,Z_m\) are independent validation observations, also independent of \(D_{\mathrm{tr}}\). Let \(\mathcal W_0\) be a finite, nonempty set of candidate weights. Assume \(0\leq \ell(Y,g_w(X))\leq B\) almost surely for every \(w\in\mathcal W_0\). Define \(\widehat R_{\mathrm{val}}(w)=m^{-1}\sum_{i=1}^m\ell(Y_i,g_w(X_i))\), and let \(\hat w\) minimize it over \(\mathcal W_0\). For \(0<\delta<1\), with probability at least \(1-\delta\), conditional risks satisfy $$ R(\hat w)\leq \min_{w\in\mathcal W_0}R(w)+2B\sqrt{\frac{\log(2|\mathcal W_0|/\delta)}{2m}}. $$

Proof. Condition on \(D_{\mathrm{tr}}\). For each fixed \(w\), the \(m\) validation losses are independent, identically distributed, and lie in \([0,B]\), with mean \(R(w)\). Hoeffding's inequality therefore gives, for any \(t>0\), $$ P\left(\left|\widehat R_{\mathrm{val}}(w)-R(w)\right|>t\mid D_{\mathrm{tr}}\right)\leq 2\exp\left(-\frac{2mt^2}{B^2}\right). $$ Taking a union bound over the \(|\mathcal W_0|\) candidates shows that, with conditional probability at least \(1-\delta\), every candidate satisfies \(\left|\widehat R_{\mathrm{val}}(w)-R(w)\right|\leq t\), where \(t=B\sqrt{\log(2|\mathcal W_0|/\delta)/(2m)}\). On this simultaneous event, choose any risk minimizer \(w^\star\) in the finite set. The definition of \(\hat w\) and the two deviation bounds give $$ R(\hat w)\leq \widehat R_{\mathrm{val}}(\hat w)+t \leq \widehat R_{\mathrm{val}}(w^\star)+t \leq R(w^\star)+2t. $$ This proves the claim. Since the conditional failure probability is at most \(\delta\) for every training set, the same probability bound holds unconditionally. \(\square\)

The bound compares the selected weight with the best candidate in \(\mathcal W_0\), not automatically with every point in the simplex. A continuum of possible weights needs an additional uniform-convergence argument, or a finite candidate set with a suitable approximation guarantee. The bound also concerns the risk of the particular members fitted on \(D_{\mathrm{tr}}\); it does not silently change them to models fitted on more data.

Worked Example: Estimating a Two-Member Weight on a Holdout Set

Suppose the member models are fitted on a training split, and their validation predictions for three observations are \(A=(0,2,2)\) and \(B=(2,1,4)\). The observed outcomes are \(y=(1,2,3)\). Use squared error and the convex combination \(g_\alpha=\alpha A+(1-\alpha)B\), where \(0\leq\alpha\leq1\).

At \(\alpha=0\), the residual vector for \(B\) is \(B-y=(1,-1,1)\). The prediction difference is \(A-B=(-2,1,-2)\), so the residual at weight \(\alpha\) is \((1,-1,1)+\alpha(-2,1,-2)\). Thus the validation sum of squared errors is $$ (1-2\alpha)^2+(-1+\alpha)^2+(1-2\alpha)^2 =3-10\alpha+9\alpha^2. $$

Differentiating gives \(-10+18\alpha=0\), hence \(\hat\alpha=5/9\), which is in the allowed interval. The minimized sum is \(3-50/9+25/9=2/9\), and the validation mean squared error is \(2/27\). This calculation produces a validation-selected weight. The small minimized score is not an unbiased estimate of the selected ensemble's risk: the weight was chosen to minimize this same score. A separate test sample, or an applicable uniform risk bound, is needed for an honest comparison.

Cross-Fitting and Its Risk Target

For \(K\) folds, write \(I_k\) for the indices in fold \(k\), with \(m_k=|I_k|\) and \(\sum_{k=1}^K m_k=n\). For each fold \(k\), fit every member using observations outside \(I_k\), and denote those predictors by \(g_{1,-k},\ldots,g_{J,-k}\). For \(i\in I_k\), form the out-of-fold prediction \(g_{w,-k}(X_i)=\sum_jw_jg_{j,-k}(X_i)\). The pooled cross-fitted criterion is $$ \widehat R_{\mathrm{CF}}(w)=\sum_{k=1}^K\frac{m_k}{n}\left\{\frac{1}{m_k}\sum_{i\in I_k}\ell(Y_i,g_{w,-k}(X_i))\right\}. $$

Its natural target is the weighted risk of the fold-specific fitted members, not necessarily the risk of a single model fitted on all \(n\) observations. For fixed \(w\), let \(R_k(w)\) be the risk on a fresh observation of the ensemble whose members were fitted outside fold \(k\), conditional on those fitted members. Set \(R_{\mathrm{CF}}(w)=\sum_k(m_k/n)R_k(w)\). This target can vary with the observed training folds.

Theorem (finite-class cross-fitting bound): Assume the observations are independent and identically distributed. For each fold \(k\), the member predictors used for that fold are fitted using only observations outside \(I_k\). Let \(\mathcal W_0\) be finite and nonempty, and suppose all relevant losses lie in \([0,B]\) almost surely. If \(\hat w\) minimizes \(\widehat R_{\mathrm{CF}}(w)\) over \(\mathcal W_0\), then with probability at least \(1-\delta\), $$ R_{\mathrm{CF}}(\hat w)\leq \min_{w\in\mathcal W_0}R_{\mathrm{CF}}(w) +2\sum_{k=1}^K\frac{m_k}{n}B\sqrt{\frac{\log(2K|\mathcal W_0|/\delta)}{2m_k}}. $$

Proof. Fix a fold \(k\). Conditional on the observations outside \(I_k\), the fold-\(k\) predictors are fixed. The observations in \(I_k\) are independent draws from the target distribution and are independent of those predictors. For each fixed \(w\in\mathcal W_0\), Hoeffding's inequality bounds the probability that the fold's average loss differs from \(R_k(w)\) by more than \(t_k\) by \(2\exp(-2m_kt_k^2/B^2)\). Choose $$ t_k=B\sqrt{\frac{\log(2K|\mathcal W_0|/\delta)}{2m_k}}. $$ The bound for one fold and one candidate is then \(\delta/(K|\mathcal W_0|)\). A union bound over every fold and candidate shows that, with probability at least \(1-\delta\), every fold average differs from its fold risk by at most \(t_k\). This union bound does not require the fold averages to be mutually independent.

On that simultaneous event, the pooled empirical criterion differs from \(R_{\mathrm{CF}}(w)\) by at most \(\sum_k(m_k/n)t_k\), for every candidate \(w\). Compare the selected \(\hat w\) with a minimizer of \(R_{\mathrm{CF}}\) in the finite set. The empirical minimizing property gives the same three-step comparison as in the validation proof: population risk of \(\hat w\) is at most its empirical risk plus the deviation bound; that empirical risk is no larger than the comparator's; and the comparator's empirical risk is at most its population risk plus the deviation bound. The total penalty is twice \(\sum_k(m_k/n)t_k\), as claimed. \(\square\)

For equal-sized folds, the penalty is \(2B\sqrt{\log(2K|\mathcal W_0|/\delta)/(2m)}\), where \(m=n/K\). The proof conditions separately on the training data for each fold and then uses a union bound. It does not incorrectly treat all out-of-fold losses as independent: the fitted models overlap in their training data, and different fold averages can be dependent.

Worked Example: Choosing a Weight From Out-of-Fold Predictions

Consider four observations split into two folds of size two. Suppose the outcomes are \(y=(1,2,3,4)\), and the out-of-fold predictions from members \(A\) and \(B\) are \(A=(0,2,2,5)\) and \(B=(2,1,4,3)\). Each entry of \(A\) or \(B\) was produced by a model fitted without the fold containing that observation. For \(g_\alpha=\alpha A+(1-\alpha)B\), compute the pooled squared error.

Here \(B-y=(1,-1,1,-1)\) and \(A-B=(-2,1,-2,2)\). Consequently, $$ \sum_{i=1}^4(g_\alpha(X_i)-y_i)^2 =4-14\alpha+13\alpha^2. $$

The derivative is \(-14+26\alpha\), so the minimum occurs at \(\hat\alpha=7/13\), within \([0,1]\). The minimized sum of squares is \(4-98/13+637/169=39/169\), giving pooled mean squared error \(39/676\). This is an optimization of the cross-fitted criterion. The corresponding risk comparison is to the fold-weighted risk \(R_{\mathrm{CF}}\) of the fold-trained members, subject to conditions such as those in the theorem.

When Cross-Fitted Weights Are Applied to Refit Models

In practice, one may estimate weights from out-of-fold predictions and then refit each member on all available development data before deployment. That can be sensible, but the cross-fitted risk bound alone does not establish the risk of the full-data refits. The fold-specific members were trained on fewer observations and differ across folds; the deployed members are different fitted predictors.

A sufficient way to make a comparison is to assume a transfer bound. Let \(R_{\mathrm{full}}(w)\) be the risk of the full-data-refitted ensemble, conditional on the full development data, evaluated on a fresh observation. If, for every \(w\in\mathcal W_0\), $$ |R_{\mathrm{full}}(w)-R_{\mathrm{CF}}(w)|\leq\Delta, $$ then the cross-fitting theorem implies $$ R_{\mathrm{full}}(\hat w)\leq \min_{w\in\mathcal W_0}R_{\mathrm{full}}(w) +2\sum_{k=1}^K\frac{m_k}{n}B\sqrt{\frac{\log(2K|\mathcal W_0|/\delta)}{2m_k}}+2\Delta. $$ Indeed, transfer from the full-data risk of \(\hat w\) to its cross-fitted risk costs at most \(\Delta\); the cross-fitted oracle comparison adds the theorem's penalty; and transferring the minimizing cross-fitted risk back to the full-data risk costs at most another \(\Delta\). The assumption may follow from appropriate stability or convergence properties of the fitting procedure, but it is not automatic.

Worked Example: Accounting for Refit Transfer

Suppose an applicable cross-fitting bound gives \(R_{\mathrm{CF}}(\hat w)\leq\min_wR_{\mathrm{CF}}(w)+0.08\), and suppose a separate argument establishes \(\Delta=0.02\) uniformly over the candidate weights. Then $$ R_{\mathrm{full}}(\hat w)\leq \min_wR_{\mathrm{full}}(w)+0.08+2(0.02) =\min_wR_{\mathrm{full}}(w)+0.12. $$

The two transfer terms are both needed: one compares the selected cross-fitted ensemble with its refit, and the other compares the cross-fitted oracle with the full-data oracle. Without a bound such as \(\Delta\), the numerical cross-fitting result does not imply this full-data risk comparison.

Common Pitfall and Practical Implications

  • Using fitted predictions as if they were held out. The error of a model on observations used to fit it need not estimate its fresh-observation risk. Weight fitting can exploit this optimism. Use an independent validation split or genuinely out-of-fold predictions.
  • Calling the minimized validation score an unbiased risk estimate. Even when each fixed candidate's validation loss is unbiased conditional on the training data, selecting the smallest score creates a search effect. Use a separate evaluation sample or a justified uniform risk bound.
  • Claiming cross-fitting proves the risk of full-data refits. Cross-fitted predictions evaluate fold-trained members. Transfer to full-data-refitted members requires an additional argument.
  • Treating fold losses as independent. Their member fits share data. A valid argument can condition fold by fold and combine bounds by a union bound, as above; it must not assume unconditional independence without justification.

Sample splitting uses fewer observations to fit the members, while cross-fitting lets each observation contribute an out-of-fold loss and uses most observations for each fold-specific fit. Neither procedure removes the need to specify the risk target. The central check is whether the predictions used to estimate weights were produced without their outcomes, and whether the risk comparison concerns those same fitted predictors or a justified refit.

Check Your Understanding

Answer each question by identifying the relevant risk target and the assumptions it needs.

  1. Why is a validation loss for a fixed weight vector different, as an estimator, from the validation loss after minimizing over candidate weights?
  2. In the finite-class validation bound, what role do the bounded-loss and finite-candidate assumptions play?
  3. Why can the cross-fitting proof use a union bound even when losses from different folds are dependent?
  4. What member predictors define \(R_{\mathrm{CF}}(w)\), and how does that target differ from the risk of full-data refits?
  5. If a uniform transfer bound is \(\Delta\), why does the full-data oracle comparison pay \(2\Delta\) rather than \(\Delta\)?
Clinical Biostats