Simple Moving Average and Exponentially Weighted Moving Average

Sandhya Krishnan
CodeX
Published in
5 min readNov 13, 2021

--

Photo by Burak Kebapci from Pexels

A trend is a pattern, which shows the movement of data with respect to time. It can be measured by using Freehand Graphical Method, Method of selected points, Method of semi-averages, Method of moving averages, Method of Least Squares.

The method of moving average is based on the principle that the total effect of periodic variations at different points of time completely neutralizes. It uses the average of a number of adjoining data points or periods, the average is calculated by overlapping observations and it highlights the long-term trends in time series while capturing short-term fluctuations. Time series prediction model such as ARIMA uses moving average for prediction.

Simple Moving Average (SMA)

SMA is the unweighted mean of the previous k data points. If p1, p2…, pn are the time series data point and the mean over the last k data-points is denoted as SMAk then it can be calculated as:

Here the same sampling width of k is the range from (n-k+1) to n. When calculating SMA for the next k data points the width of k will be from range (n-k+2) to (n+1) and a new value p(n+1) comes into the picture and p(n-k+1) drops out. SMA for next k values can be summarized with previous

SMA can be implemented by using pandas.DataFrame.rolling() function is used to calculate the moving average over a fixed window.

General Syntax for the rolling function is

DataFrame.rolling(window, min_periods=None, center=False, win_type=None, on=None, axis=0, closed=None, method=’single’)

Where the window will be a fixed size and it is the number of observations used for calculating the statistic. Other values if not provided will take the default values.

The rolling average creates a subset from the complete series with the size of the window value, which will be the first moving average. Then the subset is changed by moving forward to the next subset with window size.

For explaining rolling(), I am using a dataset that describes daily female births in California in 1959(from Jan 1st to 31st Dec). The first 5 rows of the dataset are as below

Once the data frame is plotted, it will look like this:

The rolling() function is used with a window size of 5 days then the details will be as below:

The value for the first 4 days, i.e., (window-1) value will NaN, as we have provided to roll the average from 5 days. We can plot it in a line plot.

The orange line indicates the rolling mean and unlike the daily plot is having a smooth trend as it smoothens the time series.

Exponentially Weighted Moving Average (EWMA)

EWMA is also known as an exponential moving average (EMA).

EWMA gives more weight to recent observations or it gives less and less weight to data as they are further removed in time, thus it can capture recent trends more quickly unlike SMA which will give the genric trend as the window is fixed and for the entire dataset.

EMA is implemented by using the function pandas.DataFrame.ewm().

General syntax is

DataFrame.ewm(com=None, span=None, halflife=None, alpha=None, min_periods=0, adjust=True, ignore_na=False, axis=0, times=None)

In EWMA, the weight applied depends on the “adjust parameters” and the number of periods used. The adjust is by default true.

The weight is calculated using the formula

where alpha is a smoothing factor that takes values between 0 and 1, including 1.

When the adjust parameters is true yt is calculated as

and when adjust parameter is false yt is calculated as

Span corresponds to what is commonly called an “N-day EW moving average”

If plot the last few rows the plot will be as below.

EWMA is used to calculate the day-to-day fluctuations in accounting/financial processes which may seem to be very large, it can be used to calculate the fluctuations of an e-commerce website or other website, etc.,

For ewm() function comass, span, halflife, and alpha are mutually exclusive, so to plot ewm().mean() for different alpha values, span cannot be provided. When the span is provided alpha is calculated as 2/(span+1) where span >= 1.

As the value of alpha increases, it becomes more and more similar to the original feature.
Let’s plot the original feature and alpha value=0.98999.

Here the ewma plot almost overlaps the original feature, a few points I have marked, where the blue lines are visible. If alpha value is taken as 1, it would overlap the original one. So alpha value should be taken as per the requirement.

You can access the full code from here.

Thanks for reading!!!! If this article was helpful to you, feel free to clap, share and respond.

--

--