Compare OLS and Huber robust regression slopes with statsmodels
Quick Overview
Write a Python function that fits the same data with ordinary least squares and with Huber robust regression in statsmodels and returns both slopes. It tests building a design matrix with an intercept, reading the right coefficient, and explaining how each estimator reacts to a single corrupted point.
Compare OLS and Huber robust regression slopes with statsmodels
Role: Data Scientist
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Implement `ols_vs_rlm_slope(x, y)` with statsmodels. Fit `y` on `x` with both ordinary least squares (OLS) and Huber robust regression (RLM), and return `(ols_slope, rlm_slope)` as two plain floats.
```python
def ols_vs_rlm_slope(x: np.ndarray, y: np.ndarray) -> tuple:
...
```
Requirements:
- Add an intercept with `sm.add_constant` for both fits.
- Fit `sm.OLS(y, X)` and `sm.RLM(y, X, M=sm.robust.norms.HuberT())`, each with the default `.fit()`.
- Return each model's slope, `params[1]`, not the intercept `params[0]`.
Example:
```python
# x has shape (12,); y is near-linear in x except for one grossly corrupted point
ols_slope, rlm_slope = ols_vs_rlm_slope(x, y)
# rlm_slope is closer to the clean underlying slope than ols_slope
```
```hint Where the slope sits
Look at the column order `sm.add_constant` produces. It decides which entry of `params` belongs to `x`.
```
### Constraints & Assumptions
- `x` and `y` are 1-D numeric arrays of the same length (12 points in the example).
- `x` is not constant, so a slope exists.
- `numpy` is imported as `np` and `statsmodels.api` as `sm`.
### What a Strong Answer Covers
- Building one design matrix with an intercept and using it for both fits
- Reading the slope at the correct index and returning plain Python floats
- Why squared loss lets a single outlier tilt the OLS line
- How Huber's loss and the reweighting inside RLM limit an outlier's pull
- Edge cases such as a constant `x`, mismatched lengths or missing values
### Follow-up Questions
- Why does `HuberT` resist the outlier while OLS does not?
- How would you measure each point's influence on the fit directly, without eyeballing the two slopes?
- How does the robust slope change as you lower or raise the Huber tuning constant `t`?
Overview: Write a Python function that fits the same data with ordinary least squares and with Huber robust regression in statsmodels and returns both slopes. It tests building a design matrix with an intercept, reading the right coefficient, and explaining how each estimator reacts to a single corrupted point.
Compare OLS and Huber robust regression slopes with statsmodels
General
Nov 24, 2026
easyData ScientistOtherMachine Learning
0
0
Implement ols_vs_rlm_slope(x, y) with statsmodels. Fit y on x with both ordinary least squares (OLS) and Huber robust regression (RLM), and return (ols_slope, rlm_slope) as two plain floats.
Add an intercept with
sm.add_constant
for both fits.
Fit
sm.OLS(y, X)
and
sm.RLM(y, X, M=sm.robust.norms.HuberT())
, each with the default
.fit()
.
Return each model's slope,
params[1]
, not the intercept
params[0]
.
Example:
# x has shape (12,); y is near-linear in x except for one grossly corrupted point
ols_slope, rlm_slope = ols_vs_rlm_slope(x, y)
# rlm_slope is closer to the clean underlying slope than ols_slope
Constraints & Assumptions
x
and
y
are 1-D numeric arrays of the same length (12 points in the example).
x
is not constant, so a slope exists.
numpy
is imported as
np
and
statsmodels.api
as
sm
.
What a Strong Answer Covers Guidance
Building one design matrix with an intercept and using it for both fits
Reading the slope at the correct index and returning plain Python floats
Why squared loss lets a single outlier tilt the OLS line
How Huber's loss and the reweighting inside RLM limit an outlier's pull
Edge cases such as a constant
x
, mismatched lengths or missing values
Follow-up Questions Guidance
Why does
HuberT
resist the outlier while OLS does not?
How would you measure each point's influence on the fit directly, without eyeballing the two slopes?
How does the robust slope change as you lower or raise the Huber tuning constant
t
?