Inference on a Linear Combination of OLS Coefficients

Quick Overview

Implement ordinary least squares with an intercept in NumPy and run a two-sided t-test on a linear combination of coefficients, such as the difference between two slopes. It tests whether you can carry the full coefficient covariance into a standard error, p-value and confidence interval.

Inference on a Linear Combination of OLS Coefficients

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Fit ordinary least squares (OLS) of `y` on `X` with an intercept, then run inference on the scalar contrast `theta = c @ beta`, where `c` is a 1-D array of length `p+1` aligned to `[intercept, b1, ..., bp]`. Implement `linear_combination_inference(X, y, c, alpha=0.05)` returning `(estimate, std_error, t_stat, p_value, half_width)` as five Python floats. Use these definitions exactly. Values are compared with `numpy.testing.assert_allclose`. - `A` is `X` with a column of ones prepended, and `beta_hat = inv(A.T @ A) @ A.T @ y`. - `sigma_hat_sq = RSS / (n - p - 1)` and `Cov(beta_hat) = sigma_hat_sq * inv(A.T @ A)`. - `estimate = c @ beta_hat` and `std_error = sqrt(c @ Cov(beta_hat) @ c)`, using the full covariance matrix including the off-diagonal terms. - `t_stat = estimate / std_error` for the null `theta = 0`, `p_value = 2 * scipy.stats.t.sf(abs(t_stat), n - p - 1)`, and `half_width = t.ppf(1 - alpha/2, n - p - 1) * std_error`. Example call: ```python # X: shape (26, 3), y: shape (26,) c = np.array([0.0, 1.0, -1.0, 0.0]) # tests whether b1 == b2 est, se, t, p, hw = linear_combination_inference(X, y, c, alpha=0.05) # CI for (b1 - b2) is est +/- hw ``` ```hint Line up the columns `c` has length `p+1` because its first entry belongs to the intercept. Build the design matrix so column 0 is the ones column and the remaining columns follow `X` in order. ``` ```hint Watch the counts `p` is the number of columns of `X`, not of the design matrix. Check which one feeds the degrees of freedom, and convert NumPy scalars to Python floats before returning. ``` ### Constraints & Assumptions - `X` has shape `(n, p)`, `y` has shape `(n,)`, and `c` has length `p+1`. - `A.T @ A` is invertible and `n > p + 1`, so no rank-deficiency handling is required. - The null hypothesis is `theta = 0` and the test is two-sided. - Results are checked with `numpy.testing.assert_allclose`, so the formulas above must be matched exactly. ### What a Strong Answer Covers - Building the design matrix with the intercept in column 0 so it lines up with `c`, and fitting the coefficients by OLS. - Estimating the residual variance with `n - p - 1` degrees of freedom and forming the full coefficient covariance matrix. - Computing the standard error of the contrast from the full covariance, and explaining why combining per-coefficient standard errors is wrong. - Getting the two-sided p-value and the confidence-interval half-width from the t distribution with the correct degrees of freedom. - Numerical choices (`lstsq` or a linear solve versus an explicit inverse) and edge cases such as an all-zero `c`. ### Follow-up Questions - Why use the full covariance matrix instead of just the per-coefficient standard errors? - How would you generalize this to test several linear combinations jointly? - What changes if the errors are heteroscedastic?

Overview: Implement ordinary least squares with an intercept in NumPy and run a two-sided t-test on a linear combination of coefficients, such as the difference between two slopes. It tests whether you can carry the full coefficient covariance into a standard error, p-value and confidence interval.

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

Fit ordinary least squares (OLS) of y on X with an intercept, then run inference on the scalar contrast theta = c @ beta, where c is a 1-D array of length p+1 aligned to [intercept, b1, ..., bp]. Implement linear_combination_inference(X, y, c, alpha=0.05) returning (estimate, std_error, t_stat, p_value, half_width) as five Python floats.

Use these definitions exactly. Values are compared with numpy.testing.assert_allclose.

  • A is X with a column of ones prepended, and beta_hat = inv(A.T @ A) @ A.T @ y .
  • sigma_hat_sq = RSS / (n - p - 1) and Cov(beta_hat) = sigma_hat_sq * inv(A.T @ A) .
  • estimate = c @ beta_hat and std_error = sqrt(c @ Cov(beta_hat) @ c) , using the full covariance matrix including the off-diagonal terms.
  • t_stat = estimate / std_error for the null theta = 0 , p_value = 2 * scipy.stats.t.sf(abs(t_stat), n - p - 1) , and half_width = t.ppf(1 - alpha/2, n - p - 1) * std_error .

Example call:

# X: shape (26, 3), y: shape (26,)
c = np.array([0.0, 1.0, -1.0, 0.0])  # tests whether b1 == b2
est, se, t, p, hw = linear_combination_inference(X, y, c, alpha=0.05)
# CI for (b1 - b2) is est +/- hw

Constraints & Assumptions

  • X has shape (n, p) , y has shape (n,) , and c has length p+1 .
  • A.T @ A is invertible and n > p + 1 , so no rank-deficiency handling is required.
  • The null hypothesis is theta = 0 and the test is two-sided.
  • Results are checked with numpy.testing.assert_allclose , so the formulas above must be matched exactly.

What a Strong Answer Covers Guidance

  • Building the design matrix with the intercept in column 0 so it lines up with c , and fitting the coefficients by OLS.
  • Estimating the residual variance with n - p - 1 degrees of freedom and forming the full coefficient covariance matrix.
  • Computing the standard error of the contrast from the full covariance, and explaining why combining per-coefficient standard errors is wrong.
  • Getting the two-sided p-value and the confidence-interval half-width from the t distribution with the correct degrees of freedom.
  • Numerical choices ( lstsq or a linear solve versus an explicit inverse) and edge cases such as an all-zero c .

Follow-up Questions Guidance

  • Why use the full covariance matrix instead of just the per-coefficient standard errors?
  • How would you generalize this to test several linear combinations jointly?
  • What changes if the errors are heteroscedastic?