Implement a Python function that fits ordinary least squares with an intercept and returns a two-sided confidence interval for every coefficient. It tests building the design matrix, estimating coefficient standard errors, and choosing the right Student's t critical value and degrees of freedom.
Implement `coefficient_confidence_intervals(X, y, alpha=0.05)` that fits OLS of `y` on `X` with an intercept and returns the two-sided `100*(1-alpha)%` confidence interval for every coefficient. Return an `np.ndarray` of shape `(p+1, 2)` where row `j` is `[lower_j, upper_j]` (row 0 is the intercept, rows `1..p` match the columns of `X`).
The interval for each coefficient is `beta_hat_j +/- t_crit * se(beta_hat_j)`, where `t_crit` is the `1 - alpha/2` quantile of Student's t with `n - p - 1` degrees of freedom.
```hint Where the standard errors come from
Each coefficient's variance sits on the diagonal of one matrix built from the design matrix. Work out which matrix that is and which estimate has to scale it.
```
### Constraints & Assumptions
- Results are compared with `numpy.testing.assert_allclose` against standard OLS formulas.
- The grader uses a fixed `X` of shape `(30, 2)`, a fixed `y`, and both `alpha=0.05` and `alpha=0.01`.
- `statsmodels` and `numpy` are available.
### What a Strong Answer Covers
- Adding an intercept column so row 0 is the intercept and the result has shape `(p+1, 2)` with the lower bound in column 0.
- Standard errors from the diagonal of the coefficient covariance matrix, scaled by the residual variance with `n - p - 1` degrees of freedom.
- The t critical value at `1 - alpha/2` with `n - p - 1` degrees of freedom, and why a normal quantile would fail the tolerance check.
- Edge cases: a constant column already in `X`, collinear columns, too few rows, and a 1-D `X`.
### Follow-up Questions
- How would you compute the intervals without `statsmodels`?
- Why does the critical value come from Student's t, and when would a normal quantile give nearly the same intervals?
- If the errors were heteroskedastic, which part of the interval would be wrong, and what would you compute instead?
- What happens to the intervals when two columns of `X` are nearly collinear?
Overview: Implement a Python function that fits ordinary least squares with an intercept and returns a two-sided confidence interval for every coefficient. It tests building the design matrix, estimating coefficient standard errors, and choosing the right Student's t critical value and degrees of freedom.
Implement coefficient_confidence_intervals(X, y, alpha=0.05) that fits OLS of y on X with an intercept and returns the two-sided 100*(1-alpha)% confidence interval for every coefficient. Return an np.ndarray of shape (p+1, 2) where row j is [lower_j, upper_j] (row 0 is the intercept, rows 1..p match the columns of X).
The interval for each coefficient is beta_hat_j +/- t_crit * se(beta_hat_j), where t_crit is the 1 - alpha/2 quantile of Student's t with n - p - 1 degrees of freedom.
Constraints & Assumptions
Results are compared with
numpy.testing.assert_allclose
against standard OLS formulas.
The grader uses a fixed
X
of shape
(30, 2)
, a fixed
y
, and both
alpha=0.05
and
alpha=0.01
.
statsmodels
and
numpy
are available.
What a Strong Answer Covers Guidance
Adding an intercept column so row 0 is the intercept and the result has shape
(p+1, 2)
with the lower bound in column 0.
Standard errors from the diagonal of the coefficient covariance matrix, scaled by the residual variance with
n - p - 1
degrees of freedom.
The t critical value at
1 - alpha/2
with
n - p - 1
degrees of freedom, and why a normal quantile would fail the tolerance check.
Edge cases: a constant column already in
X
, collinear columns, too few rows, and a 1-D
X
.
Follow-up Questions Guidance
How would you compute the intervals without
statsmodels
?
Why does the critical value come from Student's t, and when would a normal quantile give nearly the same intervals?
If the errors were heteroskedastic, which part of the interval would be wrong, and what would you compute instead?
What happens to the intervals when two columns of
X
are nearly collinear?