Mean-Centering and OLS Coefficients: Compare Raw and Centered Simple Regression Fits
Role: Data Scientist
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Implement `def centering_effect(x: np.ndarray, y: np.ndarray) -> tuple` for 1-D float arrays of equal length `n`. Fit two OLS models: (A) `y` on raw `x`, (B) `y` on `xc = x - x.mean()`. Return `(intercept_raw, slope_raw, intercept_centered, slope_centered)`.
Requirements:
- Both models include an intercept.
- The two slopes must be identical.
- `intercept_centered` must equal `y.mean()`.
- Return plain Python `float` values in the exact tuple order.
Example:
```python
x = np.array([1., 2., 3., 4.])
y = np.array([2., 4., 5., 7.])
intercept_raw, slope_raw, intercept_centered, slope_centered = centering_effect(x, y)
# (intercept_raw, slope_raw, intercept_centered, slope_centered) == (0.5, 1.6, 4.5, 1.6)
# slope_raw == slope_centered
# intercept_centered == y.mean()
```
```hint What the slope depends on
Write the simple OLS slope in terms of deviations from the mean, then check what a constant shift of `x` does to those deviations.
```
### Constraints & Assumptions
- `x` and `y` are 1-D float arrays of the same length `n`.
- `x` is not constant, so the slope is defined.
### What a Strong Answer Covers
- Two fits that both include an intercept, returned as plain floats in the exact tuple order.
- An argument for why the slope is the same in both models.
- An argument for why the centered intercept equals `y.mean()`, and how the raw intercept relates to it.
- How the implementation guarantees the two slopes come out identical in floating point.
- Time and space cost, and the constant-`x` case.
### Follow-up Questions
- Why does centering leave the slope unchanged but shift the intercept?
- When does centering actually change a coefficient's value or interpretation?
- Does centering change the fitted values, the residuals or $R^2$?
- How does centering change the standard error of the intercept?
Overview: Fit simple OLS twice in NumPy, once on the raw predictor and once on the mean-centered predictor, and return both intercepts and slopes as floats. It tests why centering leaves the slope unchanged, where the intercept moves, and when centering does change coefficients.