Confidence vs Prediction Interval Widths at New x Values in OLS

Quick Overview

Write a Python function that fits simple linear regression with statsmodels and returns the widths of the 95% confidence and prediction intervals at a set of new x values. It tests building matching design matrices, reading prediction summaries, and how leverage shapes interval width.

Confidence vs Prediction Interval Widths at New x Values in OLS

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `def interval_widths_at(x: np.ndarray, y: np.ndarray, x_new: np.ndarray) -> tuple`. Fit simple OLS of `y` on `x` and return `(mean_ci_widths, obs_ci_widths)`: two 1-D arrays of shape `(len(x_new),)` giving the width (upper minus lower) of the 95% mean-response confidence interval and of the 95% prediction interval at each `x_new` point. Requirements: - Fit OLS with an intercept (add a constant to the design matrix). - Call `results.get_prediction(exog_new).summary_frame(alpha=0.05)` with the same constant added to `x_new`. - Mean widths are `mean_ci_upper - mean_ci_lower`. Observation widths are `obs_ci_upper - obs_ci_lower`. - Use `alpha=0.05` exactly and return both results as `np.ndarray`. For example: ```python rng = np.random.default_rng(0) x = np.arange(12.0) # 0..11, mean 5.5 y = 2.0 * x + 1.0 + rng.normal(scale=1.0, size=12) # any noisy y works x_new = np.array([x.mean(), x.mean() + 3, 20.0]) mean_w, obs_w = interval_widths_at(x, y, x_new) # obs_w > mean_w at every point # mean_w is smallest at x.mean() and grows with |x_new - x.mean()| ``` The noise matters. With `y = 2.0 * x + 1.0` exactly, the fit is perfect, the residual variance is zero and both widths collapse to about zero. ```hint Matching the design Build the evaluation matrix exactly like the training matrix. Check what `sm.add_constant` does when `x_new` holds a single value or the same value repeated. ``` ```hint Where the width comes from Write the variance of the fitted mean at a point as a function of that point's distance from the mean of `x`. ``` ### Constraints & Assumptions - `numpy` and `statsmodels.api` are available. - `x` and `y` are 1-D arrays of the same length. `x_new` is a 1-D array of any length, and its values may lie outside the range of `x`. - Both intervals are two-sided at the 95% level (`alpha=0.05`). ### What a Strong Answer Covers - Building the training and evaluation design matrices with the constant in the same column position, including forcing the constant for `x_new`. - Reading the interval columns from `summary_frame(alpha=0.05)` and returning the two width arrays as NumPy arrays of shape `(len(x_new),)`. - The leverage formula that makes the mean width smallest at the mean of `x`, and the extra error variance that keeps the prediction width larger at every point. - What the intervals do and do not account for when `x_new` lies outside the training range. ### Follow-up Questions - Why does the mean-response interval widen away from `xbar`? - Why is extrapolation risky even when the intervals look finite? - How would the widths change if every training point were duplicated, doubling `n` over the same range?

Overview: Write a Python function that fits simple linear regression with statsmodels and returns the widths of the 95% confidence and prediction intervals at a set of new x values. It tests building matching design matrices, reading prediction summaries, and how leverage shapes interval width.

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

Implement def interval_widths_at(x: np.ndarray, y: np.ndarray, x_new: np.ndarray) -> tuple. Fit simple OLS of y on x and return (mean_ci_widths, obs_ci_widths): two 1-D arrays of shape (len(x_new),) giving the width (upper minus lower) of the 95% mean-response confidence interval and of the 95% prediction interval at each x_new point.

Requirements:

  • Fit OLS with an intercept (add a constant to the design matrix).
  • Call results.get_prediction(exog_new).summary_frame(alpha=0.05) with the same constant added to x_new .
  • Mean widths are mean_ci_upper - mean_ci_lower . Observation widths are obs_ci_upper - obs_ci_lower .
  • Use alpha=0.05 exactly and return both results as np.ndarray .

For example:

rng = np.random.default_rng(0)
x = np.arange(12.0)                                   # 0..11, mean 5.5
y = 2.0 * x + 1.0 + rng.normal(scale=1.0, size=12)    # any noisy y works
x_new = np.array([x.mean(), x.mean() + 3, 20.0])
mean_w, obs_w = interval_widths_at(x, y, x_new)
# obs_w > mean_w at every point
# mean_w is smallest at x.mean() and grows with |x_new - x.mean()|

The noise matters. With y = 2.0 * x + 1.0 exactly, the fit is perfect, the residual variance is zero and both widths collapse to about zero.

Constraints & Assumptions

  • numpy and statsmodels.api are available.
  • x and y are 1-D arrays of the same length. x_new is a 1-D array of any length, and its values may lie outside the range of x .
  • Both intervals are two-sided at the 95% level ( alpha=0.05 ).

What a Strong Answer Covers Guidance

  • Building the training and evaluation design matrices with the constant in the same column position, including forcing the constant for x_new .
  • Reading the interval columns from summary_frame(alpha=0.05) and returning the two width arrays as NumPy arrays of shape (len(x_new),) .
  • The leverage formula that makes the mean width smallest at the mean of x , and the extra error variance that keeps the prediction width larger at every point.
  • What the intervals do and do not account for when x_new lies outside the training range.

Follow-up Questions Guidance

  • Why does the mean-response interval widen away from xbar ?
  • Why is extrapolation risky even when the intervals look finite?
  • How would the widths change if every training point were duplicated, doubling n over the same range?