Time series analysis and forecasting are important concepts in data science that have a variety of applications. There are different techniques used to make forecasting with time-series data. The exponential smoothing and moving average are the two basic and important techniques used for this purpose. In this article, we will take a look at the comparison between Exponential smoothing and the Moving average for time series forecasting. The major points to be discussed in the article are listed below.
Table of contents
- About exponential smoothing?
- Implementation of exponential smoothing methods
- About the Moving average?
- Implementation of Moving average
- Exponential smoothing vs Moving average
Let’s start with understanding exponential smoothing
About exponential smoothing
In time series exponential smoothing can be considered as a method to smooth the time series data. We can also consider it as a thumb rule technique which is an approximate method of doing something. This method uses an exponential window function that we use to assign exponentially decreasing weights over time. This method of smoothing time series can also be used for making many determinations based on some prior assumption. But generally, we apply it to smooth the time series data.
Are you looking for a complete repository of Python libraries used in data science, check out here.
Application of widow can also be considered as applying a filter to time series data so that we can remove the noise out of time series data. Let’s say that a univariate time series data is xt at time t = 0 and the result of smoothing using the exponential smoothing is st . we can also say the St is the best estimation of the next value of the time series. Mathematically we can give exponential smoothing in the form of the following formula.
s0 = x0
st = xt + (1-)st-1 , t>0
(0<<1) = smoothing factor.
A simple exponential smoothing can also be considered as recursive filtering and mathematically can be given as follows
Exponential smoothing at a time t ( st ) can also be considered as the simple weighted average value of time series at time t (xt) and the last smoothed value st-1 . We can even apply these statistics with two sequential values. One thing that is noticeable here is if the value of increases the level of smoothing decreases and there is no perfect methods that can be applied for choosing the value of .
This method can also be referred to as exponentially weighted moving average (EWMA), and autoregressive integrated moving average (ARIMA) without constant term in some other cases. There are three types of exponential smoothing we can find:
- Simple/single exponential smoothing: This smoothing can be used for making forecasts based in a time series that has no trend and seasonality.
- Double exponential smoothing: This type of exponential smoothing comes with the support for trend components of time series.
- Triple exponential smoothing: This type of exponential smoothing comes with the support for trend and seasonality components of time series.
Let’s take a look at how this method can be implemented.
Forecasting using exponential smoothing
We can use the statsmodel library for the implementation of exponential smoothing methods. In this library under the tsa.holt winters package we can get all the methods implemented for exponential smoothing. Let’s take a look at the implementation of simple exponential smoothing.
import pandas as pd
ts = pd.read_csv('/content/drive/MyDrive/Copy of shampoo-sales .csv')
Importing and fitting module:
from statsmodels.tsa.holtwinters import SimpleExpSmoothing
model = SimpleExpSmoothing(ts['Sales'])
fit_model = model.fit(smoothing_level=0.2, optimized=False)
Plotting smoothed data:
import matplotlib.pyplot as plt
Here we can see how the function defined in the library works using the simple exponential smoothing method.
About the Moving average
As the name suggests it is a method in which we can say the average is moving. In time series analysis it is a method for forecasting works by capturing the average changes in the time series. In various places, we can also find that this method is referred to as rolling average, running average, moving mean (MM)or rolling mean. It is based on making a series of averages using the subsets of full data. In general, we can also consider it as a calculation method to analyze sequential data.
This also works by applying a filter on a time series and the filter can be considered a finite impulse response filter. This filter works by taking the average of a fixed subset of a series of numbers. Let’s say there is a time series that can be divided based on months which means we have a set of values and taking the average of values from the first month of the time series can be considered as the first moving average of the smoothing process. Not as the time will move in the series the set of numbers will change and accordingly the average value will keep changing.
The below image is a representation of applying a moving average to smooth the time series.
Here we can see that the red line is the result of the smoothing and we can say that we apply it to remove short time fluctuation from the time series so that the long term trend of the time series can be highlighted. There are three types of moving average
- Simple moving average: It is the simplest form of moving average where the addition of recent values of the time series gets divided by the time.
- Exponential moving average: This type of moving average focuses more on the recent values by providing a larger weight of the recent values to the moving average.
- Cumulative moving average: This method focuses on all the values of the time series by applying the mean of all values and the mean should be unweighted.
Let’s take a look at how we can implement a simple moving average using python.
Forecasting using moving average
We can perform time series forecasting using the moving average method just with the pandas’ library. In the above, we have imported the shampoo sales data. Let’s plot the data.
Let’s plot the data after applying the moving average.
In the above, we have seen how we can implement and define both of the methods. Let’s take a look at the difference between these two methods of smoothing.
Exponential smoothing vs Moving average
One of the important differences between these two methods can be assumed using their name. On the one end, the exponential smoothing is applying an exponential window while on the second side we can see that the average window is applied to the data to reduce the noise of the data.
Under exponential smoothing, we have methods that provide support against the components of time series such as trend and seasonality while talking about the moving average we can say that methods under this smoothing process are more focused on the values with their timings. Like the exponential moving average is focused on the recent values.
One more thing that creates a big difference between these methods is that exponential smoothing can be done using even two data points and to start the moving average we are required to set the window size which needs to be more than three for performing actual smoothing.
Here we can say that the exponential removes more noise from the data according to the smoothing factor and the moving average is removing noise using the mean value of data points.
In the article, we have discussed the exponential smoothing and moving average. These both are smoothing methods for time series data. The way of working of the algorithms makes both of them different to each other as we have discussed in our last sentient.