Compute t-statistics and two-sided p-values for OLS regression coefficients from scratch with NumPy and SciPy so they match statsmodels. It tests standard errors from the coefficient covariance matrix, the residual degrees of freedom, and an accurate two-tailed probability from the t distribution.
Implement `t_stats_and_pvalues(X, y)` that fits OLS of `y` on `X` with an intercept and returns `(t_values, p_values)`, each a 1-D array of shape `(p+1,)` ordered `[intercept, b1, ..., bp]`. Use only `numpy` and `scipy.stats` (no `statsmodels`).
The function must meet these requirements:
- Prepend a ones column to form `A`, then solve for `beta_hat`.
- Estimate `sigma_hat_sq = RSS / (n - p - 1)` and take standard errors from the diagonal of `sigma_hat_sq * inv(A.T @ A)`.
- Compute `t_j = beta_hat_j / se(beta_hat_j)` and return it signed.
- Compute p-values as `2 * scipy.stats.t.sf(abs(t_j), df)` with `df = n - p - 1`.
- The output must match statsmodels `res.tvalues` and `res.pvalues` under `numpy.testing.assert_allclose`.
```hint Which degrees of freedom
The t distribution behind the p-values needs the same residual degrees of freedom as the variance estimate. Count the intercept.
```
```hint Tails and signs
Keep the sign on the t-statistic you return. For the p-value, think about what a tail probability does when t is negative, and how precise it stays when the probability is tiny.
```
### Constraints & Assumptions
- The grader uses a fixed `X` of shape `(26, 3)` and `y` of shape `(26,)`.
- Assume `A.T @ A` is invertible.
### What a Strong Answer Covers
- The coefficient fit and the residual variance, with the intercept counted in the degrees of freedom.
- How each standard error is obtained and how the t-statistic is formed, sign included.
- The two-sided p-value calculation and its accuracy in the far tail.
- Output shapes and coefficient order that line up with statsmodels.
### Follow-up Questions
- Why use `t.sf(abs(t))` instead of `2 * (1 - t.cdf(t))`?
- How would you numerically stabilize the `inv(A.T @ A)` step?
- How would you test whether two slopes are both zero at the same time, instead of one at a time?
Overview: Compute t-statistics and two-sided p-values for OLS regression coefficients from scratch with NumPy and SciPy so they match statsmodels. It tests standard errors from the coefficient covariance matrix, the residual degrees of freedom, and an accurate two-tailed probability from the t distribution.
Implement t_stats_and_pvalues(X, y) that fits OLS of y on X with an intercept and returns (t_values, p_values), each a 1-D array of shape (p+1,) ordered [intercept, b1, ..., bp]. Use only numpy and scipy.stats (no statsmodels).
The function must meet these requirements:
Prepend a ones column to form
A
, then solve for
beta_hat
.
Estimate
sigma_hat_sq = RSS / (n - p - 1)
and take standard errors from the diagonal of
sigma_hat_sq * inv(A.T @ A)
.
Compute
t_j = beta_hat_j / se(beta_hat_j)
and return it signed.
Compute p-values as
2 * scipy.stats.t.sf(abs(t_j), df)
with
df = n - p - 1
.
The output must match statsmodels
res.tvalues
and
res.pvalues
under
numpy.testing.assert_allclose
.
Constraints & Assumptions
The grader uses a fixed
X
of shape
(26, 3)
and
y
of shape
(26,)
.
Assume
A.T @ A
is invertible.
What a Strong Answer Covers Guidance
The coefficient fit and the residual variance, with the intercept counted in the degrees of freedom.
How each standard error is obtained and how the t-statistic is formed, sign included.
The two-sided p-value calculation and its accuracy in the far tail.
Output shapes and coefficient order that line up with statsmodels.
Follow-up Questions Guidance
Why use
t.sf(abs(t))
instead of
2 * (1 - t.cdf(t))
?
How would you numerically stabilize the
inv(A.T @ A)
step?
How would you test whether two slopes are both zero at the same time, instead of one at a time?