OLS Sum-of-Squares Decomposition

Quick Overview

Implement a NumPy function that fits a simple linear regression with an intercept and returns the total, regression and error sums of squares as plain floats. It tests computing fitted values, the SST = SSR + SSE identity and how it connects to R squared.

OLS Sum-of-Squares Decomposition

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `sums_of_squares`. Fit the simple OLS line `y ≈ b0 + b1 * x` (with intercept), compute the fitted values `y_hat`, and return `(sst, ssr, sse)` in that exact order. ```python def sums_of_squares(x: np.ndarray, y: np.ndarray) -> tuple[float, float, float]: ... ``` Requirements: - SST is `sum((y - y.mean())**2)`. - SSR is `sum((y_hat - y.mean())**2)`. - SSE is `sum((y - y_hat)**2)`. - Each value is a plain `float` (not a NumPy scalar or array). - `SST == SSR + SSE` must hold, up to floating-point tolerance. Example: ```python x = np.array([1.0, 2.0, 3.0, 4.0]) y = np.array([2.0, 4.0, 5.0, 4.0]) sst, ssr, sse = sums_of_squares(x, y) # sst == 4.75, ssr == 2.45, sse == 2.3 (up to floating-point tolerance) # sst == ssr + sse ``` The fitted line here is `y_hat = 2.0 + 0.7 * x`. ```hint Watch the fit and the baseline If you fit with `np.polyfit`, check the order of the coefficients it returns. Use the same `y.mean()` baseline for SST and SSR. ``` ### Constraints & Assumptions - Inputs are 1-D float arrays of shape `(n,)`. - The grader compares values with `numpy.testing.assert_allclose` and separately asserts the decomposition. ### What a Strong Answer Covers - Fitting the line with an intercept and computing the fitted values correctly. - Computing the three sums from their definitions and returning plain floats in the required order. - Why the identity SST = SSR + SSE holds for a fit with an intercept. - Degenerate inputs such as a constant `x` or a constant `y`. ### Follow-up Questions - How does the decomposition change if you fit without an intercept? - How would you derive $R^2$ from these values?

Overview: Implement a NumPy function that fits a simple linear regression with an intercept and returns the total, regression and error sums of squares as plain floats. It tests computing fitted values, the SST = SSR + SSE identity and how it connects to R squared.

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

Implement sums_of_squares. Fit the simple OLS line y ≈ b0 + b1 * x (with intercept), compute the fitted values y_hat, and return (sst, ssr, sse) in that exact order.

def sums_of_squares(x: np.ndarray, y: np.ndarray) -> tuple[float, float, float]:
    ...

Requirements:

  • SST is sum((y - y.mean())**2) .
  • SSR is sum((y_hat - y.mean())**2) .
  • SSE is sum((y - y_hat)**2) .
  • Each value is a plain float (not a NumPy scalar or array).
  • SST == SSR + SSE must hold, up to floating-point tolerance.

Example:

x = np.array([1.0, 2.0, 3.0, 4.0])
y = np.array([2.0, 4.0, 5.0, 4.0])
sst, ssr, sse = sums_of_squares(x, y)
# sst == 4.75, ssr == 2.45, sse == 2.3  (up to floating-point tolerance)
# sst == ssr + sse

The fitted line here is y_hat = 2.0 + 0.7 * x.

Constraints & Assumptions

  • Inputs are 1-D float arrays of shape (n,) .
  • The grader compares values with numpy.testing.assert_allclose and separately asserts the decomposition.

What a Strong Answer Covers Guidance

  • Fitting the line with an intercept and computing the fitted values correctly.
  • Computing the three sums from their definitions and returning plain floats in the required order.
  • Why the identity SST = SSR + SSE holds for a fit with an intercept.
  • Degenerate inputs such as a constant x or a constant y .

Follow-up Questions Guidance

  • How does the decomposition change if you fit without an intercept?
  • How would you derive R2R^2 from these values?