Let's start with a bit of theory.
In statistics, a moving average (rolling average or running average) is a calculation to analyze data points by creating series of averages of different subsets of the full data set. It is also called a moving mean (MM) or rolling mean and is a finite impulse response filter... Given a series of numbers and a fixed subset size, the moving average's first element is obtained by taking the average of the initial fixed subset of the number series. The subset is then modified by "shifting forward"; that is, excluding the first number of the series and including the next value in the subset. A moving average is commonly used with time-series data to smooth out short-term fluctuations and highlight longer-term trends or cycles.1
Moving average described above is also called one-sided moving average, and can be expressed using the following formula:
, where t changes from k+1 to n.
There is also two-sided moving average, which can be expressed as:
, where t changes from k+1 to n-k.
The difference is the placement of the moving average. For a one-sided moving average, the moving average is placed at the end of values being averaged. For a two-sided moving average, the moving average is centered at the middle of the values being averaged. Here is a little example for 3 periods moving average
|Values||One-sided 3MA||Two-sided 3MA|
Two-sided moving averages are used to smooth a time series and estimate or see the trend; one-sided moving averages can be used as a simple forecasting method.
As you can see, both types of moving averages use an odd number of periods. However, when working with time series, you often smooth to remove seasonality effects, which requires a period to be equal to the seasonal length, often an even number, such as 12 months or 4 quarters. Where to place the two-sided moving average value in case of an even smoothing period?
The solution is centered moving average. Idea is simple. Let's consider 4-period MA. At given time t we can calculate either or . In the first case, we can say that we have and in the second case - . Now we can smooth the smoothed values again, and get .
This is our centered moving average (CMA) aka 2*4 MA. Note that smoothing moving averages by another moving average, in general, is known as double moving average and CMA is the example of it (2*n MA).
The calculator below plots CMA for given time series and period (even value). If you want to smooth the edges, it simply adds first and last values to the calculation, as needed.