OLS Confidence Intervals From Scratch

Quick Overview

Implement two-sided confidence intervals for OLS regression coefficients from scratch with NumPy and SciPy so they match statsmodels output. It tests building the design matrix with an intercept, estimating the residual variance with the right degrees of freedom, and turning standard errors and a t critical value into intervals.

OLS Confidence Intervals From Scratch

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `confidence_intervals_from_scratch(X, y, alpha)`. Fit OLS of `y` on `X` with an intercept and return the two-sided intervals `beta_hat_j -/+ t_crit * se_j` as `(lower, upper)`, where `t_crit = scipy.stats.t.ppf(1 - alpha/2, n - p - 1)`. Do not call `.conf_int()`. The function must meet these requirements: - Coefficient order is `[intercept, b1, ..., bp]`. - Return a tuple of two 1-D `np.ndarray`, each of shape `(p+1,)`. A single `(p+1, 2)` matrix does not pass. - Compute `se_j` from `sigma^2 * (XᵀX)⁻¹` with `sigma^2 = RSS / (n - p - 1)`, where `X` here is the design matrix including the intercept column. - The output must match columns 0 and 1 of `res.conf_int(alpha=alpha)` under `numpy.testing.assert_allclose`. ```hint Where the intercept goes The intercept has to be a column of the matrix you invert, and its position decides the coefficient order. Check that the matrix you invert is (p+1) by (p+1). ``` ```hint Degrees of freedom Count every estimated coefficient, the intercept included, when you set the residual degrees of freedom. The variance estimate and the t quantile must use the same number. ``` ### Constraints & Assumptions - The grader uses a fixed `X` of shape `(28, 2)` and calls the function with `alpha=0.05` and `alpha=0.10`. - `numpy` and `scipy` are available. `statsmodels` may be used to fit a reference model, never for `.conf_int()`. - `X` holds the features only. The function adds the intercept column itself. ### What a Strong Answer Covers - How the design matrix is built and why the intercept sits at index 0. - The residual variance estimate and its degrees of freedom. - How the standard errors come out of the coefficient covariance matrix, and which quantile of which distribution sets the interval width. - Returning the bounds in the exact shape and order the grader compares. - Numerical concerns with inverting `XᵀX`, and input-shape edge cases. ### Follow-up Questions - How would the intervals change if you used the normal quantile instead of the `t` distribution? - How would you extend this to return prediction intervals for a new `x0`? - If the noise variance differs across observations, how would you change the standard errors?

Overview: Implement two-sided confidence intervals for OLS regression coefficients from scratch with NumPy and SciPy so they match statsmodels output. It tests building the design matrix with an intercept, estimating the residual variance with the right degrees of freedom, and turning standard errors and a t critical value into intervals.

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

Implement confidence_intervals_from_scratch(X, y, alpha). Fit OLS of y on X with an intercept and return the two-sided intervals beta_hat_j -/+ t_crit * se_j as (lower, upper), where t_crit = scipy.stats.t.ppf(1 - alpha/2, n - p - 1). Do not call .conf_int().

The function must meet these requirements:

  • Coefficient order is [intercept, b1, ..., bp] .
  • Return a tuple of two 1-D np.ndarray , each of shape (p+1,) . A single (p+1, 2) matrix does not pass.
  • Compute se_j from sigma^2 * (XᵀX)⁻¹ with sigma^2 = RSS / (n - p - 1) , where X here is the design matrix including the intercept column.
  • The output must match columns 0 and 1 of res.conf_int(alpha=alpha) under numpy.testing.assert_allclose .

Constraints & Assumptions

  • The grader uses a fixed X of shape (28, 2) and calls the function with alpha=0.05 and alpha=0.10 .
  • numpy and scipy are available. statsmodels may be used to fit a reference model, never for .conf_int() .
  • X holds the features only. The function adds the intercept column itself.

What a Strong Answer Covers Guidance

  • How the design matrix is built and why the intercept sits at index 0.
  • The residual variance estimate and its degrees of freedom.
  • How the standard errors come out of the coefficient covariance matrix, and which quantile of which distribution sets the interval width.
  • Returning the bounds in the exact shape and order the grader compares.
  • Numerical concerns with inverting XᵀX , and input-shape edge cases.

Follow-up Questions Guidance

  • How would the intervals change if you used the normal quantile instead of the t distribution?
  • How would you extend this to return prediction intervals for a new x0 ?
  • If the noise variance differs across observations, how would you change the standard errors?