OLS Coefficient Standard Errors

Quick Overview

Implement a Python function that fits ordinary least squares with an intercept and returns the standard error of every coefficient as a 1-D array. It tests building the design matrix, estimating the error variance with the right degrees of freedom, and reading uncertainty off the coefficient covariance matrix.

OLS Coefficient Standard Errors

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def coefficient_standard_errors(X: np.ndarray, y: np.ndarray) -> np.ndarray`: fit OLS of `y` on `X` with an intercept and return the raw (unrounded) coefficient standard errors as a 1-D array of shape `(p+1,)`, where `p = X.shape[1]`. Element 0 is the intercept standard error, followed by each column of `X` in original order. ```hint Where to read them off Each standard error comes from one entry of a matrix built from the design matrix and the residuals. Make sure the intercept is part of that design matrix. ``` ### Constraints & Assumptions - The grader uses a fixed float `X` of shape `(20, 3)` and `y` of shape `(20,)`, compared with `numpy.testing.assert_allclose`. - `statsmodels` is available. `sm.add_constant` adds the intercept as the first column. ### What a Strong Answer Covers - Prepending the intercept column so element 0 is the intercept and the result has shape `(p+1,)`. - The degrees of freedom used for the error variance, with the intercept counted. - Where in the coefficient covariance the standard errors come from, returned unrounded as a 1-D array. - Edge cases such as a constant column already in `X`, collinear columns, and pandas inputs. ### Follow-up Questions - How are these standard errors computed from the design matrix? - How would you compute heteroskedasticity-robust standard errors instead? - Which assumptions make these classical standard errors valid? - Roughly how do the standard errors change if you double the sample size with the same noise level?

Overview: Implement a Python function that fits ordinary least squares with an intercept and returns the standard error of every coefficient as a 1-D array. It tests building the design matrix, estimating the error variance with the right degrees of freedom, and reading uncertainty off the coefficient covariance matrix.

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

Implement def coefficient_standard_errors(X: np.ndarray, y: np.ndarray) -> np.ndarray: fit OLS of y on X with an intercept and return the raw (unrounded) coefficient standard errors as a 1-D array of shape (p+1,), where p = X.shape[1]. Element 0 is the intercept standard error, followed by each column of X in original order.

Constraints & Assumptions

  • The grader uses a fixed float X of shape (20, 3) and y of shape (20,) , compared with numpy.testing.assert_allclose .
  • statsmodels is available. sm.add_constant adds the intercept as the first column.

What a Strong Answer Covers Guidance

  • Prepending the intercept column so element 0 is the intercept and the result has shape (p+1,) .
  • The degrees of freedom used for the error variance, with the intercept counted.
  • Where in the coefficient covariance the standard errors come from, returned unrounded as a 1-D array.
  • Edge cases such as a constant column already in X , collinear columns, and pandas inputs.

Follow-up Questions Guidance

  • How are these standard errors computed from the design matrix?
  • How would you compute heteroskedasticity-robust standard errors instead?
  • Which assumptions make these classical standard errors valid?
  • Roughly how do the standard errors change if you double the sample size with the same noise level?