OLS Prediction Summary Frame with statsmodels get_prediction

Quick Overview

Write a Python function that fits a simple linear regression with statsmodels and returns the prediction summary frame for new x values, with fitted means, standard errors, confidence intervals and prediction intervals. It tests design matrix construction and reading regression prediction output.

OLS Prediction Summary Frame with statsmodels get_prediction

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `prediction_summary_frame(x, y, x_new)` that fits OLS of `y` on `x` with `statsmodels` and returns `results.get_prediction(exog_new).summary_frame(alpha=0.05)`, where `exog_new` is `x_new` with a leading constant column. Requirements: - Add an intercept with `sm.add_constant` before fitting. - Use `alpha=0.05` exactly, with one row per element of `x_new`. - The columns must be exactly, in order: `mean`, `mean_se`, `mean_ci_lower`, `mean_ci_upper`, `obs_ci_lower`, `obs_ci_upper`. This is the default output, so no renaming is needed. - Use a default `RangeIndex`. For example, with `x = [1, 2, 3, 4, 5]`, `y = [2, 4, 5, 4, 5]` and `x_new = [3, 4, 6]`, the fitted line is $\hat{y} = 2.2 + 0.6x$ and the returned frame is (rounded to three decimals here; the function returns full precision): | | mean | mean_se | mean_ci_lower | mean_ci_upper | obs_ci_lower | obs_ci_upper | |---|---|---|---|---|---|---| | 0 | 4.000 | 0.400 | 2.727 | 5.273 | 0.882 | 7.118 | | 1 | 4.600 | 0.490 | 3.041 | 6.159 | 1.355 | 7.845 | | 2 | 5.800 | 0.938 | 2.815 | 8.785 | 1.675 | 9.925 | ```hint The constant for new points Check what `sm.add_constant` does when `x_new` has a single value, or the same value repeated. The new design matrix needs the constant in the same column position as the training one. ``` ```hint What the comparison checks A frame comparison looks at the index, the column names and order, and the dtypes, along with the values. ``` ### Constraints & Assumptions - The tests use a fixed `x` of shape `(12,)`, a fixed `y` and an `x_new` of shape `(3,)`. - The result is compared with `pandas.testing.assert_frame_equal`. ### What a Strong Answer Covers - Fitting OLS on a constant-augmented `x` and building `exog_new` with the constant in the first column for every row, including the single-point case. - Returning the frame from `summary_frame(alpha=0.05)` with the six columns in order and a default `RangeIndex`. - What each column means and how `mean_se` connects the two interval pairs. - How `alpha` maps to the confidence level. ### Follow-up Questions - What is the difference between the `mean_ci` and `obs_ci` columns? - How would you change the confidence level to 90%? - Why does `mean_se` change from row to row when the model has one residual variance?

Overview: Write a Python function that fits a simple linear regression with statsmodels and returns the prediction summary frame for new x values, with fitted means, standard errors, confidence intervals and prediction intervals. It tests design matrix construction and reading regression prediction output.

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

Implement prediction_summary_frame(x, y, x_new) that fits OLS of y on x with statsmodels and returns results.get_prediction(exog_new).summary_frame(alpha=0.05), where exog_new is x_new with a leading constant column.

Requirements:

  • Add an intercept with sm.add_constant before fitting.
  • Use alpha=0.05 exactly, with one row per element of x_new .
  • The columns must be exactly, in order: mean , mean_se , mean_ci_lower , mean_ci_upper , obs_ci_lower , obs_ci_upper . This is the default output, so no renaming is needed.
  • Use a default RangeIndex .

For example, with x = [1, 2, 3, 4, 5], y = [2, 4, 5, 4, 5] and x_new = [3, 4, 6], the fitted line is y^=2.2+0.6x\hat{y} = 2.2 + 0.6x and the returned frame is (rounded to three decimals here; the function returns full precision):

meanmean_semean_ci_lowermean_ci_upperobs_ci_lowerobs_ci_upper
04.0000.4002.7275.2730.8827.118
14.6000.4903.0416.1591.3557.845
25.8000.9382.8158.7851.6759.925

Constraints & Assumptions

  • The tests use a fixed x of shape (12,) , a fixed y and an x_new of shape (3,) .
  • The result is compared with pandas.testing.assert_frame_equal .

What a Strong Answer Covers Guidance

  • Fitting OLS on a constant-augmented x and building exog_new with the constant in the first column for every row, including the single-point case.
  • Returning the frame from summary_frame(alpha=0.05) with the six columns in order and a default RangeIndex .
  • What each column means and how mean_se connects the two interval pairs.
  • How alpha maps to the confidence level.

Follow-up Questions Guidance

  • What is the difference between the mean_ci and obs_ci columns?
  • How would you change the confidence level to 90%?
  • Why does mean_se change from row to row when the model has one residual variance?