Fit a Huber robust linear regression in statsmodels and return its coefficients

Quick Overview

Write a Python function that fits a robust linear regression with the Huber T norm in statsmodels and returns the intercept and coefficients in a fixed order. It tests correct use of the RLM API, the role of the default fit settings, and how Huber's loss limits the influence of an outlier compared to OLS.

Fit a Huber robust linear regression in statsmodels and return its coefficients

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `rlm_huber_params(X, y)`. It fits a robust linear model of `y` on `X` with the Huber T M-estimator and returns the fitted coefficients as a 1-D `np.ndarray` of shape `(p+1,)`, ordered `[intercept, b1, ..., bp]`. ```python def rlm_huber_params(X: np.ndarray, y: np.ndarray) -> np.ndarray: ... ``` Requirements: - Prepend the intercept with `sm.add_constant(X)`. - Fit with `sm.RLM` using `M=sm.robust.norms.HuberT()` and call `.fit()` with its defaults. - Do not pass a custom tuning constant, `scale_est`, `maxiter` or `tol`. Example: ```python # X has shape (12, 1); y follows a line in X except for one strong outlier in y params = rlm_huber_params(X, y) # params.shape == (2,) # params[0] is the intercept and params[1] is the coefficient on X[:, 0] # both stay close to the line through the 11 clean points ``` ```hint Order and type of the result Check where `sm.add_constant` puts the column of ones, and what type `params` has on the fitted results, before you return it. ``` ### Constraints & Assumptions - `X` has shape `(n, p)` and contains no constant column. The test case uses `n = 12` and `p = 1`, with one strong outlier in `y`. - `y` has shape `(n,)`. - `numpy` and `statsmodels.api` (imported as `sm`) are available. ### What a Strong Answer Covers - The model call with an intercept, the HuberT norm and untouched fit defaults - Returning a 1-D float array in the order `[intercept, b1, ..., bp]` - How IRLS with Huber's loss limits the outlier's pull compared to OLS - Which defaults the fit relies on, and why changing them changes the coefficients - Edge cases such as an `X` that already contains a constant column ### Follow-up Questions - How does the Huber T norm reduce the influence of the outlier compared to OLS? - When would you prefer a redescending norm like `TukeyBiweight` over `HuberT`? - How would you get standard errors or confidence intervals for the robust coefficients?

Overview: Write a Python function that fits a robust linear regression with the Huber T norm in statsmodels and returns the intercept and coefficients in a fixed order. It tests correct use of the RLM API, the role of the default fit settings, and how Huber's loss limits the influence of an outlier compared to OLS.

General logo
General
Nov 24, 2026
easyData ScientistOtherMachine Learning
0
0

Implement rlm_huber_params(X, y). It fits a robust linear model of y on X with the Huber T M-estimator and returns the fitted coefficients as a 1-D np.ndarray of shape (p+1,), ordered [intercept, b1, ..., bp].

def rlm_huber_params(X: np.ndarray, y: np.ndarray) -> np.ndarray:
    ...

Requirements:

  • Prepend the intercept with sm.add_constant(X) .
  • Fit with sm.RLM using M=sm.robust.norms.HuberT() and call .fit() with its defaults.
  • Do not pass a custom tuning constant, scale_est , maxiter or tol .

Example:

# X has shape (12, 1); y follows a line in X except for one strong outlier in y
params = rlm_huber_params(X, y)
# params.shape == (2,)
# params[0] is the intercept and params[1] is the coefficient on X[:, 0]
# both stay close to the line through the 11 clean points

Constraints & Assumptions

  • X has shape (n, p) and contains no constant column. The test case uses n = 12 and p = 1 , with one strong outlier in y .
  • y has shape (n,) .
  • numpy and statsmodels.api (imported as sm ) are available.

What a Strong Answer Covers Guidance

  • The model call with an intercept, the HuberT norm and untouched fit defaults
  • Returning a 1-D float array in the order [intercept, b1, ..., bp]
  • How IRLS with Huber's loss limits the outlier's pull compared to OLS
  • Which defaults the fit relies on, and why changing them changes the coefficients
  • Edge cases such as an X that already contains a constant column

Follow-up Questions Guidance

  • How does the Huber T norm reduce the influence of the outlier compared to OLS?
  • When would you prefer a redescending norm like TukeyBiweight over HuberT ?
  • How would you get standard errors or confidence intervals for the robust coefficients?