Show that WLS equals intercept-free OLS on square-root-weighted data

Quick Overview

Implement a function that fits weighted least squares with statsmodels and then fits plain OLS on data scaled by the square root of the weights, returning both coefficient vectors. It tests whether you understand why WLS reduces to OLS and how the intercept column must be handled.

Show that WLS equals intercept-free OLS on square-root-weighted data

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `wls_equals_transformed_ols(X, y, w)` to show numerically that weighted least squares (WLS) with weights `w` equals intercept-free OLS on data transformed by the square root of the weights. Return the tuple `(wls_params, transformed_ols_params)`. The two arrays must be equal to numerical tolerance. The function must follow these steps: - Build `Xc = sm.add_constant(X)` and compute `wls_params` from `sm.WLS(y, Xc, weights=w).fit().params`. - Transform the data: `Xs = Xc * np.sqrt(w)[:, None]` and `ys = y * np.sqrt(w)`. - Compute `transformed_ols_params` from `sm.OLS(ys, Xs).fit().params` with no added intercept. - The intercept column in `Xc` is itself scaled by `sqrt(w)`, so do not call `add_constant` again on `Xs`. - Return both as 1-D ndarrays of shape `(p+1,)` ordered `[intercept, b1, ..., bp]`. ```hint Where the weights go Write the WLS objective as a sum of weighted squared residuals, then ask what multiplying one row of both `y` and `Xc` by a positive number does to that row's squared residual. ``` ```hint Count the columns Before the second fit, look at what the column of ones in `Xc` turns into after the row scaling, and check how many columns `Xs` should have. ``` ### Constraints & Assumptions - The grader supplies a fixed `X` of shape `(10, 1)`, a fixed `y`, and a fixed strictly positive `w`, so `p = 1` and both returned arrays have length 2. - `numpy` and `statsmodels.api` (as `sm`) are available. - The two parameter vectors are compared to numerical tolerance, not bit for bit. ### What a Strong Answer Covers - Both fits built as specified, with the constant added once, before the transformation. - Scaling of `y` and of every column of `Xc`, including the intercept column. - An argument from the objective or the normal equations for why the two coefficient vectors coincide. - Return values as plain 1-D arrays in the required order, compared with a tolerance. ### Follow-up Questions - Why does scaling rows by `sqrt(w)` reproduce the WLS estimator? - Do the standard errors also match between the two fits? - Which statistics in the two `summary()` tables would differ, and why? - What happens to the coefficients and standard errors if every weight is multiplied by the same constant?

Overview: Implement a function that fits weighted least squares with statsmodels and then fits plain OLS on data scaled by the square root of the weights, returning both coefficient vectors. It tests whether you understand why WLS reduces to OLS and how the intercept column must be handled.

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

Implement wls_equals_transformed_ols(X, y, w) to show numerically that weighted least squares (WLS) with weights w equals intercept-free OLS on data transformed by the square root of the weights. Return the tuple (wls_params, transformed_ols_params). The two arrays must be equal to numerical tolerance.

The function must follow these steps:

  • Build Xc = sm.add_constant(X) and compute wls_params from sm.WLS(y, Xc, weights=w).fit().params .
  • Transform the data: Xs = Xc * np.sqrt(w)[:, None] and ys = y * np.sqrt(w) .
  • Compute transformed_ols_params from sm.OLS(ys, Xs).fit().params with no added intercept.
  • The intercept column in Xc is itself scaled by sqrt(w) , so do not call add_constant again on Xs .
  • Return both as 1-D ndarrays of shape (p+1,) ordered [intercept, b1, ..., bp] .

Constraints & Assumptions

  • The grader supplies a fixed X of shape (10, 1) , a fixed y , and a fixed strictly positive w , so p = 1 and both returned arrays have length 2.
  • numpy and statsmodels.api (as sm ) are available.
  • The two parameter vectors are compared to numerical tolerance, not bit for bit.

What a Strong Answer Covers Guidance

  • Both fits built as specified, with the constant added once, before the transformation.
  • Scaling of y and of every column of Xc , including the intercept column.
  • An argument from the objective or the normal equations for why the two coefficient vectors coincide.
  • Return values as plain 1-D arrays in the required order, compared with a tolerance.

Follow-up Questions Guidance

  • Why does scaling rows by sqrt(w) reproduce the WLS estimator?
  • Do the standard errors also match between the two fits?
  • Which statistics in the two summary() tables would differ, and why?
  • What happens to the coefficients and standard errors if every weight is multiplied by the same constant?