Hello, readers! In this article, we will be focusing on **Weighted Moving Average Method in Python**, in detail.

So, let us begin!! ðŸ™‚

## What are Moving Averages?

Moving Average plays a very important role in the time series analysis within the Data Science domain. Within the time series analysis, moving average enables us to map or track the fluctuations keeping the higher trends within the data into consideration.

In Moving Average, we tend to calculate the average of different pieces of the dataset. That is, it calculates the overall average of the various subsets within the entire dataset. By this, we can understand the trend in the data with respect to different scenarios within the same set of data values being randomized altogether.

There are various types of Moving Averages such as:

**Simple Moving Average****Weighted Moving Average****Exponential Moving Average**, etc.

In the course of this topic, we will be focusing on Weighted Moving Average method in Python.

## Understanding weighted moving average in Python

In the weighted moving average method, we make use of weights to have the information about the fluctuations in the data values.

Here, it gives a larger/greater weight(value) to a data point that is most recent in the queue and a smaller data value to a point which is less frequent or at a distant in the past data values.

In order to calculate the Weighted Moving Average (WMA), we multiply every data point with their corresponding weights and finally calculate the summation of the results.

For example, let us try to calculate the WMA for the 2 closest rates of shares on a daily graph. The prices being 100rs and 90rs. Here 100rs is the latest rate.

So, the higher weight will be assigned to 100rs i.e. 2 while 90rs will have 1 as the assigned weight to it (considering it as an example).

So, to calculate the Weighted Moving Average Method, we multiply the rates with the weights and then divide by the sum of weights as shown below–

[(100*2)+(90*1)]/3 = 96.66666667.

## Implementation of Weighted moving average in Python

In Python, we are provided with a built-in NumPy package that has various in-built methods which can be used, to sum up, the entire method for WMA, that can work on any kind of Time series data to fetch and calculate the Weighted Moving Average Method.

- We make use of numpy.arange() method to generate a weighted matrix.
- We perform the multiplication of the weighted data with the Data points.
- Further, WMA is calculated by dividing the multiplied and summation value by the sum of the weights.

**Example: Calculation Weighted Moving Average in Python**

```
def weightedmovingaverage(Data, period):
weighted = []
for i in range(len(Data)):
try:
total = numpy.arange(1, period + 1, 1)
matrix = Data[i - period + 1: i + 1, 3:4]
matrix = numpy.ndarray.flatten(matrix)
matrix = total * matrix
wma = (matrix.sum()) / (total.sum()) # WMA
weighted = numpy.append(weighted, wma)
except ValueError:
pass
return weighted
```

## Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.

For more such posts related to Python programming, Stay tuned with us!

Till then, Happy Learning!! ðŸ™‚