Validation Strategies and Data Leakage › Formula
Machine Learning from ScratchAll chapters Validation Strategies and Data Leakage › FormulaSection view · Sections in this chapter4 / 10 ↓
Reading tools Definitions, links & notes
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
The formulas separate the population quantity we want from the sample quantity we can compute. We first define risk and held-out loss, then describe which observations may contribute to fitting. The grouped and temporal constraints express different deployment tasks; cross-validation repeats the corresponding fitting and evaluation procedure.
Let the collected observations be
Here is the feature vector, is the regression target, and identifies the customer or other group. The examples use the group column to construct splits, rather than as an input to 1-NN. We use squared loss, ; classification uses a loss suited to its target.
Training, validation, and final test roles
Let be the joint distribution of a fresh deployment observation , and let be a fixed predictor. Assume the loss has a finite expectation. Generalization risk is its expected loss:
We generally do not know this population expectation. Validation estimates it from a finite sample. The fitted model and all learned transformations must be evaluated under the information restrictions of the intended task, rather than on rows that contributed to their fitting or selection.
Let , , and be disjoint sets of row indices. The corresponding data subsets are denoted by , , and . Fit on the training subset and let be nonempty. Its empirical test risk is the average loss on the held-out indices:
Two details in that expression carry all the weight:
- We divided by , not by : the estimator is an average over the specific rows in the test set, not over the whole data.
- The model is fixed before those rows are scored; nothing about them influenced the fit.
Sufficient conditions for this additive mean loss to be conditionally unbiased for , given the fitted model, are:
- each with is drawn from the same distribution as the future data, and
- each test observation is statistically independent of the fitted model .
Under these conditions, conditioning on the fitted model leaves each test loss with expectation , so their mean has that expectation too. Dependence among test rows can still affect its variance. Correlated training and test observations can make the model and test rows statistically dependent even without direct test-data reuse. Distribution shift and adaptive use of test information can invalidate this argument. The result concerns the mean loss; applying a nonlinear operation such as a square root need not preserve unbiasedness.
In practice we divide into three disjoint sets with sizes , , and such that
- Training fits model parameters under a given hyperparameter setting.
- Validation compares candidate hyperparameters and chooses the best.
- Test assesses the chosen, frozen procedure on data that did not guide that choice.
The third role matters because validation outcomes guide model selection. Selecting the candidate with the best noisy validation result and reporting that same result can introduce optimism. Separate test data assess the choice without participating in it; another option is an outer resampling scheme that includes the entire selection procedure inside each outer training split.
A practical protocol is to inspect the final test result after choices are fixed. Recomputing the same frozen result does not itself change its statistical role. Changing the model, features, threshold, or evaluation protocol in response to that result uses it for validation; the revised procedure then needs fresh uninvolved evaluation data.
Allocations such as or are examples, not prescriptions. The needed sizes depend on the learning problem, event counts, group structure, and required precision. Cross-validation rotates validation folds within the development data after the final test portion has been reserved.
Preprocessing and target leakage
Preprocessing is part of the fitted procedure. Let be a scaler, imputer, encoder, or feature selector; fit its parameters on training data and fit the predictor on the transformed training rows. If the transform is instead learned from all collected rows, the resulting composite predictor is
The transform has used held-out information. A scaler or unsupervised imputer uses held-out feature values; a supervised selector or target encoder can also use held-out labels. This violates the standard inductive evaluation boundary even when the fitted predictor itself only receives training rows.
For example, a target encoder replaces category by its mean target. If is the category of row and occurs in the collected data, the full-data mean is
This mean includes the labels of held-out rows in category . It therefore cannot be used to encode those rows in an inductive test. A training-only mean is
Apply this fitted mapping to held-out categories. If a category has no training observations, the denominator is zero, so an explicit fallback is needed; the implementation uses the overall training-target mean. Training rows encoded using their own labels can still overfit even when the outer held-out labels are excluded. Inner cross-fitting can construct training encodings from other training folds; scikit-learn documents this behavior for TargetEncoder.fit_transform, whereas fit followed by transform does not invoke that cross-fitting scheme.
Target leakage occurs when a feature supplies the outcome, or information unavailable when that prediction would have been made. A discharge field recorded after the prediction origin or a refund recorded only after the predicted return are examples when those events have not yet occurred. A historical purchase count can be valid. Audit the computation and availability of each feature at the prediction origin; neither a grouped split nor a modest score proves that this audit has passed.
Stratified, grouped, and temporal splits
For an i.i.d. prediction task, a random row split can be appropriate. Exchangeability means the joint distribution is unchanged by permuting row order; it does not mean the rows are independent. Repeated entities and time order require checking whether the available information and dependence match the intended task, rather than treating every row as a fresh independent case.
Stratified splits aim to preserve class proportions across folds, up to integer and allocation constraints. If is the fraction of class among indices in fold , the aim is . This reduces variation in class counts, but does not create independence or preserve all sources of uncertainty. A rare class with fewer observations than folds cannot appear in every fold. When groups or time impose other constraints, exact class balance may be infeasible or inappropriate.
Grouped splits hold out whole entities for a new-entity task. To see why this can matter, consider with mutually independent, zero-mean residuals that are independent of the random group effects, all with finite second moments. Two distinct rows in the same group then have covariance , which is positive when that effect varies. A learner that recognizes the group may use its training history. For an unseen-group test, impose
Every group is assigned wholly to one side. This removes direct entity overlap for the chosen grouping level. Five hundred rows from five customers still provide only five sampled customer histories; their statistical information also depends on within-customer loss dependence and group sizes, so it is not generally equivalent to exactly five independent rows. Group disjointness does not ensure that customers are otherwise independent or representative of future customers.
Temporal splits are appropriate when the task predicts later observations from information available earlier. For a simple chronological hold-out, let be the event time and choose cutoff :
Training precedes the held-out event period in this construction, but event times alone do not establish label or feature availability. Delayed labels and overlapping outcome windows may require additional exclusions. A known future calendar value can be available at prediction time; an unobserved future outcome cannot. Future observations from existing patients call for chronology with eligible patient history. Future observations from previously unseen patients require both temporal and group hold-outs.
Out-of-fold predictions and reproducible evaluation
For the following cross-validation notation, use only development data after the final test set has been reserved, and let now denote the development sample size. Choose an integer with and partition the development indices into nonempty disjoint folds . Fit on all development rows outside , including fitting every learned transform there. The unweighted mean of the fold mean losses is
Here is the complete pipeline fitted without fold . For a complete K-fold partition, each development observation appears in exactly one held-out fold. Its out-of-fold prediction is
The function identifies the fold holding out row . An OOF prediction excludes that row from fitting its prediction pipeline, provided preprocessing and any selection are also restricted correctly. OOF values support error analysis, stacking, calibration, and threshold selection. If they guide a choice, they serve as validation data; the reused selected score is not an independent final test.
GroupKFold forms a complete partition of the groups, so every row can receive one group-held-out prediction. TimeSeriesSplit instead trains on earlier indices and tests on later blocks. With its default unbounded training window, successive training sets expand. Its initial training block receives no held-out prediction, and max_train_size can limit the training history. A manual temporal prediction array should leave unscored rows explicit; cross_val_predict requires every input row to appear in a test fold exactly once and therefore does not accept ordinary TimeSeriesSplit output.
Record the data snapshot, stable row identities, split configuration or exact fold assignments, and the fitted procedure. For a randomized splitter or estimator, also record the relevant randomness settings and dependency versions. A fixed seed does not protect against a changed row order or changed data. Save and apply indices to the same ordered dataset, or map them through stable row identifiers.
Mathematical properties
A few properties of the estimators above matter for how we interpret them.
CV fits use fewer development observations than a final fit on all development data. For equal row folds, each fit uses rows; grouped folds can differ in size. The CV result therefore describes those training sizes and held-out tasks. It can be pessimistic when the learning procedure improves with additional data, but its bias and variance do not have a universal direction or monotonic relationship with .
The variation of a held-out mean depends on the individual loss variances and their covariances. Under independent losses with a common finite variance , its variance is for scored rows. The general variance formula includes pairwise covariances; dependent losses can still be uncorrelated. CV fits share data, so treating the fold scores as independent replicates and dividing their standard deviation by does not give a general confidence interval.
For a chosen pair of evaluations, define a signed observed loss difference:
A positive difference means the evaluation labeled leaky reports a smaller loss in this pair; a negative value means it reports a larger loss. Its sign and magnitude are not guaranteed. If the two evaluations also change customers, training sizes, or fitted models, the gap does not isolate the causal effect of one information leak. The examples below diagnose the information boundary directly.
Loss units matter. Squared-error risk has squared-target units, whereas RMSE has target units. A gap between two RMSEs is therefore a separate descriptive quantity, not the same number as the squared-loss difference above. Neither gap is generally equal to a customer-effect variance.
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!