Compare OLS residual sums with and without an intercept
Role: Data Scientist
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Implement the function below. Given `X` of shape `(n, k)` (no intercept column) and `y` of shape `(n,)`, fit OLS with design `[ones, X]` and with design `X` alone. Return `(sum_with, sum_without)`, each the signed sum of that model's residuals.
```python
import numpy as np
def residual_sum_with_and_without_intercept(
X: np.ndarray, y: np.ndarray
) -> tuple[float, float]:
...
```
The function must:
- Fit via OLS (for example `np.linalg.lstsq` or the normal equations).
- Return the raw signed sums as two Python floats, with no absolute values.
- Produce a `sum_with` that is about zero (order `1e-14`).
```hint Look at the normal equations
Write the normal equations for each design and ask which one contains an equation about the plain sum of the residuals.
```
### Constraints & Assumptions
- `X` has shape `(n, k)`, contains no intercept column, and has full column rank.
- `y` has shape `(n,)`.
- The grader's input has a strongly non-zero mean response, so `sum_without` is clearly non-zero.
### What a Strong Answer Covers
- Builds two separate designs, `[ones, X]` and `X` unchanged, and fits each with a stable least-squares solver.
- Computes each model's residuals from its own coefficients and returns the signed sums as floats.
- Explains, from the normal equations, why the intercept model's residuals sum to zero up to rounding.
- Explains why the model without an intercept has no such guarantee.
### Follow-up Questions
- Why does including an intercept force the residuals to sum to exactly zero?
- Does a zero residual sum imply the model is unbiased or well-specified?
- Can a model with no explicit intercept column still have residuals that sum to zero?
- Why do regression packages report a different kind of R² for a model fit without an intercept?
Overview: Implement a NumPy function that fits ordinary least squares with and without an intercept and returns the signed sum of residuals for each model. It tests the normal equations, why an intercept column forces residuals to sum to zero, and why that identity says nothing about model bias.