Fit OLS and inverse-variance WLS on the same data and return both coefficient vectors

Quick Overview

Fit ordinary least squares and weighted least squares with inverse-variance weights on the same design in Python, returning both coefficient vectors. It tests converting standard deviations into weights, why the two fits differ under heteroskedastic errors, and when WLS is the more efficient estimator.

Fit OLS and inverse-variance WLS on the same data and return both coefficient vectors

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `wls_vs_ols_params(X, y, sigma)`, where `X` is a raw `(n, p)` design matrix without an intercept, `y` has length `n`, and `sigma` holds the known error standard deviation of each observation (length `n`). Fit ordinary least squares (OLS) and weighted least squares (WLS) with inverse-variance weights on the same data and return `(ols_params, wls_params)`. The function must: - Prepend an intercept with `sm.add_constant` for both fits. - Use WLS weights `w = 1.0 / sigma**2`. `sigma` is a standard deviation, not a variance. - Return each result as a 1-D `np.ndarray` of shape `(p+1,)` ordered `[intercept, b1, ..., bp]`. With a single predictor taking the values 1 to 10 and a `sigma` that varies strongly across observations, both arrays have shape `(2,)` and differ, because WLS leans toward the low-variance points. A small case you can check by hand: ```python X = np.array([[0.0], [1.0], [2.0]]) # shape (3, 1) y = np.array([1.0, 3.0, 2.0]) sigma = np.array([1.0, 1.0, 0.5]) # standard deviations, so w = [1, 1, 4] ols, wls = wls_vs_ols_params(X, y, sigma) # ols -> [1.5, 0.5] # wls -> [1.5714..., 0.2857...] (exactly [11/7, 2/7]) ``` The third point has the smallest standard deviation, so WLS pulls the line toward it: the fitted value at x = 2 drops from 2.5 under OLS to about 2.14. ```hint One design, two fits Build the intercept-augmented design once and pass the same matrix to both models, so the weighting is the only difference between the fits. Check that `sm.add_constant` adds the column for every possible `X`. ``` ```hint Explaining the difference Compare the objective each fit minimizes, and look at which observations get the largest weight and where they sit relative to the OLS line. ``` ### Constraints & Assumptions - `X` has shape `(n, p)` with no intercept column; `y` and `sigma` have length `n`. - `sigma` is known for every observation and is a standard deviation, not a variance. - NumPy and `statsmodels.api as sm` are available. ### What a Strong Answer Covers - One shared design matrix with the intercept first, used for both fits. - Weights computed as inverse variances from the standard deviations, and what goes wrong with the wrong power. - Two 1-D arrays of shape `(p+1,)` returned in the required coefficient order and tuple order. - Why the two coefficient vectors differ and which estimator is more efficient under heteroskedastic errors. - Edge cases such as zero or non-finite `sigma`, constant `sigma`, and a constant column already in `X`. ### Follow-up Questions - Why does WLS reduce to OLS when all weights are equal? - What would you do if the true error variances were unknown? - How do the standard errors reported by the two fits compare, and which ones can you trust when `sigma` varies?

Overview: Fit ordinary least squares and weighted least squares with inverse-variance weights on the same design in Python, returning both coefficient vectors. It tests converting standard deviations into weights, why the two fits differ under heteroskedastic errors, and when WLS is the more efficient estimator.

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

Implement wls_vs_ols_params(X, y, sigma), where X is a raw (n, p) design matrix without an intercept, y has length n, and sigma holds the known error standard deviation of each observation (length n). Fit ordinary least squares (OLS) and weighted least squares (WLS) with inverse-variance weights on the same data and return (ols_params, wls_params).

The function must:

  • Prepend an intercept with sm.add_constant for both fits.
  • Use WLS weights w = 1.0 / sigma**2 . sigma is a standard deviation, not a variance.
  • Return each result as a 1-D np.ndarray of shape (p+1,) ordered [intercept, b1, ..., bp] .

With a single predictor taking the values 1 to 10 and a sigma that varies strongly across observations, both arrays have shape (2,) and differ, because WLS leans toward the low-variance points. A small case you can check by hand:

X = np.array([[0.0], [1.0], [2.0]])   # shape (3, 1)
y = np.array([1.0, 3.0, 2.0])
sigma = np.array([1.0, 1.0, 0.5])     # standard deviations, so w = [1, 1, 4]

ols, wls = wls_vs_ols_params(X, y, sigma)
# ols -> [1.5, 0.5]
# wls -> [1.5714..., 0.2857...]   (exactly [11/7, 2/7])

The third point has the smallest standard deviation, so WLS pulls the line toward it: the fitted value at x = 2 drops from 2.5 under OLS to about 2.14.

Constraints & Assumptions

  • X has shape (n, p) with no intercept column; y and sigma have length n .
  • sigma is known for every observation and is a standard deviation, not a variance.
  • NumPy and statsmodels.api as sm are available.

What a Strong Answer Covers Guidance

  • One shared design matrix with the intercept first, used for both fits.
  • Weights computed as inverse variances from the standard deviations, and what goes wrong with the wrong power.
  • Two 1-D arrays of shape (p+1,) returned in the required coefficient order and tuple order.
  • Why the two coefficient vectors differ and which estimator is more efficient under heteroskedastic errors.
  • Edge cases such as zero or non-finite sigma , constant sigma , and a constant column already in X .

Follow-up Questions Guidance

  • Why does WLS reduce to OLS when all weights are equal?
  • What would you do if the true error variances were unknown?
  • How do the standard errors reported by the two fits compare, and which ones can you trust when sigma varies?