Test an OLS slope against a non-zero null value with a t-test in Python

Quick Overview

Implement a two-sided t-test of one OLS slope against a non-zero hypothesized value using numpy and scipy. It tests building the design matrix with an intercept, computing coefficient standard errors and residual degrees of freedom, and matching statsmodels output when the null is zero.

Test an OLS slope against a non-zero null value with a t-test in Python

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `t_test_against_value(X, y, index, hypothesized)`. Fit OLS of `y` on `X` with an intercept and test the null that the slope on 0-based `X` column `index` equals `hypothesized` (generally non-zero). Return `(t_statistic, p_value)` as Python floats. ```python def t_test_against_value(X: np.ndarray, y: np.ndarray, index: int, hypothesized: float) -> tuple[float, float]: ... ``` Requirements: - The intercept is at design position 0, so the tested coefficient is at position `index + 1`. - $t = (\hat{\beta} - \beta_0) / \operatorname{se}(\hat{\beta})$, where $\beta_0$ is `hypothesized` and the standard error comes from the residual variance times the diagonal of $(X^\top X)^{-1}$. - The p-value is two-sided: `2 * scipy.stats.t.sf(abs(t), n - p - 1)`, where `p` is the number of non-intercept predictors. - With `hypothesized=0.0`, the result must match statsmodels' `res.tvalues` and `res.pvalues` for that coefficient under `numpy.testing.assert_allclose`. - You may use `numpy` and `scipy.stats`. statsmodels is only the grader's reference. For example, with `X` of shape `(27, 3)` and a fixed `y`: ```python t, p = t_test_against_value(X, y, index=1, hypothesized=2.0) # tests the slope on X[:, 1] (true coefficient 2) against 2.0 t0, p0 = t_test_against_value(X, y, index=0, hypothesized=0.0) # must equal res.tvalues[1], res.pvalues[1] from statsmodels ``` ```hint Bookkeeping After you prepend the column of ones, work out which coefficient position belongs to `X` column `index`, and how many fitted parameters the residual degrees of freedom must subtract. ``` ```hint What the null changes Go through each piece of the t-statistic and ask whether it depends on the hypothesized value at all. ``` ### What a Strong Answer Covers - Builds the design matrix with an intercept column and maps `X` column `index` to coefficient position `index + 1`. - Estimates the residual variance with `n - p - 1` degrees of freedom and takes standard errors from the diagonal of the coefficient covariance matrix. - Centers the t-statistic on the hypothesized value and computes the two-sided p-value from the t distribution with the same degrees of freedom. - Checks the zero-null case against statsmodels and handles failure cases such as an out-of-range index, too few rows or a rank-deficient design. ### Follow-up Questions - How would you extend this to a one-sided test? - How does testing against a non-zero null differ from the default regression output? - How would you test whether two slopes are equal, for example $\beta_1 = \beta_2$?

Overview: Implement a two-sided t-test of one OLS slope against a non-zero hypothesized value using numpy and scipy. It tests building the design matrix with an intercept, computing coefficient standard errors and residual degrees of freedom, and matching statsmodels output when the null is zero.

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

Implement t_test_against_value(X, y, index, hypothesized). Fit OLS of y on X with an intercept and test the null that the slope on 0-based X column index equals hypothesized (generally non-zero). Return (t_statistic, p_value) as Python floats.

def t_test_against_value(X: np.ndarray, y: np.ndarray, index: int,
                         hypothesized: float) -> tuple[float, float]:
    ...

Requirements:

  • The intercept is at design position 0, so the tested coefficient is at position index + 1 .
  • t=(β^−β0)/se⁡(β^)t = (\hat{\beta} - \beta_0) / \operatorname{se}(\hat{\beta}) , where β0\beta_0 is hypothesized and the standard error comes from the residual variance times the diagonal of (X⊤X)−1(X^\top X)^{-1} .
  • The p-value is two-sided: 2 * scipy.stats.t.sf(abs(t), n - p - 1) , where p is the number of non-intercept predictors.
  • With hypothesized=0.0 , the result must match statsmodels' res.tvalues and res.pvalues for that coefficient under numpy.testing.assert_allclose .
  • You may use numpy and scipy.stats . statsmodels is only the grader's reference.

For example, with X of shape (27, 3) and a fixed y:

t, p = t_test_against_value(X, y, index=1, hypothesized=2.0)
# tests the slope on X[:, 1] (true coefficient 2) against 2.0

t0, p0 = t_test_against_value(X, y, index=0, hypothesized=0.0)
# must equal res.tvalues[1], res.pvalues[1] from statsmodels

What a Strong Answer Covers Guidance

  • Builds the design matrix with an intercept column and maps X column index to coefficient position index + 1 .
  • Estimates the residual variance with n - p - 1 degrees of freedom and takes standard errors from the diagonal of the coefficient covariance matrix.
  • Centers the t-statistic on the hypothesized value and computes the two-sided p-value from the t distribution with the same degrees of freedom.
  • Checks the zero-null case against statsmodels and handles failure cases such as an out-of-range index, too few rows or a rank-deficient design.

Follow-up Questions Guidance

  • How would you extend this to a one-sided test?
  • How does testing against a non-zero null differ from the default regression output?
  • How would you test whether two slopes are equal, for example β1=β2\beta_1 = \beta_2 ?