Compute centered and uncentered R-squared for OLS with and without an intercept

Quick Overview

Fit linear regression with and without an intercept and compute three R-squared values against the centered and uncentered total sums of squares. Tests whether you can explain why a no-intercept model can score well on one baseline and worse than predicting the mean on another.

Compute centered and uncentered R-squared for OLS with and without an intercept

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `def r2_with_and_without_intercept(X: np.ndarray, y: np.ndarray) -> tuple`. Given `X` of shape `(18, 2)` and target `y`, fit OLS model A with an intercept and model B with no constant column at all, then return three R² values: - `r2_a_centered = 1 - RSS_A/TSS_centered`, where `TSS_centered = sum((y - y.mean())**2)`. - `r2_b_uncentered = 1 - RSS_B/TSS_uncentered`, where `TSS_uncentered = sum(y**2)` (matches `statsmodels` `results.rsquared` for a no-constant model). - `r2_b_centered = 1 - RSS_B/TSS_centered`. Return `(r2_a_centered, r2_b_uncentered, r2_b_centered)` in that order. ```hint Relate the two baselines Write `sum(y**2)` in terms of `TSS_centered`, `n` and `y.mean()`. That tells you how much the choice of baseline matters when the mean is large. ``` ```hint Sanity checks The columns of model B are a subset of the columns of model A. Use that to predict how `RSS_A` and `RSS_B` must compare before you trust your numbers. ``` ### Constraints & Assumptions - `X` has shape `(18, 2)` and contains no constant column; `y` has 18 values. - The test `y` has a large non-zero mean, so `r2_b_centered` may be much lower than `r2_b_uncentered` (possibly negative). - `statsmodels` or plain `numpy` least squares may be used. ### What a Strong Answer Covers - Fitting model A with an explicit intercept column and model B on `X` alone, and computing each residual sum of squares. - Scoring each model against the right total sum of squares and returning the three values in the required order. - Why model B's uncentered R² looks high when `y` has a large mean. - What a negative centered R² says about model B compared with predicting the mean. - Which baseline `statsmodels` uses for `rsquared` with and without a constant. ### Follow-up Questions - Why does `statsmodels` use the uncentered TSS for a no-constant model? - When is a no-intercept model actually justified? - How would you decide fairly whether model A or model B fits better?

Overview: Fit linear regression with and without an intercept and compute three R-squared values against the centered and uncentered total sums of squares. Tests whether you can explain why a no-intercept model can score well on one baseline and worse than predicting the mean on another.

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

Implement def r2_with_and_without_intercept(X: np.ndarray, y: np.ndarray) -> tuple. Given X of shape (18, 2) and target y, fit OLS model A with an intercept and model B with no constant column at all, then return three R² values:

  • r2_a_centered = 1 - RSS_A/TSS_centered , where TSS_centered = sum((y - y.mean())**2) .
  • r2_b_uncentered = 1 - RSS_B/TSS_uncentered , where TSS_uncentered = sum(y**2) (matches statsmodels results.rsquared for a no-constant model).
  • r2_b_centered = 1 - RSS_B/TSS_centered .

Return (r2_a_centered, r2_b_uncentered, r2_b_centered) in that order.

Constraints & Assumptions

  • X has shape (18, 2) and contains no constant column; y has 18 values.
  • The test y has a large non-zero mean, so r2_b_centered may be much lower than r2_b_uncentered (possibly negative).
  • statsmodels or plain numpy least squares may be used.

What a Strong Answer Covers Guidance

  • Fitting model A with an explicit intercept column and model B on X alone, and computing each residual sum of squares.
  • Scoring each model against the right total sum of squares and returning the three values in the required order.
  • Why model B's uncentered R² looks high when y has a large mean.
  • What a negative centered R² says about model B compared with predicting the mean.
  • Which baseline statsmodels uses for rsquared with and without a constant.

Follow-up Questions Guidance

  • Why does statsmodels use the uncentered TSS for a no-constant model?
  • When is a no-intercept model actually justified?
  • How would you decide fairly whether model A or model B fits better?