Compute R² for Nested OLS Fits and Show It Never Decreases

Quick Overview

Write a NumPy function that fits nested OLS models on the first k predictor columns, each with an intercept, and returns the in-sample R-squared for every k. It tests least-squares fitting, why in-sample R-squared never decreases as predictors are added, and how to handle collinear columns and overfitting.

Compute R² for Nested OLS Fits and Show It Never Decreases

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `def r2_is_monotone_in_predictors(X: np.ndarray, y: np.ndarray) -> np.ndarray`. For each `k = 1, 2, ..., X.shape[1]`, fit OLS with an intercept on `X[:, :k]` and record its in-sample `R2`. The resulting sequence must be non-decreasing. Requirements: - Fit `X.shape[1]` nested models, each with an intercept, model `k` using exactly `X[:, :k]`. - Return a 1-D `np.ndarray` of shape `(X.shape[1],)`, dtype `float64`, in increasing-`k` order. Example: ```python X.shape == (30, 4) # 4th column is pure noise, unrelated to y out = r2_is_monotone_in_predictors(X, y) out.shape # (4,) out.dtype # float64 np.all(np.diff(out) >= -1e-12) # True (non-decreasing) ``` ```hint Nested fits Compare the set of fitted vectors that model `k` can produce with the set that model `k + 1` can produce. ``` ```hint Numerical care Think about what your solver does when a later column is close to a linear combination of earlier ones, and why the check above allows a tiny tolerance. ``` ### What a Strong Answer Covers - Each design matrix has an intercept column plus exactly the first `k` predictors. - One total sum of squares for all models, with `R2 = 1 - RSS/TSS` for each fit. - A numerically stable least-squares solve that copes with collinear columns. - An argument from nested column spaces for why the sequence cannot decrease. - The output shape, dtype and order, and what happens when `y` is constant. ### Follow-up Questions - Why can noise-only predictors still bump R² up in-sample? - How would you detect this kind of overfitting when choosing how many predictors to keep? - How would you guard against numerical issues from collinear columns?

Overview: Write a NumPy function that fits nested OLS models on the first k predictor columns, each with an intercept, and returns the in-sample R-squared for every k. It tests least-squares fitting, why in-sample R-squared never decreases as predictors are added, and how to handle collinear columns and overfitting.

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

Implement def r2_is_monotone_in_predictors(X: np.ndarray, y: np.ndarray) -> np.ndarray. For each k = 1, 2, ..., X.shape[1], fit OLS with an intercept on X[:, :k] and record its in-sample R2. The resulting sequence must be non-decreasing.

Requirements:

  • Fit X.shape[1] nested models, each with an intercept, model k using exactly X[:, :k] .
  • Return a 1-D np.ndarray of shape (X.shape[1],) , dtype float64 , in increasing- k order.

Example:

X.shape == (30, 4)   # 4th column is pure noise, unrelated to y
out = r2_is_monotone_in_predictors(X, y)
out.shape   # (4,)
out.dtype   # float64
np.all(np.diff(out) >= -1e-12)   # True (non-decreasing)

What a Strong Answer Covers Guidance

  • Each design matrix has an intercept column plus exactly the first k predictors.
  • One total sum of squares for all models, with R2 = 1 - RSS/TSS for each fit.
  • A numerically stable least-squares solve that copes with collinear columns.
  • An argument from nested column spaces for why the sequence cannot decrease.
  • The output shape, dtype and order, and what happens when y is constant.

Follow-up Questions Guidance

  • Why can noise-only predictors still bump R² up in-sample?
  • How would you detect this kind of overfitting when choosing how many predictors to keep?
  • How would you guard against numerical issues from collinear columns?