Minimum-Norm Least Squares Profile Across p/n Ratios
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: hard
Interview Round: Other
Implement `min_norm_profile(X_trains, y_trains, X_test, feature_counts)`. For each `p` in `feature_counts`, fit the minimum-norm least-squares solution (pseudoinverse) on the first `p` columns of each of the `R` training sets, then summarise the fits across replicates. Return a dict of the profile with these entries:
- `p_over_n`, `param_norm`, `condition_number`, `variance`: arrays aligned with `feature_counts` order. `param_norm` is the mean L2 norm of `beta` over replicates.
- `condition_number`: mean over replicates of `(s_max / s_min)**2` (Gram condition), using only singular values above `s_max * max(n, p) * eps`.
- `variance`: per test point, population variance (`ddof=0`) of predictions across replicates, averaged over test points.
- `peak_p_over_n` (float): ratio at max variance. Ties go to the earliest entry in `feature_counts`.
Raise `ValueError` for wrong ndim, inconsistent shapes, empty `feature_counts`, or any `p < 1` or `p > P`.
Example:
```python
X_trains = [[[1,0,0],[0,1,0]], [[2,0,0],[0,1,0]]] # (R=2, n=2, P=3)
y_trains = [[1,2],[2,4]]
X_test = [[1,1,1]]
min_norm_profile(X_trains, y_trains, X_test, [1, 2, 3])
# p_over_n -> [0.5, 1.0, 1.5]
# param_norm -> [1.0, 3.1796, 3.1796] # (sqrt(5)+sqrt(17))/2
# condition_number -> [1.0, 2.5, 2.5]
# variance -> [0.0, 1.0, 1.0]
# peak_p_over_n -> 1.0
```
```hint One decomposition per fit
The thin SVD of each `n x p` slice gives you both the minimum-norm coefficients and the singular values the condition number needs. Apply the same cutoff to both.
```
```hint Which axis the variance runs over
Stack the predictions of all replicates into an `(R, m)` array. The variance is taken across replicates for each test point before anything is averaged.
```
```hint Ties at the peak
Check which index `np.argmax` returns when two entries are equal before you write any tie-breaking of your own.
```
### Constraints & Assumptions
- `X_trains` has shape `(R, n, P)`, `y_trains` has shape `(R, n)` and `X_test` has shape `(m, P)`.
- Every `p` in `feature_counts` satisfies `1 <= p <= P`; the fit for a given `p` uses the first `p` columns of the training and test designs.
- `eps` is the float64 machine epsilon (`np.finfo(float).eps`).
- With `R = 1`, variance is `0`.
### What a Strong Answer Covers
- A minimum-norm fit that works for full-rank, rank-deficient and underdetermined (`p > n`) slices, through a truncated SVD or an equivalent pseudoinverse.
- The singular value cutoff applied consistently to the coefficients and to the Gram condition number, with the ratio squared.
- The variance computed across replicates per test point with `ddof=0`, then averaged over test points, and the earliest-peak tie rule.
- Input validation that raises `ValueError` for every listed case before any fitting starts.
- An explanation of why the norm, condition number and variance spike near $p/n = 1$ and fall again past it.
### Follow-up Questions
- `np.linalg.pinv` and `np.linalg.lstsq` also return the minimum-norm solution. When would you still write the SVD out by hand?
- Why is the Gram condition number the square of the design's condition number, and what does that mean for solving the normal equations directly?
- How does adding a small ridge penalty change the profile near $p = n$?
- If you knew the true coefficients, how would you extend the profile to report squared bias and the full test risk curve?
Overview: Implement a NumPy function that fits minimum-norm least squares on growing feature subsets of several training replicates and reports parameter norm, Gram condition number and prediction variance at each p/n ratio. It tests SVD-based pseudoinverse fitting, singular value cutoffs and the variance peak behind double descent.