Sub topic 16
Forecasting with the regression and prediction intervals
This chapter assumes you can already fit a simple linear regression by ordinary least squares (OLS). It also assumes you know the standard error of estimate (SEE) and can read a t-critical value with n − 2 degrees of freedom. Here we use the fitted line to forecast a value of the dependent variable, then attach an interval that states how uncertain the forecast is.
From fitted line to forecast
Once you have estimated the intercept b0 and the slope b1, a forecast for a chosen value X_f of the independent variable is:
Yhat = b0 + b1 · Xf
This point forecast is the model's best single guess. It says nothing about how far the real outcome might land from that guess. Two sources of error make the real outcome differ.
- Error in the estimated line. b0 and b1 come from a sample, so the true line may sit slightly higher, lower, or steeper.
- Error in the individual observation. Even on the true line, a single outcome scatters around it by the regression error term, whose spread SEE estimates.
A prediction interval covers both sources. A confidence interval for the average outcome at X_f covers only the first.
The forecast standard error
The estimated standard deviation of the forecast error is:
sf² = SEE² · [1 + 1/n + (Xf − Xbar)² / ((n − 1) · sx²)]
Here sx² is the sample variance of X, so (n − 1) · sx² is the sum of squared deviations of X from its mean. The prediction interval is:
Yhat ± tcrit · s_f
The critical value t_crit uses n − 2 degrees of freedom and the significance level you choose. Each part of the bracket has a meaning:
| Term | Meaning | Behavior |
|---|---|---|
| 1 | Scatter of a single new observation around the true line | Never shrinks, however large n is |
| 1/n | Uncertainty in the intercept from a finite sample | Shrinks as n grows |
| (X_f − X_bar)² / ((n − 1) · s_x²) | Uncertainty in the slope, magnified by distance from the mean of X | Grows as X_f moves away from X_bar |
The third term is why the interval is narrowest at X_bar and widens in both directions. Forecasting far outside the range of the data is the least reliable use of a regression.
Worked example
Use the data from the earlier chapters: X = 1, 2, 3, 4, 5 and Y = 2.1, 3.9, 6.2, 7.8, 10.1. The fit gives these values:
- b0 = 0.05 and b1 = 1.99.
- X_bar = 3, and the sum of squared deviations of X is 10.
- SSE = 0.107, so SEE = sqrt(0.107 / 3) ≈ 0.1889.
- The 95% critical value with 3 degrees of freedom is 3.182.
Forecast Y at X_f = 6.
- Point forecast: 0.05 + 1.99 · 6 = 11.99.
- Distance term: (6 − 3)² / 10 = 0.9.
- Bracket: 1 + 0.2 + 0.9 = 2.1.
- sf² = 0.035667 · 2.1 ≈ 0.0749, so sf ≈ 0.2737.
- Half-width: 3.182 · 0.2737 ≈ 0.871.
The 95% prediction interval is roughly 11.12 to 12.86. If you forecast at X_f = 3 (the mean), the bracket is 1.2, and the interval narrows to about 5.36 to 6.68 around a point forecast of 6.02.
Computing it in code
The script below reproduces the hand calculation and also shows the narrower confidence interval for the mean response, which drops the leading 1 from the bracket.
import numpy as np
from scipy import stats
x = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
y = np.array([2.1, 3.9, 6.2, 7.8, 10.1])
n = len(x)
res = stats.linregress(x, y)
b0, b1 = res.intercept, res.slope
y_hat = b0 + b1 * x
see = np.sqrt(((y - y_hat) ** 2).sum() / (n - 2))
sxx = ((x - x.mean()) ** 2).sum() # equals (n - 1) * var(x)
t_crit = stats.t.ppf(1 - 0.05 / 2, df=n - 2)
def forecast(x_f):
point = b0 + b1 * x_f
dist = (x_f - x.mean()) ** 2 / sxx
s_pred = see * np.sqrt(1 + 1 / n + dist) # single new observation
s_mean = see * np.sqrt(1 / n + dist) # average response
return (
point,
(point - t_crit * s_pred, point + t_crit * s_pred),
(point - t_crit * s_mean, point + t_crit * s_mean),
)
for x_f in (3.0, 6.0, 10.0):
point, pred, conf = forecast(x_f)
print(f"X_f={x_f}: forecast={point:.2f}, "
f"prediction interval=({pred[0]:.2f}, {pred[1]:.2f}), "
f"mean-response interval=({conf[0]:.2f}, {conf[1]:.2f})")At X_f = 10 the interval is about 1.48 wide on each side of the forecast, compared with about 0.66 at the mean. That growth comes entirely from the distance term.
Prediction interval versus confidence interval
Two similar-looking intervals answer different questions.
- Confidence interval for the mean response: where does the average Y for all cases with X = X_f lie? It uses the bracket 1/n + distance term.
- Prediction interval: where will one new observation with X = X_f fall? It adds the 1 and is always wider.
Exam questions usually ask for a prediction interval for a single outcome, so check the wording. "The return next year" is one observation. "The expected return for all such years" is the mean response.
Limits of regression forecasts
A valid interval depends on the model being right. Several conditions weaken it.
- Extrapolation. The relationship fitted over the sample range may not hold outside it. The formula widens the interval, but it still assumes the line stays straight, which it may not.
- Parameter instability. If the relationship changed over time, forecasts from old data mislead.
- Violated assumptions. Heteroskedasticity (error variance that changes with X) means SEE is not one constant, so the interval is wrong in width. Autocorrelation (errors correlated over time) makes the standard errors unreliable.
- Predicted independent variable. If you must forecast X_f itself, its error passes into the forecast of Y, and the interval above understates the true uncertainty.
- Confusing fit with forecast quality. A high R-squared does not make forecasts precise. SEE sets the width, and it can be large even when R-squared is high.
Exam checklist
- Compute Yhat = b0 + b1 · Xf.
- Find the sum of squared deviations of X, or use (n − 1) · s_x².
- Build the bracket 1 + 1/n + distance term, multiply by SEE², and take the square root.
- Use the t-value with n − 2 degrees of freedom.
- Report Yhat ± tcrit · s_f, and say whether the interval is for one observation or for the mean.
Exercise
A regression fitted on n = 20 observations has SEE = 2.0, Xbar = 10, and a sum of squared deviations of X equal to 80. The fitted line is Yhat = 1 + 0.5 X. Build a 95% prediction interval for Y at Xf = 14. Use tcrit = 2.101 (18 degrees of freedom).
Show answer
Yhat = 1 + 0.5 · 14 = 8. Distance term = (14 − 10)² / 80 = 0.2. Bracket = 1 + 1/20 + 0.2 = 1.25. sf = 2.0 · sqrt(1.25) ≈ 2.236. Half-width = 2.101 · 2.236 ≈ 4.70. The interval is 8 ± 4.70, or about 3.30 to 12.70.
Exercise
Why is a prediction interval for one new observation always wider than the confidence interval for the mean response at the same X_f, and where does the prediction interval become narrowest?
Show answer
The prediction interval adds the term 1 inside the bracket, which reflects the scatter of a single observation around the true line. The mean-response interval leaves it out. Both intervals are narrowest at Xf = Xbar, where the distance term is zero.