Lesson 256 asked whether attaching several indicators makes judgments more accurate. The same question comes up in regression models. Adding one more explanatory variable almost always improves the fit to past data. But does it also improve the fit to data the model has never seen?
Lesson 588 counted testing several settings of a rule as the number of trials. In regression, the number of variables put into one model plays the same role. This lab fits regressions with 0 to 40 variables to synthetic data whose true relationship is set in advance. It measures how the in-sample error and the out-of-sample error diverge, and splits the source of that gap into bias and variance.
Regression with regularization and cross-validation are covered in Lesson 602. Every number in this lesson is an example computed from synthetic data and says nothing about the predictive power of any real variable.
1. Experiment design: synthetic data with a known true relationship
The data are synthetic data made with fixed seeds. In each period, 40 explanatory variables are drawn from independent standard normal distributions. The next period's return (%) is 0.5 + 0.8 × x1 + 0.5 × x2 + 0.3 × x3 + noise, and the noise is normal with a standard deviation of 4%. The 37 variables x4 to x40 are noise variables with no relationship to the return.
With these settings, the share of the return variance explained by the variables is 0.98 ÷ (0.98 + 16) = about 5.8%. This means that even if you knew the true coefficients exactly and applied them to new data, the coefficient of determination you could expect (the share of variance explained by the fitted values) is about 5.8%. The in-sample coefficient of determination fitted to the same sample usually comes out larger than this (section 2). It is common for the coefficient of determination in return prediction to be small, at a few percent; small does not automatically mean useless, and large does not automatically mean good.
The model is a hypothetical teaching rule. It fits a least-squares regression with an intercept and the first k variables, with k from 0 to 40. With k = 0, the model predicts the next return with one number, the mean of the training period. The true variables were placed first, so k = 3 has the same set of variables as the true model.
The training sample is 120 periods (10 years if monthly). The out-of-sample error is the mean squared error on new data made the same way; because the settings are known, it was computed exactly by formula rather than by drawing a test sample. The seeds were fixed before the lesson was written, 200 seeds starting from 20262101, and the numbers in the text are medians of the 200 seeds. The unit of error is % squared, and the noise variance of 16 is a floor that no model can lower.
2. Least-squares regression and reading coefficients
Least-squares regression picks the coefficients that make the sum of squared differences between fitted and actual values as small as possible. A coefficient of 0.8 means that when x1 is one standard deviation higher, the next period's return is on average 0.8 percentage points higher. A coefficient's unit is the unit of the outcome divided by the unit of the explanatory variable, so changing a variable's scale changes the size of its coefficient.
Coefficients have standard errors too. For seed 20262101, set as the first seed before the lesson was written, the model with the three true variables (k = 3) gave coefficients of x1 0.29 (standard error 0.46), x2 0.89 (0.44) and x3 0.60 (0.42). The true values are 0.8, 0.5 and 0.3. The coefficient on x1 was far below its true value, and those on x2 and x3 were above theirs.
If the standard error of one coefficient is around 0.4 with 120 observations, an effect with a true value of 0.3 is hard to tell apart from 0 at this sample size. The t-statistic of x3 (coefficient ÷ standard error) was 1.43, short of the commonly used threshold of 1.96. For this seed the in-sample coefficient of determination was 5.0%, and the median across 200 seeds was 7.2%. The in-sample coefficient of determination is larger than the true 5.8% because the coefficients were also fitted to the noise of that sample.
3. Adding variables makes in-sample and out-of-sample diverge
The medians of 200 seeds by number of variables k are as follows.
| Number of variables k | In-sample mean squared error | Out-of-sample mean squared error | In-sample coefficient of determination | Out-of-sample coefficient of determination |
|---|---|---|---|---|
| 0 | 16.86 | 17.04 | 0% | 0% |
| 2 | 15.55 | 16.39 | 5.8% | 4.0% |
| 3 | 15.31 | 16.43 | 7.2% | 3.9% |
| 10 | 14.47 | 17.53 | 13.5% | −2.6% |
| 20 | 12.87 | 19.37 | 22.0% | −13.8% |
| 40 | 10.29 | 24.27 | 38.2% | −41.8% |

The in-sample error falls with every variable added and reaches 10.29 at k = 40. This number, below the noise variance of 16, is a sign that the model has memorized the noise as well. The out-of-sample error is smallest at k = 2, at 16.39, and then grows again, reaching 24.27 at k = 40.
The out-of-sample coefficient of determination was computed as the percentage by which a model lowered the error compared with the model that predicts with the training-period mean (k = 0). This is the same comparison that Campbell and Thompson (2008), "Predicting Excess Stock Returns Out of Sample," used to set return predictions against the historical average. It is negative from k = 10 on. The share of seeds with an out-of-sample coefficient of determination above 0 was 92% at k = 3, 24% at k = 10 and 0% at k = 20. A model with an in-sample coefficient of determination of 38.2% did worse out of sample than a single mean.
4. Splitting into bias and variance
What remains of the out-of-sample error after subtracting the noise variance of 16 is the error from estimating the coefficients wrongly. Averaged over 200 seeds, this part splits exactly into two. One is the squared bias, the squared distance of the average coefficients over 200 seeds from the true values. The other is the variance, how much the coefficients swing from seed to seed. A frequently cited text that frames this split as a trade-off in choosing model size is Geman, Bienenstock and Doursat (1992), "Neural Networks and the Bias/Variance Dilemma."
| Number of variables k | Squared bias | Variance |
|---|---|---|
| 0 | 0.98 | 0.12 |
| 1 | 0.34 | 0.27 |
| 2 | 0.09 | 0.40 |
| 3 | 0.00 | 0.53 |
| 10 | 0.01 | 1.63 |
| 40 | 0.05 | 8.62 |

Leaving out true variables creates bias. The squared bias of 0.98 at k = 0 is almost the same as the sum of the squares of the three missing effects, 0.8² + 0.5² + 0.3² = 0.98. From k = 3, with all the true variables in, the bias drops to near 0; the 0.05 at k = 40 is a small remainder due to the finite number of seeds. The variance, on the other hand, grows with every variable added, and grows fast as the number of variables approaches the number of observations.
This also shows why the out-of-sample error was smallest at k = 2 rather than k = 3. Adding x3 lowered the squared bias by 0.09 but raised the variance by 0.12. For a true variable with a small effect, the estimation noise with 120 observations is larger than the effect, so leaving it out gave a smaller out-of-sample error. The true model and the model that does best out of sample need not be the same.
5. Adding only noise variables still raises the coefficient of determination and yields significant coefficients
Fitting a model with only the 37 noise variables and none of the true ones gave an in-sample coefficient of determination with a median of 31.0% (5th to 95th percentile of 200 seeds 21.8 to 41.9%). Even with variables completely unrelated to the return, the model appears in sample to explain about three tenths of the variance.
In the model with all 40 variables, the number of noise-variable coefficients (out of 37) with an absolute t-statistic above 1.96 had a median of 2, and 80% of seeds had at least one. If you run 37 tests at the 5% level, the expected number that pass by chance is 37 × 0.05 = 1.85, so about two come out by luck. Picking these two variables to build a new model repeats the multiple testing of Lesson 588 inside a regression.
For the first seed, no noise variable's t-statistic exceeded 1.96, and the largest absolute value was 1.88. Looking at one seed alone, it is easy to read this as "noise variables get filtered out," but across 200 seeds they were not filtered out about four times in five.
6. Sample size, model size and the record
Doubling the sample to 240 periods shrinks the variance a lot. At k = 3 it fell from 0.53 to 0.27, and at k = 40 from 8.62 to 3.24. The out-of-sample error at k = 40 falls from 24.27 to 19.20, and the lowest point moves to k = 3, at 16.22. With more observations, including the small-effect x3 became the better choice. Even with the same number of variables, the degree of overfitting differs with the number of observations and the size of the effects.
In this experiment the out-of-sample error could be measured exactly by formula because the true relationship was known. With real data, the out-of-sample error has to be estimated with a held-out period or with cross-validation, and that estimate swings too (Lesson 602). The resulting numbers are values computed under these synthetic settings, not properties of any real variable.
The record for this lesson is a regression model box. Write down the number of candidate variables and the number actually included, the number of observations, and the in-sample and out-of-sample coefficients of determination (the benchmark being the training-period mean). Add the way the variables were chosen and the number of t-statistics looked at along the way.
Try it yourself
- From the table in section 3, read the in-sample and out-of-sample mean squared errors for k = 3 and k = 40, and write why the order of the two models flips between in sample and out of sample.
- From section 5, read the median and the 5th to 95th percentile of the in-sample coefficient of determination of the model with only the 37 noise variables, and choose two numbers to write alongside when recording such a model's results.
- From section 6, read the out-of-sample mean squared error at k = 40 and the k with the smallest error when the sample is extended to 240 periods, and compare them with 120 periods.
- Assumptions: with 120 observations, a model with an intercept and 10 variables has an in-sample mean squared error of 14.5. If you estimate the noise variance by dividing the residual sum of squares not by the number of observations but by the degrees of freedom (number of observations − number of estimated coefficients), what do you get?
Answers
First, k = 3 has 15.31 in sample and 16.43 out of sample, and k = 40 has 10.29 in sample and 24.27 out of sample. Adding variables fits the noise of that sample as well, so the in-sample error always falls. On new data that noise does not repeat, and only the swing of the coefficients (the variance) remains as error.
Second, the median is 31.0%, with a 5th to 95th percentile of 21.8 to 41.9%. Write the number of candidate variables (37) and the number of observations (120) together. Then a reader can see that with many variables relative to observations, an in-sample coefficient of determination this large appears even without any relationship.
Third, the out-of-sample error at k = 40 falls from 24.27 to 19.20, and the k with the smallest error moves from 2 to 3. With more observations the variance of the coefficients shrinks, so including the small-effect x3 gained more than it lost.
Fourth, the residual sum of squares is 14.5 × 120 = 1,740, and 11 coefficients were estimated. 1,740 ÷ (120 − 11) = about 15.96. Dividing by the number of observations, 14.5, understates the noise variance, and the gap grows with the number of variables.
Data: synthetic data made with fixed seeds 20262101 to 20262300 (200 seeds, set before the lesson was written). In each period there are 40 independent standard normal explanatory variables, and the next period's return = 0.5 + 0.8 × x1 + 0.5 × x2 + 0.3 × x3 + normal noise (standard deviation 4%). The training sample is the first 120 periods (240 periods in the doubling experiment). The numbers in the text are medians and percentiles of the 200 seeds, and the first seed, 20262101, was used only as an example of reading coefficients.
