Fit OLS with an intercept and return the signed sum of residuals
Quick Overview
Implement a NumPy function that fits ordinary least squares with an intercept and returns the signed sum of the residuals. It tests building a design matrix, solving least squares in a numerically stable way, and explaining what the normal equations imply about residuals when the model includes an intercept.
Fit OLS with an intercept and return the signed sum of residuals
Role: Data Scientist
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Implement a function that fits ordinary least squares (OLS) with an intercept and returns the signed sum of the residuals.
```python
import numpy as np
def residual_sum(X: np.ndarray, y: np.ndarray) -> float:
...
```
`X` has shape `(n, k)` and does not include an intercept column. `y` has shape `(n,)`. Fit $y \approx \beta_0 + X\beta$ by least squares and return $\sum_i (y_i - \hat{y}_i)$ as a Python `float`.
Requirements:
- Add the intercept term yourself before fitting.
- Return the plain signed sum of the residuals, with no squaring and no absolute values.
- The result is zero up to floating-point error (on the order of `1e-14` for unit-scale data), so it will usually not be exactly `0.0`.
Example:
```python
X = np.array([[1.0], [2.0], [3.0], [4.0]])
y = np.array([2.0, 4.1, 5.9, 8.2])
residual_sum(X, y) # about 0.0, up to floating-point rounding
```
```hint Building the fit
Stack a column of ones in front of `X` and pass the result to a least-squares solver such as `np.linalg.lstsq`. Then ask what the least-squares conditions say about the residuals and that column of ones.
```
### Constraints & Assumptions
- `X` has shape `(n, k)` with no constant column added; `y` has shape `(n,)` with the same `n`.
- All entries are finite floats.
### What a Strong Answer Covers
- Building the design matrix with an intercept column and fitting it with a numerically stable least-squares solver.
- Returning the raw signed sum of residuals as a Python `float`.
- Explaining, from the normal equations, why the sum is zero up to rounding when the model has an intercept.
- Handling awkward inputs such as a 1-D `X` or collinear columns without crashing.
- Testing the result with a tolerance that scales with the data.
### Follow-up Questions
- Why does adding an intercept guarantee the residuals sum to zero?
- What would you expect if you dropped the intercept?
- If you fit ridge regression with an intercept, do the residuals still sum to zero? Does it matter whether the intercept is penalized?
Overview: Implement a NumPy function that fits ordinary least squares with an intercept and returns the signed sum of the residuals. It tests building a design matrix, solving least squares in a numerically stable way, and explaining what the normal equations imply about residuals when the model includes an intercept.
X has shape (n, k) and does not include an intercept column. y has shape (n,). Fit y≈β0+Xβ by least squares and return ∑i(yi−y^i) as a Python float.
Requirements:
Add the intercept term yourself before fitting.
Return the plain signed sum of the residuals, with no squaring and no absolute values.
The result is zero up to floating-point error (on the order of
1e-14
for unit-scale data), so it will usually not be exactly
0.0
.
Example:
X = np.array([[1.0], [2.0], [3.0], [4.0]])
y = np.array([2.0, 4.1, 5.9, 8.2])
residual_sum(X, y) # about 0.0, up to floating-point rounding
Constraints & Assumptions
X
has shape
(n, k)
with no constant column added;
y
has shape
(n,)
with the same
n
.
All entries are finite floats.
What a Strong Answer Covers Guidance
Building the design matrix with an intercept column and fitting it with a numerically stable least-squares solver.
Returning the raw signed sum of residuals as a Python
float
.
Explaining, from the normal equations, why the sum is zero up to rounding when the model has an intercept.
Handling awkward inputs such as a 1-D
X
or collinear columns without crashing.
Testing the result with a tolerance that scales with the data.
Follow-up Questions Guidance
Why does adding an intercept guarantee the residuals sum to zero?
What would you expect if you dropped the intercept?
If you fit ridge regression with an intercept, do the residuals still sum to zero? Does it matter whether the intercept is penalized?