Friday, December 27, 2024

How to calculate moving averages with NumPy

Share


Photo by editor | Ideogram

Let’s learn how to calculate moving averages using NumPy.

Preparation

Make sure you have the NumPy library installed in your environment. If not, you can install them with pip using the following code:

After installing the NumPy library, we will learn more about calculating moving averages in the next section.

Calculate moving averages with NumPy

Moving averages (MA) are often used in the economy and financial industry to understand current trends, forecasts and signal indicators. The MA technique is also considered a lagging indicator because it is based on historical data and provides information about the current situation.

Let’s utilize NumPy to calculate moving averages. First, we will try to calculate the basic moving average (SMA). It is claimed to be so basic that it only calculates the data set within sliding windows and takes the average as the data point.

For example, we have ten data points for which we want to take an SMA with a window size of five. We can do this with the code below.

import numpy as np

data = np.array([10, 15, 10, 30, 20, 45, 70, 50, 40, 60])
window_size = 5

weights = np.ones(window_size) / window_size
sma = np.convolve(data, weights, mode="valid")
Output>>
[17. 24. 35. 43. 45. 53.]

As you can see from the results, we get a moving average from the data with a window size of 5.

Another moving average technique we can utilize is the cumulative moving average (CMA). The CMA technique would provide data points by taking the average of previous sets of data items, including itself, for each item,

data = np.array([10, 15, 10, 30, 20, 45, 70, 50, 40, 60])
cma = np.cumsum(data) / np.arange(1, len(data) + 1)

cma
Output>>
array([10, 12.5, 11.66666667, 16.25, 17.,
      21.66666667, 28.57142857, 31.2, 32.22222222, 35.])

Then there is an MA technique that incorporates weight into its calculations, called exponential moving averages (EMA). EMA attaches more importance to more recent data than to later data. EMA is much more sensitive than SMA because it provides information about recent changes in calculations. This information is represented as alpha.

Let’s try the NumPy implementation in Python.

data = np.array([10, 15, 10, 30, 20, 45, 70, 50, 40, 60])

def exponential_moving_average(data, alpha):
    ema = np.zeros_like(data)
    ema[0] = data[0]
   
    for i in range(1, len(data)):
        ema[i] = alpha * data[i] + (1 - alpha) * ema[i-1]
   
    return ema

ema = exponential_moving_average(data, 0.5) 
Output>>
array([10, 12, 11, 20, 20, 32, 51, 50, 45, 52])

Additional resources

Cornelius Yudha Vijaya is an assistant data analytics manager and data writer. Working full time at Allianz Indonesia, he loves sharing Python tips and data through social media and writing media. Cornellius writes on a variety of topics related to artificial intelligence and machine learning.

Our top 3 partner recommendations

1. The best VPN for engineers – 3 months free – Stay protected online with a free trial

2. The best project management tool for technical teams – Escalate your team’s effectiveness today

4. The best password management tool for technical teams – zero trust and zero knowledge security

Latest Posts

More News