Measure How Added Noise Features Inflate OLS Prediction Variance

Quick Overview

Implement a bias, variance and noise decomposition for a matrix of refit predictions, then refit ordinary least squares with a growing number of pure-noise features. It tests NumPy design matrices and least squares, and whether you can explain what extra irrelevant features do to bias and to prediction variance.

Measure How Added Noise Features Inflate OLS Prediction Variance

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `decompose_predictions(preds, f_true, noise_var)` for an `(R, T)` refits-by-test-points matrix (1-D input means one refit). Then implement `noise_feature_experiment`: for each `m` in `n_noise_list`, fit OLS via `np.linalg.lstsq` on `[1, X_train[r], Z_train[r][:, :m]]` for every refit `r`, predict on `[1, X_test, Z_test[:, :m]]`, and decompose the predictions. ```python def decompose_predictions(preds, f_true, noise_var) -> dict: ... def noise_feature_experiment(X_train, Z_train, y_train, X_test, Z_test, f_test, noise_var, n_noise_list) -> dict: ... ``` Example: ```python preds = [[1, 2], [3, 4], [2, 0]] f_true = [2, 1] noise_var = 0.25 decompose_predictions(preds, f_true, noise_var) # {'bias_sq': 0.5, 'variance': 1.6667, 'noise': 0.25, 'expected_error': 2.4167} (rounded) ``` ```hint One refit A 1-D `preds` holds one refit across all test points. Check what `axis=0` would average over if you left it 1-D. ``` ```hint Matching designs Coefficient `k` from the training fit must multiply the same feature at prediction time. Look at how you slice `Z` for train and for test. ``` ### Constraints & Assumptions - `bias_sq` is the mean over test points of `(mean_pred - f_true)**2`, where `mean_pred` is the mean prediction across refits. `variance` is the mean over test points of the per-point variance across refits (`ddof=0`). - `noise` = `noise_var` and `expected_error` = `bias_sq + variance + noise`. `decompose_predictions` returns floats. - Shapes: `X_train` `(R, n, p)`, `Z_train` `(R, n, M)`, `y_train` `(R, n)`, `X_test` `(T, p)`, `Z_test` `(T, M)`, `f_test` `(T,)`. - The experiment returns keys `n_noise` (int array), `bias_sq`, `variance`, `noise`, `expected_error` (arrays aligned with `n_noise_list`). - Any `m < 0` or `m` greater than the available noise columns raises `ValueError`. ### What a Strong Answer Covers - The four-part decomposition with means across refits per test point, population variance, floats returned, and a 1-D input treated as one refit. - Validation of every `m` against the available noise columns before any fitting. - Train and test designs with an intercept, `X`, and the same leading `m` noise columns, one `lstsq` fit per refit, and predictions stacked into an `(R, T)` matrix. - An output dict with an int `n_noise` array and metric arrays aligned with `n_noise_list`. - An explanation of what added noise features do to squared bias and to variance, with how variance scales in the number of fitted columns. ### Follow-up Questions - If the true function is linear in `X` with an intercept, why does squared bias stay near zero as noise features are added? - How would ridge regression change the bias and variance curves? - What happens as the number of fitted columns approaches `n`, and what happens past it?

Overview: Implement a bias, variance and noise decomposition for a matrix of refit predictions, then refit ordinary least squares with a growing number of pure-noise features. It tests NumPy design matrices and least squares, and whether you can explain what extra irrelevant features do to bias and to prediction variance.

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

Implement decompose_predictions(preds, f_true, noise_var) for an (R, T) refits-by-test-points matrix (1-D input means one refit). Then implement noise_feature_experiment: for each m in n_noise_list, fit OLS via np.linalg.lstsq on [1, X_train[r], Z_train[r][:, :m]] for every refit r, predict on [1, X_test, Z_test[:, :m]], and decompose the predictions.

def decompose_predictions(preds, f_true, noise_var) -> dict:
    ...


def noise_feature_experiment(X_train, Z_train, y_train, X_test, Z_test, f_test, noise_var, n_noise_list) -> dict:
    ...

Example:

preds = [[1, 2], [3, 4], [2, 0]]
f_true = [2, 1]
noise_var = 0.25
decompose_predictions(preds, f_true, noise_var)
# {'bias_sq': 0.5, 'variance': 1.6667, 'noise': 0.25, 'expected_error': 2.4167}  (rounded)

Constraints & Assumptions

  • bias_sq is the mean over test points of (mean_pred - f_true)**2 , where mean_pred is the mean prediction across refits. variance is the mean over test points of the per-point variance across refits ( ddof=0 ).
  • noise = noise_var and expected_error = bias_sq + variance + noise . decompose_predictions returns floats.
  • Shapes: X_train (R, n, p) , Z_train (R, n, M) , y_train (R, n) , X_test (T, p) , Z_test (T, M) , f_test (T,) .
  • The experiment returns keys n_noise (int array), bias_sq , variance , noise , expected_error (arrays aligned with n_noise_list ).
  • Any m < 0 or m greater than the available noise columns raises ValueError .

What a Strong Answer Covers Guidance

  • The four-part decomposition with means across refits per test point, population variance, floats returned, and a 1-D input treated as one refit.
  • Validation of every m against the available noise columns before any fitting.
  • Train and test designs with an intercept, X , and the same leading m noise columns, one lstsq fit per refit, and predictions stacked into an (R, T) matrix.
  • An output dict with an int n_noise array and metric arrays aligned with n_noise_list .
  • An explanation of what added noise features do to squared bias and to variance, with how variance scales in the number of fitted columns.

Follow-up Questions Guidance

  • If the true function is linear in X with an intercept, why does squared bias stay near zero as noise features are added?
  • How would ridge regression change the bias and variance curves?
  • What happens as the number of fitted columns approaches n , and what happens past it?