Mean Response Confidence Interval at x0 with statsmodels OLS

Quick Overview

Implement a Python function that fits a simple linear regression with statsmodels and returns the 95% confidence interval for the mean response at a new x value as a tuple of two floats. It tests regression inference basics and the difference between mean and prediction intervals.

Mean Response Confidence Interval at x0 with statsmodels OLS

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `mean_confidence_interval(x, y, x0)` that fits OLS of `y` on the single predictor `x` and returns the 95% confidence interval for the mean response at `x0`. Requirements: - Add an intercept with `sm.add_constant`. - Predict with `results.get_prediction(np.array([[1.0, x0]]))`. - Use the mean interval (`obs=False`, the default) with `alpha=0.05` exactly. Reading `mean_ci_lower` and `mean_ci_upper` from `summary_frame(alpha=0.05)` is equivalent. - Return a plain tuple `(lower, upper)` of two floats. A DataFrame or a NumPy array does not count. For example: ```python x = np.array([1.0, 2.0, 3.0, 4.0, 5.0]) y = np.array([2.0, 4.0, 5.0, 4.0, 5.0]) mean_confidence_interval(x, y, 3.0) # about (2.727, 5.273) ``` The fitted line here is $\hat{y} = 2.2 + 0.6x$, so the interval is centered on 4.0 at `x0 = 3`. ```hint Shape of the result The prediction object gives one row per new point, with a lower and an upper column. Pull the two scalars out of that row before you build the tuple. ``` ### Constraints & Assumptions - `x` and `y` are 1-D arrays of the same length. The tests use a fixed `x` of shape `(10,)`, a fixed `y` and a scalar `x0` inside the training range. ### What a Strong Answer Covers - Fitting OLS with an intercept and passing a new-point row `[1.0, x0]` whose columns match the design matrix. - Choosing the mean-response interval at `alpha=0.05` and knowing how it differs from the prediction interval. - Returning the two bounds as plain floats in a tuple. - The standard error behind the interval and how it depends on the distance of `x0` from the mean of `x`. ### Follow-up Questions - How does the mean interval differ from a prediction interval? - Why does the interval widen as `x0` moves away from the mean of `x`? - How would you compute the same interval without `get_prediction`?

Overview: Implement a Python function that fits a simple linear regression with statsmodels and returns the 95% confidence interval for the mean response at a new x value as a tuple of two floats. It tests regression inference basics and the difference between mean and prediction intervals.

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

Implement mean_confidence_interval(x, y, x0) that fits OLS of y on the single predictor x and returns the 95% confidence interval for the mean response at x0.

Requirements:

  • Add an intercept with sm.add_constant .
  • Predict with results.get_prediction(np.array([[1.0, x0]])) .
  • Use the mean interval ( obs=False , the default) with alpha=0.05 exactly. Reading mean_ci_lower and mean_ci_upper from summary_frame(alpha=0.05) is equivalent.
  • Return a plain tuple (lower, upper) of two floats. A DataFrame or a NumPy array does not count.

For example:

x = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
y = np.array([2.0, 4.0, 5.0, 4.0, 5.0])
mean_confidence_interval(x, y, 3.0)   # about (2.727, 5.273)

The fitted line here is y^=2.2+0.6x\hat{y} = 2.2 + 0.6x, so the interval is centered on 4.0 at x0 = 3.

Constraints & Assumptions

  • x and y are 1-D arrays of the same length. The tests use a fixed x of shape (10,) , a fixed y and a scalar x0 inside the training range.

What a Strong Answer Covers Guidance

  • Fitting OLS with an intercept and passing a new-point row [1.0, x0] whose columns match the design matrix.
  • Choosing the mean-response interval at alpha=0.05 and knowing how it differs from the prediction interval.
  • Returning the two bounds as plain floats in a tuple.
  • The standard error behind the interval and how it depends on the distance of x0 from the mean of x .

Follow-up Questions Guidance

  • How does the mean interval differ from a prediction interval?
  • Why does the interval widen as x0 moves away from the mean of x ?
  • How would you compute the same interval without get_prediction ?