Return the final IRLS observation weights from a Huber robust regression in statsmodels
Quick Overview
Write a Python function that fits a Huber robust linear regression with statsmodels and returns the final per-observation IRLS weights. It tests how RLM scales residuals, how the Huber weight function treats small and large residuals, and what the weights reveal about outliers.
Return the final IRLS observation weights from a Huber robust regression in statsmodels
Role: Data Scientist
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Implement `rlm_observation_weights(X, y)`. It fits a Huber-norm robust linear regression with statsmodels, which fits by iteratively reweighted least squares (IRLS), and returns the final per-observation IRLS weights.
```python
def rlm_observation_weights(X: np.ndarray, y: np.ndarray) -> np.ndarray:
...
```
Requirements:
- Fit `sm.RLM(y, sm.add_constant(X), M=sm.robust.norms.HuberT()).fit()` with all default `.fit()` parameters.
- Return `results.weights`, the weights used in the last weighted least squares step, as a 1-D `np.ndarray` of shape `(n,)`.
- An observation whose scaled residual is within the Huber tuning constant gets weight `1.0`. An observation with a larger scaled residual gets a weight strictly below `1.0`.
No manual IRLS loop or tuning constant is needed. Rely on the statsmodels defaults.
Example:
```python
# X has shape (12, 1); y has a strong outlier at index 7, and the other points lie close to a line
w = rlm_observation_weights(X, y)
# w.shape == (12,)
# w[7] is the unique minimum and is < 1.0
# all non-outlier weights == 1.0 for this data
```
```hint What "scaled" means
Each weight depends on a residual divided by a scale estimate. Find out which scale RLM uses by default and what Huber's weight function does on each side of its threshold.
```
### Constraints & Assumptions
- `X` is a 2-D array of shape `(n, p)` without a constant column (12 by 1 in the example), and `y` has shape `(n,)`.
- `numpy` is imported as `np` and `statsmodels.api` as `sm`.
### What a Strong Answer Covers
- The exact model call, with an intercept, the HuberT norm and default fit settings
- Returning the final IRLS weights as a 1-D float array of length `n`
- How Huber's weight function maps a scaled residual to a weight
- Why residuals are divided by a scale estimate, and which estimate RLM uses by default
- When a clean point can still end up with a weight below 1
### Follow-up Questions
- How does the Huber norm assign weights compared to Tukey's biweight?
- Why scale residuals before computing weights, and how is the scale estimated?
- How would you use these weights to flag outliers in a report, and what cutoff would you choose?
Overview: Write a Python function that fits a Huber robust linear regression with statsmodels and returns the final per-observation IRLS weights. It tests how RLM scales residuals, how the Huber weight function treats small and large residuals, and what the weights reveal about outliers.
Return the final IRLS observation weights from a Huber robust regression in statsmodels
General
Nov 24, 2026
easyData ScientistOtherMachine Learning
0
0
Implement rlm_observation_weights(X, y). It fits a Huber-norm robust linear regression with statsmodels, which fits by iteratively reweighted least squares (IRLS), and returns the final per-observation IRLS weights.
Fit
sm.RLM(y, sm.add_constant(X), M=sm.robust.norms.HuberT()).fit()
with all default
.fit()
parameters.
Return
results.weights
, the weights used in the last weighted least squares step, as a 1-D
np.ndarray
of shape
(n,)
.
An observation whose scaled residual is within the Huber tuning constant gets weight
1.0
. An observation with a larger scaled residual gets a weight strictly below
1.0
.
No manual IRLS loop or tuning constant is needed. Rely on the statsmodels defaults.
Example:
# X has shape (12, 1); y has a strong outlier at index 7, and the other points lie close to a line
w = rlm_observation_weights(X, y)
# w.shape == (12,)
# w[7] is the unique minimum and is < 1.0
# all non-outlier weights == 1.0 for this data
Constraints & Assumptions
X
is a 2-D array of shape
(n, p)
without a constant column (12 by 1 in the example), and
y
has shape
(n,)
.
numpy
is imported as
np
and
statsmodels.api
as
sm
.
What a Strong Answer Covers Guidance
The exact model call, with an intercept, the HuberT norm and default fit settings
Returning the final IRLS weights as a 1-D float array of length
n
How Huber's weight function maps a scaled residual to a weight
Why residuals are divided by a scale estimate, and which estimate RLM uses by default
When a clean point can still end up with a weight below 1
Follow-up Questions Guidance
How does the Huber norm assign weights compared to Tukey's biweight?
Why scale residuals before computing weights, and how is the scale estimated?
How would you use these weights to flag outliers in a report, and what cutoff would you choose?