Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Calculate Moving Averages in Python?

  • Last Updated : 28 Nov, 2021

In this article, we are going to see how to calculate the moving Average in Python. Moving average refers to a series of averages of fixed size subsets of the total set of observations. It is also known as rolling average, running average, rolling means or running average.

Consider the set of n observations and k be the size of the window for determining the average at any time t. Then moving average list is calculated by initially taking the average of the first k observations present in the current window and storing it in the list. Now, the window is expanded according to the condition of the moving average to be determined and again average of the elements present in the window is calculated and stored in the list. This process is continued until the window has reached the end of the set. 

For example: Given a list of five integers arr=[1, 2, 3, 7, 9] and we need to calculate moving averages of the list with window size specified as 3. We will first calculate average of first 3 elements and that will be stored as first moving average. Then window will be shifted one position to the right and again average of elements present in the window will be calculated and stored in the list. Similarly, the process will repeat till the window reaches the last element of the array. Following is the illustration of the above approach:

Below is the implementation:

Python3




# Program to calculate moving average
arr = [1, 2, 3, 7, 9]
window_size = 3
  
i = 0
# Initialize an empty list to store moving averages
moving_averages = []
  
# Loop through the array to consider
# every window of size 3
while i < len(arr) - window_size + 1:
    
    # Store elements from i to i+window_size
    # in list to get the current window
    window = arr[i : i + window_size]
  
    # Calculate the average of current window
    window_average = round(sum(window) / window_size, 2)
      
    # Store the average of current
    # window in moving average list
    moving_averages.append(window_average)
      
    # Shift window to right by one position
    i += 1
  
print(moving_averages)

Output:

[2.0, 4.0, 6.33]

Simple Moving Average:

SMA is calculated by taking the unweighted mean of k (size of the window) observations at a time that is present in the current window. It is used for analyzing trends.

Formulae:

SMAj = (1/k) * ∑ (i=j-1 to j+k-1)  ai

where,

  • SMAj = Simple Moving Average of jth window
  • k = size of the window
  • ai = ith element of the set of observations

Method 1: Using Numpy

Numpy module of Python provides an easy way to calculate the simple moving average of the array of observations. It provides a method called numpy.sum() which returns the sum of elements of the given array. A moving average can be calculated by finding the sum of elements present in the window and dividing it with window size.

Python3






# Program to calculate moving average using numpy
  
import numpy as np
  
arr = [1, 2, 3, 7, 9]
window_size = 3
  
i = 0
# Initialize an empty list to store moving averages
moving_averages = []
  
# Loop through the array t o
#consider every window of size 3
while i < len(arr) - window_size + 1:
  
    # Calculate the average of current window
    window_average = round(np.sum(arr[
      i:i+window_size]) / window_size, 2)
      
    # Store the average of current
    # window in moving average list
    moving_averages.append(window_average)
      
    # Shift window to right by one position
    i += 1
  
print(moving_averages)

Output:

[2.0, 4.0, 6.33]

Method 2: Using Pandas

Pandas module of Python provides an easy way to calculate the simple moving average of the series of observations. It provides a method called pandas.Series.rolling(window_size) which returns a rolling window of specified size. The mean of the window can be calculated by using pandas.Series.mean() function on the object of window obtained above. pandas.Series.rolling(window_size) will return some null series since it need at least k (size of window) elements to be rolling. 

Python




# Python program to calculate
# simple moving averages using pandas
import pandas as pd
  
arr = [1, 2, 3, 7, 9]
window_size = 3
  
# Convert array of integers to pandas series
numbers_series = pd.Series(arr)
  
# Get the window of series
# of observations of specified window size
windows = numbers_series.rolling(window_size)
  
# Create a series of moving
# averages of each window
moving_averages = windows.mean()
  
# Convert pandas series back to list
moving_averages_list = moving_averages.tolist()
  
# Remove null entries from the list
final_list = moving_averages_list[window_size - 1:]
  
print(final_list)

Output:

[2.0, 4.0, 6.33]

Cumulative Moving Average

CMA is calculated by taking the unweighted mean of all the observations up to the time of calculation. It is used for time series analysis.

Formulae:

CMAt = (1/kt) * ∑ (i=0 to k)  ai

where:

  • CMAt = Cumulative Moving Average at time t
  • kt = number of observations upto time t
  • ai = ith element of the set of observations

Method 1: Using Numpy

Numpy module of Python provides an easy way to calculate the cumulative moving average of the array of observations. It provides a method called numpy.cumsum() which returns the array of the cumulative sum of elements of the given array. A moving average can be calculated by dividing the cumulative sum of elements by window size.

Python






# Program to calculate cumulative moving average
# using numpy
  
import numpy as np
  
arr = [1, 2, 3, 7, 9]
  
i = 1
# Initialize an empty list to store cumulative moving
# averages
moving_averages = []
  
# Store cumulative sums of array in cum_sum array
cum_sum = np.cumsum(arr);
  
# Loop through the array elements
while i <= len(arr):
  
    # Calculate the cumulative average by dividing
    # cumulative sum by number of elements till 
    # that position
    window_average = round(cum_sum[i-1] / i, 2)
      
    # Store the cumulative average of
    # current window in moving average list
    moving_averages.append(window_average)
      
    # Shift window to right by one position
    i += 1
  
print(moving_averages)

Output:

[1.0, 1.5, 2.0, 3.25, 4.4]

Method 2: Using Pandas

Pandas module of Python provides an easy way to calculate the cumulative moving average of the series of observations. It provides a method called pandas.Series.expanding() which returns a window spanning over all the observations up to time t. Mean of the window can be calculated by using pandas.Series.mean() function on the object of window obtained above. 

Python




# Python program to calculate
# cumulative moving averages using pandas
import pandas as pd
  
arr = [1, 2, 3, 7, 9]
window_size = 3
  
# Convert array of integers to pandas series
numbers_series = pd.Series(arr)
  
# Get the window of series of
# observations till the current time
windows = numbers_series.expanding()
  
# Create a series of moving averages of each window
moving_averages = windows.mean()
  
# Convert pandas series back to list
moving_averages_list = moving_averages.tolist()
  
print(moving_averages_list)

Output:

[1.0, 1.5, 2.0, 3.25, 4.4]

Exponential Moving Average

EMA is calculated by taking the weighted mean of the observations at a time. The weight of the observation exponentially decreases with time. It is used for analyzing recent changes.

Formulae:

EMAt =  αat + (1- α)EMAt-1

where:

  • EMAt = Exponential Moving Average at time t
  • α = degree of decrease in weight of observation with time
  • at = observation at time t

Python




# Program to calculate exponential
# moving average using formula
  
import numpy as np
  
arr = [1, 2, 3, 7, 9]
x=0.5  # smoothening factor
  
i = 1
# Initialize an empty list to
# store exponential moving averages
moving_averages = []
  
# Insert first exponential average in the list
moving_averages.append(arr[0])
  
# Loop through the array elements
while i < len(arr):
  
    # Calculate the exponential
    # average by using the formula
    window_average = round((x*arr[i])+
                           (1-x)*moving_averages[-1], 2)
      
    # Store the cumulative average
    # of current window in moving average list
    moving_averages.append(window_average)
      
    # Shift window to right by one position
    i += 1
  
print(moving_averages)

Output:

[1, 1.5, 2.25, 4.62, 6.81]

Method 1: Using Pandas

Pandas module of Python provides an easy way to calculate the exponential moving average of the series of observations. It provides a method called pandas.Series.ewm.mean() calculates the exponential moving average of given observations. pandas.Series.ewm() takes a parameter called smoothening factor i.e. degree with which weight of observation decrease with time. The value of a smoothening factor is always between 0 and 1.

Python




# Python program to
# calculate exponential moving averages
import pandas as pd
  
arr = [1, 2, 3, 7, 9]
  
# Convert array of integers to pandas series
numbers_series = pd.Series(arr)
  
# Get the moving averages of series
# of observations till the current time
moving_averages = round(numbers_series.ewm(
  alpha=0.5, adjust=False).mean(), 2)
  
# Convert pandas series back to list
moving_averages_list = moving_averages.tolist()
  
print(moving_averages_list)

Output:

[1.0, 1.5, 2.25, 4.62, 6.81]

Applications

  1. Time-Series Analysis: It is used to smooth out short-term variation and highlight long-term observations such as trends and cycles.
  2. Financial Analysis: It is used in financial analysis of stock markets like calculation of stock prices, returns, and analyzing trends of the market.
  3. Environmental Engineering: It is used in analyzing environmental conditions by considering various factors such as the concentration of pollutants, etc.
  4. Computer Performance Analysis: It is used in analyzing computer performance by calculating metrics such as average CPU utilization, average process queue length, etc.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!