BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

LINQ
.NET 3.5+

A LINQ Style Simple Moving Average Operator

Raw data series with extreme peaks and troughs can be difficult to interpret. One way to remove sudden spikes is by applying a simple moving average calculation. This article describes a simple moving average extension method with a LINQ style.

Simple Moving Average

When you have a data series that includes lots of peaks and troughs, it can be tricky to understand the underlying trends. There are many ways in which such a series can be processed in order to remove extreme values, to allow for easier comprehension. One option is to calculate a simple moving average.

A simple moving average smooths numeric data series by calculating one average value for each datum in the original sequence. Each average is based upon the mean of the current point and a sample of the values that precede it. The sample size remains constant throughout the process, effectively moving along the data as the process progresses, hence the term, "moving average". The only time that the sample size differs is at the beginning of the process, when there are not enough data points to analyse.

For example, when calculating the moving average for a year with a three month sample, the first average is January's figure. The second average will be half of the total of the January and February numbers. The third is the mean of January to March, using the desired sample size. The fourth value would be the average of February to April, and so on.

Let's look at an example. The following data shows a year of sales figures for a fictitious company.

MonthSales
Jan4
Feb4
Mar4
Apr20
May5
Jun5
Jul0
Aug10
Sep5
Oct5
Nov4
Dec7

Examining the raw data makes it difficult to understand whether sales are rising, falling or stagnant. Visualising the data using a line chart, as shown below, can help.

Chart with raw data

Even with the chart it is difficult to see the underlying sales trend. However, if we apply the simple moving average calculation, we can remove the unusual peak of April and the rapid changes of July and August. The chart below shows the original data and the simple moving average with a sample size of five months. The averaged values make the trend much easier to spot. Unfortunately for our company, the sales figures do appear to be rather flat.

Chart with simple moving average

29 November 2013