Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Chapter 9 — Lab: Parameters, Hyperparameters and Model Fit

Open in Colab

Lab Overview

You will fit models of growing capacity, watch them underfit and overfit, tame them with a penalty, and write your own hyperparameter search.

ExerciseTopicPoints
1Polynomial regression by least squares15
2Choosing the degree on validation data15
3Ridge regression in closed form20
4Diagnosing a fit15
5Grid search15
6Random search20
ChallengeMeasuring bias and variance+10

How it works. Each exercise has a function to complete where you see # TODO. Run the check cell under it: ✅ means your function is right, ❌ tells you what is wrong. Use NumPy and plain Python. Do not change the check cells.


Exercise 1: Polynomial Regression (15 points)

  1. design(x, d) returns the matrix with columns 1,x,x2,…,xd1, x, x^2, \dots, x^d (shape (len(x), d + 1)).

  2. fit_poly(x, y, d) returns the least-squares weights w0,…,wdw_0, \dots, w_d; use np.linalg.lstsq(A, y, rcond=None)[0].

  3. predict_poly(w, x) returns ∑jwjxj\sum_j w_j x^j.

  4. mse(y, y_hat) returns the mean squared error.


Exercise 2: Choosing the Degree (15 points)

best_degree(x_tr, y_tr, x_va, y_va, degrees) fits a polynomial of each degree on the training data, measures its MSE on the validation data, and returns (best degree, list of validation errors). On a tie, keep the smaller degree.

Question. Print the training MSE for the same degrees. What happens to it, and why can it never be used to choose the degree?


Exercise 3: Ridge Regression (20 points)

fit_ridge(x, y, d, lam) returns the ridge weights for a polynomial of degree d:

w=(A⊤A+λI′)−1A⊤y\mathbf{w} = (A^\top A + \lambda I')^{-1} A^\top \mathbf{y}

where AA = design(x, d) and I′I' is the identity matrix with a 0 in its top-left corner, so that the intercept w0w_0 is not penalised. Use np.linalg.solve.


Exercise 4: Diagnosing a Fit (15 points)

diagnose(train_err, val_err, target, gap) returns:

  • 'underfitting' if the training error is above target (the model cannot even fit what it has seen);

  • otherwise 'overfitting' if the validation error exceeds the training error by more than gap;

  • otherwise 'good'.


Exercise 5: Grid Search (15 points)

grid_search(score, grid) tries every combination of the values in the dictionary grid (use itertools.product over the values, keys in the dictionary’s order), calls score(params) on each params dictionary (higher is better), and returns (best params, best score). On a tie, keep the first combination tried.


Exercise 6: Random Search (20 points)

random_search(score, space, n, seed) makes rng = np.random.default_rng(seed), then n times builds a params dictionary by choosing, for each key in order, space[key][rng.integers(len(space[key]))]. It returns (best params, best score), keeping the first best on a tie.

Question. Only a matters much in toy. With 9 trials, a 3 × 3 grid tries 3 values of a. How many different values of a do 9 random trials try, typically? Why does this favour random search?


Challenge: Measuring Bias and Variance (+10 points)

bias_variance(degree, x0, n_sets, n_points) fits a polynomial of the given degree on n_sets training sets drawn with sample(n_points, 100 + s) for s = 0, 1, …, predicts at x0 with each, and returns (bias², variance) where the truth is sin⁡(πx0)\sin(\pi x_0) and the variance uses np.var.


Lab Report

Submit this notebook with:

  • every check cell showing ✅,

  • your answers to the two questions,

  • one paragraph: for a model you have used, list two parameters and two hyperparameters, and say how you would choose the hyperparameters.