Time Series • Introduction to Autoregressive Models and Forecasting
Time Series / P4.04 Predict New Observations

P4.04 Predict New Observations

Practical 4 Introduction to Autoregressive Models and Forecasting

Use the fitted trend to predict sales for the next three months. Distinguish a point prediction from a prediction interval, and keep forecasts in chronological order.

Practical / Solution

P4.04 Predict New Observations

Problem Statement

Use the fitted trend to predict sales for the next three months. Distinguish a point prediction from a prediction interval, and keep forecasts in chronological order.

Learning Outcomes

  • Form new t values beyond the sample.
  • Produce point predictions.
  • Read a prediction interval as a range, not a guarantee.

Theory

Prediction plugs a new x into the fitted equation. A confidence interval for the mean response is narrower than a prediction interval for a new observation, because a new point also has error variance. Extrapolating far beyond the observed t range is risky if the trend changes.

Dataset / Data Source

Same 24-month constructed sales series. Forecast months 25–27.

Analysis / Program

import numpy as np import pandas as pd import matplotlib.pyplot as plt import statsmodels.api as sm idx = pd.date_range("2024-01-01", periods=24, freq="MS") t = np.arange(24) sales = 80 + 1.5 * t + np.array([ 2, -1, 3, 0, -2, 4, 1, -3, 2, 0, -1, 3, 1, -2, 2, 0, 1, -1, 3, -2, 0, 2, -1, 1 ]) y = pd.Series(sales, index=idx) X = sm.add_constant(t) fit = sm.OLS(y.values, X).fit() # New time points for three months ahead t_new = np.arange(24, 27) X_new = sm.add_constant(t_new, has_constant="add") pred = fit.get_prediction(X_new).summary_frame(alpha=0.05) pred.index = pd.date_range("2026-01-01", periods=3, freq="MS") print(pred[["mean", "obs_ci_lower", "obs_ci_upper"]]) # Plot history and predicted means ax = y.plot(label="history") pred["mean"].plot(ax=ax, style="o--", label="forecast") plt.legend() plt.tight_layout() plt.show()

Expected Output

A three-row table with predicted mean and lower/upper observation intervals, plus a plot continuing the trend line. Interval widths depend on residual variance; do not invent a single memorised number.

Result / Interpretation

The next three months follow the same slope. The interval is a plausible range for a new observation under the linear model, not a promise. If seasonality exists, a pure time trend will miss the seasonal peak or trough.

Note

Prediction extends the fitted equation forward. A prediction interval is wider than an interval for the mean line.