Image by Editor | Ideogram
Â
Let’s learn how to calculate moving averages with NumPy.
Â
Preparation
Â
Ensure you have the NumPy library installed in your environment. If not, you can install them via pip using the following code:
Â
With the NumPy library installed, we will learn more about how to compute moving averages in the next part.
Â
Compute Moving Averages with NumPy
Â
Moving Averages (MA) is a statistical technique that creates a series of data points averaged from different windows of the dataset. It’s often used in time-series analysis to smooth the dataset for an easier outlook on longer-term trends that are hard to see because of the short-term noises.
Moving Averages (MAs) 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 use NumPy to compute Moving Averages. First, we would try calculate the Simple Moving Average (SMA). It’s deemed as simple as it only calculates the dataset within the rolling windows and takes the average as a 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 that 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 output, we get the moving average with a window size of 5 from the data.
Another Moving Average technique we can perform is the Cumulative Moving Average (CMA). The CMA technique would provide data points by taking the average of the previous set elements of data, 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). EMA gives more weight to more recent data points than the later ones. EMA is much more sensitive than SMA as it allows information on recent changes in the calculation. 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])
Â
That’s all for the basic NumPy implementation for computing Moving Averages with NumPy. Try to master them to make your time-series analysis easier.
Â
Additional Resources
Â
Â
Â
Cornellius Yudha Wijaya is a data science assistant manager and data writer. While working full-time at Allianz Indonesia, he loves to share Python and data tips via social media and writing media. Cornellius writes on a variety of AI and machine learning topics.
Our Top 3 Partner Recommendations
1. Best VPN for Engineers – 3 Months Free – Stay secure online with a free trial
2. Best Project Management Tool for Tech Teams – Boost team efficiency today
4. Best Password Management Tool for Tech Teams – zero-trust and zero-knowledge security