Build a 95% Prediction Interval for Simple OLS From Scratch
Quick Overview
Write a Python function that fits simple linear regression by hand and returns the two-sided 95% prediction interval for a new observation at a given x. It tests the OLS closed form, residual variance with n - 2 degrees of freedom, the t critical value, and the difference between prediction and confidence intervals.
Build a 95% Prediction Interval for Simple OLS From Scratch
Role: Data Scientist
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `def manual_prediction_interval(x: np.ndarray, y: np.ndarray, x0: float) -> tuple`. Fit simple OLS of `y` on `x` and return the two-sided 95% prediction interval `(lower, upper)` for a new observation at `x0`, computed from first principles (no `statsmodels`).
Use the textbook formula:
```text
yhat0 +/- t_crit * sqrt(s2 * (1 + 1/n + (x0 - xbar)**2 / Sxx))
s2 = RSS / (n - 2) # residual variance
Sxx = sum((x - xbar)**2)
t_crit = scipy.stats.t.ppf(1 - 0.05/2, df=n-2) # two-sided, alpha=0.05
```
The function must meet these requirements:
- `yhat0` is the OLS fitted value at `x0`.
- Use the `(1 + 1/n + ...)` term (prediction interval), not the `1/n`-only confidence interval for the mean.
- Compute `t_crit` with `scipy.stats.t.ppf` and `df = n - 2`.
- Return plain floats, lower first, matching statsmodels' `get_prediction([[1.0, x0]]).conf_int(obs=True, alpha=0.05)` within tolerance.
```hint Sources of uncertainty
List what makes a single new observation at `x0` uncertain, and check that each source has its own term under the square root.
```
```hint Degrees of freedom
Count how many parameters the line estimates. That count sets both the divisor of the residual variance and the degrees of freedom of the t distribution.
```
### Constraints & Assumptions
- `x` and `y` are 1-D arrays of shape `(10,)`. Assume `n >= 3`.
- NumPy and SciPy are available.
### What a Strong Answer Covers
- Closed-form OLS slope and intercept computed from centered sums.
- Residual variance with `n - 2` degrees of freedom, and a t critical value that uses the same degrees of freedom.
- A standard error that includes the `1` term for the new observation's noise and the `(x0 - xbar)**2 / Sxx` leverage term.
- Output as two plain floats in `(lower, upper)` order that agree with statsmodels within tolerance.
- Inputs that break the formula, such as all `x` values being equal.
### Follow-up Questions
- How does the prediction interval differ from the confidence interval for the mean response?
- Why does the interval widen as `x0` moves away from `xbar`?
- How would you compute the same interval for a regression with several predictors?
- What happens to the coverage of this interval when the errors are heteroscedastic or heavy-tailed?
Overview: Write a Python function that fits simple linear regression by hand and returns the two-sided 95% prediction interval for a new observation at a given x. It tests the OLS closed form, residual variance with n - 2 degrees of freedom, the t critical value, and the difference between prediction and confidence intervals.
Build a 95% Prediction Interval for Simple OLS From Scratch
General
Nov 24, 2026
mediumData ScientistOtherMachine Learning
0
0
Implement def manual_prediction_interval(x: np.ndarray, y: np.ndarray, x0: float) -> tuple. Fit simple OLS of y on x and return the two-sided 95% prediction interval (lower, upper) for a new observation at x0, computed from first principles (no statsmodels).