Tutorials › Exam SRM - Statistics for Risk Modeling › Cross-Validation Error For Elastic Net

Exam SRM · Elastic Net and Validation · Tutorial 246 of 926

Cross-Validation Error For Elastic Net

Learn how to turn held-out predictions from a fixed elastic net configuration into a cross-validation estimate of prediction error.

Advanced 12 min read

What You'll Learn

  • Define a fixed elastic net configuration and its fold-specific fitted models
  • Calculate cross-validation error from held-out observations and predictions
  • Apply squared-error loss and interpret its units
  • Explain why training residuals are not cross-validation error
  • Pool losses correctly when fold sizes are unequal
  • Describe what training-set size the estimate represents

Why Held-Out Prediction Error Matters

An elastic net model can fit the observations used to estimate its coefficients quite closely, but that does not by itself show how well it predicts new outcomes. Training errors are calculated on data that helped determine the model. Cross-validation instead evaluates predictions for observations withheld from the fit that produced those predictions.

Here the target is the cross-validation estimate of prediction error for one fixed elastic net configuration. A configuration specifies choices such as the mixing parameter \(\alpha\), the penalty parameter \(\lambda\), and any other parts of the fitting procedure that are being held fixed. The coefficients are still estimated afresh from the training data available in each fold. Thus, “fixed configuration” does not mean “fixed coefficient estimates.”

Definition: Let the observed data be \((x_i,y_i)\), for \(i=1,\ldots,n\), where \(x_i\) is the predictor vector and \(y_i\) is the observed response. Partition the observations into disjoint validation sets \(V_1,\ldots,V_K\), whose union is all \(n\) observations. For fold \(j\), fit the elastic net with the fixed configuration on the observations outside \(V_j\). Write the resulting prediction function as \(\hat f^{(-j)}_{\alpha,\lambda}\). For a specified loss function \(L(y,\hat y)\), the cross-validation estimate is $$ \widehat{\operatorname{Err}}_{\mathrm{CV}}(\alpha,\lambda) = \frac{1}{n}\sum_{j=1}^{K}\sum_{i\in V_j} L\!\left(y_i,\hat f^{(-j)}_{\alpha,\lambda}(x_i)\right). $$ Each observation contributes exactly one loss: the loss from the model fit without that observation’s fold.

For squared-error prediction, the loss is \(L(y,\hat y)=(y-\hat y)^2\). The estimate is then the average of the squared differences between observed responses and their held-out predictions. Its units are the square of the response units: if the response is measured in dollars, squared error is measured in dollars squared. It is not the average absolute prediction error and is not the square root of the average squared error.

Why the Formula Uses Held-Out Predictions

For a particular fold, every observation in \(V_j\) is absent from the data used to fit \(\hat f^{(-j)}_{\alpha,\lambda}\). Consequently, its response did not help estimate the coefficients of that fold’s model. The prediction error on \(V_j\) therefore gives a more direct check of prediction than the residuals from fitting on those same observations.

The averaging formula follows by counting observations. Define the fold error as the mean loss within that fold: $$ \widehat{\operatorname{Err}}_j = \frac{1}{|V_j|}\sum_{i\in V_j} L\!\left(y_i,\hat f^{(-j)}_{\alpha,\lambda}(x_i)\right). $$ The sum of the losses over all folds is \(\sum_j |V_j|\widehat{\operatorname{Err}}_j\). Since the validation folds partition the data, \(\sum_j |V_j|=n\). Dividing the total loss by \(n\) therefore gives $$ \widehat{\operatorname{Err}}_{\mathrm{CV}} = \sum_{j=1}^{K}\frac{|V_j|}{n}\widehat{\operatorname{Err}}_j. $$ So the overall estimate is a size-weighted average of fold errors. If all folds have the same size, this reduces to the unweighted mean of the fold errors. If they do not, weighting each fold equally generally gives a different answer.

This calculation estimates the prediction error of a fitting procedure trained on roughly the number of observations outside one validation fold. It does not exactly measure the error of a final model refitted on all \(n\) observations, because that final model has a larger training sample. The estimate is useful for assessing the fixed configuration, but its interpretation depends on how the data and folds were formed.

Worked Example: Averaging Squared Errors Across Equal Folds

A hypothetical insurer uses an elastic net to predict an account-level annual service cost, in hundreds of dollars. The configuration is fixed at \(\alpha=0.6\) and \(\lambda=1.2\). Four observations are divided into two validation folds of size two. The fold-specific held-out predictions are shown below. Calculate the cross-validation estimate using squared-error loss.

FoldObserved responseHeld-out prediction
1109
11211
2810
21513

Method. For each row, subtract the held-out prediction from the observed response, square the difference, and average the four losses.

Fold 1 has residuals \(10-9=1\) and \(12-11=1\), giving squared losses \(1^2=1\) and \(1^2=1\). Its fold mean squared error is \((1+1)/2=1\).

Fold 2 has residuals \(8-10=-2\) and \(15-13=2\), giving squared losses \((-2)^2=4\) and \(2^2=4\). Its fold mean squared error is \((4+4)/2=4\).

The folds are equal in size, so averaging their fold errors gives \((1+4)/2=2.5\). Equivalently, the total squared loss is \(1+1+4+4=10\), and \(10/4=2.5\).

Answer and check. The cross-validation estimate is \(2.5\) hundreds-of-dollars squared. The pooled calculation and the equal-fold calculation agree, as they should for equal-sized folds. The common mistake to avoid is averaging the signed residuals: \(1,1,-2,2\) average to \(0.5\), which is not the squared-error estimate.

Unequal Fold Sizes and Correct Weighting

When folds have different sizes, each held-out observation should have the same weight in the overall mean unless the problem explicitly specifies a different target. An unweighted average of fold means gives each fold equal influence, not each observation. A small fold would then count as much as a large fold.

Worked Example: Pooling Losses When Folds Differ in Size

A hypothetical model for repair costs is assessed on five held-out observations using one fixed elastic net configuration. Squared-error losses, already calculated from the held-out predictions, are \(1,4,0\) in the first fold and \(9,1\) in the second fold. The fold sizes are three and two. Find the overall cross-validation estimate and compare it with the unweighted mean of fold errors.

The first fold’s total loss is \(1+4+0=5\), so its mean loss is \(5/3\). The second fold’s total loss is \(9+1=10\), so its mean loss is \(10/2=5\).

Pool the five observation-level losses to obtain the cross-validation estimate: \((1+4+0+9+1)/5=15/5=3\). The size-weighted fold calculation gives the same result: \((3/5)(5/3)+(2/5)(5)=1+2=3\).

The unweighted mean of the fold means would be \((5/3+5)/2=(5/3+15/3)/2=(20/3)/2=10/3\), approximately \(3.3333\). That is not the mean loss across the five observations.

Answer and check. The correct overall estimate is \(3\) squared cost units. The calculation is checked by both pooling all five losses and weighting each fold mean by its share of the observations. The common mistake is to use \(10/3\), which overweights the smaller fold.

What “Fixed Configuration” Holds Constant

The elastic net configuration is fixed across folds, while the fitted model changes with the training observations. For example, the same \(\alpha\) and \(\lambda\) are used for each fit, but different training subsets can produce different coefficient estimates and therefore different held-out predictions. This distinction matters: cross-validation assesses the performance of a specified fitting rule, not a single coefficient vector estimated once from the full data.

A fixed loss must also be used for every held-out observation in the calculation. For squared-error loss, a prediction that misses by \(3\) contributes \(9\), regardless of whether that miss is above or below the observed response. The loss is nonnegative, so large misses can contribute substantially to the mean.

A standard interpretation assumes observations are suitably exchangeable for the chosen validation design and that each held-out prediction is produced without using its response in the fit. If observations have a structure that makes ordinary random partitioning inappropriate, the validation design must reflect that structure. The formula alone cannot repair an unsuitable way of splitting dependent or ordered data.

Worked Example: Fixed Tuning Values, Different Fold Fits

A hypothetical benefit administrator predicts a continuous claim-processing time, in days. The fixed elastic net configuration is \(\alpha=0.75\), \(\lambda=0.8\), with squared-error loss. Three validation folds each contain two observations. The predictions below were generated by fitting the same configuration separately on the observations outside each fold.

FoldObserved timeHeld-out prediction
165
1911
278
2108
31210
389

Step 1: Calculate residuals and squared losses. Fold 1 residuals are \(6-5=1\) and \(9-11=-2\), so its squared losses are \(1\) and \(4\). Fold 2 residuals are \(7-8=-1\) and \(10-8=2\), so its squared losses are \(1\) and \(4\). Fold 3 residuals are \(12-10=2\) and \(8-9=-1\), so its squared losses are \(4\) and \(1\).

Step 2: Add and average all six losses. The total is \(1+4+1+4+4+1=15\). Therefore, the estimate is \(15/6=2.5\) days squared.

Answer and check. The cross-validation estimate is \(2.5\) days squared. Each fold has two observations, and each fold’s mean loss is \(5/2=2.5\), so their average also equals \(2.5\). The common mistake is to assume the coefficients are identical in all three fits merely because \(\alpha\) and \(\lambda\) are fixed; the training data, and generally the estimated coefficients, differ by fold.

Common Interpretation Traps

  • Using training residuals. The validation response must not have been used to fit the model that predicts it. In-sample residual loss answers a different question.
  • Confusing fixed tuning values with fixed coefficients. Hold the configuration constant; refit the elastic net on each fold’s training observations.
  • Taking the wrong average. Pool all observation-level losses, or weight fold means by fold size. An unweighted mean of fold means is justified only when fold sizes are equal.
  • Changing the loss midway. If the stated loss is squared error, square each residual before averaging. Averaging residuals, taking absolute values, or taking a square root produces a different quantity.
  • Overstating what the estimate represents. The cross-validation estimate describes held-out performance for the specified procedure and validation design. It is not the exact prediction error of a model fit on all observations.

For a fixed elastic net configuration, the essential calculation is direct: obtain one prediction for each observation from the fit that excluded its fold, calculate that observation’s loss, and average the \(n\) losses. This gives a clear, reproducible estimate of held-out prediction error without confusing tuning choices, fitted coefficients, and validation outcomes.

Check Your Understanding

For each problem, use the stated loss and include every held-out observation exactly once.

  1. A fixed elastic net configuration produces held-out responses \(4,7,9\) and predictions \(5,6,9\). Using squared-error loss, calculate the cross-validation estimate. (Answer: \(2/3\) response-units squared.)
  2. Two equal-sized folds have squared losses \(0,4\) in the first fold and \(1,9\) in the second. Find the overall estimate and the two fold mean losses. (Answer: overall estimate \(3.5\); fold means \(2\) and \(5\).)
  3. Fold A has four observations with total loss \(8\); Fold B has two observations with total loss \(6\). Find the pooled estimate and the unweighted mean of the fold means. (Answer: pooled estimate \(14/6=7/3\); unweighted mean of fold means \(2.5\).)
  4. For a fixed configuration, the held-out residuals are \(-2,1,3\), and squared-error loss is specified. Calculate the estimate and explain why averaging the residuals is not the answer. (Answer: squared-error estimate \(14/3\); residual averaging gives a signed mean, not mean squared loss.)
  5. Two folds have sizes three and one. Their fold mean squared errors are \(2\) and \(10\), respectively. Calculate the observation-weighted cross-validation estimate and compare it with the unweighted fold average. (Answer: weighted estimate \(4\); unweighted fold average \(6\).)
Clinical Biostats