In Lesson 601, the least-squares regression with all 40 candidate variables fit best in sample and went most wrong out of sample. There are two broad ways to prevent this. One is regularization, which penalizes large coefficients and shrinks them toward 0. The other is cross-validation, which chooses a model by estimating its error on data it has not seen.
Used the wrong way, both methods only inflate confidence. This lab applies ridge and lasso to synthetic data made the same way as in Lesson 601, and chooses the strength of the penalty by cross-validation. It then measures what changes when cross-validation on time-series data with overlapping labels is done with random shuffling and when it keeps the time order.
Splitting periods and gaps were covered in Lesson 590, and the rule against normalizing with whole-period statistics in Lesson 583. Here we see how those rules are used inside the procedure for choosing a model.
1. Experiment design: independent data and data with overlapping labels
The first data set is fixed-seed synthetic data with the same settings as Lesson 601 and new seeds. Of 40 independent explanatory variables, only x1, x2 and x3 are related to the next period's return (coefficients 0.8, 0.5 and 0.3, noise standard deviation 4%), and the training sample is 120 periods. The seeds were set before the lesson was written: 100 seeds starting from 20263001. As in Lesson 601, the out-of-sample error was computed exactly by formula, thanks to the known settings.
The second data set is a time-series variant. All 20 explanatory variables move slowly (one-period autocorrelation 0.95), and the next period's return is 0.3 × x1 + 0.2 × x2 + noise (standard deviation 4%). The label is the sum of returns over the next 12 periods, so neighboring labels share 11 periods. After 240 training labels and a gap, a final test segment of 1,200 is held back, and the chosen model is run on it only once. The seeds are 100 seeds starting from 20263201.
Both models are hypothetical teaching rules that fit synthetic returns with synthetic explanatory variables. Standardizing the variables (subtracting the mean and dividing by the standard deviation) used only the mean and standard deviation of the training data each time. Standardizing with whole-period statistics mixes information from the test segment into training (Lesson 583). The numbers in the text are medians of 100 seeds.
2. Ridge: a penalty on the sum of squared coefficients
Ridge regression picks the coefficients that minimize the sum of squared errors plus the sum of squared coefficients × λ (lambda, the strength of the penalty). It was first set out by Hoerl and Kennard (1970), "Ridge Regression: Biased Estimation for Nonorthogonal Problems." With λ = 0 it is the same as least squares, and as λ grows all coefficients shrink toward 0 together. It is a method that deliberately accepts bias to reduce variance (the bias and variance of Lesson 601).
| λ (assumed grid) | Out-of-sample mean squared error | Coefficient size (square root of the sum of squares) |
|---|---|---|
| 0 (least squares) | 24.07 | 3.00 |
| 0.5 | 18.65 | 1.65 |
| 2 | 17.17 | 0.81 |
| 5 | 16.94 | 0.41 |
| 100 | 17.03 | 0.03 |

Adding the penalty cut the out-of-sample error from 24.07 to 16.94. But the out-of-sample error of the model that predicts with one number, the training-period mean, is 17.05, so even the best ridge is only slightly better than the mean. This is because ridge cannot set the 37 noise variables to 0; it shrinks them together with the true variables. Regularization guards against overfitting, but it does not create information that the data do not contain.
3. Lasso: a penalty on the sum of absolute coefficients sets some to 0
Lasso regression penalizes the sum of the absolute values of the coefficients. It was proposed by Tibshirani (1996), "Regression Shrinkage and Selection via the Lasso." Because of the shape of the penalty, small coefficients become exactly 0, which adds a variable-selection effect.
| λ (assumed grid) | Coefficients set to 0 (of 40) | Seeds keeping all 3 true variables | Out-of-sample mean squared error |
|---|---|---|---|
| 1.0 | 39 | 1% | 17.01 |
| 0.5 | 33 | 7% | 16.98 |
| 0.3 | 24 | 25% | 17.47 |
| 0.1 | 10 | 61% | 19.99 |
| 0.02 | 2 | 89% | 22.90 |
As λ shrinks, more seeds keep all the true variables, but more noise variables stay in with them and the out-of-sample error grows. No λ in this table keeps all three true variables while removing almost all of the noise variables. When effects are small and observations few, the list of variables chosen by the lasso differs from the list of true variables. That is why the variables the lasso keeps must not be read as "factors it found."
4. Choosing λ by cross-validation, and its optimism
K-fold cross-validation splits the training data into k groups and estimates the error by repeating k times: fit the model without one group and predict the group left out. In these data the periods are independent, so the groups can be split at random. Choosing the λ with the smallest 5-fold cross-validation error gave a median λ of 5 for ridge (5th to 95th percentile 1.95 to 100) and a median λ of 1.0 for lasso.
The out-of-sample error of the model chosen with ridge had a median of 17.03, smaller than least squares in all 100 seeds. But the cross-validation error of the chosen λ had a median of 16.69, and the out-of-sample error minus the cross-validation error, seed by seed, had a median of 0.54. Since the smallest of several λ values was picked, the cross-validation error of the chosen value is optimistic by about that much (the same structure as the selection bias of Lesson 588).
The model chosen with lasso kept a median of 1 variable, and only 4% of seeds kept all three true variables. Cross-validation only picks the λ that lowers the out-of-sample error; it is not a procedure for finding the true variables.
5. What random shuffling does with overlapping labels
In the second data set, neighboring labels share the same 11 periods of returns, so the one-period autocorrelation of the labels had a median of 0.91. In randomly shuffled 5-fold cross-validation, the immediate neighbors of the labels in the test group mostly (about four in five) end up in training. The explanatory variables also move slowly, so if the model memorizes the noise of that stretch, it nearly hits the neighboring labels.
Time-order cross-validation splits the periods into five consecutive groups and removes the 11 labels before and after the test group from training. This gap empties out the labels whose return periods overlap the test group; it is the same idea as the purging and embargo of Lesson 590 (López de Prado, 2018, Advances in Financial Machine Learning).
| λ (assumed grid) | Random cross-validation | Time-order cross-validation | Final test segment |
|---|---|---|---|
| 0 (least squares) | 104.18 | 449.00 | 417.48 |
| 0.1 | 102.52 | 333.20 | 327.02 |
| 1 | 126.25 | 230.37 | 242.16 |
| 10 | 175.60 | 210.31 | 214.96 |
| 100 | 191.34 | 210.96 | 216.43 |

Random cross-validation said that the weaker the penalty, the smaller the error, and the actual test error said exactly the opposite. The λ chosen by random cross-validation had a median of 0.05; that model's cross-validation error was 101.75, but its error in the final test segment was 356.65. The λ chosen by time-order cross-validation had a median of 50, with a cross-validation error of 204.26 and a test error of 225.36, a much smaller gap. The test error was smaller on the time-order side in 99 of 100 seeds.
An honest procedure does not guarantee a good model. The test error of the model using the training-period mean was 216.35, and the model chosen by time-order cross-validation did better than that in 42% of seeds. In this setting with a weak true relationship, honest cross-validation produced the result "no model clearly better than the mean was found," and that is the right conclusion for these data.
6. A chosen value is a trial too, and the final test segment is used only once
If you compared a grid of 12 λ values by cross-validation, you made 12 trials. If you repeated cross-validation while also changing the variable set or the label period, every one of those combinations counts in the number of trials (the trial record of Lesson 306). The numbers for the setting with the smallest cross-validation error are optimistic, as seen in section 4, so measure once more on a test segment left untouched after the choice.
If you change λ or the variables after seeing that test-segment result, the test segment is no longer out of sample (Lesson 476, Lesson 660). If you do change them, record a new version and wait for a new test segment. The resulting numbers are values computed under these synthetic settings, not properties of any real variable.
The record for this lesson is a model-selection box. Write side by side the type of regularization and the λ grid, and the segment on which the standardization statistics were computed. Add the cross-validation method (random or time-order) and the gap length, how many periods ahead the label looks, and the cross-validation error of the chosen value next to the error of the final test segment.
Try it yourself
- From the table in section 2, read the out-of-sample mean squared error and the coefficient size for λ = 0 and λ = 5, and compare them with the 17.05 of the model using only the training-period mean.
- From the table in section 3, read the number of coefficients set to 0 at λ = 0.3 and the share of seeds keeping all three true variables. How many variables did the lasso chosen by cross-validation keep?
- From section 5, read the λ chosen by random cross-validation and by time-order cross-validation, with each one's cross-validation error and final test-segment error, and write which numbers belong in a report.
- Assumptions: 240 labels are split into 5 consecutive groups (48 each), and each label looks 12 periods ahead. When the middle group is used for testing and 11 labels are emptied on each side, how many labels remain for training?
Answers
First, λ = 0 gives 24.07 and λ = 5 gives 16.94. The coefficient size shrank from 3.00 to 0.41. The penalty cut the error a lot, but the result is only slightly better than the 17.05 of the mean alone, so most of the reduction removed the overfitting of least squares.
Second, at λ = 0.3, 24 coefficients were set to 0, and 25% of seeds kept all three true variables. The lasso chosen by cross-validation kept a median of only 1 variable, and 4% of seeds kept all three.
Third, random cross-validation chose λ 0.05, with a cross-validation error of 101.75 and a test error of 356.65. Time-order cross-validation chose λ 50, with 204.26 and 225.36. In the report, write the setting chosen by time-order cross-validation and its test error. Write the 216.35 of the mean-only model next to it, to record that the model cannot be said to beat the mean.
Fourth, 240 − 48 − 11 × 2 = 170. The shrinking of the training data because of the gap is the cost of honest testing; the first and last groups empty only one side, so a few more labels remain.
Data: two synthetic data sets made with fixed seeds. The independent data use seeds 20263001 to 20263100 (100 seeds, the same settings as Lesson 601, 152 training periods). The time-series data use seeds 20263201 to 20263300 (100 seeds, 20 explanatory variables with autocorrelation 0.95, labels that sum returns over 12 overlapping periods, 240 for training and 1,200 for the final test). The seeds were set before the lesson was written, and the numbers in the text are medians and percentiles of the 100 seeds.
