8  Forecasting Theory and Best Linear Prediction

Part II. Stationarity and Linear Time-Domain Models
Chapter goal: develop forecasting as a mathematical projection problem. We connect conditional expectation, best linear prediction, orthogonality of forecast errors, Toeplitz covariance systems, AR and ARMA forecast recursions, forecast uncertainty, and the practical workflow used before ARIMA, state-space models, and deep sequence models.

8.1 Learning objectives

After studying this chapter, you should be able to:

  1. define forecasting as prediction of \(X_{n+m}\) from the observed history \(X_1,\ldots,X_n\);
  2. explain why conditional expectation is the minimum mean-square error predictor;
  3. derive the orthogonality equations for best linear prediction;
  4. write and solve the covariance linear system \(\Gamma_n\phi_n=\gamma_n\);
  5. compute the one-step prediction error variance \(\gamma(0)-\gamma_n^T\Gamma_n^{-1}\gamma_n\);
  6. forecast AR(1), AR(\(p\)), MA(\(q\)), and causal ARMA processes;
  7. distinguish point forecasts, forecast errors, and forecast intervals;
  8. understand why recursive forecasting algorithms such as Durbin–Levinson, innovations algorithms, and Kalman filters are important;
  9. implement basic forecasting methods in Python without data leakage;
  10. use AI tools to check derivations and code while preserving the mathematical assumptions of time-series forecasting.

8.2 6.1 Forecasting as conditional prediction

Suppose we observe a time series

\[ x_1,x_2,\ldots,x_n. \]

The forecasting problem is to predict a future value

\[ X_{n+m}, \qquad m\ge 1, \]

using the information available at time \(n\). The integer \(m\) is called the forecast horizon. The case \(m=1\) is one-step-ahead forecasting. The case \(m>1\) is multi-step forecasting.

We write the information available at time \(n\) as

\[ \mathcal F_n=\sigma(X_1,\ldots,X_n), \]

or, more concretely for this chapter, as the vector

\[ \mathbf X_n=(X_n,X_{n-1},\ldots,X_1)^T. \]

A forecast is a function of the observed past:

\[ \widehat X_{n+m|n}=g(X_1,\ldots,X_n). \]

The subscript \(n+m|n\) means: forecast time \(n+m\) using information through time \(n\).

NoteMain idea

Forecasting is not guessing the future. Forecasting is choosing a function of the observed past that minimizes a loss. In classical time-series theory, the default loss is mean squared error.

8.3 6.2 The minimum mean-square error predictor

Let \(Y\) be a random variable we want to predict from information \(\mathcal F\). We measure prediction quality by mean squared error:

\[ E\left[(Y-g)^2\right], \]

where \(g\) must be \(\mathcal F\)-measurable. The best predictor among all measurable functions of the information is the conditional expectation.

ImportantTheorem: conditional expectation is the MMSE predictor

Among all predictors \(g\) based on \(\mathcal F\), the unique predictor minimizing

\[ E\left[(Y-g)^2\right] \]

is

\[ g^*=E(Y\mid \mathcal F). \]

8.3.1 Proof idea

For any \(\mathcal F\)-measurable \(g\), write

\[ Y-g = \left(Y-E(Y\mid\mathcal F)\right)+\left(E(Y\mid\mathcal F)-g\right). \]

Squaring and taking expectation gives

\[ E[(Y-g)^2] =E\left[(Y-E(Y\mid\mathcal F))^2\right] +E\left[(E(Y\mid\mathcal F)-g)^2\right] +2E\left[(Y-E(Y\mid\mathcal F))(E(Y\mid\mathcal F)-g)\right]. \]

The cross term is zero because \(E(Y-E(Y\mid\mathcal F)\mid\mathcal F)=0\). Therefore,

\[ E[(Y-g)^2] =E\left[(Y-E(Y\mid\mathcal F))^2\right] +E\left[(E(Y\mid\mathcal F)-g)^2\right]. \]

The second term is nonnegative and is minimized when

\[ g=E(Y\mid\mathcal F). \]

For forecasting,

\[ \widehat X_{n+m|n}=E(X_{n+m}\mid X_1,\ldots,X_n) \]

is the optimal mean-square forecast if the conditional expectation is known.

TipGaussian special case

If the time series is Gaussian, conditional expectations are linear functions of the observed variables. Therefore, for Gaussian time series, the best linear predictor is also the best predictor among all measurable predictors.

8.4 6.3 Why best linear prediction matters

The conditional expectation may be difficult to compute. It requires knowledge of the full joint distribution of

\[ (X_1,\ldots,X_n,X_{n+m}). \]

Classical time-series analysis often restricts attention to linear predictors because they only require first and second moments. For a stationary process, those moments are described by the mean and autocovariance function.

A general linear predictor has the form

\[ \widehat X_{n+m|n}=a_0+a_1X_n+a_2X_{n-1}+\cdots+a_nX_1. \]

If the process is stationary with mean \(\mu\), it is convenient to center the variables:

\[ Y_t=X_t-\mu. \]

Then

\[ \widehat X_{n+m|n}=\mu+\sum_{j=1}^n a_j(X_{n+1-j}-\mu). \]

Thus, for most derivations, we assume without loss of generality that \(E(X_t)=0\).

8.5 6.4 Orthogonality principle

For zero-mean data, consider a one-step-ahead linear predictor

\[ \widehat X_{n+1|n}=\phi_{n1}X_n+\phi_{n2}X_{n-1}+\cdots+\phi_{nn}X_1. \]

Define the prediction error

\[ e_{n+1}=X_{n+1}-\widehat X_{n+1|n}. \]

The best linear predictor minimizes

\[ E(e_{n+1}^2). \]

Differentiate the mean squared error with respect to each coefficient \(\phi_{nk}\). The normal equations are

\[ E\left[e_{n+1}X_{n+1-k}\right]=0, \qquad k=1,\ldots,n. \]

Equivalently,

\[ E\left[\left(X_{n+1}-\sum_{j=1}^n\phi_{nj}X_{n+1-j}\right)X_{n+1-k}\right]=0. \]

NoteOrthogonality principle

The forecast error of the best linear predictor is orthogonal to every variable used in the prediction.

This is the same geometric idea as least squares projection: residuals are orthogonal to the fitted subspace.

Using stationarity, the normal equations become

\[ \gamma(k)-\sum_{j=1}^n\phi_{nj}\gamma(k-j)=0, \qquad k=1,\ldots,n. \]

Here

\[ \gamma(h)=\operatorname{Cov}(X_{t+h},X_t) \]

is the autocovariance function.

8.6 6.5 Matrix form of the prediction equations

Define the Toeplitz covariance matrix

\[ \Gamma_n= \begin{bmatrix} \gamma(0) & \gamma(1) & \cdots & \gamma(n-1)\\ \gamma(1) & \gamma(0) & \cdots & \gamma(n-2)\\ \vdots & \vdots & \ddots & \vdots\\ \gamma(n-1) & \gamma(n-2) & \cdots & \gamma(0) \end{bmatrix}, \]

and the covariance vector

\[ \gamma_n= \begin{bmatrix} \gamma(1)\\ \gamma(2)\\ \vdots\\ \gamma(n) \end{bmatrix}. \]

Let

\[ \phi_n= \begin{bmatrix} \phi_{n1}\\ \phi_{n2}\\ \vdots\\ \phi_{nn} \end{bmatrix}. \]

Then the prediction equations are

\[ \Gamma_n\phi_n=\gamma_n. \]

If \(\Gamma_n\) is invertible, then

\[ \phi_n=\Gamma_n^{-1}\gamma_n. \]

The one-step-ahead forecast is

\[ \widehat X_{n+1|n}=\phi_n^T\mathbf X_n, \]

where

\[ \mathbf X_n=(X_n,X_{n-1},\ldots,X_1)^T. \]

8.6.1 Prediction error variance

The mean squared prediction error is

\[ \begin{aligned} E[(X_{n+1}-\widehat X_{n+1|n})^2] &=E[(X_{n+1}-\phi_n^T\mathbf X_n)^2]\\ &=\gamma(0)-2\gamma_n^T\phi_n+\phi_n^T\Gamma_n\phi_n. \end{aligned} \]

Since \(\Gamma_n\phi_n=\gamma_n\),

\[ E[(X_{n+1}-\widehat X_{n+1|n})^2] =\gamma(0)-\gamma_n^T\phi_n. \]

Therefore,

\[ \boxed{\operatorname{MSPE}_n=\gamma(0)-\gamma_n^T\Gamma_n^{-1}\gamma_n.} \]

ImportantInterpretation

The term \(\gamma_n^T\Gamma_n^{-1}\gamma_n\) is the reduction in uncertainty obtained by using the observed past. If the future has little covariance with the past, this term is small. If the future is strongly predictable from the past, this term is large.

8.7 6.6 Interactive: AR(1) forecast cone

The AR(1) model is

\[ X_{t+1}=\phi X_t+W_{t+1}, \qquad W_t\sim WN(0,\sigma_w^2). \]

For \(|\phi|<1\), the \(m\)-step forecast is

\[ \widehat X_{t+m|t}=\phi^mX_t. \]

The \(m\)-step forecast error variance is

\[ \operatorname{Var}(X_{t+m}-\widehat X_{t+m|t}) =\sigma_w^2\sum_{j=0}^{m-1}\phi^{2j}. \]

Use the interactive below to see how \(\phi\), the current value \(X_t\), and the horizon affect the forecast path and uncertainty.

Interactive: AR(1) forecast path and uncertainty

Move the sliders. The line is the conditional mean forecast and the shaded region is an approximate 95% forecast interval.

TipWhat to observe

When \(|\phi|\) is close to \(1\), the conditional mean decays slowly and the forecast interval expands slowly toward its long-run variance. When \(\phi\) is near \(0\), the conditional mean quickly returns to \(0\) and future uncertainty is dominated by new shocks.

8.8 6.7 Example: one-step forecast for AR(1)

Assume

\[ X_{t+1}=\phi X_t+W_{t+1}, \qquad E(W_t)=0, \qquad \operatorname{Var}(W_t)=\sigma_w^2, \]

with \(|\phi|<1\). The stationary variance is

\[ \gamma(0)=\frac{\sigma_w^2}{1-\phi^2}. \]

The lag-one covariance is

\[ \gamma(1)=\phi\gamma(0). \]

For \(n=1\), the best linear predictor of \(X_2\) from \(X_1\) has coefficient

\[ \phi_{11}=\frac{\gamma(1)}{\gamma(0)}=\phi. \]

Therefore,

\[ \widehat X_{2|1}=\phi X_1. \]

The prediction error is

\[ X_2-\widehat X_{2|1}=W_2, \]

so

\[ \operatorname{MSPE}=\sigma_w^2. \]

This matches the intuition: for a correctly specified AR(1), the only unknown part of tomorrow is tomorrow’s innovation.

8.9 6.8 Multi-step forecasting for AR(1)

For \(m\ge 1\),

\[ X_{t+m}=\phi^mX_t+\phi^{m-1}W_{t+1}+\phi^{m-2}W_{t+2}+\cdots+W_{t+m}. \]

Taking conditional expectation given \(X_t\) gives

\[ \widehat X_{t+m|t}=\phi^mX_t. \]

The forecast error is

\[ X_{t+m}-\widehat X_{t+m|t} =\phi^{m-1}W_{t+1}+\phi^{m-2}W_{t+2}+\cdots+W_{t+m}. \]

Thus

\[ \operatorname{Var}(X_{t+m}-\widehat X_{t+m|t}) =\sigma_w^2\left(1+\phi^2+\cdots+\phi^{2(m-1)}\right). \]

If \(|\phi|<1\),

\[ \operatorname{Var}(X_{t+m}-\widehat X_{t+m|t}) =\sigma_w^2\frac{1-\phi^{2m}}{1-\phi^2}. \]

As \(m\to\infty\),

\[ \widehat X_{t+m|t}\to 0, \]

and

\[ \operatorname{Var}(X_{t+m}-\widehat X_{t+m|t})\to \frac{\sigma_w^2}{1-\phi^2}=\gamma(0). \]

Long-horizon forecasts lose memory of the present and approach the unconditional distribution.

8.10 6.9 Forecast intervals

A point forecast is incomplete without uncertainty. If the model is Gaussian, then

\[ X_{t+m}\mid X_t \sim N\left(\widehat X_{t+m|t},\,\operatorname{Var}(X_{t+m}-\widehat X_{t+m|t})\right). \]

An approximate 95% forecast interval is

\[ \widehat X_{t+m|t} \pm 1.96\sqrt{\operatorname{Var}(X_{t+m}-\widehat X_{t+m|t})}. \]

WarningForecast interval is not a confidence interval for the mean

A forecast interval describes uncertainty about a future observation. A confidence interval describes uncertainty about an estimated parameter or mean. Forecast intervals are usually wider because they include both parameter/model uncertainty and future random shocks.

8.11 6.10 Python example: AR(1) forecast and forecast intervals

Code
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

rng = np.random.default_rng(7339)

phi = 0.75
sigma_w = 1.0
n = 120
x = np.zeros(n)

# Start close to stationarity
x[0] = rng.normal(0, sigma_w / np.sqrt(1 - phi**2))
for t in range(1, n):
    x[t] = phi * x[t-1] + rng.normal(0, sigma_w)

x_t = x[-1]
H = 25
horizons = np.arange(1, H + 1)
forecast_mean = phi**horizons * x_t
forecast_var = sigma_w**2 * np.array([sum(phi**(2*j) for j in range(m)) for m in horizons])
forecast_se = np.sqrt(forecast_var)

future_index = np.arange(n, n + H)

plt.figure(figsize=(9, 4.8))
plt.plot(np.arange(n), x, label="observed")
plt.plot(future_index, forecast_mean, marker="o", label="forecast mean")
plt.fill_between(
    future_index,
    forecast_mean - 1.96 * forecast_se,
    forecast_mean + 1.96 * forecast_se,
    alpha=0.25,
    label="approx. 95% forecast interval"
)
plt.axvline(n - 1, linestyle="--", linewidth=1)
plt.xlabel("time")
plt.ylabel("value")
plt.title("AR(1) multi-step forecast")
plt.legend()
plt.show()

8.12 6.11 Best linear prediction from an autocovariance function

The matrix formula

\[ \Gamma_n\phi_n=\gamma_n \]

is useful because it only needs the autocovariance function. For example, suppose a process has autocovariance

\[ \gamma(h)=\frac{\sigma_w^2}{1-\phi^2}\phi^{|h|}, \]

which is the autocovariance of a stationary AR(1). If we solve the best linear prediction equations using many lags, we should discover that only the first lag is needed.

Code
import numpy as np
import pandas as pd

def ar1_autocov(phi, sigma_w, h):
    return sigma_w**2 / (1 - phi**2) * phi**abs(h)

def blp_weights_from_autocov(gamma_func, n_lags):
    Gamma = np.array([
        [gamma_func(i - j) for j in range(n_lags)]
        for i in range(n_lags)
    ], dtype=float)
    gamma_vec = np.array([gamma_func(k + 1) for k in range(n_lags)], dtype=float)
    weights = np.linalg.solve(Gamma, gamma_vec)
    mspe = gamma_func(0) - gamma_vec @ weights
    return weights, mspe

phi_true = 0.65
sigma_w = 1.0
weights, mspe = blp_weights_from_autocov(
    lambda h: ar1_autocov(phi_true, sigma_w, h),
    n_lags=6
)

pd.DataFrame({
    "lag": np.arange(1, 7),
    "BLP weight": weights
})
lag BLP weight
0 1 6.500000e-01
1 2 3.790162e-16
2 3 -2.513009e-16
3 4 1.287715e-16
4 5 -7.848340e-17
5 6 3.852137e-17
Code
print("MSPE from covariance equations:", round(mspe, 6))
print("Innovation variance sigma_w^2:", sigma_w**2)
MSPE from covariance equations: 1.0
Innovation variance sigma_w^2: 1.0

The first coefficient is approximately \(\phi\), and the remaining coefficients are approximately zero. This illustrates the Markov property of the AR(1) model: after knowing \(X_t\), older observations do not improve the one-step forecast.

8.13 6.12 Interactive: best linear prediction weights

For an AR(1) autocovariance, solve the best linear prediction system using several past lags. The plot shows the coefficient vector \(\phi_n\).

Interactive: BLP weights from the autocovariance function

For an AR(1) process, the best one-step linear predictor only needs the most recent value. The covariance equations reveal this automatically.

8.14 6.13 Forecasting AR(\(p\)) models

Consider the causal AR(\(p\)) model

\[ X_t=\phi_1X_{t-1}+\phi_2X_{t-2}+\cdots+\phi_pX_{t-p}+W_t. \]

For one-step-ahead forecasting, if \(t\ge p\), then

\[ \widehat X_{t+1|t} =\phi_1X_t+\phi_2X_{t-1}+\cdots+\phi_pX_{t-p+1}. \]

The one-step error is \(W_{t+1}\), so if the parameters are known,

\[ \operatorname{Var}(X_{t+1}-\widehat X_{t+1|t})=\sigma_w^2. \]

For multi-step forecasting, recursively replace future unknown values by their forecasts. For example, for AR(2),

\[ X_{t+1}=\phi_1X_t+\phi_2X_{t-1}+W_{t+1}, \]

so

\[ \widehat X_{t+1|t}=\phi_1X_t+\phi_2X_{t-1}. \]

Then

\[ X_{t+2}=\phi_1X_{t+1}+\phi_2X_t+W_{t+2}, \]

so

\[ \widehat X_{t+2|t}=\phi_1\widehat X_{t+1|t}+\phi_2X_t. \]

Similarly,

\[ \widehat X_{t+m|t}=\phi_1\widehat X_{t+m-1|t}+\phi_2\widehat X_{t+m-2|t}, \qquad m\ge3. \]

The rule is simple: use observed values when they are available and forecasted values when the model refers to future values.

8.15 6.14 Companion matrix representation

For AR(\(p\)), define the state vector

\[ \mathbf s_t= \begin{bmatrix} X_t\\ X_{t-1}\\ \vdots\\ X_{t-p+1} \end{bmatrix}. \]

Then

\[ \mathbf s_{t+1}=A\mathbf s_t+\mathbf e_1W_{t+1}, \]

where

\[ A= \begin{bmatrix} \phi_1 & \phi_2 & \cdots & \phi_{p-1} & \phi_p\\ 1 & 0 & \cdots & 0 & 0\\ 0 & 1 & \cdots & 0 & 0\\ \vdots & \vdots & \ddots & \vdots & \vdots\\ 0 & 0 & \cdots & 1 & 0 \end{bmatrix}. \]

The multi-step forecast state is

\[ \widehat{\mathbf s}_{t+m|t}=A^m\mathbf s_t. \]

The point forecast is the first component:

\[ \widehat X_{t+m|t}=\mathbf e_1^TA^m\mathbf s_t. \]

This representation makes AR forecasting look like a linear dynamical system. It also prepares us for state-space models and the Kalman filter.

8.16 6.15 Python example: recursive AR(2) forecasting

Code
import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(2026)
phi1, phi2 = 0.7, -0.25
sigma_w = 0.8
n = 150
x = np.zeros(n)
for t in range(2, n):
    x[t] = phi1 * x[t-1] + phi2 * x[t-2] + rng.normal(0, sigma_w)

H = 30
forecasts = []
# history contains most recent values [X_t, X_{t-1}]
history = [x[-1], x[-2]]

for m in range(1, H + 1):
    next_forecast = phi1 * history[0] + phi2 * history[1]
    forecasts.append(next_forecast)
    history = [next_forecast, history[0]]

plt.figure(figsize=(9, 4.8))
plt.plot(np.arange(n), x, label="observed")
plt.plot(np.arange(n, n + H), forecasts, marker="o", label="recursive AR(2) forecast")
plt.axvline(n - 1, linestyle="--", linewidth=1)
plt.xlabel("time")
plt.ylabel("value")
plt.title("Recursive multi-step AR(2) forecasting")
plt.legend()
plt.show()

8.17 6.16 MA(\(q\)) forecasting

Consider an MA(\(q\)) model

\[ X_t=W_t+\theta_1W_{t-1}+\cdots+\theta_qW_{t-q}. \]

If the innovations \(W_t,W_{t-1},\ldots\) are known, then the one-step forecast is

\[ \widehat X_{t+1|t}=\theta_1W_t+\theta_2W_{t-1}+\cdots+\theta_qW_{t-q+1}. \]

The one-step forecast error is \(W_{t+1}\).

For \(m>q\), all innovations appearing in \(X_{t+m}\) are future innovations, whose conditional expectation is zero. Therefore,

\[ \widehat X_{t+m|t}=0,\qquad m>q, \]

for a zero-mean MA(\(q\)) process.

8.17.1 Important practical issue

In real data, the innovations are not directly observed. They must be estimated from the fitted model. This is one reason why forecasting ARMA models requires recursive algorithms.

8.18 6.17 Forecasting causal ARMA processes

A causal ARMA process has the representation

\[ X_t=\psi(B)W_t=\sum_{j=0}^{\infty}\psi_jW_{t-j}, \qquad \psi_0=1. \]

Then

\[ X_{t+m}=\sum_{j=0}^{m-1}\psi_jW_{t+m-j} +\sum_{j=m}^{\infty}\psi_jW_{t+m-j}. \]

The first sum contains future innovations and has conditional expectation zero. The second sum contains known or estimated past innovations. Therefore,

\[ \widehat X_{t+m|t}=\sum_{j=m}^{\infty}\psi_jW_{t+m-j}. \]

The forecast error is

\[ X_{t+m}-\widehat X_{t+m|t} =\sum_{j=0}^{m-1}\psi_jW_{t+m-j}. \]

Thus, if \(W_t\) has variance \(\sigma_w^2\),

\[ \operatorname{Var}(X_{t+m}-\widehat X_{t+m|t}) =\sigma_w^2\sum_{j=0}^{m-1}\psi_j^2. \]

NoteKey formula

For a causal ARMA model, forecast uncertainty is determined by the impulse-response weights \(\psi_j\):

\[ \operatorname{MSPE}(m)=\sigma_w^2\sum_{j=0}^{m-1}\psi_j^2. \]

This formula is one of the cleanest links between ARMA dynamics, shocks, and forecast uncertainty.

8.19 6.18 Python example: forecast error variance from impulse responses

Code
import numpy as np
import pandas as pd

def arma_psi_weights(ar_params, ma_params, max_lag):
    """
    Compute psi weights for an ARMA(p, q) model:
    phi(B) X_t = theta(B) W_t.

    ar_params = [phi_1, ..., phi_p]
    ma_params = [theta_1, ..., theta_q]
    psi_0 = 1
    """
    p = len(ar_params)
    q = len(ma_params)
    psi = np.zeros(max_lag + 1)
    psi[0] = 1.0

    # theta_0 = 1, theta_j = 0 for j > q
    theta = np.zeros(max_lag + 1)
    theta[0] = 1.0
    for j, val in enumerate(ma_params, start=1):
        theta[j] = val

    for k in range(1, max_lag + 1):
        ar_part = 0.0
        for i in range(1, p + 1):
            if k - i >= 0:
                ar_part += ar_params[i-1] * psi[k-i]
        psi[k] = theta[k] + ar_part
    return psi

ar_params = [0.6]
ma_params = [0.4]
H = 12
psi = arma_psi_weights(ar_params, ma_params, max_lag=50)
sigma_w = 1.0
mspe = [sigma_w**2 * np.sum(psi[:m]**2) for m in range(1, H + 1)]

pd.DataFrame({
    "horizon": np.arange(1, H + 1),
    "forecast_error_variance": mspe
})
horizon forecast_error_variance
0 1 1.000000
1 2 2.000000
2 3 2.360000
3 4 2.489600
4 5 2.536256
5 6 2.553052
6 7 2.559099
7 8 2.561276
8 9 2.562059
9 10 2.562341
10 11 2.562443
11 12 2.562479

8.20 6.19 Parameter estimation and forecasting

So far, many formulas assumed that the model and its parameters were known. In practice, we estimate them.

A typical classical workflow is:

  1. Plot the time series.
  2. Transform the data if necessary.
  3. Remove or difference trend and seasonality if necessary.
  4. Fit a stationary model to the residual or differenced series.
  5. Forecast the stationary component.
  6. Invert transformations to return to the original scale.
  7. Diagnose forecast errors and residuals.

8.20.1 Method of moments for AR(\(p\))

For a causal AR(\(p\)),

\[ X_t=\phi_1X_{t-1}+\cdots+\phi_pX_{t-p}+W_t. \]

Multiplying by \(X_{t-h}\) and taking expectations gives the Yule–Walker equations:

\[ \gamma(h)=\phi_1\gamma(h-1)+\phi_2\gamma(h-2)+\cdots+\phi_p\gamma(h-p), \qquad h=1,\ldots,p. \]

In matrix form,

\[ \Gamma_p\phi=\gamma_p. \]

Replacing population autocovariances by sample autocovariances gives method-of-moments estimates.

8.20.2 Maximum likelihood

For Gaussian ARMA models, maximum likelihood estimates parameters by maximizing the likelihood of the observed series. In practice, modern software usually uses numerical optimization with recursive likelihood evaluation.

TipTheory-to-software connection

The same covariance and recursion ideas appear repeatedly: forecasting, Yule–Walker estimation, innovations algorithms, Kalman filters, and likelihood evaluation are all versions of the same projection/update principle.

8.21 6.20 Python example: rolling one-step forecasts without leakage

A common mistake is to train on data that includes future observations. Time-series forecasting must respect chronological order.

Code
import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(11)
phi = 0.8
sigma_w = 1.0
n = 250
x = np.zeros(n)
for t in range(1, n):
    x[t] = phi * x[t-1] + rng.normal(0, sigma_w)

train_size = 80
preds = []
actuals = []
phis_hat = []

for t in range(train_size, n - 1):
    train = x[:t+1]
    y = train[1:]
    z = train[:-1]
    # OLS estimate for AR(1) with zero mean: y_t = phi z_t + error_t
    phi_hat = np.dot(z, y) / np.dot(z, z)
    pred = phi_hat * x[t]
    preds.append(pred)
    actuals.append(x[t+1])
    phis_hat.append(phi_hat)

preds = np.array(preds)
actuals = np.array(actuals)
rmse = np.sqrt(np.mean((actuals - preds)**2))

plt.figure(figsize=(9, 4.8))
plt.plot(actuals[:80], label="actual next value")
plt.plot(preds[:80], label="rolling one-step forecast")
plt.xlabel("forecast origin index")
plt.ylabel("value")
plt.title(f"Rolling one-step forecasts; RMSE = {rmse:.3f}")
plt.legend()
plt.show()

8.22 6.21 Recursive algorithms and bounded memory

The covariance equation

\[ \Gamma_n\phi_n=\gamma_n \]

is conceptually clear, but it has a problem: as \(n\) grows, the system changes size. Solving a new \(n\times n\) system from scratch at every time step is expensive.

This motivates recursive forecasting algorithms:

  • Durbin–Levinson algorithm solves Toeplitz systems efficiently and is central for AR prediction.
  • Innovations algorithm updates forecasts using new innovations.
  • Kalman filter performs recursive prediction and updating for state-space models.
  • Particle filters extend filtering ideas to nonlinear and non-Gaussian state-space models.

These algorithms all follow a common pattern:

\[ \text{new forecast} = \text{old forecast} + \text{correction from new information}. \]

This pattern will return in later chapters when we discuss state-space models, hidden Markov models, and recurrent neural networks.

8.23 6.22 Forecasting after transformations

Many time series are not stationary on the original scale. Suppose

\[ X_t=\mu_t+Y_t, \]

where \(\mu_t\) is trend/seasonality and \(Y_t\) is stationary. A common strategy is:

  1. estimate \(\mu_t\);
  2. compute residuals \(\widehat Y_t=X_t-\widehat\mu_t\);
  3. forecast \(\widehat Y_{t+m}\) using a stationary model;
  4. add back the forecasted trend/seasonal component.

If we difference the data, then we must invert differencing after forecasting. For example, if

\[ Z_t=\nabla X_t=X_t-X_{t-1}, \]

and we forecast \(\widehat Z_{n+1|n},\ldots,\widehat Z_{n+m|n}\), then

\[ \widehat X_{n+m|n}=X_n+\sum_{j=1}^m\widehat Z_{n+j|n}. \]

WarningCommon mistake

Students often forecast differenced data and forget to return to the original scale. A forecast for \(\nabla X_t\) is not directly a forecast for \(X_t\).

8.24 6.23 Forecasting and modern machine learning

The best linear predictor is the classical foundation of forecasting. Modern machine learning generalizes it in several directions.

Classical forecasting object Modern ML analogue
\(\widehat X_{t+1|t}=\sum_j a_jX_{t+1-j}\) linear layer on lag features
AR(\(p\)) fixed-window autoregressive neural model
MA(\(\infty\)) impulse response convolutional filter over past shocks/features
recursive prediction recurrent hidden-state update
covariance projection learned representation/projection
Gaussian forecast interval probabilistic neural forecast

A windowed neural network forecast has the form

\[ \widehat X_{t+1|t}=f_\Theta(X_t,X_{t-1},\ldots,X_{t-p+1}), \]

where \(f_\Theta\) is nonlinear and learned from data. This is a nonlinear extension of the best linear predictor.

However, the same rule remains: the input window must only use past information.

8.25 6.24 AI-assisted study: useful prompts and guardrails

AI tools can help you learn forecasting theory, but only if you ask for verifiable work.

8.25.1 Prompt 1: Derivation check

I derived the best linear predictor equations for \(X_{n+1}\) from \(X_n,\ldots,X_1\). Check whether the orthogonality equations imply \(\Gamma_n\phi_n=\gamma_n\). Explain every index carefully and identify possible off-by-one errors.

8.25.2 Prompt 2: Code review for leakage

Review this time-series forecasting code. Check whether any future observations enter the training data, feature construction, scaling, or validation procedure. Mark every possible source of data leakage.

8.25.3 Prompt 3: Model interpretation

I fitted an AR(2) model with coefficients \(\phi_1\) and \(\phi_2\). Explain how to compute one-step and multi-step forecasts recursively. Then explain how the characteristic roots affect the shape of the forecast path.

8.25.4 Prompt 4: Forecast interval explanation

Explain the difference between a confidence interval for a parameter and a forecast interval for a future observation in an AR(1) model. Use formulas and a simple numerical example.

WarningAI guardrail

Do not accept an AI-generated forecast workflow unless it explicitly states the forecast origin, the training window, the prediction horizon, and how transformations are inverted. Without those details, the answer may be mathematically correct but operationally wrong.

8.26 6.25 Common mistakes

  1. Using future data in features. A rolling mean centered at time \(t\) may use future values. Use past-only rolling features for forecasting.
  2. Random train-test split. Random splits destroy time order and usually create leakage.
  3. Confusing fitted values with forecasts. Fitted values use the data used to estimate the model. Forecasts predict future unseen values.
  4. Ignoring forecast horizon. A one-step model may perform well but fail for long horizons.
  5. Forgetting mean adjustment. If \(E(X_t)=\mu\), forecast deviations from \(\mu\), then add \(\mu\) back.
  6. Forgetting to invert transformations. Log transforms and differencing must be inverted carefully.
  7. Reporting point forecasts only. Forecast uncertainty is part of the model output.
  8. Assuming linear prediction is always optimal. Linear prediction is optimal under Gaussian assumptions, but nonlinear dependence may require nonlinear methods.

8.27 6.26 Mini project: classical forecasting report

Choose one real or simulated time series and produce a short forecasting report.

8.27.1 Requirements

  1. Plot the series and describe trend, seasonality, outliers, and variance changes.
  2. Choose a stationary transformation or justify why none is needed.
  3. Fit at least two forecasting models:
    • a naive or persistence baseline;
    • an AR or ARMA-type model.
  4. Use chronological train/test splitting.
  5. Compare one-step and multi-step forecasts.
  6. Report RMSE or MAE and include forecast intervals when possible.
  7. Include a paragraph explaining whether your model uses only past information.

8.27.2 Suggested datasets

  • daily temperature;
  • electricity demand;
  • S&P 500 returns;
  • airline passengers after log transform;
  • simulated AR(2) with complex roots;
  • generated seasonal-plus-AR residual data.

8.28 6.27 Exercises

8.28.1 Conceptual exercises

  1. Explain why white noise is difficult to forecast even though its distribution is easy to describe.
  2. What does it mean for a forecast error to be orthogonal to the prediction variables?
  3. Why is conditional expectation the best mean-square predictor?
  4. Why is the best linear predictor especially important for Gaussian time series?
  5. Explain the difference between \(\widehat X_{t+1|t}\) and \(\widehat X_{t+5|t}\).
  6. Why does forecast uncertainty generally increase with horizon?

8.28.2 Mathematical exercises

  1. Let \(X_t\) be zero-mean stationary with autocovariance \(\gamma(h)\). Derive the best linear predictor of \(X_2\) from \(X_1\) and compute its MSPE.
  2. Derive \(\Gamma_2\phi_2=\gamma_2\) explicitly for predicting \(X_3\) from \(X_2\) and \(X_1\).
  3. For AR(1), derive the \(m\)-step forecast and forecast error variance.
  4. For AR(2), write the recursive formulas for \(\widehat X_{t+1|t}\), \(\widehat X_{t+2|t}\), and \(\widehat X_{t+3|t}\).
  5. For a causal ARMA process \(X_t=\sum_{j=0}^\infty\psi_jW_{t-j}\), derive the formula

\[ \operatorname{MSPE}(m)=\sigma_w^2\sum_{j=0}^{m-1}\psi_j^2. \]

  1. Suppose \(X_t\) has mean \(\mu\). Derive the centered form of the best linear predictor and explain why centering simplifies the equations.

8.28.3 Python exercises

  1. Simulate an AR(1) process with \(\phi=0.9\). Compare one-step rolling forecasts with \(\phi\) known and with \(\phi\) estimated recursively.
  2. Simulate an AR(2) process with complex roots. Plot the forecast path from a large positive starting value.
  3. Implement the covariance matrix \(\Gamma_n\) for a given autocovariance function and solve for BLP coefficients.
  4. Estimate an AR(1) model using only the training set. Forecast the test set recursively and compute RMSE.
  5. Compare random train/test splitting with chronological splitting on a simulated AR(1). Explain why the random split is misleading.
  6. Use bootstrap simulation to estimate empirical forecast intervals for an AR(1) and compare them with the Gaussian formula.

8.29 6.28 Chapter checklist

You are ready to move on when you can:

  • state the forecasting problem using \(X_{n+m}\) and \(\mathcal F_n\);
  • explain conditional expectation as the MMSE predictor;
  • derive the orthogonality equations for best linear prediction;
  • write \(\Gamma_n\phi_n=\gamma_n\) and interpret every term;
  • compute the MSPE formula;
  • forecast AR(1) and AR(\(p\)) models recursively;
  • explain why MA and ARMA forecasting depends on innovations;
  • compute forecast error variance from \(\psi\) weights;
  • construct forecast intervals under Gaussian assumptions;
  • evaluate forecasting code for time leakage;
  • describe how classical forecasting ideas connect to ML sequence models.

8.30 6.29 Key takeaways

Best linear prediction is the mathematical core of classical forecasting. The forecast is a projection of the future value onto the space spanned by the observed past. The residual is orthogonal to all information used in the prediction. For stationary processes, this projection is determined by the autocovariance function. For AR and ARMA models, the same theory leads to simple recursive forecasts and explicit formulas for forecast uncertainty. These ideas remain central in modern machine learning, where lag windows, convolutional filters, recurrent states, and attention mechanisms are nonlinear extensions of the same basic goal: use the past to predict the future without leaking information from the future.