Moving Average Algorithm: Crash Course in Forecasting

Ananthakrishnan Harikumar
AI Skunks
Published in
4 min readMar 31, 2023

Moving Average is a simple forecasting algorithm that is commonly used to predict future values of a time series. The algorithm calculates the average of a rolling window of past observations, and uses this average as the prediction for the next value in the series.

Here’s how the Moving Average algorithm works:

  1. Choose the size of the window: The first step is to choose the size of the window that will be used to calculate the moving average. This is typically done by selecting a number of past observations, such as the last 5 or 10 data points.
  2. Calculate the average: Once the window size is chosen, the algorithm calculates the average of the past observations within the window. For example, if the window size is 5, the algorithm would take the average of the last 5 data points.
  3. Use the average as the prediction: The moving average value calculated in step 2 is then used as the prediction for the next value in the time series.
  4. Repeat steps 2–3 for each new data point: The algorithm then moves the window one data point forward in time and repeats steps 2 and 3 to predict the next value in the series.

The Moving Average algorithm is easy to implement and can be useful for simple time series forecasting problems. However, it has some limitations, such as being unable to capture trends or seasonality in the data. For more complex time series, other algorithms such as ARIMA or Prophet may be more appropriate.

Here’s an example of how the Moving Average algorithm works.

Suppose we have the following time series data representing the monthly sales of a product for the past 12 months:

Let’s say we want to use the Moving Average algorithm to predict the sales for the next three months. We’ll choose a window size of 3, meaning we’ll take the average of the three most recent sales values to predict the next value.

To start, we’ll calculate the moving average for the first prediction, which will be for January. We’ll take the average of the sales values for October, November, and December:

Moving Average = (55 + 60 + 65) / 3 = 60

So our prediction for January’s sales is 60. Now we move the window forward one month to February and calculate the moving average again:

Moving Average = (60 + 65 + 10) / 3 = 45

Our prediction for February’s sales is 45. We repeat this process for each month to predict the sales for the next three months:

As you can see, the Moving Average algorithm produces a smoothed version of the time series, and the predictions lag behind the actual values of the time series. While this algorithm is simple and easy to implement, it may not be appropriate for all time series forecasting problems, particularly those with more complex patterns or trends.

Moving Average on City Temperature Dataset

Importing the libraries and loading the dataset

import pandas as pd

# Load the temperature data
df = pd.read_csv('city_temperature.csv')

Data Exploration

df.head()

Filter data for a specific city and time period

city = 'Delhi'
start_year = 2000
end_year = 2010
start_month = 1
end_month = 12
start_day = 1
end_day = 31

mask = (df['City'] == city) & (df['Day'] >= start_day) & (df['Day'] <= end_day) & (df['Month'] >= start_month) & (df['Month'] <= end_month) & (df['Year'] >= start_year) & (df['Year'] <= end_year)
data = df.loc[mask]
data

Calculate the Moving Average

window_size = 5 # Change this value to adjust the window size
data['Moving_Average'] = data['AvgTemperature'].rolling(window_size).mean()
# Print the data with Moving Average
data.tail()

--

--

Ananthakrishnan Harikumar
AI Skunks
0 Followers
Writer for

A common man who believes in love and compassion more than religion and boundaries.