Implement weighted least squares with the closed-form normal equation in NumPy

Quick Overview

Implement weighted least squares in NumPy by adding an intercept column, building a diagonal weight matrix and solving the weighted normal equations. It tests the WLS objective, matching statsmodels output, and why to solve the linear system without forming an explicit inverse.

Implement weighted least squares with the closed-form normal equation in NumPy

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `wls_closed_form(X, y, w)` using the weighted least squares (WLS) normal equation $$ \beta = (X^\top W X)^{-1} X^\top W y, \qquad W = \operatorname{diag}(w), $$ where `X` in the formula is the design matrix including the intercept column. Use `numpy` only (no `statsmodels` or `sklearn`). Return a 1-D array of shape `(p+1,)` ordered `[intercept, b1, ..., bp]`. The function must: - Take `X` as a raw `(n, p)` matrix without an intercept and prepend a ones column before solving. - Build `W = np.diag(w)` and use the weights as given (`w` has length n and is strictly positive). - Solve with `np.linalg.solve(X.T @ W @ X, X.T @ W @ y)` on the design matrix, without an explicit `np.linalg.inv`. - Match `sm.WLS(y, add_constant(X), weights=w).fit().params`. For example: ```python X.shape == (9, 2) # raw, no intercept w.shape == (9,) # strictly positive beta = wls_closed_form(X, y, w) beta.shape == (3,) # [intercept, b1, b2] ``` ```hint Matching statsmodels Compare the objective each side minimizes. `sm.WLS` multiplies each squared residual by its weight, which tells you whether `w` needs any transform before it goes into `W`. ``` ### Constraints & Assumptions - `X` has shape `(n, p)` with no intercept column, and `y` has length n. - `w` has length n and every entry is strictly positive. - Only `numpy` is allowed. ### What a Strong Answer Covers - Prepends the ones column so the coefficients come out intercept first. - Puts the weights, as given, on the diagonal of `W`, matching the objective `sm.WLS` minimizes. - Solves the normal equations with `np.linalg.solve`, never forming an explicit inverse. - Returns a 1-D array of shape `(p+1,)`. ### Follow-up Questions - Why use `np.linalg.solve` over `np.linalg.inv` here? - How does WLS relate to OLS on transformed data? - How would you avoid building the n × n matrix `W` when n is large?

Overview: Implement weighted least squares in NumPy by adding an intercept column, building a diagonal weight matrix and solving the weighted normal equations. It tests the WLS objective, matching statsmodels output, and why to solve the linear system without forming an explicit inverse.

General logo
General
Nov 25, 2026
mediumData ScientistOtherMachine Learning
0
0

Implement wls_closed_form(X, y, w) using the weighted least squares (WLS) normal equation

β=(X⊤WX)−1X⊤Wy,W=diag⁡(w),\beta = (X^\top W X)^{-1} X^\top W y, \qquad W = \operatorname{diag}(w),

where X in the formula is the design matrix including the intercept column. Use numpy only (no statsmodels or sklearn). Return a 1-D array of shape (p+1,) ordered [intercept, b1, ..., bp].

The function must:

  • Take X as a raw (n, p) matrix without an intercept and prepend a ones column before solving.
  • Build W = np.diag(w) and use the weights as given ( w has length n and is strictly positive).
  • Solve with np.linalg.solve(X.T @ W @ X, X.T @ W @ y) on the design matrix, without an explicit np.linalg.inv .
  • Match sm.WLS(y, add_constant(X), weights=w).fit().params .

For example:

X.shape == (9, 2)   # raw, no intercept
w.shape == (9,)     # strictly positive
beta = wls_closed_form(X, y, w)
beta.shape == (3,)  # [intercept, b1, b2]

Constraints & Assumptions

  • X has shape (n, p) with no intercept column, and y has length n.
  • w has length n and every entry is strictly positive.
  • Only numpy is allowed.

What a Strong Answer Covers Guidance

  • Prepends the ones column so the coefficients come out intercept first.
  • Puts the weights, as given, on the diagonal of W , matching the objective sm.WLS minimizes.
  • Solves the normal equations with np.linalg.solve , never forming an explicit inverse.
  • Returns a 1-D array of shape (p+1,) .

Follow-up Questions Guidance

  • Why use np.linalg.solve over np.linalg.inv here?
  • How does WLS relate to OLS on transformed data?
  • How would you avoid building the n × n matrix W when n is large?