OLS Coefficient Covariance Matrix

Quick Overview

Implement a Python function that fits ordinary least squares with an intercept and returns the full variance-covariance matrix of the coefficient estimates. It tests the design matrix layout, the unbiased residual variance, and how the matrix relates to standard errors and statsmodels output.

OLS Coefficient Covariance Matrix

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `def coefficient_covariance_matrix(X: np.ndarray, y: np.ndarray) -> np.ndarray`. Fit OLS of `y` on `X` with an intercept and return the full variance-covariance matrix of the estimated coefficients: `sigma_hat_sq * inv(A.T @ A)`, where `A` is `X` with a prepended ones column and `sigma_hat_sq = RSS / (n - p - 1)`. Requirements: - Return shape `(p+1, p+1)` for `X` with `p` columns. - Row and column `0` are the intercept, and `1..p` are the columns of `X` in order. - Return the covariance matrix itself. Do not normalize it into a correlation matrix. - The result must equal `sm.OLS(y, sm.add_constant(X)).fit().cov_params()`, and `np.sqrt(np.diag(result))` must equal `res.bse`. ```hint Watch the denominator Before dividing RSS, count every parameter the fit estimated, the intercept included. ``` ```hint Check yourself The square roots of your diagonal should match `bse` from a statsmodels fit on the same data. Try it on a small random design first. ``` ### Constraints & Assumptions - The grader uses a fixed `X` of shape `(24, 3)` and a fixed `y`, compared with `numpy.testing.assert_allclose`. - NumPy or `statsmodels` may be used. ### What a Strong Answer Covers - A design matrix with the ones column first, so index 0 is the intercept and the result has shape `(p+1, p+1)`. - The unbiased residual variance `RSS / (n - p - 1)`, with the intercept counted as a parameter. - Returning the full scaled inverse, whose diagonal square roots equal `res.bse`. - Numerical care: a least-squares solver for the coefficients, and what to do when `A.T @ A` is singular or ill-conditioned. ### Follow-up Questions - Why divide RSS by `n - p - 1`, and what goes wrong if you divide by `n`? - How would you derive the correlation matrix of the coefficients from this result? - What if `A.T @ A` is singular or ill-conditioned? - How would you use this matrix to get the standard error of a combination of coefficients, such as $\hat\beta_1 - \hat\beta_2$?

Overview: Implement a Python function that fits ordinary least squares with an intercept and returns the full variance-covariance matrix of the coefficient estimates. It tests the design matrix layout, the unbiased residual variance, and how the matrix relates to standard errors and statsmodels output.

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

Implement def coefficient_covariance_matrix(X: np.ndarray, y: np.ndarray) -> np.ndarray. Fit OLS of y on X with an intercept and return the full variance-covariance matrix of the estimated coefficients: sigma_hat_sq * inv(A.T @ A), where A is X with a prepended ones column and sigma_hat_sq = RSS / (n - p - 1).

Requirements:

  • Return shape (p+1, p+1) for X with p columns.
  • Row and column 0 are the intercept, and 1..p are the columns of X in order.
  • Return the covariance matrix itself. Do not normalize it into a correlation matrix.
  • The result must equal sm.OLS(y, sm.add_constant(X)).fit().cov_params() , and np.sqrt(np.diag(result)) must equal res.bse .

Constraints & Assumptions

  • The grader uses a fixed X of shape (24, 3) and a fixed y , compared with numpy.testing.assert_allclose .
  • NumPy or statsmodels may be used.

What a Strong Answer Covers Guidance

  • A design matrix with the ones column first, so index 0 is the intercept and the result has shape (p+1, p+1) .
  • The unbiased residual variance RSS / (n - p - 1) , with the intercept counted as a parameter.
  • Returning the full scaled inverse, whose diagonal square roots equal res.bse .
  • Numerical care: a least-squares solver for the coefficients, and what to do when A.T @ A is singular or ill-conditioned.

Follow-up Questions Guidance

  • Why divide RSS by n - p - 1 , and what goes wrong if you divide by n ?
  • How would you derive the correlation matrix of the coefficients from this result?
  • What if A.T @ A is singular or ill-conditioned?
  • How would you use this matrix to get the standard error of a combination of coefficients, such as β^1−β^2\hat\beta_1 - \hat\beta_2 ?