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.


 * $$p_0(0)=p_0(-1)=\ldots = p_0(-(n-1))= \frac{1}{n}$$

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

Let $$C(t)$$ be the cost/price of product at time $$t\in V$$. If those prices are $$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 $$t=100$$ and looking back for time span of $$n=5$$ days then the formula is


 * $$\begin{align}

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{align}$$

When calculating successive values for other days/time $$t \in V = \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,
 * $$\begin{align}

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{align}$$


 * $$\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 $$\textit{SMA}(t)$$ and cost function $$C(t)$$, you will identify, that the graph of $$\textit{SMA}$$ runs smoother in the time $$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.

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. This requires using an odd number of datum points in the sample window.


 * $$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 $$p_0(x)=0$$ for $$x \notin \{-n,\dots,-1,0,1,\dots,n\}$$ with $$V=\Z$$ as additive group.


 * $$\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 $$f(t)$$ at time $$t$$. The objective is to smooth the values $$f(t)$$ with a continous simple moving average. We look a time span $$s > 0$$ in the past. As probability distribution we use a uniform distribution (mathematics) for the intervall $$[-n,0]$$. The density function is:



p_0(x)=\begin{cases} \frac{1}{n} & \mathrm{for}\ -n \le x \le 0, \\[8pt] 0 & \mathrm{for}\ x0 \end{cases} $$ and $$ p_t(x):=p_0(x-t)$$ Application on the moving average definition for continuous probability distriubtions we get:
 * $$SMA(t):= \int_{\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 $$