Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Simple Moving Average | Stationary Models
Time Series Analysis

Simple Moving AverageSimple Moving Average

Mathematically, the simple moving average model is represented as follows:

In this equation, the prediction SMA consists of variables, where k is the window size (how many past values we will take to calculate the next value), and p is the value taken.

How does this model work? In fact, the simple moving average at each prediction moment calculates the average of several past values - the resulting number is the next prediction. You can imagine a graph on the path of which a window of size n moves (you can take 2 values, you can take 3, etc.). The smaller the window, the smoother the predictions. Let's demonstrate it below:

In the first image, the window size for which the average value is calculated is 8, while on the second graph, n = 3. The smaller the window, the fewer peaks it can capture.

Python allows you to implement this model like this:

rolling() - a function used to calculate the moving average.

The simple moving average is one of the simplest models, so it is enough to use the pandas library to implement it.

Task

Make predictions for dataset pr_air_quality.csv with window size 5.

  1. Convert the "date.utc" column to datetime type.
  2. Calculate moving averages using moving windows with the size of 5.
  3. Compare the results on a plot: visualize the first 50 values of the "value" column of the df within the first call of the .plot() function and the first 50 values of the pred within the second call.
  4. Display the legend and the plot.

Everything was clear?

Section 4. Chapter 2
toggle bottom row
course content

Course Content

Time Series Analysis

Simple Moving AverageSimple Moving Average

Mathematically, the simple moving average model is represented as follows:

In this equation, the prediction SMA consists of variables, where k is the window size (how many past values we will take to calculate the next value), and p is the value taken.

How does this model work? In fact, the simple moving average at each prediction moment calculates the average of several past values - the resulting number is the next prediction. You can imagine a graph on the path of which a window of size n moves (you can take 2 values, you can take 3, etc.). The smaller the window, the smoother the predictions. Let's demonstrate it below:

In the first image, the window size for which the average value is calculated is 8, while on the second graph, n = 3. The smaller the window, the fewer peaks it can capture.

Python allows you to implement this model like this:

rolling() - a function used to calculate the moving average.

The simple moving average is one of the simplest models, so it is enough to use the pandas library to implement it.

Task

Make predictions for dataset pr_air_quality.csv with window size 5.

  1. Convert the "date.utc" column to datetime type.
  2. Calculate moving averages using moving windows with the size of 5.
  3. Compare the results on a plot: visualize the first 50 values of the "value" column of the df within the first call of the .plot() function and the first 50 values of the pred within the second call.
  4. Display the legend and the plot.

Everything was clear?

Section 4. Chapter 2
toggle bottom row
some-alt