Mathew K Analytics

Lesson 22 · Python For Time Series

Understanding Autoregressive, Moving Average, and ARMA Models for Time Series Forecasting

In this lesson, we will explore how to use Python to work with time series data. We will focus on two foundational forecasting models: Autoregressive (AR)…

⬇ Download notebookOpen in Colab ↗

📓 Full notebook

Download .ipynb
 

Welcome to Python Time Series: AR and MA Models#

In this lesson, we will explore how to use Python to work with time series data.

We will focus on two foundational forecasting models:

  • Autoregressive (AR) models
  • Moving Average (MA) models

By the end, you will know how to load real data, build, and interpret basic AR and MA models.

Let us get started!

# Imports and setup
import warnings; warnings.filterwarnings("ignore")
import pandas as pd
import matplotlib.pyplot as plt

Why Use Autoregressive and Moving Average Models?#

Autoregressive (AR) and Moving Average (MA) models are basic building blocks in time series forecasting.

They help us make sense of trends and random changes in data that arrives in order over time.

Let us load a real dataset next.

# Data setup: Airline Passengers
df = pd.read_csv("https://raw.githubusercontent.com/jbrownlee/Datasets/master/airline-passengers.csv")
print("Data shape:", df.shape)
df.head()
# Plot the time series
plt.figure(figsize=(10,4))
plt.plot(df["Passengers"])
plt.title("Monthly Airline Passengers")
plt.xlabel("Month")
plt.ylabel("Passengers")
plt.show()

What Is an Autoregressive (AR) Model?#

An AR model uses past values of the data to predict future values.

Think of it like guessing tomorrow's weather based on the last few days.

# Lagged values: creating past-value features
df["lag1"] = df["Passengers"].shift(1)
df["lag2"] = df["Passengers"].shift(2)
df[["Passengers", "lag1", "lag2"]].head(6)
# Simple AR(1) model: prediction by last month
df["ar1_pred"] = df["lag1"]
df[["Passengers", "ar1_pred"]].head(10)

What Is a Moving Average (MA) Model?#

A Moving Average model predicts future values by looking at past errors.

Errors are just the differences between what the model expected and what really happened.

It is like averaging out the surprise from recent events.

# Calculate errors: AR(1) model
df["error1"] = df["Passengers"] - df["ar1_pred"]
df[["Passengers", "ar1_pred", "error1"]].head(10)
# Simple MA(1) model: prediction by past error
df["ma1_pred"] = df["Passengers"].shift(1) + df["error1"].shift(1)
df[["Passengers", "ma1_pred"]].head(10)

Fitting AR and MA Models with statsmodels#

Python's statsmodels library makes fitting better AR or MA models easy.

Let us use some real modeling functions!

# AR model with statsmodels
from statsmodels.tsa.ar_model import AutoReg
ar_model = AutoReg(df["Passengers"], lags=1, old_names=False).fit()
print("AR(1) params:", ar_model.params)
# MA model with statsmodels
from statsmodels.tsa.arima.model import ARIMA
ma_model = ARIMA(df["Passengers"], order=(0,0,1)).fit()
print("MA(1) params:", ma_model.params)
# Visualize predictions: AR vs real data
df["ar_pred_sm"] = ar_model.predict(start=1, end=len(df)-1)
plt.figure(figsize=(10,4))
plt.plot(df["Passengers"], label="Actual")
plt.plot(df["ar_pred_sm"], label="AR(1) Prediction")
plt.legend()
plt.title("AR(1) Model Prediction vs Actual")
plt.show()
# Visualize predictions: MA vs real data
df["ma_pred_sm"] = ma_model.predict(start=1, end=len(df))
plt.figure(figsize=(10,4))
plt.plot(df["Passengers"], label="Actual")
plt.plot(df["ma_pred_sm"], label="MA(1) Prediction")
plt.legend()
plt.title("MA(1) Model Prediction vs Actual")
plt.show()

What About ARMA Models?#

ARMA models combine both AR and MA ideas: they use past values and past errors.

ARMA is great when your data has both trends and unpredictable changes.

Let us build one!

# ARMA(1,1) model with statsmodels
from statsmodels.tsa.arima.model import ARIMA
arma_model = ARIMA(df["Passengers"], order=(1,0,1)).fit()
print("ARMA(1,1) params:", arma_model.params)
# Visualize ARMA predictions vs actual
df["arma_pred_sm"] = arma_model.predict(start=1, end=len(df))
plt.figure(figsize=(10,4))
plt.plot(df["Passengers"], label="Actual")
plt.plot(df["arma_pred_sm"], label="ARMA(1,1) Prediction")
plt.legend()
plt.title("ARMA(1,1) Model Prediction vs Actual")
plt.show()

Train/Test Split: Testing Model Predictions#

To see how a model performs, we need to test on new data.

Let us split our time series into training and testing sets.

# Split data: last 12 months for testing
split_point = len(df) - 12
train = df["Passengers"][:split_point]
test = df["Passengers"][split_point:]
print("Train size:", len(train), ", Test size:", len(test))
# Fit and predict with ARMA on train/test
arma_model_tt = ARIMA(train, order=(1,0,1)).fit()
forecast = arma_model_tt.forecast(steps=12)
plt.figure(figsize=(10,4))
plt.plot(train.index, train, label="Train")
plt.plot(test.index, test, label="Test")
plt.plot(test.index, forecast, label="Forecast")
plt.legend()
plt.title("ARMA(1,1) Forecast vs True Values")
plt.show()
# Evaluate forecast accuracy for test set
from sklearn.metrics import mean_squared_error
mse = mean_squared_error(test, forecast)
print("Test MSE:", mse)

Mini Project: Forecast Shampoo Sales#

Practice time! Let us forecast the next few months of shampoo sales using ARMA.

We will load a similar retail sales dataset and repeat what we have learned.

# Load shampoo sales data
shampoo = pd.read_csv("https://raw.githubusercontent.com/jbrownlee/Datasets/master/shampoo.csv")
plt.figure(figsize=(10,4))
plt.plot(shampoo["Sales"])
plt.title("Monthly Shampoo Sales")
plt.xlabel("Month")
plt.ylabel("Sales")
plt.show()
# Fit ARMA to shampoo sales and forecast next 6 months
from statsmodels.tsa.arima.model import ARIMA
shampoo_model = ARIMA(shampoo["Sales"], order=(1,0,1)).fit()
next6 = shampoo_model.forecast(steps=6)
print("Next 6 month forecast:")
print(next6)

Recap: What Did We Learn?#

  • Loaded real-world time series data.
  • Built basic AR and MA models by hand and with statsmodels.
  • Compared AR, MA, and ARMA predictions and plots.
  • Checked forecasting on new data using train/test split.

Well done! You now have the tools to continue time series forecasting in Python.

Challenge: Your Turn#

  • Try a new AR or MA model order on the airline data.
  • Plot the predictions and compare errors.
  • Load a different time series dataset and apply ARMA.

Want more? Like and subscribe for new lessons every week!

Found this useful?

All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.