STAT 432
  • Welcome
  • Lectures
    • Overview
    • Week 1: Setup and AI Tools
    • Week 2: Training and Test Error
    • Week 3: Ridge Regression and Optimization
    • Week 4: Lasso and Variable Selection
    • Week 5: K-Nearest Neighbors
    • Week 6: Classification Error and Evaluation
  • Discussion
  • Quizzes
  • Final Project
  • Syllabus
  • Canvas

On this page

  • Before you begin
  • Question 1: Training and test error under a fixed design
  • Question 2: Prediction error at one target point
  • Question 3: The optimism correction
  • Question 4: Comparing Mallows’ CpC_p, AIC, and BIC
  • Question 5: Validation and final test data
  • Reference
Skip to main content

Homework 02

Before you begin

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.

Download the materials

  • Download homework-02.qmd
  • Download solutions-02.qmd
  • Download combined ZIP file

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 n=100n=100 observations and p=20p=20 available covariates. Generate one matrix 𝑿allβˆˆβ„nΓ—p\mathbf X_{\mathrm{all}}\in\mathbb R^{n\times p} whose entries are independent 𝒩(0,1)\mathcal N(0,1) random variables, and then keep this realized matrix fixed. Let

Ξ²j=0.4j,j=1,…,p, \beta_j=0.4^{\sqrt{j}}, \qquad j=1,\ldots,p,

and define

𝝁=𝑿all𝜷. \boldsymbol\mu=\mathbf X_{\mathrm{all}}\boldsymbol\beta.

Generate one training response and one independent test response from

π’š=𝝁+𝝐,π’š*=𝝁+𝝐*, \begin{aligned} \mathbf y &=\boldsymbol\mu+\boldsymbol\epsilon,\\ \mathbf y^* &=\boldsymbol\mu+\boldsymbol\epsilon^*, \end{aligned}

where

𝝐,𝝐*∼ind𝒩n(𝟎,𝑰n). \boldsymbol\epsilon,\boldsymbol\epsilon^* \overset{\mathrm{ind}}{\sim} \mathcal N_n(\mathbf 0,\mathbf I_n).

Here 𝑰n\mathbf I_n is the nΓ—nn\times n identity matrix.

For m=0,1,…,pm=0,1,\ldots,p, fit a linear model with an intercept and the first mm columns of 𝑿all\mathbf X_{\mathrm{all}} using π’š\mathbf y. If π’šΜ‚m\widehat{\mathbf y}_m is its fitted mean vector, define

MSE⁑train,m=1nβˆ₯π’šβˆ’π’šΜ‚mβˆ₯22, \operatorname{MSE}_{\mathrm{train},m} =\frac{1}{n} \left\lVert \mathbf y-\widehat{\mathbf y}_m \right\rVert_2^2,

and

MSE⁑test,m=1nβˆ₯π’š*βˆ’π’šΜ‚mβˆ₯22. \operatorname{MSE}_{\mathrm{test},m} =\frac{1}{n} \left\lVert \mathbf y^*-\widehat{\mathbf y}_m \right\rVert_2^2.

Repeat this process independently 200 times, keeping 𝑿all\mathbf X_{\mathrm{all}} fixed.

Use seed 43202 before generating 𝑿all\mathbf X_{\mathrm{all}} and the response errors. R and Python use different random-number generators, so their exact numerical values need not agree.

  1. For each mm, calculate the average training and test MSE over the 200 simulation runs. Plot both curves against mm. Verify numerically that training MSE does not increase as predictors are added in each simulation run.

  2. For the model containing the first mm predictors, write

𝑿m=[𝟏,𝒙1,…,𝒙m],𝑯m=𝑿m(𝑿m𝖳𝑿m)βˆ’1𝑿m𝖳. \mathbf X_m = [\mathbf 1,\mathbf x_1,\ldots,\mathbf x_m], \qquad \mathbf H_m = \mathbf X_m (\mathbf X_m^{\mathsf T}\mathbf X_m)^{-1} \mathbf X_m^{\mathsf T}.

Here 𝑿mβˆˆβ„nΓ—(m+1)\mathbf X_m\in\mathbb R^{n\times(m+1)} and 𝑯mβˆˆβ„nΓ—n\mathbf H_m\in\mathbb R^{n\times n}. The model has m+1m+1 fitted coefficients, including the intercept. Define its total squared approximation bias as

Bm2=βˆ₯(𝑰nβˆ’π‘―m)𝝁βˆ₯22. B_m^2 =\left\lVert (\mathbf I_n-\mathbf H_m)\boldsymbol\mu \right\rVert_2^2.

Thus, Bm2/nB_m^2/n is the mean squared approximation bias.

Calculate the two theoretical expectations

E(MSE⁑train,mβˆ£π‘Ώall)=Bm2n+1βˆ’m+1n, E\!\left( \operatorname{MSE}_{\mathrm{train},m} \mid \mathbf X_{\mathrm{all}} \right) =\frac{B_m^2}{n}+1-\frac{m+1}{n},

and

E(MSE⁑test,mβˆ£π‘Ώall)=Bm2n+1+m+1n. E\!\left( \operatorname{MSE}_{\mathrm{test},m} \mid \mathbf X_{\mathrm{all}} \right) =\frac{B_m^2}{n}+1+\frac{m+1}{n}.

Add these expectations to your plot and compare them with the simulation averages.

  1. 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 n=100n=100, p=6p=6, and Οƒ2=1\sigma^2=1. Construct the fixed covariate matrix 𝑿allβˆˆβ„nΓ—p\mathbf X_{\mathrm{all}}\in\mathbb R^{n\times p} with entries

(𝑿all)ij=2cos⁑{Ο€j(iβˆ’12)n},i=1,…,n,j=1,…,p. (\mathbf X_{\mathrm{all}})_{ij} =\sqrt{2}\cos\left\{ \frac{\pi j(i-\tfrac12)}{n} \right\}, \qquad i=1,\ldots,n, \quad j=1,\ldots,p.

Its columns satisfy

πŸπ–³π‘Ώall=πŸŽπ–³,1n𝑿all𝖳𝑿all=𝑰p. \mathbf 1^{\mathsf T}\mathbf X_{\mathrm{all}}=\mathbf 0^{\mathsf T}, \qquad \frac{1}{n}\mathbf X_{\mathrm{all}}^{\mathsf T}\mathbf X_{\mathrm{all}}=\mathbf I_p.

Here 𝑰p\mathbf I_p is the pΓ—pp\times p identity matrix.

The following code shows a direct construction of 𝑿all\mathbf X_{\mathrm{all}}.

  • R
  • Python
Show the starter code
n <- 100
p <- 6
X_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 np

n, p = 100, 6
i = 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 𝑿all\mathbf X_{\mathrm{all}} fixed and generate one training response from

π’š=𝑿all𝜷+𝝐,𝜷=(0.5,0.5,0.5,0.5,0.5,0.5)𝖳, \mathbf y =\mathbf X_{\mathrm{all}}\boldsymbol\beta+\boldsymbol\epsilon, \qquad \boldsymbol\beta =(0.5,0.5,0.5,0.5,0.5,0.5)^{\mathsf T},

where

πβˆΌπ’©n(𝟎,𝑰n). \boldsymbol\epsilon \sim \mathcal N_n(\mathbf 0,\mathbf I_n).

Here 𝑰n\mathbf I_n is the nΓ—nn\times n identity matrix.

For m=0,…,pm=0,\ldots,p, fit a linear model with an intercept and the first mm predictors. Consider prediction of the mean response at

𝒙0=(0,1,0,0,1,0)𝖳. \mathbf x_0=(0,1,0,0,1,0)^{\mathsf T}.

Write

ΞΌ0=𝒙0π–³πœ·, \mu_0=\mathbf x_0^{\mathsf T}\boldsymbol\beta,

and let ΞΌΜ‚0,m\widehat\mu_{0,m} be the prediction from the model containing the first mm predictors. Here x0jx_{0j} denotes the jjth coordinate of 𝒙0\mathbf x_0. For this design,

E[(ΞΌΜ‚0,mβˆ’ΞΌ0)2βˆ£π‘Ώall]=(βˆ‘j=m+1px0jΞ²j)2+Οƒ2n(1+βˆ‘j=1mx0j2). E\!\left[ \left(\widehat\mu_{0,m}-\mu_0\right)^2 \mid \mathbf X_{\mathrm{all}} \right] = \left( \sum_{j=m+1}^{p}x_{0j}\beta_j \right)^2 +\frac{\sigma^2}{n} \left( 1+\sum_{j=1}^{m}x_{0j}^2 \right).

  1. Calculate ΞΌ0\mu_0, the theoretical squared bias, and the expected squared error for m=0,…,pm=0,\ldots,p 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 mm where the expected squared error changes and where it remains unchanged.

  2. Use seed 43203. Generate the response, fit the models, and calculate

(ΞΌΜ‚0,mβˆ’ΞΌ0)2. \left(\widehat\mu_{0,m}-\mu_0\right)^2.

Repeat this independently 200 times. Plot the simulation averages and theoretical expectations together. Report the smallest mm that minimizes the theoretical error.

  1. All six regression coefficients are nonzero. Explain why only predictors 2 and 5 contribute directly to the mean response at 𝒙0\mathbf x_0. Contrast this target-specific error with the test MSE averaged over all rows of 𝑿all\mathbf X_{\mathrm{all}} in Question 1.

Question 3: The optimism correction

Choose one candidate model before observing the response. Let pp be its number of predictors and let π‘Ώβˆˆβ„nΓ—(p+1)\mathbf X\in\mathbb R^{n\times(p+1)} be its full-rank design matrix, including the intercept. The model has p+1p+1 fitted coefficients. Let

𝑯=𝑿(𝑿𝖳𝑿)βˆ’1𝑿𝖳 \mathbf H =\mathbf X (\mathbf X^{\mathsf T}\mathbf X)^{-1} \mathbf X^{\mathsf T}

be its hat matrix. Suppose

π’š=𝝁+𝝐,π’š*=𝝁+𝝐*, \mathbf y=\boldsymbol\mu+\boldsymbol\epsilon, \qquad \mathbf y^*=\boldsymbol\mu+\boldsymbol\epsilon^*,

Conditional on the fixed design 𝑿\mathbf X, the two error vectors are independent, have mean zero, and have covariance matrix Οƒ2𝑰n\sigma^2\mathbf I_n. Define

MSE⁑train=1nβˆ₯π’šβˆ’π‘―π’šβˆ₯22,MSE⁑test=1nβˆ₯π’š*βˆ’π‘―π’šβˆ₯22. \operatorname{MSE}_{\mathrm{train}} = \frac{1}{n} \left\lVert \mathbf y-\mathbf H\mathbf y \right\rVert_2^2, \qquad \operatorname{MSE}_{\mathrm{test}} = \frac{1}{n} \left\lVert \mathbf y^*-\mathbf H\mathbf y \right\rVert_2^2.

The conditional bias vector of the fitted mean is

E(π‘―π’šβˆ£π‘Ώ)βˆ’π=βˆ’(𝑰nβˆ’π‘―)𝝁. E(\mathbf H\mathbf y\mid\mathbf X)-\boldsymbol\mu = -(\mathbf I_n-\mathbf H)\boldsymbol\mu.

Let B2B^2 denote the total squared approximation bias:

B2=βˆ₯(𝑰nβˆ’π‘―)𝝁βˆ₯22. B^2 =\left\lVert (\mathbf I_n-\mathbf H)\boldsymbol\mu \right\rVert_2^2.

Thus, B2/nB^2/n is the mean squared approximation bias.

You may use

𝑯𝖳=𝑯,𝑯2=𝑯,tr⁑(𝑯)=p+1. \mathbf H^{\mathsf T}=\mathbf H, \qquad \mathbf H^2=\mathbf H, \qquad \operatorname{tr}(\mathbf H)=p+1.

For any fixed matrix 𝑨\mathbf A, you may also use

E(ππ–³π‘¨πβˆ£π‘Ώ)=Οƒ2tr⁑(𝑨). E\!\left( \boldsymbol\epsilon^{\mathsf T} \mathbf A \boldsymbol\epsilon \mid \mathbf X \right) =\sigma^2\operatorname{tr}(\mathbf A).

  1. Derive

E(MSE⁑trainβˆ£π‘Ώ)=B2n+Οƒ2(1βˆ’p+1n), E(\operatorname{MSE}_{\mathrm{train}}\mid\mathbf X) =\frac{B^2}{n} +\sigma^2\left(1-\frac{p+1}{n}\right),

and

E(MSE⁑testβˆ£π‘Ώ)=B2n+Οƒ2(1+p+1n). E(\operatorname{MSE}_{\mathrm{test}}\mid\mathbf X) =\frac{B^2}{n} +\sigma^2\left(1+\frac{p+1}{n}\right).

  1. Deduce the expected optimism, defined as E(MSE⁑testβˆ’MSE⁑trainβˆ£π‘Ώ)E(\operatorname{MSE}_{\mathrm{test}}-\operatorname{MSE}_{\mathrm{train}}\mid\mathbf X). For

n=120,p=4,B2n=0.04,Οƒ2=1, n=120, \qquad p=4, \qquad \frac{B^2}{n}=0.04, \qquad \sigma^2=1,

calculate the expected training MSE, expected test MSE, and their difference. Explain why the ordering need not hold for every realized pair of responses.

  1. Continue with n=120n=120 and p=4p=4. Suppose the candidate model has residual sum of squares RSS⁑=116.4\operatorname{RSS}=116.4, and suppose a common estimate of the error variance is ΟƒΜ‚2=0.96\widehat\sigma^2=0.96.

Calculate

MSEΜ‚test=RSS⁑+2(p+1)ΟƒΜ‚2n, \widehat{\operatorname{MSE}}_{\mathrm{test}} =\frac{ \operatorname{RSS}+2(p+1)\widehat\sigma^2 }{n},

and

Cp=RSSΟƒΜ‚2βˆ’n+2(p+1). C_p =\frac{\operatorname{RSS}}{\widehat\sigma^2} -n+2(p+1).

Show how these two quantities are related, and explain why minimizing CpC_p is equivalent to minimizing the corrected test MSE when nn and ΟƒΜ‚2\widehat\sigma^2 are common to all candidate models.

Question 4: Comparing Mallows’ CpC_p, AIC, and BIC

Use data/diabetes.csv, with y as the response. Use rows 1 through 370 as the training data, and let n=370n=370 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 pp
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 pp counts predictors, so each model has p+1p+1 fitted coefficients including the intercept. Estimate one common noise variance from the full reference model, using p=10p=10:

ΟƒΜ‚2=RSS⁑fullnβˆ’(p+1). \widehat\sigma^2 =\frac{\operatorname{RSS}_{\mathrm{full}}}{n-(p+1)}.

For Models A and B, calculate

Cp=RSSΟƒΜ‚2βˆ’n+2(p+1), C_p =\frac{\operatorname{RSS}}{\widehat\sigma^2} -n+2(p+1),

AIC⁑red=nlog⁑(RSSn)+2(p+1), \operatorname{AIC}^{\mathrm{red}} =n\log\left(\frac{\operatorname{RSS}}{n}\right)+2(p+1),

and

BIC⁑red=nlog⁑(RSSn)+(p+1)log⁑(n). \operatorname{BIC}^{\mathrm{red}} =n\log\left(\frac{\operatorname{RSS}}{n}\right) +(p+1)\log(n).

The reduced AIC and BIC omit constants shared by the two candidate models. Smaller values are preferred within each criterion.

  1. Fit the three models. Report pp, RSS, the residual degrees of freedom of the full model, and ΟƒΜ‚2\widehat\sigma^2.

  2. Calculate CpC_p, reduced AIC, and reduced BIC for Models A and B. State which model each criterion selects. Do not compare numerical values across different criteria.

  3. 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 m=0,1,…,10m=0,1,\ldots,10, the model with mm predictors contains an intercept and the first mm predictors in this order:

age, sex, bmi, bp, s1, s2, s3, s4, s5, s6.

An analyst proposes the following procedure:

  1. Fit all eleven candidate models using the training data.
  2. Calculate the MSE of each model on the final test data.
  3. Select the model with the smallest test MSE.
  4. 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.

  1. 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.

  2. 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.

  3. 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?

Reference

James, Witten, Hastie, Tibshirani, and Taylor, An Introduction to Statistical Learning, Chapters 3, 5, and 6.

STAT 432 | Basics of Statistical Learning

 
  • Instructor