Compute standard errors and t-values for OLS regression through the origin

Quick Overview

Fit an ordinary least squares regression with no intercept and return the coefficient standard errors and t-values in Python. It tests residual degrees of freedom for a through-the-origin model, the coefficient covariance formula, and matching statsmodels output.

Compute standard errors and t-values for OLS regression through the origin

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `no_intercept_inference(X, y, alpha=0.05)`. Fit OLS of `y` on `X` through the origin and return `(standard_errors, t_values)`, each a 1-D `np.ndarray` of shape `(p,)` aligned to the columns of `X`. ```python def no_intercept_inference(X: np.ndarray, y: np.ndarray, alpha: float = 0.05) -> tuple[np.ndarray, np.ndarray]: ... ``` Requirements: - No intercept: do not add a constant column (no `add_constant`). - `sigma_hat_sq = RSS / (n - p)`, not `n - p - 1`. - The t-statistics use `n - p` degrees of freedom. - The results must match `.bse` and `.tvalues` of `sm.OLS(y, X).fit()`. - `alpha` is in the signature but does not affect the returned values. ```hint Work it out by hand The coefficient covariance needs only the residual variance and $(X^\top X)^{-1}$, computed on `X` exactly as given. ``` ```hint Check the design Think about what your function should do when `X` has two collinear columns or too few rows. ``` ### Constraints & Assumptions - The grader uses a fixed `X` of shape `(20, 3)` and a fixed `y` of shape `(20,)`, comparing with `numpy.testing.assert_allclose`. ### What a Strong Answer Covers - Fits on `X` exactly as given, with no constant column, so the fitted line passes through the origin. - Uses `n - p` residual degrees of freedom and explains why the count differs from a model with an intercept. - Computes the standard errors from the diagonal of $\hat{\sigma}^2 (X^\top X)^{-1}$ and the t-values as coefficient divided by standard error, in `X` column order. - Checks the output against statsmodels with a tolerance and says what `alpha` would be used for. ### Follow-up Questions - Why does the no-intercept model use `n - p` degrees of freedom instead of `n - p - 1`? - How would you compute `.bse` manually without statsmodels? - Why is R² unreliable for a through-the-origin model?

Overview: Fit an ordinary least squares regression with no intercept and return the coefficient standard errors and t-values in Python. It tests residual degrees of freedom for a through-the-origin model, the coefficient covariance formula, and matching statsmodels output.

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

Implement no_intercept_inference(X, y, alpha=0.05). Fit OLS of y on X through the origin and return (standard_errors, t_values), each a 1-D np.ndarray of shape (p,) aligned to the columns of X.

def no_intercept_inference(X: np.ndarray, y: np.ndarray,
                           alpha: float = 0.05) -> tuple[np.ndarray, np.ndarray]:
    ...

Requirements:

  • No intercept: do not add a constant column (no add_constant ).
  • sigma_hat_sq = RSS / (n - p) , not n - p - 1 .
  • The t-statistics use n - p degrees of freedom.
  • The results must match .bse and .tvalues of sm.OLS(y, X).fit() .
  • alpha is in the signature but does not affect the returned values.

Constraints & Assumptions

  • The grader uses a fixed X of shape (20, 3) and a fixed y of shape (20,) , comparing with numpy.testing.assert_allclose .

What a Strong Answer Covers Guidance

  • Fits on X exactly as given, with no constant column, so the fitted line passes through the origin.
  • Uses n - p residual degrees of freedom and explains why the count differs from a model with an intercept.
  • Computes the standard errors from the diagonal of σ^2(X⊤X)−1\hat{\sigma}^2 (X^\top X)^{-1} and the t-values as coefficient divided by standard error, in X column order.
  • Checks the output against statsmodels with a tolerance and says what alpha would be used for.

Follow-up Questions Guidance

  • Why does the no-intercept model use n - p degrees of freedom instead of n - p - 1 ?
  • How would you compute .bse manually without statsmodels?
  • Why is R² unreliable for a through-the-origin model?