Implement a weighted least squares fit with an intercept in statsmodels

Quick Overview

Implement a weighted least squares regression in Python with statsmodels, adding an intercept and returning the coefficient array in a fixed order. It tests how per-observation weights enter the fit, how they relate to error variances, and the edge cases that change the output shape.

Implement a weighted least squares fit with an intercept in statsmodels

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `wls_params(X, y, w)` that fits a weighted least squares (WLS) regression of `y` on `X` and returns the coefficients. `X` is a raw `(n, p)` matrix without an intercept column, `y` has length `n`, and `w` is a 1-D array of length `n` with one weight per observation. The function must: - Prepend an intercept column (for example with `sm.add_constant(X, prepend=True)`) so the intercept is the first coefficient. - Fit with `sm.WLS(y, X, weights=w)`, passing `w` as given, with no rescaling or normalizing. - Return the raw `results.params` array (not a Series or a summary) of shape `(p+1,)`, ordered `[intercept, b1, ..., bp]`. For example: ```python X.shape == (8, 2) # raw design, no intercept y.shape == (8,) w.shape == (8,) # strictly positive weights beta = wls_params(X, y, w) beta.shape # (3,) -> [intercept, b1, b2] ``` ```hint Check the output shape Make sure the result always has p + 1 entries. Look at what `sm.add_constant` does by default when `X` already contains a column with the same value in every row, and at the type `params` takes when the inputs are pandas objects. ``` ### Constraints & Assumptions - `X` has shape `(n, p)` with no intercept column; `y` and `w` have length `n`. - The grader builds fixed `X`, `y` and strictly positive `w`. - `statsmodels` is available as `statsmodels.api as sm`, along with NumPy. ### What a Strong Answer Covers - The weighted objective WLS minimizes and how the `weights` argument enters it. - An intercept added as the first column, with the `(p+1,)` shape guaranteed even when `X` already has a constant column. - A plain NumPy array returned in the order `[intercept, b1, ..., bp]`. - Edge cases: a 1-D `X`, pandas inputs, and zero or negative weights. ### Follow-up Questions - How do the weights relate to the observation variances, and what changes if every weight is multiplied by the same constant? - How would you reproduce the coefficients without statsmodels, and which way of solving is numerically safer? - If the error variances are unknown, how would you choose the weights?

Overview: Implement a weighted least squares regression in Python with statsmodels, adding an intercept and returning the coefficient array in a fixed order. It tests how per-observation weights enter the fit, how they relate to error variances, and the edge cases that change the output shape.

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

Implement wls_params(X, y, w) that fits a weighted least squares (WLS) regression of y on X and returns the coefficients. X is a raw (n, p) matrix without an intercept column, y has length n, and w is a 1-D array of length n with one weight per observation.

The function must:

  • Prepend an intercept column (for example with sm.add_constant(X, prepend=True) ) so the intercept is the first coefficient.
  • Fit with sm.WLS(y, X, weights=w) , passing w as given, with no rescaling or normalizing.
  • Return the raw results.params array (not a Series or a summary) of shape (p+1,) , ordered [intercept, b1, ..., bp] .

For example:

X.shape == (8, 2)   # raw design, no intercept
y.shape == (8,)
w.shape == (8,)     # strictly positive weights

beta = wls_params(X, y, w)
beta.shape          # (3,)  -> [intercept, b1, b2]

Constraints & Assumptions

  • X has shape (n, p) with no intercept column; y and w have length n .
  • The grader builds fixed X , y and strictly positive w .
  • statsmodels is available as statsmodels.api as sm , along with NumPy.

What a Strong Answer Covers Guidance

  • The weighted objective WLS minimizes and how the weights argument enters it.
  • An intercept added as the first column, with the (p+1,) shape guaranteed even when X already has a constant column.
  • A plain NumPy array returned in the order [intercept, b1, ..., bp] .
  • Edge cases: a 1-D X , pandas inputs, and zero or negative weights.

Follow-up Questions Guidance

  • How do the weights relate to the observation variances, and what changes if every weight is multiplied by the same constant?
  • How would you reproduce the coefficients without statsmodels, and which way of solving is numerically safer?
  • If the error variances are unknown, how would you choose the weights?