Compute the residual sum of squares from an OLS fit with an intercept

Quick Overview

Implement a NumPy function that fits ordinary least squares with an intercept and returns the residual sum of squares as a float. It tests building the design matrix, choosing a stable least-squares solver, and keeping RSS separate from the mean squared error and the residual standard error.

Compute the residual sum of squares from an OLS fit with an intercept

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `residual_sum_of_squares(X: np.ndarray, y: np.ndarray) -> float`. Given `X` of shape `(n, k)` with no intercept column and `y` of shape `(n,)`, fit OLS with an intercept and return `RSS = sum(e**2)`, where `e = y - y_hat`. This is the value statsmodels reports as `results.ssr`. - Add an intercept term before fitting. - Return a scalar `float`, not a NumPy array. - Do not divide by `n` or `n-k-1`, and do not take a square root. For example: ```python X = np.array([[1.0], [2.0], [3.0]]) y = np.array([2.0, 4.0, 5.0]) residual_sum_of_squares(X, y) # -> 0.1666... (a float) ``` ```hint Watch the solver outputs A least-squares solver can hand back more than the coefficients. Check what each output contains when the design matrix is not full rank before you rely on it. ``` ### What a Strong Answer Covers - Adding the intercept column and fitting with a numerically stable least-squares solver. - Computing the residuals from the fitted values and summing their squares with no normalization. - Returning a plain Python float. - Handling a 1-D `X` and a rank-deficient design matrix. ### Follow-up Questions - How would you compute RSS without forming the normal equations explicitly? - How do you get the residual variance estimate and $R^2$ from RSS? - What happens to RSS when you add a predictor, and why does that make it a poor model-selection criterion on its own?

Overview: Implement a NumPy function that fits ordinary least squares with an intercept and returns the residual sum of squares as a float. It tests building the design matrix, choosing a stable least-squares solver, and keeping RSS separate from the mean squared error and the residual standard error.

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

Implement residual_sum_of_squares(X: np.ndarray, y: np.ndarray) -> float. Given X of shape (n, k) with no intercept column and y of shape (n,), fit OLS with an intercept and return RSS = sum(e**2), where e = y - y_hat. This is the value statsmodels reports as results.ssr.

  • Add an intercept term before fitting.
  • Return a scalar float , not a NumPy array.
  • Do not divide by n or n-k-1 , and do not take a square root.

For example:

X = np.array([[1.0], [2.0], [3.0]])
y = np.array([2.0, 4.0, 5.0])
residual_sum_of_squares(X, y)  # -> 0.1666... (a float)

What a Strong Answer Covers Guidance

  • Adding the intercept column and fitting with a numerically stable least-squares solver.
  • Computing the residuals from the fitted values and summing their squares with no normalization.
  • Returning a plain Python float.
  • Handling a 1-D X and a rank-deficient design matrix.

Follow-up Questions Guidance

  • How would you compute RSS without forming the normal equations explicitly?
  • How do you get the residual variance estimate and R2R^2 from RSS?
  • What happens to RSS when you add a predictor, and why does that make it a poor model-selection criterion on its own?