You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Statsmodels中OLS回归后get_prediction函数的预测区间公式问询

Statsmodels OLS 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:18:47