Double Descent with Minimum-Norm Least Squares

Quick Overview

Implement minimum-norm least squares over nested feature prefixes with the pseudoinverse, recording train risk, test risk and weight norm for each dimension. Then run a seeded simulation that averages the curves over trials and finds the test-risk peak, testing how well you understand double descent near the interpolation threshold.

Double Descent with Minimum-Norm Least Squares

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement two NumPy functions that trace the double descent curve of minimum-norm least squares. `min_norm_risk_curve(X_train, y_train, X_test, y_test, dims)`: for each `p` in `dims`, fit `w = pinv(X_train[:, :p]) @ y_train` on the first `p` columns and record the train MSE, the test MSE on `X_test[:, :p]`, and `||w||_2`. `simulate_double_descent(n_train, n_test, max_dim, dims, noise_std, n_trials, seed)`: draw synthetic regression data from a seeded generator, compute the curves above for each trial, average them over trials, and report the dimension where the averaged test risk peaks. Example for the first function: ```python X_train = [[1, 0, 0], [0, 1, 0]] y_train = [2, 3] X_test = [[1, 1, 1]] y_test = [4] min_norm_risk_curve(X_train, y_train, X_test, y_test, [1, 2, 3]) # {"dims": [1, 2, 3], "train_risk": [4.5, 0, 0], # "test_risk": [4, 1, 1], "weight_norm": [2, sqrt(13), sqrt(13)]} ``` With one column the fit is `w = [2]`, which misses the second training target by 3 (train MSE 4.5) and predicts 2 for the test target 4. With two or three columns the model interpolates both training points, and at `p = 3` the pseudoinverse puts zero weight on the all-zero third column, so `w = [2, 3, 0]`. The simulation must draw its random numbers in this order, so that a seed pins the result: ```python rng = np.random.default_rng(seed) w_true = rng.standard_normal(max_dim) / np.sqrt(max_dim) for _ in range(n_trials): X_tr = rng.standard_normal((n_train, max_dim)) e_tr = noise_std * rng.standard_normal(n_train) X_te = rng.standard_normal((n_test, max_dim)) e_te = noise_std * rng.standard_normal(n_test) y_tr = X_tr @ w_true + e_tr y_te = X_te @ w_true + e_te ``` ```hint One solver for both regimes Compare what `pinv(A) @ y` returns when `A` has more rows than columns with what it returns when many weight vectors fit the training data exactly. ``` ```hint Watch the conditioning Track the smallest singular value of `X_train[:, :p]` as `p` grows and compare it with the weight norm you record. ``` ```hint Keep the draws nested Generate each trial's full `max_dim`-column design once and take prefixes of it. Drawing fresh data for every `p` breaks the seed contract and makes the dimensions incomparable. ``` ### Constraints & Assumptions - Both functions return a dict with keys `dims` (int array), `train_risk`, `test_risk` and `weight_norm` (float arrays aligned with `dims`). The simulation also returns `peak_dim`. - Train and test risk are mean squared errors. - Simulation curves are arithmetic means over trials. `peak_dim = int(dims[argmax(test_risk)])`, taking the first maximum on ties. - An empty `dims`, or any value outside `[1, n_features]` (`[1, max_dim]` for the simulation), raises `ValueError`. - A non-2-D `X`, or train and test matrices with different column counts, raises `ValueError`. - A fixed `seed` must give identical results. ### What a Strong Answer Covers - What the pseudoinverse returns on each side of the interpolation threshold. - Fitting nested column prefixes of one generated design per trial, with the random draws in the pinned order. - Input validation for every stated `ValueError` case and the first-maximum rule for `peak_dim`. - An explanation of the shape of the train-risk, test-risk and weight-norm curves around the interpolation threshold, tied to the conditioning of the design. - The time cost of the loop over dims and trials. ### Follow-up Questions - How does the height of the peak change as `noise_std` grows, and is there still a peak when `noise_std = 0`? - What does a small ridge penalty do to the curve, and how would you choose its strength? - The features past `p` still carry signal in this simulation. How does that misspecification enter the test risk? - Why can the minimum-norm interpolant generalize at all when `p` is much larger than `n_train`?

Overview: Implement minimum-norm least squares over nested feature prefixes with the pseudoinverse, recording train risk, test risk and weight norm for each dimension. Then run a seeded simulation that averages the curves over trials and finds the test-risk peak, testing how well you understand double descent near the interpolation threshold.

General logo
General
Nov 25, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement two NumPy functions that trace the double descent curve of minimum-norm least squares.

min_norm_risk_curve(X_train, y_train, X_test, y_test, dims): for each p in dims, fit w = pinv(X_train[:, :p]) @ y_train on the first p columns and record the train MSE, the test MSE on X_test[:, :p], and ||w||_2.

simulate_double_descent(n_train, n_test, max_dim, dims, noise_std, n_trials, seed): draw synthetic regression data from a seeded generator, compute the curves above for each trial, average them over trials, and report the dimension where the averaged test risk peaks.

Example for the first function:

X_train = [[1, 0, 0], [0, 1, 0]]
y_train = [2, 3]
X_test = [[1, 1, 1]]
y_test = [4]
min_norm_risk_curve(X_train, y_train, X_test, y_test, [1, 2, 3])
# {"dims": [1, 2, 3], "train_risk": [4.5, 0, 0],
#  "test_risk": [4, 1, 1], "weight_norm": [2, sqrt(13), sqrt(13)]}

With one column the fit is w = [2], which misses the second training target by 3 (train MSE 4.5) and predicts 2 for the test target 4. With two or three columns the model interpolates both training points, and at p = 3 the pseudoinverse puts zero weight on the all-zero third column, so w = [2, 3, 0].

The simulation must draw its random numbers in this order, so that a seed pins the result:

rng = np.random.default_rng(seed)
w_true = rng.standard_normal(max_dim) / np.sqrt(max_dim)
for _ in range(n_trials):
    X_tr = rng.standard_normal((n_train, max_dim))
    e_tr = noise_std * rng.standard_normal(n_train)
    X_te = rng.standard_normal((n_test, max_dim))
    e_te = noise_std * rng.standard_normal(n_test)
    y_tr = X_tr @ w_true + e_tr
    y_te = X_te @ w_true + e_te

Constraints & Assumptions

  • Both functions return a dict with keys dims (int array), train_risk , test_risk and weight_norm (float arrays aligned with dims ). The simulation also returns peak_dim .
  • Train and test risk are mean squared errors.
  • Simulation curves are arithmetic means over trials. peak_dim = int(dims[argmax(test_risk)]) , taking the first maximum on ties.
  • An empty dims , or any value outside [1, n_features] ( [1, max_dim] for the simulation), raises ValueError .
  • A non-2-D X , or train and test matrices with different column counts, raises ValueError .
  • A fixed seed must give identical results.

What a Strong Answer Covers Guidance

  • What the pseudoinverse returns on each side of the interpolation threshold.
  • Fitting nested column prefixes of one generated design per trial, with the random draws in the pinned order.
  • Input validation for every stated ValueError case and the first-maximum rule for peak_dim .
  • An explanation of the shape of the train-risk, test-risk and weight-norm curves around the interpolation threshold, tied to the conditioning of the design.
  • The time cost of the loop over dims and trials.

Follow-up Questions Guidance

  • How does the height of the peak change as noise_std grows, and is there still a peak when noise_std = 0 ?
  • What does a small ridge penalty do to the curve, and how would you choose its strength?
  • The features past p still carry signal in this simulation. How does that misspecification enter the test risk?
  • Why can the minimum-norm interpolant generalize at all when p is much larger than n_train ?