OLS Coefficient t-Statistics

Quick Overview

Implement a Python function that fits ordinary least squares with an intercept and returns the signed t-statistic for every coefficient. It tests computing coefficient standard errors from the residual variance and the design matrix, and reading the results as tests of whether each coefficient is zero.

OLS Coefficient t-Statistics

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def coefficient_t_statistics(X: np.ndarray, y: np.ndarray) -> np.ndarray`. Fit OLS of `y` on `X` with an intercept and return the t-statistic for each coefficient: `t_j = beta_hat_j / se(beta_hat_j)` (null hypothesis: the coefficient equals zero). Requirements: - Return an `np.ndarray` of shape `(p+1,)` ordered `[intercept, b1, ..., bp]`. - Values are signed (no absolute values) and are compared element-wise with `numpy.testing.assert_allclose`. ```hint The denominator The work is in `se(beta_hat_j)`. Work out which matrix holds the coefficient variances and what the residuals contribute to it. ``` ### Constraints & Assumptions - The grader passes a fixed float `X` of shape `(24, 2)` and `y` of shape `(24,)`, so the result has shape `(3,)`: `[intercept, b1, b2]`. - `numpy` and `statsmodels` can be used. ### What a Strong Answer Covers - Putting the intercept column first, so the output has shape `(p+1,)` in the stated order. - How the standard errors are built, including the degrees of freedom for the error variance. - Signed ratios, with no absolute values and no rounding. - Edge cases such as a perfect fit, a constant column already in `X`, and collinear columns. ### Follow-up Questions - How would you compute `se(beta_hat_j)` without statsmodels? - How would you turn these t-statistics into two-sided p-values? - How would you test a null hypothesis other than zero, such as $\beta_1 = 1$? - Two predictors are highly correlated. What happens to their t-statistics, and what test would you run instead?

Overview: Implement a Python function that fits ordinary least squares with an intercept and returns the signed t-statistic for every coefficient. It tests computing coefficient standard errors from the residual variance and the design matrix, and reading the results as tests of whether each coefficient is zero.

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

Implement def coefficient_t_statistics(X: np.ndarray, y: np.ndarray) -> np.ndarray. Fit OLS of y on X with an intercept and return the t-statistic for each coefficient: t_j = beta_hat_j / se(beta_hat_j) (null hypothesis: the coefficient equals zero).

Requirements:

  • Return an np.ndarray of shape (p+1,) ordered [intercept, b1, ..., bp] .
  • Values are signed (no absolute values) and are compared element-wise with numpy.testing.assert_allclose .

Constraints & Assumptions

  • The grader passes a fixed float X of shape (24, 2) and y of shape (24,) , so the result has shape (3,) : [intercept, b1, b2] .
  • numpy and statsmodels can be used.

What a Strong Answer Covers Guidance

  • Putting the intercept column first, so the output has shape (p+1,) in the stated order.
  • How the standard errors are built, including the degrees of freedom for the error variance.
  • Signed ratios, with no absolute values and no rounding.
  • Edge cases such as a perfect fit, a constant column already in X , and collinear columns.

Follow-up Questions Guidance

  • How would you compute se(beta_hat_j) without statsmodels?
  • How would you turn these t-statistics into two-sided p-values?
  • How would you test a null hypothesis other than zero, such as β1=1\beta_1 = 1 ?
  • Two predictors are highly correlated. What happens to their t-statistics, and what test would you run instead?