Autoregressive (AR) Models
Swipe to show menu
Autoregressive models are a foundational class of time series models that forecast future values based on past observations. In an autoregressive model of order p, denoted as AR(p), the current value of the series is expressed as a linear combination of its previous p values plus a random error term. The general form of an AR(p) model is:
Xt=c+φ1Xt−1+φ2Xt−2+...+φpXt−p+εtwhere:
- Xt is the value at time t;
- c is a constant;
- φ1,φ2,...,φp are parameters (coefficients) to be estimated;
- εt is white noise (a random error term with zero mean and constant variance).
A key concept in AR models is the use of lagged values. Lags refer to prior time points in the series; for example, Xt−1 is the first lag, Xt−2 is the second lag, and so on. The order p determines how many lagged values are used to predict the current value.
For AR models to be valid, several assumptions should be met:
- The time series should be stationary, meaning its statistical properties like mean and variance do not change over time;
- The error terms should be uncorrelated and normally distributed;
- The model parameters should ensure the process does not explode (i.e., the series remains stable over time).
12345678910111213141516171819202122232425262728293031323334import numpy as np import pandas as pd import matplotlib.pyplot as plt from statsmodels.tsa.ar_model import AutoReg # Generate a synthetic AR(1) time series np.random.seed(42) n = 100 phi = 0.7 c = 2 errors = np.random.normal(0, 1, n) series = [0] for t in range(1, n): series.append(c + phi * series[-1] + errors[t]) series = pd.Series(series) # Fit an AR(1) model model = AutoReg(series, lags=1) model_fit = model.fit() # Predict the next 10 values pred_start = len(series) pred_end = pred_start + 9 predictions = model_fit.predict(start=pred_start, end=pred_end) # Plot the original series and predictions plt.figure(figsize=(10, 5)) plt.plot(series, label="Original Series") plt.plot(range(pred_start, pred_end + 1), predictions, label="AR(1) Predictions", marker='o') plt.legend() plt.title("AR(1) Model Fit and Forecast") plt.xlabel("Time") plt.ylabel("Value") plt.show()
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat