Verify the sum-of-squares decomposition for an OLS fit with an intercept
Quick Overview
Fit an ordinary least squares model with an intercept and check that the total sum of squares equals the explained plus residual sums. It tests the variance decomposition, how R-squared relates to ESS and RSS, and why the intercept matters.
Verify the sum-of-squares decomposition for an OLS fit with an intercept
Role: Data Scientist
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `verify_decomposition(X, y)`. It fits ordinary least squares with an intercept on a feature matrix `X` of shape `(20, 2)` and a target `y`, computes the predictions `yhat`, and returns the 4-tuple of floats `(tss, ess, rss, r2_from_ess)` in that order:
- `tss = sum((y - y.mean())**2)`
- `ess = sum((yhat - y.mean())**2)`
- `rss = sum((y - yhat)**2)`
- `r2_from_ess = ess / tss`
You may fit with `statsmodels` (`sm.add_constant` plus `sm.OLS`) or solve the least-squares problem yourself.
```hint Where the intercept goes
`X` holds only the two features. Think about what the design matrix needs before you solve for the coefficients.
```
```hint Keep the check independent
Compute ESS from the fitted values themselves. A library attribute that is defined as TSS minus RSS would make the identity true by construction.
```
### Constraints & Assumptions
- `X` has shape `(20, 2)` and holds the two features; your code adds the intercept. `y` has 20 values.
- The model is OLS with an intercept, so TSS = ESS + RSS should hold up to floating-point rounding.
- Return four built-in Python floats in the order `(tss, ess, rss, r2_from_ess)`.
### What a Strong Answer Covers
- An intercept column added to the design and an OLS fit with a numerically stable solver.
- All three sums computed from their definitions with the mean of `y` as the shared center, returned as floats in the required order.
- Why the intercept makes the cross term vanish, so `ess / tss` equals `1 - rss / tss`.
- Edge cases such as a constant `y` (TSS of zero) and `y` passed as a column vector.
### Follow-up Questions
- Why can TSS = ESS + RSS fail without an intercept?
- Can `r2_from_ess` ever exceed 1 or go negative? How does `1 - rss / tss` behave in the same cases?
- How would you test this function without relying on exact floating-point equality?
- If the two features were nearly collinear, which way of solving for the coefficients would you trust, and why?
Overview: Fit an ordinary least squares model with an intercept and check that the total sum of squares equals the explained plus residual sums. It tests the variance decomposition, how R-squared relates to ESS and RSS, and why the intercept matters.
Verify the sum-of-squares decomposition for an OLS fit with an intercept
General
Nov 24, 2026
mediumData ScientistOtherMachine Learning
0
0
Implement verify_decomposition(X, y). It fits ordinary least squares with an intercept on a feature matrix X of shape (20, 2) and a target y, computes the predictions yhat, and returns the 4-tuple of floats (tss, ess, rss, r2_from_ess) in that order:
tss = sum((y - y.mean())**2)
ess = sum((yhat - y.mean())**2)
rss = sum((y - yhat)**2)
r2_from_ess = ess / tss
You may fit with statsmodels (sm.add_constant plus sm.OLS) or solve the least-squares problem yourself.
Constraints & Assumptions
X
has shape
(20, 2)
and holds the two features; your code adds the intercept.
y
has 20 values.
The model is OLS with an intercept, so TSS = ESS + RSS should hold up to floating-point rounding.
Return four built-in Python floats in the order
(tss, ess, rss, r2_from_ess)
.
What a Strong Answer Covers Guidance
An intercept column added to the design and an OLS fit with a numerically stable solver.
All three sums computed from their definitions with the mean of
y
as the shared center, returned as floats in the required order.
Why the intercept makes the cross term vanish, so
ess / tss
equals
1 - rss / tss
.
Edge cases such as a constant
y
(TSS of zero) and
y
passed as a column vector.
Follow-up Questions Guidance
Why can TSS = ESS + RSS fail without an intercept?
Can
r2_from_ess
ever exceed 1 or go negative? How does
1 - rss / tss
behave in the same cases?
How would you test this function without relying on exact floating-point equality?
If the two features were nearly collinear, which way of solving for the coefficients would you trust, and why?