Moving Averages

Learn what moving averages are and how to use them to make forecasts.

Understanding moving averages

Moving averages can refer to two different things in time series forecasting:

  • A simpler concept, also known as a rolling mean.

  • The moving average model, also known as MA(n).

The rolling mean is the average of the value we are looking at calculated over a certain period of time. That average is updated according to the period, so it's always relative.

Using weather data as an example, we could look at the average temperature over the past thirty days. If we do that for every day in our data series, we would get the moving average over thirty days for the whole period. Let's see how to do this in practice and how we can use this to make forecasts.

Calculating simple moving averages

Using our Seattle weather data as an example, we can plot the original time series with the moving average to see how it behaves. In pandas, we can do this with the rolling() method.

Press + to interact
main.py
seattle_weather.csv
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as dt
df = pd.read_csv('seattle_weather.csv')
# Changing the datatype
df["date"] = pd.to_datetime(df['date'], format='%Y-%m-%d')
# Setting the Date as index
df = df.set_index('date')
# Creating the moving average for "temp_max" over 30 days
df['temp_max_ma'] = df['temp_max'].rolling(window=30).mean()
# Plotting
fig, ax = plt.subplots(figsize=(7, 4.5), dpi=300)
ax.plot(df['temp_max'], label = "temp_max")
ax.plot(df['temp_max_ma'], label = "Moving average of temp_max")
# Formatting axe to make it easier to read
ax.xaxis.set_major_locator(dt.YearLocator())
ax.xaxis.set_minor_locator(dt.MonthLocator((1,4,7,10)))
ax.xaxis.set_major_formatter(dt.DateFormatter("\n%Y"))
ax.xaxis.set_minor_formatter(dt.DateFormatter("%b"))
plt.setp(ax.get_xticklabels(), rotation=0, ha="center")
plt.subplots_adjust(bottom=0.15)
# Labelling
plt.xlabel("Date")
plt.ylabel("Temperature (max)")
plt.title("Seattle daily max temperature")
plt.legend()
fig.savefig("output/output.png")
plt.close(fig)

Notice how the moving average over thirty days smooths out our time series? That's because over thirty days, there are warmer days and colder days, and they tend to balance each other out when we take their average.

Forecasting with moving averages

To use moving averages for forecasting, we usually apply the second version of the moving average concept, which we will call an MA(n) model.

xt=μ+wt+θ1wt1+...+θnwtnx_t = \mu +w_t+\theta_1w_{t-1}+...+\theta_nw_{t-n} ...