Homework 03
Before you begin
Homework 03 contains five required questions on AI-agent skill design, ridge regression, coefficient stability, optimization, and penalty selection. It is evaluated as Complete or Incomplete. For Questions 2 and 5, use either R or Python. You are not expected to use both languages.
Use an AI agent as directed in Question 1. For Questions 2 through 5, 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
The ZIP file contains the two editable QMD files, data/realestate.csv, and data/realestate-split-folds.csv. The simulation in Question 2 must be generated from the model stated below.
Question 1: Build an explain-to-me skill
Create a small skill named explain-to-me that helps you understand a homework question.
Design your
SKILL.mdfile so that, when you explicitly ask your agent to use the skill to read a homework question, the agent communicates with you and explains what the question is asking.Keep the skill file short. It should contain a few useful rules, instructions, and a clear statement of your intent. For example, you may ask the agent to explain the goal of the question, clarify what work is required, and point you to an appropriate section of the Week 3 lectures: Ridge Regression: Stability Through Shrinkage or From a Penalized Objective to a Fitted Ridge Model. The skill should help you understand the question without completing the solution for you.
Create the skill yourself or with the help of AI. Save it as
explain-to-me/SKILL.md. Then ask your AI agent to install the skill using its normal skill-installation method. Test it by explicitly asking the agent to useexplain-to-meto read and explain Question 2.If the skill does not work well, or if it begins to disturb your usual workflow, ask your agent to remove the installed skill. You may revise and reinstall it if you wish.
In all cases, append the complete contents of your developed
explain-to-me/SKILL.mdfile at the end of your Homework 3 submission under the headingexplain-to-meskill.
Question 2: Ridge regression with nearly collinear predictors
Let . Generate mutually independent standard normal random variables
and define
Center and standardize each column of to have mean zero and average squared value one, following the lectureβs scaling convention.
Continue to use for the standardized matrix and keep it fixed throughout the question, where is the number of predictors. The unpenalized intercept gives total fitted coefficients. Let
Generate a training response and an independent test response at the same predictor values:
where
Center the training response as . For each , fit the slopes using the lectureβs ridge formula:
The unpenalized intercept is ; add this training mean back when predicting either response. The case is ordinary least squares. Use seed 43231 before generating the design and responses. R and Python use different random-number generators, so their exact numerical results need not agree.
Generate once. Report the correlation between its first two columns and its largest and smallest singular values. Use these quantities to explain why the estimator contrast should be much more variable than .
Independently repeat the generation of and , followed by all three fits, 200 times while keeping fixed. Draw the training and test noise inside each repetition, then reuse that realized pair for all three values of . For each value of , report the empirical mean and standard deviation of
Also report the average training MSE and the average test MSE calculated from the independent test response, where
- Interpret the coefficient estimates and test MSE using bias and variance. Explain why ridge can substantially stabilize the individual coefficients without producing an equally large change in the fitted values.
Question 3: What does ridge regression shrink?
Let be a centered predictor matrix, and let be the centered response. Use the supplied predictor scales for this algebraic question; the columns need not have unit variance. Here counts predictors, contains their slopes, and counts all fitted coefficients, including the separately fitted, unpenalized intercept. Ridge regression minimizes
- Derive the ridge normal equation
Explain why it has a unique solution whenever , even if is rank deficient or .
- Let
be a compact singular value decomposition, where , , , and . Show that ridge multiplies the fitted response component in the th left singular-vector direction, for , by
Suppose , , and the nonzero singular values are
Calculate the three shrinkage factors and the total effective degrees of freedom, including the unpenalized intercept:
- Which direction receives the strongest shrinkage? Explain why shrinking this direction can reduce prediction variance. Under what circumstance could the same shrinkage produce substantial prediction bias?
Question 4: Ridge regression and optimization
Let be a centered predictor matrix with . Its columns need not have unit variance in this question. The intercept is fitted separately and is not penalized. Suppose the two eigenvalues of
are and . Ridge regression with has curvature matrix
Calculate the condition numbers of and . Explain what the change says about the geometry of the ridge objective.
Gradient descent uses
Let be the ridge minimizer and define the optimization error
Show that the component of in an eigendirection with unpenalized eigenvalue is multiplied at each step by
Let , the largest eigenvalue of . Find the interval of step sizes that contracts every eigendirection. Then compare these two choices:
- For , calculate the two contraction factors and explain which direction converges faster.
- For , determine whether the method converges. Assume the initial error has a nonzero component in the eigendirection corresponding to .
- Increasing generally improves the condition number. Explain why choosing to make gradient descent converge as quickly as possible is not a valid statistical tuning rule.
Question 5: Selecting the ridge penalty for real-estate prediction
The supplied data/realestate.csv contains 414 real-estate transactions from Sindian District, New Taipei City, Taiwan. The quantitative response price is the unit house price. The six predictors are the transaction date, house age, distance to the nearest mass rapid transit station, number of nearby convenience stores, latitude, and longitude. Treat all six predictors as quantitative in this question. The column row_id identifies an observation and must not be used as a predictor.
The file data/realestate-split-folds.csv assigns 332 observations to the training set and 82 observations to the final test set. It also gives ten fold labels for the training observations. Match the two files using row_id and do not use the test responses while selecting a model.
Use the penalty grid
For every fit, center and standardize each predictor using only the observations available to that fit, with the variance divided by the number of fitting observations. Center the response and leave the intercept unpenalized.
- Perform ten-fold cross-validation over using the supplied training folds. Estimate the predictor means and scales separately within each set of nine training folds. For each , calculate the mean validation MSE and , the sample standard deviation of the ten fold errors divided by . Use this SE for the conventional one-standard-error heuristic. Plot the mean validation MSE and identify . Choose as the largest candidate in , including OLS, satisfying
- Use all training observations to calculate the lectureβs GCV criterion for each :
Here is the training MSE, and includes the unpenalized intercept, as in Question 3. Estimate means and scales once from all training observations and keep them fixed for this calculation. Plot the GCV curve and identify .
- Fix all three choices before examining the test responses. Refit OLS and ridge at , , and using all training observations. Report each modelβs training MSE, test MSE, and effective degrees of freedom. Interpret the differences and explain why the model with the smallest observed test MSE must not be selected after viewing this table.
After matching the two files by row_id, use this order:
for each validation fold:
estimate predictor means and scales from the other nine folds
for each lambda:
fit on those nine folds and record validation MSE
summarize the ten fold errors and fix lambda_min and lambda_1se
compute GCV using all training observations and fix lambda_GCV
only then refit the four reported models and evaluate the test responses
Reference
The real-estate data come from I-Cheng Yehβs Real Estate Valuation dataset in the UCI Machine Learning Repository. The distributed data retain the original observations and add only the course train-test and fold assignments.