Adjusted R² penalty for a junk predictor in nested OLS models

Quick Overview

Implement a function that fits two nested OLS models on a small sample, one with an extra pure-noise predictor, and returns R² and adjusted R² for each. It tests the adjusted R² degrees-of-freedom penalty, how raw R² behaves as columns are added, and when a new predictor earns its place.

Adjusted R² penalty for a junk predictor in nested OLS models

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `junk_predictor_effect(X, y, junk)`. It fits two nested ordinary least squares (OLS) models, both with an intercept, and reports how a predictor that carries no signal changes R² and adjusted R². - Model A regresses `y` on `X`, which has shape `(n, p) = (15, 3)`. - Model B regresses `y` on `X` with `junk` (shape `(n,)`) appended as a fourth predictor using `np.column_stack`. ```python def junk_predictor_effect(X: np.ndarray, y: np.ndarray, junk: np.ndarray) -> tuple: ... ``` Requirements: - Compute the in-sample R² of each model. Add the intercept explicitly (for example a column of ones) when you solve least squares. - Compute adjusted R² as `adj = 1 - (1 - R2) * (n - 1) / (n - p - 1)`, where `p` excludes the intercept (`p = 3` for A, `p = 4` for B). - Return the tuple `(r2_a, adj_a, r2_b, adj_b)` of four floats in that order. On the grader's data the expected outcome is `r2_b > r2_a` and `adj_b < adj_a`. The grader's `junk` is pure noise unrelated to `y`, and `n` is deliberately small. You may use `numpy` (for example `np.linalg.lstsq`) or `statsmodels`. ```hint Same denominator Both models share one total sum of squares, taken around the mean of `y`. Only the residual sum of squares differs between A and B. ``` ```hint What the penalty compares Rewrite adjusted R² in terms of the residual sum of squares divided by its degrees of freedom. Then work out how far RSS has to fall for the fourth column to pay for itself when `n` is 15. ``` ### Constraints & Assumptions - `n = 15` observations and `p = 3` predictors in `X`. `junk` is a 1-D array of length 15. - Both models include an intercept, and the intercept is not counted in `p`. - `junk` is independent noise, so any R² it adds comes from chance fit to this sample. - `numpy` or `statsmodels` is available. ### What a Strong Answer Covers - Builds both design matrices with an explicit intercept column, with the junk appended as the last predictor of model B. - Computes R² from RSS and the TSS around the mean of `y`, and adjusted R² with the correct `p` for each model. - Explains why R² cannot fall when a column is added to an OLS fit. - States the exact condition under which adjusted R² falls when a predictor is added, and why the expected outcome is likely but not guaranteed for a noise column. - Returns plain floats in the required order. ### Follow-up Questions - Why does R² never decrease when you add a predictor? - When can adjusted R² still be misleading for model selection? - If you repeated this experiment with many independent junk columns, how often would adjusted R² go up, and which test statistic decides it?

Overview: Implement a function that fits two nested OLS models on a small sample, one with an extra pure-noise predictor, and returns R² and adjusted R² for each. It tests the adjusted R² degrees-of-freedom penalty, how raw R² behaves as columns are added, and when a new predictor earns its place.

General logo
General
Nov 24, 2026
mediumData ScientistOtherMachine Learning
0
0

Implement junk_predictor_effect(X, y, junk). It fits two nested ordinary least squares (OLS) models, both with an intercept, and reports how a predictor that carries no signal changes R² and adjusted R².

  • Model A regresses y on X , which has shape (n, p) = (15, 3) .
  • Model B regresses y on X with junk (shape (n,) ) appended as a fourth predictor using np.column_stack .
def junk_predictor_effect(X: np.ndarray, y: np.ndarray, junk: np.ndarray) -> tuple:
    ...

Requirements:

  • Compute the in-sample R² of each model. Add the intercept explicitly (for example a column of ones) when you solve least squares.
  • Compute adjusted R² as adj = 1 - (1 - R2) * (n - 1) / (n - p - 1) , where p excludes the intercept ( p = 3 for A, p = 4 for B).
  • Return the tuple (r2_a, adj_a, r2_b, adj_b) of four floats in that order.

On the grader's data the expected outcome is r2_b > r2_a and adj_b < adj_a. The grader's junk is pure noise unrelated to y, and n is deliberately small. You may use numpy (for example np.linalg.lstsq) or statsmodels.

Constraints & Assumptions

  • n = 15 observations and p = 3 predictors in X . junk is a 1-D array of length 15.
  • Both models include an intercept, and the intercept is not counted in p .
  • junk is independent noise, so any R² it adds comes from chance fit to this sample.
  • numpy or statsmodels is available.

What a Strong Answer Covers Guidance

  • Builds both design matrices with an explicit intercept column, with the junk appended as the last predictor of model B.
  • Computes R² from RSS and the TSS around the mean of y , and adjusted R² with the correct p for each model.
  • Explains why R² cannot fall when a column is added to an OLS fit.
  • States the exact condition under which adjusted R² falls when a predictor is added, and why the expected outcome is likely but not guaranteed for a noise column.
  • Returns plain floats in the required order.

Follow-up Questions Guidance

  • Why does R² never decrease when you add a predictor?
  • When can adjusted R² still be misleading for model selection?
  • If you repeated this experiment with many independent junk columns, how often would adjusted R² go up, and which test statistic decides it?