Compute the standard errors of OLS regression coefficients from scratch in NumPy so they match the statsmodels bse values. It tests building a design matrix with an intercept, estimating the error variance with the correct degrees of freedom, and reading standard errors off the coefficient covariance matrix.
Implement `standard_errors_from_scratch(X, y)` returning OLS coefficient standard errors computed from the normal-equations covariance matrix. Return a 1-D `np.ndarray` of shape `(p+1,)` ordered `[intercept, b1, ..., bp]`, where `n = X.shape[0]` and `p = X.shape[1]`.
The function must meet these requirements:
- Prepend a ones column to `X` to form `A`, then solve OLS for `beta_hat`.
- Estimate `sigma_hat_sq = RSS / (n - p - 1)`.
- Compute `Var(beta_hat) = sigma_hat_sq * inv(A.T @ A)` and return `np.sqrt(np.diag(Var))`.
- The output must match `sm.OLS(y, sm.add_constant(X)).fit().bse` under `numpy.testing.assert_allclose`.
```hint Watch the shapes
Check the shapes before the matrix algebra: `A` should have p+1 columns, and `y` should be 1-D so the residual sum of squares comes out as a scalar.
```
### Constraints & Assumptions
- The grader passes a fixed, well-conditioned `X` of shape `(22, 3)` and a fixed `y`, so no ill-conditioning handling is needed.
- Use `numpy` only. `statsmodels` is not allowed in the implementation.
### What a Strong Answer Covers
- Building the design matrix with the intercept column first.
- Solving for the coefficients and computing the residual sum of squares.
- The error variance estimator and its degrees of freedom.
- Turning the coefficient covariance matrix into standard errors in the required shape and order.
### Follow-up Questions
- Why divide `RSS` by `n - p - 1` instead of `n`?
- How would you make this numerically stable for ill-conditioned `X`?
- Which assumptions about the errors must hold for these standard errors to be valid?
Overview: Compute the standard errors of OLS regression coefficients from scratch in NumPy so they match the statsmodels bse values. It tests building a design matrix with an intercept, estimating the error variance with the correct degrees of freedom, and reading standard errors off the coefficient covariance matrix.
Implement standard_errors_from_scratch(X, y) returning OLS coefficient standard errors computed from the normal-equations covariance matrix. Return a 1-D np.ndarray of shape (p+1,) ordered [intercept, b1, ..., bp], where n = X.shape[0] and p = X.shape[1].
The function must meet these requirements:
Prepend a ones column to
X
to form
A
, then solve OLS for
beta_hat
.
Estimate
sigma_hat_sq = RSS / (n - p - 1)
.
Compute
Var(beta_hat) = sigma_hat_sq * inv(A.T @ A)
and return
np.sqrt(np.diag(Var))
.
The output must match
sm.OLS(y, sm.add_constant(X)).fit().bse
under
numpy.testing.assert_allclose
.
Constraints & Assumptions
The grader passes a fixed, well-conditioned
X
of shape
(22, 3)
and a fixed
y
, so no ill-conditioning handling is needed.
Use
numpy
only.
statsmodels
is not allowed in the implementation.
What a Strong Answer Covers Guidance
Building the design matrix with the intercept column first.
Solving for the coefficients and computing the residual sum of squares.
The error variance estimator and its degrees of freedom.
Turning the coefficient covariance matrix into standard errors in the required shape and order.
Follow-up Questions Guidance
Why divide
RSS
by
n - p - 1
instead of
n
?
How would you make this numerically stable for ill-conditioned
X
?
Which assumptions about the errors must hold for these standard errors to be valid?