Statsmodels中OLS回归后get_prediction函数的预测区间公式问询
get_prediction(): Formula Breakdown Great question! Let's clarify exactly what formulas power the get_prediction() function for OLS models in statsmodels, especially when it comes to prediction intervals.
Core Background
First, it's important to distinguish between two key intervals this function calculates:
- Confidence intervals: For the conditional mean of the response variable ($E[y|X_{\text{new}}]$)
- Prediction intervals: For an individual new observation ($y_{\text{new}}$)
Both rely on standard errors (not raw RMSE, though they’re closely related) and use the t-distribution for critical values—this is more appropriate for finite sample sizes than the normal distribution.
Exact Formulas
1. Confidence Interval for the Conditional Mean
The interval is calculated as:
$$\hat{y}{\text{new}} \pm t{\alpha/2, n-k} \times \text{SE}(\hat{y}_{\text{new}})$$
Where:
- $\hat{y}{\text{new}}$ is the predicted mean for the new input $X{\text{new}}$
- $t_{\alpha/2, n-k}$ is the critical t-value for your chosen significance level $\alpha$, with $n-k$ degrees of freedom ($n$ = sample size, $k$ = number of model parameters including the intercept)
- $\text{SE}(\hat{y}{\text{new}})$ is the standard error of the predicted mean:
$$\text{SE}(\hat{y}{\text{new}}) = \hat{\sigma} \times \sqrt{X_{\text{new}} (X^T X)^{-1} X_{\text{new}}^T}$$
$\hat{\sigma}$ here is the degrees-of-freedom adjusted residual standard deviation (equal to $\sqrt{\text{MSE}}$, where MSE = mean squared error of the residuals, calculated as $\text{SSE}/(n-k)$).
2. Prediction Interval for a New Observation
This interval accounts for both the uncertainty in the predicted mean and the random error of a new individual observation. The formula is:
$$\hat{y}{\text{new}} \pm t{\alpha/2, n-k} \times \text{SE}(\text{prediction})$$
Where the prediction standard error is:
$$\text{SE}(\text{prediction}) = \hat{\sigma} \times \sqrt{1 + X_{\text{new}} (X^T X)^{-1} X_{\text{new}}^T}$$
Notice the extra +1 under the square root—this captures the variance of the unobserved error term $\epsilon_{\text{new}}$ that affects every new data point.
Key Note on RMSE
You asked about RMSE: if your definition of RMSE is $\sqrt{\text{SSE}/n}$ (unadjusted for degrees of freedom), statsmodels does not use this directly. Instead, it uses the adjusted residual standard deviation ($\sqrt{\text{MSE}}$), which is a more unbiased estimator of the true population error standard deviation $\sigma$.
Quick Recap
get_prediction() doesn’t use any non-standard or custom formulas—it implements the classic OLS interval calculations taught in introductory regression courses. The core components are the adjusted residual standard deviation, prediction standard errors, and t-distribution critical values.
内容的提问来源于stack exchange,提问作者jerbear

