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
Skip to main content

Homework 01

On this page

  • Before you begin
  • Question 1 (Least squares and matrix calculations)
  • Question 2 (Gaussian likelihood for linear regression)
  • Question 3 (Starting values in nonconvex optimization)
  • Question 4 (Nearly collinear predictors)
  • Question 5 (Data preparation and summary statistics)
  • Question 6 (Can 39 million Fitbit records represent US adults?)

Before you begin

Homework 01 contains six required questions on the mathematical and computing foundations used later in the course:

  1. matrix notation and least squares;
  2. the Gaussian likelihood for linear regression;
  3. nonconvex numerical optimization;
  4. near collinearity and coefficient instability;
  5. basic data preparation and summaries; and
  6. repeated measurements, selection bias, and causal interpretation in wearable data.

Complete all six questions. Use your work to identify any topics that you should revisit before they appear later in the course.

Questions 1 through 5 may be completed in either R or Python. Use software for numerical calculations, and show mathematical reasoning where a task asks for it. Use stable numerical routines, and explain the statistical meaning of each result rather than reporting code output alone.

NoteUsing an AI agent

First make a serious attempt using your current knowledge. You may then use an AI agent to clarify an error, check code, or compare approaches. You should still be able to explain every quantity that you report.

Download the materials

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

Extract the ZIP file inside the homework/ folder of your stat432-fall2026 repository. The ZIP creates week-01/, so the complete path is homework/week-01/:

week-01/
    ├── homework-01.qmd
    ├── solutions-01.qmd
    └── data/
        └── data-manipulation.csv

The data file can also be downloaded separately:

  • Small data table for Question 5

Work in homework/week-01/ and write your answers in homework-01.qmd. Render the completed file as homework-01.html or homework-01.pdf. That rendered report is the file you submit through GitHub.

Question 1 (Least squares and matrix calculations)

Set the random seed to 43201. Generate n=100n=100 observations with p=5p=5 predictors according to

xij∼iidN(0,1),εi∼iidN(0,0.72), x_{ij}\overset{\mathrm{iid}}{\sim}N(0,1), \qquad \varepsilon_i\overset{\mathrm{iid}}{\sim}N(0,0.7^2),

with all predictors and errors generated independently. Let

𝑿=[𝟏𝒙1⋯𝒙5],𝜷=(3,1.4,−0.9,0.7,0,1.1)𝖳, \mathbf X = \begin{bmatrix} \mathbf 1 & \mathbf x_1 & \cdots & \mathbf x_5 \end{bmatrix}, \qquad \boldsymbol\beta = (3,1.4,-0.9,0.7,0,1.1)^{\mathsf T},

and generate

𝒚=𝑿𝜷+𝜺. \mathbf y=\mathbf X\boldsymbol\beta+\boldsymbol\varepsilon.

  1. Generate the data and report the dimensions and rank of 𝑿\mathbf X. Calculate the least-squares estimate 𝜷̂\widehat{\boldsymbol\beta} using a numerically stable least-squares routine, without explicitly calculating (𝑿𝖳𝑿)−1(\mathbf X^{\mathsf T}\mathbf X)^{-1}. Compare the estimates with the coefficients used to generate the data.

  2. Calculate the fitted values, the residual vector 𝒓=𝒚−𝑿𝜷̂\mathbf r=\mathbf y-\mathbf X\widehat{\boldsymbol\beta}, and the training root mean squared error

RMSE⁡=(1n∑i=1nri2)1/2. \operatorname{RMSE} = \left( \frac{1}{n}\sum_{i=1}^{n}r_i^2 \right)^{1/2}.

  1. Report ‖𝑿𝖳𝒓‖∞\lVert\mathbf X^{\mathsf T}\mathbf r\rVert_\infty. Explain why this value should be close to zero and what it tells us about the residual vector.

Question 2 (Gaussian likelihood for linear regression)

Continue with the data from Question 1. Suppose

𝒀∣𝑿∼Nn(𝑿𝜷,σ2𝑰n). \mathbf Y\mid\mathbf X \sim N_n\left(\mathbf X\boldsymbol\beta,\sigma^2\mathbf I_n\right).

The log-likelihood, including the constant term, is

ℓ(𝜷,σ2)=−n2log⁡(2πσ2)−12σ2(𝒚−𝑿𝜷)𝖳(𝒚−𝑿𝜷). \ell(\boldsymbol\beta,\sigma^2) = -\frac{n}{2}\log(2\pi\sigma^2) -\frac{1}{2\sigma^2} (\mathbf y-\mathbf X\boldsymbol\beta)^{\mathsf T} (\mathbf y-\mathbf X\boldsymbol\beta).

  1. For fixed σ2\sigma^2, explain why maximizing ℓ(𝜷,σ2)\ell(\boldsymbol\beta,\sigma^2) over 𝜷\boldsymbol\beta is equivalent to minimizing the residual sum of squares. What does this imply about the maximum-likelihood estimate of 𝜷\boldsymbol\beta?

  2. For fixed 𝜷\boldsymbol\beta, differentiate the log-likelihood with respect to σ2\sigma^2 and derive its maximum-likelihood estimate. Calculate this estimate at 𝜷̂\widehat{\boldsymbol\beta} and compare it with the unbiased estimate of σ2\sigma^2. Explain why the two denominators differ.

  3. Let

𝒆x2=(0,0,1,0,0,0)𝖳. \mathbf e_{x_2}=(0,0,1,0,0,0)^{\mathsf T}.

Using the variance estimate from part b, plot

ℓ(𝜷̂+t𝒆x2,σ̂MLE2) \ell\left( \widehat{\boldsymbol\beta}+t\mathbf e_{x_2}, \widehat{\sigma}_{\mathrm{MLE}}^2 \right)

over a grid of tt values from −1.25-1.25 to 1.251.25. State where the maximum occurs and explain why this agrees with the least-squares result.

Question 3 (Starting values in nonconvex optimization)

Consider the function

f(x)=exp⁡(1.5x)−3(x+6)2−0.05x3, f(x)=\exp(1.5x)-3(x+6)^2-0.05x^3,

with derivative

f′(x)=1.5exp⁡(1.5x)−6(x+6)−0.15x2. f'(x)=1.5\exp(1.5x)-6(x+6)-0.15x^2.

  1. Plot f(x)f(x) over the interval [−40,7][-40,7]. Based on the plot, describe the important features of the objective function that may affect numerical optimization.

  2. Use BFGS to minimize f(x)f(x) twice, starting at x(0)=−15x^{(0)}=-15 and x(0)=0x^{(0)}=0. Supply f′(x)f'(x) to the optimizer. For each run, report the final value of xx, the final objective value, |f′(x)||f'(x)|, and whether the optimizer reported convergence.

  3. Explain why the two runs can converge to different answers. Which run gives the lower objective value? What additional evidence would be needed before claiming that this point is the global minimum?

Question 4 (Nearly collinear predictors)

Continue with 𝑿\mathbf X and 𝒚\mathbf y from Question 1. Set the random seed to 43202 and generate

ui∼iidN(0,1),i=1,…,n. u_i\overset{\mathrm{iid}}{\sim}N(0,1), \qquad i=1,\ldots,n.

Define

𝒙6=𝒙1+10−4𝒖,𝑿+=[𝑿𝒙6], \mathbf x_6=\mathbf x_1+10^{-4}\mathbf u, \qquad \mathbf X_+ = \begin{bmatrix} \mathbf X & \mathbf x_6 \end{bmatrix},

and let

𝒚⋆=𝒚+10−3𝒖. \mathbf y^\star=\mathbf y+10^{-3}\mathbf u.

Use a numerically stable least-squares routine throughout.

  1. Fit 𝒚\mathbf y using 𝑿\mathbf X and 𝑿+\mathbf X_+. For each design matrix, report the condition number and training RMSE. For the augmented fit, also report the coefficients of 𝒙1\mathbf x_1 and 𝒙6\mathbf x_6.

  2. Fit 𝒚⋆\mathbf y^\star using 𝑿+\mathbf X_+. Report the changes in the coefficients of 𝒙1\mathbf x_1 and 𝒙6\mathbf x_6. Compare the two fitted-value vectors using their root mean squared difference and maximum absolute difference.

  3. Use the identity

𝒚⋆−𝒚=10(𝒙6−𝒙1) \mathbf y^\star-\mathbf y = 10(\mathbf x_6-\mathbf x_1)

to explain why the fitted values change very little while the two coefficients change substantially. What does this example suggest about interpreting separate effects for nearly collinear predictors?

Question 5 (Data preparation and summary statistics)

The file data/data-manipulation.csv is a small constructed data table containing an observation identifier, a group label, two numeric predictors, and a response. Some response values are missing. For analyses involving the response, use only observations with a recorded response. Do not replace a missing response with zero or a group mean.

  1. Read the data and report its dimensions, column types, number of duplicated identifiers, and number of missing values in each column. Create an analysis table containing only observations with a recorded response, and define

𝚏𝚎𝚊𝚝𝚞𝚛𝚎_𝚜𝚞𝚖=𝚡𝟷+𝚡𝟸. \texttt{feature\_sum}=\texttt{x1}+\texttt{x2}.

  1. For each group, report the number of observations, the mean response, and the mean of feature_sum. State clearly which observations these summaries describe.

  2. Sort the analysis table by response from largest to smallest and report the first three rows, including observation_id, group, response, and feature_sum. Add code checks verifying that the number of retained rows equals the number of nonmissing responses in the original data, that the analysis table has no missing response values, and that its identifiers are unique.

Question 6 (Can 39 million Fitbit records represent US adults?)

Patten et al. (2026) describe Fitbit data from 59,018 participants in the All of Us Research Program. The dataset spans 14 years and contains more than 39 million daily step records. Participants contributed data through one of two routes:

  • Bring Your Own Device (BYOD): participants shared data from a Fitbit they already owned.
  • Wearables Enhancing All of Us Research (WEAR): invited participants received a Fitbit at no cost.

In the general activity cohort, the BYOD and WEAR groups contained 32,035 and 22,474 participants, respectively. The BYOD value is listed first in each comparison below:

  • 77.3% versus 55.1% reported being White;
  • 6.1% versus 15.2% reported annual household income between $10,000\$10{,}000 and $25,000\$25{,}000; and
  • median daily steps were 6,867 versus 5,797.

Suppose the target is the mean of the participant-specific average daily step counts among US adults during the study period, with each adult given equal weight. A researcher writes:

“This dataset contains more than 39 million daily Fitbit records. Therefore, the average of all recorded step counts should provide an accurate estimate of mean daily activity among US adults.”

  1. Are the 39 million daily records independent observations? What characteristics or behaviors could cause some participants to contribute more recorded days than others? Explain how averaging all recorded days would then weight participants unequally.

  2. BYOD participants reported a median of 6,867 daily steps, compared with 5,797 among WEAR participants. Does this comparison show that already owning a Fitbit causes people to walk more? Explain your answer and give at least one plausible alternative explanation based on how participants entered the two groups.

  3. A very large dataset can still produce a biased estimate of a population quantity when the people and observations entering the dataset are selected. Using this study, explain how selection of both participants and recorded days could make the observed data differ from the US adult population. Why might the naive average of all recorded daily step counts fail to estimate the stated target? You do not need to determine the direction of the bias.

Source

Patten, T., Preble, E. A., Master, H., et al. (2026). The All of Us Research Program’s wearables dataset. Nature Medicine, 32, 2302-2310.

STAT 432 | Basics of Statistical Learning

 
  • Instructor