Variance decomposition in linear regression: compute TSS, ESS and RSS

Quick Overview

Implement a NumPy function that returns the total, explained and residual sums of squares for a regression fit. It tests how the variance decomposition is defined, which mean ESS is centered on, and when TSS equals ESS plus RSS.

Variance decomposition in linear regression: compute TSS, ESS and RSS

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `sum_of_squares(y_true, y_pred)` for two equal-length 1-D NumPy arrays. It returns a tuple of three Python floats `(tss, ess, rss)` that split the variation in `y_true` around its mean: - Let `ybar = y_true.mean()`. - `TSS = sum((y_true - ybar)**2)` - `ESS = sum((y_pred - ybar)**2)`, centered on the mean of `y_true` (the same center as TSS). - `RSS = sum((y_true - y_pred)**2)` Example: ```python y_true = np.array([1.0, 2.0, 3.0]) y_pred = np.array([1.1, 1.9, 3.0]) sum_of_squares(y_true, y_pred) # about (2.0, 1.82, 0.02) -> tss, ess, rss ``` This `y_pred` was picked by hand and is not an OLS fit, so ESS + RSS (1.84) does not equal TSS in the example. ```hint Check types and shapes `np.sum` returns a NumPy scalar, and subtracting a 1-D array from a column vector broadcasts to a matrix without raising an error. Decide what to cast and what to check before you return. ``` ### Constraints & Assumptions - `y_true` and `y_pred` are 1-D numeric arrays of the same length. - The grader builds `y_pred` from an OLS fit with an intercept on fixed data (`n = 12`), so TSS = ESS + RSS holds up to floating-point rounding. - The result is a tuple of built-in Python floats in the order `(tss, ess, rss)`. ### What a Strong Answer Covers - One shared center, the mean of `y_true`, used for both TSS and ESS. - Vectorized sums returned as built-in floats in the required order. - The cross term that separates TSS from ESS + RSS, and why an intercept makes it vanish. - Input problems such as mismatched shapes, empty arrays and a constant `y_true`. - Linear time in the number of observations. ### Follow-up Questions - Why does TSS = ESS + RSS only hold when the model includes an intercept? - How would you compute $R^2$ from these three quantities, and what should happen when `y_true` is constant? - For a model fitted without an intercept, which $R^2$ would you report, and why can `1 - rss / tss` and `ess / tss` disagree?

Overview: Implement a NumPy function that returns the total, explained and residual sums of squares for a regression fit. It tests how the variance decomposition is defined, which mean ESS is centered on, and when TSS equals ESS plus RSS.

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

Implement sum_of_squares(y_true, y_pred) for two equal-length 1-D NumPy arrays. It returns a tuple of three Python floats (tss, ess, rss) that split the variation in y_true around its mean:

  • Let ybar = y_true.mean() .
  • TSS = sum((y_true - ybar)**2)
  • ESS = sum((y_pred - ybar)**2) , centered on the mean of y_true (the same center as TSS).
  • RSS = sum((y_true - y_pred)**2)

Example:

y_true = np.array([1.0, 2.0, 3.0])
y_pred = np.array([1.1, 1.9, 3.0])
sum_of_squares(y_true, y_pred)  # about (2.0, 1.82, 0.02) -> tss, ess, rss

This y_pred was picked by hand and is not an OLS fit, so ESS + RSS (1.84) does not equal TSS in the example.

Constraints & Assumptions

  • y_true and y_pred are 1-D numeric arrays of the same length.
  • The grader builds y_pred from an OLS fit with an intercept on fixed data ( n = 12 ), so TSS = ESS + RSS holds up to floating-point rounding.
  • The result is a tuple of built-in Python floats in the order (tss, ess, rss) .

What a Strong Answer Covers Guidance

  • One shared center, the mean of y_true , used for both TSS and ESS.
  • Vectorized sums returned as built-in floats in the required order.
  • The cross term that separates TSS from ESS + RSS, and why an intercept makes it vanish.
  • Input problems such as mismatched shapes, empty arrays and a constant y_true .
  • Linear time in the number of observations.

Follow-up Questions Guidance

  • Why does TSS = ESS + RSS only hold when the model includes an intercept?
  • How would you compute R2R^2 from these three quantities, and what should happen when y_true is constant?
  • For a model fitted without an intercept, which R2R^2 would you report, and why can 1 - rss / tss and ess / tss disagree?