🌏 中文版
Video status: Checked: no corresponding recording link listed on the public official page. Source details
⚠️ Edition: This guide follows
hw1_release.texfrom the CS181 2026 HW1 repository (course number CS1810-S26, due 2026-02-13 at 11:59 PM). Earlier years may differ in problems and points; if you are working from another year, go by that year's.tex.
TL;DR
HW1 is about regression, and all four problems use the same ice-core temperature data:
- kNN and Kernels (35 pts): run kNN, implement kernel regression yourself, and compare test MSE and complexity.
- Geometric Least Squares (20 pts): prove that OLS is the orthogonal projection of
yonto the column space ofX. - Basis Regression (30 pts): fit linear regression with four basis sets (polynomial, RBF, two cosine sets) and decide which ones overfit.
- Probabilistic View of Regression and Regularization (30 pts): derive ridge and LASSO from Gaussian and Laplace priors, then implement LASSO with coordinate descent.
By the end you will have seen non-parametric regression (kNN, kernels) and parametric regression (linear with bases) on the same data, and you will know what regularization means in probabilistic terms.
Why HW1 deserves its own guide
- One dataset, several models: the introduction states the goal outright: implement nearest-neighbor, kernelized, and linear regression on the same dataset and compare their tradeoffs.
- Half proofs, half code: Problem 2 and the first part of Problem 4 are derivations; Problems 1, 3, and the last part of Problem 4 are notebook work. Being good at only one of the two will slow you down.
- It counts: per the 2026 syllabus, each of HW1–HW6 is worth 11%.
The data and how to get it
The data lives in hw1/data/, in two files:
earth_temperature_sampled_train.csv(57 lines including a header)earth_temperature_sampled_test.csv(26 lines including a header)
Each row has two columns: the age of the ice-core sample (years before present), and the temperature difference (K) from the average of the preceding 1,000 years. The data comes from the EPICA Dome C ice core in Antarctica (Jouzel et al. 2007, Science). Because the ages are large numbers, the assignment works in thousands of years; the provided notebook does the conversion for you.
curl -O https://raw.githubusercontent.com/harvard-ml-courses/cs181-s26-homeworks/main/hw1/data/earth_temperature_sampled_train.csv
The four problems, one by one
Problem 1: kNN and Kernels (35 pts)
Both non-parametric regressions are here.
- kNN: the prediction is the mean
yof theknearest training points. The implementation is provided. You runk = 1, 3, N−1, plot the fits, describe how they change withk, and write code to compute the test MSE for eachk. - Kernel regression: the prediction is a weighted average
f_τ(x*) = Σ K_τ(x_n, x*) y_n / Σ K_τ(x_n, x*)with kernelK_τ(x, x') = exp(−(x − x')² / τ), whereτis the squared lengthscale.- Implement
kernel_regressorand plot the model from 800,000 BC to 400,000 BC at 1,000-year intervals forτ = 1, 50, 2500. - Write the test MSE as a formula, then compute it for the three
τvalues, explain which one wins, and explain why choosingτon the training set is a bad idea. - Compare the time and space complexity of kNN and kernel regression in terms of training-set size
N: what the model must store, and how much work one new prediction takes. - The last part asks for the exact form of
f_τ(x*)asτ → 0.
- Implement
Problem 2: Geometric Least Squares (20 pts)
No code here; it is all proofs. The setup: the column space C(X) of X ∈ ℝ^{N×D} is a subspace of ℝ^N, and OLS projects y orthogonally onto it.
- With
w* = (XᵀX)⁻¹Xᵀyandŷ = Xw*, prove thatŷis the orthogonal projection ofyontoC(X). - Prove that
ŷis the vector inC(X)closest toy: for everyv ∈ C(X),‖y − ŷ‖² ≤ ‖y − v‖². The hint is the Pythagorean identity for orthogonal vectors. - Prove that the hat matrix
P = X(XᵀX)⁻¹Xᵀis symmetric and idempotent, and that its rank and trace both equald. You may use the fact that an idempotent matrix has equal rank and trace. - When the residual plot shows a U shape instead of random scatter, explain what that means in terms of projection onto the column space.
Problem 3: Basis Regression (30 pts)
The raw input is one-dimensional (the year). This problem expands it with basis functions φ into a more expressive feature set and then runs linear regression. You implement four bases in make_basis, each with a bias term added:
| Basis | Form | Transform f(x) |
|---|---|---|
| (a) polynomial | φ_j(x) = f(x)^j, j = 1…9 | x / 181 |
| (b) RBF | φ_j(x) = exp(−(f(x) − μ_j)² / 5), μ_j = (j + 7) / 8, j = 1…9 | x / 400 |
| (c) cosine | φ_j(x) = cos(f(x) / j), j = 1…9 | x / 1.81 |
| (d) cosine | φ_j(x) = cos(f(x) / j), j = 1…49 | x / 0.181 |
The transform f rescales the input for numerical stability and is already in the notebook. Then:
- Plot the four fitted curves over the training data. These four plots are all the writeup needs for this part.
- Compute the test MSE for each basis and discuss which ones overfit or underfit.
- Explain what the transforms
φare for, and analyze how linear regression's time and space cost scales withNand the number of featuresD. - Compare kNN, kernel regression, and linear regression with bases, and think about how you would use several regression functions together.
Problem 4: Probabilistic View of Regression and Regularization (30 pts)
Linear regression is rewritten as a probabilistic model y_n = wᵀx_n + ε_n with ε_n ~ N(0, σ²). Adding a prior on the weights and maximizing the posterior (MAP) gives you regularization.
- With
w ~ N(0, (σ²/λ) I), show that maximizing the posterior is equivalent to minimizing the ridge loss½‖y − Xw‖² + (λ/2)‖w‖². - Solve for the
wthat minimizes the ridge loss. - With each
w_ddrawn from a Laplace distributionL(0, 2σ²/λ), show that maximizing the posterior is equivalent to minimizing the LASSO loss½‖y − Xw‖² + (λ/2)‖w‖₁. - Explain why LASSO has no general closed-form solution.
- Implement
find_lasso_weightsusing the coordinate-descent algorithm given in the problem: start from a vector of ones, run at most 5,000 iterations, computeρ_dfor each coordinate, update the first coordinate (the bias) directly, and apply soft-thresholding to the rest. Then fit basis (d) from Problem 3 withλ = 1andλ = 10, plot the predictions on the training set, and compute the test MSE.
A 90-minute self-test
- Run the first half of the notebook: open
hw1_release.ipynb, confirm the ages are converted to thousands of years, and produce the kNN plots fork = 1, 3, N−1. - Kernel regression: once
kernel_regressorworks, plot onlyτ = 50first. The curve should sit between the jaggedk = 1fit and the flatk = N−1line. - Geometry proofs: start with part 2 (
ŷis closest). The orthogonality it relies on is the result of part 1, so the two are easiest to write together. - Basis regression: build basis (a) in
make_basisfirst and check the output shape isN × 10(nine basis functions plus bias), then copy the structure for (b)–(d). - LASSO: after writing
find_lasso_weights, run it once withλ = 0. The result should be close to the OLS fit for basis (d) from Problem 3, which is a quick sanity check.
How HW1 connects to later weeks
Next: HW2: classification and bias-variance (due 2026-02-27). Going by the problem titles in each s26 .tex, HW1 material comes back here:
- HW2: Bias-Variance & Uncertainty, MLE in classification, GD & Regularization, building on this post's least squares and regularization.
- HW3: Kernels & Feature Maps, Neural Networks, Neural Scaling Laws, extending the kernel and basis ideas here to feature maps and neural networks.
- HW4 (Part 1): Understanding the Transformer.
The previous post is HW0: linear algebra review. The full list is in the series overview.
Update log
- 2026-10-10: Added explicit video status and checked recording sources and access notes.
- 2026-10-10: Rechecked video status. Re-read the official schedule and syllabus; they still list no public lecture video for this topic.
- 2026-09-30: Rewrote the problem sections to match the official
hw1_release.tex. The earlier version described three problems (OLS, RBF kernel, MLP); the actual assignment has four: kNN & Kernels, Geometric Least Squares, Basis Regression, and Probabilistic View & Regularization. Also corrected the dataset size and age description and the Jouzel et al. 2007 citation, and removed the unsourced MLP and PyTorch material. - 2026-09-29: Corrected the due date to 2026-02-13 per the s26
hw1_release.tex; rewrote "How HW1 connects to later weeks" to link the actual series posts, with topics taken from each.tex.
Course video sources
Checked the official CS1810 Spring 2026 schedule and syllabus. This guide uses homework, section, or exam materials; the corresponding entries do not list a public lecture video. Slides and section materials are provided. No public listing does not mean that a recording never existed.
Official sources:
Checked on 2026-10-10.
References
Loading...