Sample-Wise Double Descent Simulation with Minimum-Norm Least Squares
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: hard
Interview Round: Other
With the feature dimension fixed, fit minimum-norm least squares (`pinv(X) @ y`, no intercept) on growing training sets. Measure train/test error and a bias-variance decomposition, then locate where adding data hurts.
Implement these functions:
```python
def min_norm_fit(X, y): ...
def sample_wise_curve(X_pool, y_pool, X_test, y_test, train_sizes): ...
def simulate_double_descent(w_true, train_sizes, noise_std, n_test, n_trials, seed): ...
def peak_train_size(train_sizes, test_error): ...
def harmful_size_steps(train_sizes, test_error): ...
```
Requirements:
- `min_norm_fit(X, y)`: weights of shape `(d,)`. Raise `ValueError` unless `X` is `(n, d)` and `y` is `(n,)`.
- `sample_wise_curve`: for each `n`, fit on the first `n` pool rows. Return arrays `train_mse`, `test_mse` and `weight_norm` (L2).
- `simulate_double_descent`: follow the draw order below. Return `train_sizes`, `bias_sq`, `variance` (ddof=0 over trials, averaged over test points), `noise` (`noise_std**2` per size) and `expected_error = mean((preds - f_test)**2) + noise`.
- `peak_train_size`: the `int` size with the max error (ties go to the smallest size). `harmful_size_steps`: a list of `(n_i, n_{i+1})` int tuples where error strictly increases.
- Raise `ValueError` for any of: empty, non-integer, `< 1` or non-strictly-increasing `train_sizes`, a size above the pool size, mismatched error length, or `n_trials`/`n_test < 1`.
The draw order for `simulate_double_descent`, followed by three small examples:
```python
# simulate_double_descent draw order (d = len(w_true), n_max = max size)
rng = np.random.default_rng(seed)
X_test = rng.standard_normal((n_test, d)); f_test = X_test @ w_true
for t in range(n_trials):
X_pool = rng.standard_normal((n_max, d))
y_pool = X_pool @ w_true + noise_std * rng.standard_normal(n_max)
# for each n: preds[i, t] = X_test @ min_norm_fit(X_pool[:n], y_pool[:n])
min_norm_fit([[3., 4.]], [5.]) # -> [0.6, 0.8]
peak_train_size([2, 4, 6, 8], [1, 3, 3, 0.5]) # -> 4
harmful_size_steps([2, 4, 6, 8], [1, 3, 3, 0.5]) # -> [(2, 4)]
```
```hint Shape of the prediction array
Store the simulated predictions as `(sizes, trials, n_test)`. Every returned statistic is then a mean or a variance over one named axis.
```
```hint Checking your decomposition
Before trusting the curve, confirm that `bias_sq + variance + noise` equals `expected_error` at every size. If it does not, look at how the variance is normalised.
```
```hint What to plot next to the error
Record the weight norm at each size. Comparing it with the test error tells you where the fit becomes unstable and why.
```
### Constraints & Assumptions
- In `sample_wise_curve`, `X_pool` is `(N, d)` with `y_pool` of shape `(N,)`, and `X_test` is `(m, d)` with `y_test` of shape `(m,)`.
- In `simulate_double_descent`, `d = len(w_true)` and each trial's pool has `max(train_sizes)` rows.
- Every random draw comes from the single `np.random.default_rng(seed)` generator, in the order shown, so a seed reproduces the whole curve.
- `train_sizes` is a strictly increasing sequence of integers, each at least 1.
### What a Strong Answer Covers
- `min_norm_fit` through the pseudoinverse with its shape checks, and what that fit is on each side of `n = d`.
- The exact seeded draw order, with one pool per trial and nested prefixes of it for the sizes.
- A bias-variance decomposition with `ddof=0` that adds up to `expected_error` to rounding.
- Peak and harmful-step helpers with the smallest-size tie rule, strict increases and full `train_sizes` validation.
- An explanation of why test error peaks near `n = d` and why extra samples hurt on the way there.
### Follow-up Questions
- Why does the test error peak at `n ≈ d` for min-norm least squares?
- How would ridge regularization change the curve?
- How would you make `sample_wise_curve` faster for many sizes?
Overview: Simulate sample-wise double descent by fitting minimum-norm least squares on growing training sets with a fixed feature dimension, then split the test error into bias, variance and noise. It tests pseudoinverse fitting, seeded simulation design and why adding training data can raise test error near n = d.