Homework 02 contains five required questions on training and test error, fixed-design prediction, model-selection criteria, and honest evaluation. It is evaluated as Complete or Incomplete. For Questions 1, 2, and 4, use either R or Python. You are not expected to use both languages.
Attempt each question before looking at the solutions or asking an AI tool for help. You may use AI tools, but you are responsible for checking the work and being able to explain your reasoning and results.
The ZIP file contains the two editable QMD files and data/diabetes.csv. The simulations in Questions 1 and 2 must be generated from the models stated below.
Question 1: Training and test error under a fixed design
Consider a linear regression problem with observations and available covariates. Generate one matrix whose entries are independent random variables, and then keep this realized matrix fixed. Let
and define
Generate one training response and one independent test response from
where
Here is the identity matrix.
For , fit a linear model with an intercept and the first columns of using . If is its fitted mean vector, define
and
Repeat this process independently 200 times, keeping fixed.
Use seed 43202 before generating and the response errors. R and Python use different random-number generators, so their exact numerical values need not agree.
For each , calculate the average training and test MSE over the 200 simulation runs. Plot both curves against . Verify numerically that training MSE does not increase as predictors are added in each simulation run.
For the model containing the first predictors, write
Here and . The model has fitted coefficients, including the intercept. Define its total squared approximation bias as
Thus, is the mean squared approximation bias.
Calculate the two theoretical expectations
and
Add these expectations to your plot and compare them with the simulation averages.
Add the test MSE curve from one simulation run to the plot. Explain why this single curve is less smooth than the average test MSE. Use the approximation-bias and estimation-variance terms in the expected test MSE to explain why training MSE cannot be used by itself to select the predictor count.
Question 2: Prediction error at one target point
Let , , and . Construct the fixed covariate matrix with entries
Its columns satisfy
Here is the identity matrix.
The following code shows a direct construction of .
n <-100p <-6X_all <-outer(1:n, 1:p,function(i, j) sqrt(2) *cos(pi * j * (i -0.5) / n))
Show the starter code
import numpy as npn, p =100, 6i = np.arange(1, n +1)[:, None]j = np.arange(1, p +1)[None, :]X_all = np.sqrt(2) * np.cos(np.pi * j * (i -0.5) / n)
Keep fixed and generate one training response from
where
Here is the identity matrix.
For , fit a linear model with an intercept and the first predictors. Consider prediction of the mean response at
Write
and let be the prediction from the model containing the first predictors. Here denotes the th coordinate of . For this design,
Calculate , the theoretical squared bias, and the expected squared error for using the formula above. You do not need to derive the formula. Briefly explain in words why it consists of a squared bias term from omitted predictors and a variance term from estimating coefficients; no proof is required. Identify the values of where the expected squared error changes and where it remains unchanged.
Use seed 43203. Generate the response, fit the models, and calculate
Repeat this independently 200 times. Plot the simulation averages and theoretical expectations together. Report the smallest that minimizes the theoretical error.
All six regression coefficients are nonzero. Explain why only predictors 2 and 5 contribute directly to the mean response at . Contrast this target-specific error with the test MSE averaged over all rows of in Question 1.
Question 3: The optimism correction
Choose one candidate model before observing the response. Let be its number of predictors and let be its full-rank design matrix, including the intercept. The model has fitted coefficients. Let
be its hat matrix. Suppose
Conditional on the fixed design , the two error vectors are independent, have mean zero, and have covariance matrix . Define
The conditional bias vector of the fitted mean is
Let denote the total squared approximation bias:
Thus, is the mean squared approximation bias.
You may use
For any fixed matrix , you may also use
Derive
and
Deduce the expected optimism, defined as . For
calculate the expected training MSE, expected test MSE, and their difference. Explain why the ordering need not hold for every realized pair of responses.
Continue with and . Suppose the candidate model has residual sum of squares , and suppose a common estimate of the error variance is .
Calculate
and
Show how these two quantities are related, and explain why minimizing is equivalent to minimizing the corrected test MSE when and are common to all candidate models.
Question 4: Comparing Mallowsβ , AIC, and BIC
Use data/diabetes.csv, with y as the response. Use rows 1 through 370 as the training data, and let denote the training sample size. Do not use rows 371 through 442 in this question. Fit the following ordinary least-squares models, each with an intercept.
Model
Predictors
Model A
bmi, bp, s5, sex, s1, s2, s4
7
Model B
all predictors in Model A, followed by s6
8
Full reference
all ten predictors
10
Here counts predictors, so each model has fitted coefficients including the intercept. Estimate one common noise variance from the full reference model, using :
For Models A and B, calculate
and
The reduced AIC and BIC omit constants shared by the two candidate models. Smaller values are preferred within each criterion.
Fit the three models. Report , RSS, the residual degrees of freedom of the full model, and .
Calculate , reduced AIC, and reduced BIC for Models A and B. State which model each criterion selects. Do not compare numerical values across different criteria.
Compare the improvement in fit from adding s6 with the one-parameter penalty under each criterion. Use this comparison to explain any disagreement. Why does selecting a model not establish that it is the true data-generating model?
Question 5: Validation and final test data
Use rows 1 through 370 of diabetes.csv as the training data and rows 371 through 442 as the final test data. Consider eleven nested candidate models. For , the model with predictors contains an intercept and the first predictors in this order:
age, sex, bmi, bp, s1, s2, s3, s4, s5, s6.
An analyst proposes the following procedure:
Fit all eleven candidate models using the training data.
Calculate the MSE of each model on the final test data.
Select the model with the smallest test MSE.
Report that minimum as the final estimate of prediction error.
The analyst argues that the procedure is valid because the final test data were not used to estimate the regression coefficients.
Identify the first step that misuses the final test data. Explain why the minimum of eleven test MSE values is generally too favorable as an estimate of the selected procedureβs future prediction error.
Rewrite the analysis using five-fold cross-validation within the training data. State how the predictor count is selected, what is refitted, and when the final test data may be used.
Suppose the analyst has already examined all eleven test MSE values. What can still be reported transparently, and what additional data would be needed for a new final evaluation?