Editor's Image | Ideogram
Let's learn how to calculate moving averages with NumPy.
Preparation
Make sure you have the NumPy library installed in your environment. Otherwise, you can install them via pip using the following code:
With the NumPy library installed, we will learn more about how to calculate moving averages in the next part.
Calculate moving averages with NumPy
Moving averages (MA) is a statistical technique that creates a series of data points averaged from different windows of the data set. It is often used in time series analysis to smooth the data set and gain easier insight into long-term trends that are difficult to see due to short-term noise.
Moving Averages (MA) are often used in the economics 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 use NumPy to calculate moving averages. First, we would try to calculate the simple moving average (SMA). It is considered so simple that it only calculates the data set within the moving windows and takes the average as the data point.
For example, we have ten data points for which we want to take the SMA with a window size of five. We can do it with the following code.
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 we can see from the result, we get the moving average with a window size of 5 from the data.
Another moving average technique that we can perform is the cumulative moving average (CMA). The CMA technique would provide data points by taking the average of the data elements from the previous set, including itself, for each position,
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 includes weight in its calculation, called Exponential Moving Averages (EMA). The EMA gives more weight to more recent data than later ones. The EMA is much more sensitive than the SMA as it allows information about recent changes in the calculation. This information is represented as alpha.
Let's try the implementation of NumPy 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))
That's it for the basic NumPy implementation for calculating moving averages with NumPy. Try to master them to make time series analysis easier.
Additional Resources
Cornellius Yudha Wijaya He is an assistant data science manager and data writer. While working full-time at Allianz Indonesia, he loves sharing data and Python tips through social media and print media. Cornellius writes on a variety of artificial intelligence and machine learning topics.
Our Top 3 Partner Recommendations
1. Best VPN for Engineers: 3 Months Free – Stay safe online with a free trial
2. The best project management tool for technology teams – Drive team efficiency today
4. The best password management tool for tech teams – zero trust and zero knowledge security