# Moving Average/Simple Moving Average

## Simple moving average - discrete

In financial applications a simple moving average (SMA) is the unweighted mean of the previous n data. However, in science and engineering the mean is normally taken from an equal number of data on either side of a central value. This ensures that variations in the mean are aligned with the variations in the data rather than being shifted in time. An example of a simple equally weighted running mean for a n-day sample of closing price is the mean of the previous n days' closing prices.

${\displaystyle p_{0}(0)=p_{0}(-1)=\ldots =p_{0}(-(n-1))={\frac {1}{n}}}$

and ${\displaystyle p_{0}(x)=0}$ for ${\displaystyle x\notin \{-n+1,\dots ,-1,0\}}$ with ${\displaystyle V=\mathbb {Z} }$ as additive group.

Let ${\displaystyle C(t)}$ be the cost/price of product at time ${\displaystyle t\in V}$. If those prices are ${\displaystyle C(0),C(1),\dots ,C(97),C(98),C(99),C(100),C(101),\dots }$ and we want to create a simple moving average at day ${\displaystyle t=100}$ and looking back for time span of ${\displaystyle n=5}$ days then the formula is

{\displaystyle {\begin{aligned}SMA(100)&={\frac {1}{5}}\cdot C(100)+{\frac {1}{5}}\cdot C(99)+{\frac {1}{5}}\cdot C(98)+{\frac {1}{5}}\cdot C(97)+{\frac {1}{5}}\cdot C(96)\\&={\frac {1}{5}}\sum _{i=0}^{4}C(100-i)\\&=\sum _{i=0}^{n-1}p_{100}(100-i)\cdot C(100-i)\end{aligned}}}

When calculating successive values for other days/time ${\displaystyle t\in V=\mathbb {Z} }$, a new value comes into the sum and an old value drops out, meaning a full summation each time is unnecessary for this simple case,

{\displaystyle {\begin{aligned}SMA(101)&={\frac {1}{5}}\cdot C(101)+{\frac {1}{5}}\cdot C(100)+{\frac {1}{5}}\cdot C(99)+{\frac {1}{5}}\cdot C(98)+{\frac {1}{5}}\cdot C(97)\end{aligned}}}

${\displaystyle {\textit {SMA}}(t)={\frac {1}{n}}\sum _{i=0}^{n-1}C(t-i)=\sum _{i=0}^{n-1}p_{t}(t-i)\cdot C(t-i)=\sum _{i=0}^{n-1}p_{0}(-i)\cdot C(t-i)}$

The period selected depends on the type of movement of interest, such as short, intermediate, or long-term. In financial terms moving-average levels can be interpreted as support in a falling market, or resistance in a rising market. If you draw a graph for ${\displaystyle {\textit {SMA}}(t)}$ and cost function ${\displaystyle C(t)}$, you will identify, that the graph of ${\displaystyle {\textit {SMA}}}$ runs smoother in the time ${\displaystyle t\in V}$

If the data used are not centered around the mean, a simple moving average lags behind the latest datum point by half the sample width. An SMA can also be disproportionately influenced by old datum points dropping out or new data coming in. One characteristic of the SMA is that if the data have a periodic fluctuation, then applying an SMA of that period will eliminate that variation (the average always containing one complete cycle). But a perfectly regular cycle is rarely encountered.[1]

For a number of applications, it is advantageous to avoid the shifting induced by using only 'past' data. Hence a central moving average can be computed, using data equally spaced on either side of the point in the series where the mean is calculated.[2] This requires using an odd number of datum points in the sample window.

${\displaystyle p_{0}(-n)=p_{0}(-n+1)=\ldots =p_{0}(-1)=p_{0}(0)=p_{0}(1)=\dots =p_{0}(n-1)=p_{0}(n)={\frac {1}{2n+1}}}$

and ${\displaystyle p_{0}(x)=0}$ for ${\displaystyle x\notin \{-n,\dots ,-1,0,1,\dots ,n\}}$ with ${\displaystyle V=\mathbb {Z} }$ as additive group.

${\displaystyle {\textit {SMA}}(t)={\frac {1}{2n+1}}\sum _{i=-n}^{n}C(t+i)=\sum _{i=-n}^{n}p_{t}(t+i)\cdot C(t+i)=\sum _{i=-n}^{n}p_{0}(i)\cdot C(t+i)}$

A major drawback of the SMA is that it lets through a significant amount of the signal shorter than the window length. Worse, it actually inverts it. This can lead to unexpected artifacts, such as peaks in the smoothed result appearing where there were troughs in the data. It also leads to the result being less smooth than expected since some of the higher frequencies are not properly removed.

## Simple moving average - continuous

If we consider a continous measurement of value e.g. a force ${\displaystyle f(t)}$ at time ${\displaystyle t}$. The objective is to smooth the values ${\displaystyle f(t)}$ with a continous simple moving average. We look a time span ${\displaystyle s>0}$ in the past. As probability distribution we use a uniform distribution (mathematics) for the intervall ${\displaystyle [-n,0]}$. The density function is:

${\displaystyle p_{0}(x)={\begin{cases}{\frac {1}{n}}&\mathrm {for} \ -n\leq x\leq 0,\\[8pt]0&\mathrm {for} \ x0\end{cases}}}$ and ${\displaystyle p_{t}(x):=p_{0}(x-t)}$

Application on the moving average definition for continuous probability distriubtions we get:

${\displaystyle SMA(t):=\int _{\mathbb {R} }p_{t}(x)\cdot f(x)\,dx=\int _{t-n}^{t}p_{0}(x-t)\cdot f(x)\,dx={\frac {1}{n}}\int _{t-n}^{t}f(x)\,dx}$
1. Statistical Analysis, Ya-lun Chou, Holt International, 1975, ISBN 0-03-089422-0, section 17.9.
2. The derivation and properties of the simple central moving average are given in full at Savitzky–Golay filter