# Moving Average/Cumulative Moving Average

## Cumulative moving average - discrete

In a cumulative moving average, the data arrive in an ordered datum stream with ${\displaystyle t\in \mathbb {N} _{0}=\{0,1,2,3,\dots \}}$, and the user would like to get the average of all of the data up until the current datum point ${\displaystyle t}$. For example, an investor may want the average price of all of the stock transactions for a particular stock up until the current time ${\displaystyle t}$. The starting point of data collection is ${\displaystyle t=0}$. As each new transaction occurs, the average price at the time of the transaction can be calculated for all of the transactions up to that point using the cumulative average, typically an equally weighted average of the sequence of t+1 values ${\displaystyle x_{0},x_{1}\ldots ,x_{t}}$ up to the current time ${\displaystyle t}$:

${\displaystyle {\textit {CMA}}(t)={\frac {x_{0}+x_{1}+\cdots +x_{t}}{t+1}}\,.}$

The brute-force method to calculate this would be to store all of the data and calculate the sum and divide by the number of datum points every time a new datum point arrived. However, it is possible to simply update cumulative average as a new value, ${\displaystyle x_{t}}$ becomes available, using the formula:

${\displaystyle {\textit {CMA}}(t)={\frac {x_{t}+t\cdot {\textit {CMA}}(t-1)}{t+1}}}$

Thus the current cumulative average ${\displaystyle {\textit {CMA}}(t)}$ for a new datum point ${\displaystyle x_{t}}$ is equal to the previous cumulative average ${\displaystyle {\textit {CMA}}(t-1)}$ at time t-1, times t, plus the latest datum point, all divided by the number of points received so far, n+1. When all of the datum points arrive (${\displaystyle n=N}$), then the cumulative average will equal the final average. It is also possible to store a running total of the datum point as well as the number of points and dividing the total by the number of datum points to get the CMA each time a new datum point arrives.

The derivation of the cumulative average formula is straightforward. Using

${\displaystyle x_{0}+x_{1}+\cdots +x_{t}=(t+1)\cdot {\textit {CMA}}(t)}$

and similarly for t + 1, it is seen that

{\displaystyle {\begin{aligned}x_{t}&=(x_{0}+x_{1}+\cdots +x_{t})-(x_{0}+x_{1}+\cdots +x_{t-1})\\[6pt]&=(t+1)\cdot {\textit {CMA}}(t)-t\cdot {\textit {CMA}}(t-1)\end{aligned}}}

Solving this equation for ${\displaystyle {\textit {CMA}}(t)}$ results in:

{\displaystyle {\begin{aligned}{\textit {CMA}}(t)&={\frac {x_{t}+t\cdot {\textit {CMA}}(t-1)}{t+1}}\end{aligned}}}

## Cumulative moving average - continuous

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

${\displaystyle p_{t,m}(x)={\begin{cases}{\frac {1}{m}}&\mathrm {for} \ t-m\leq x\leq t,\\[8pt]0&\mathrm {for} \ xt\end{cases}}}$.

Application on the cumulative moving average definition in time ${\displaystyle t}$ for continuous probability distriubtions we get:

${\displaystyle SMA_{m}(t):=\int _{\mathbb {R} }p_{t,m}(x)\cdot f(x)\,dx=\int _{t-m}^{t}p_{t,m}(x)\cdot f(x)\,dx={\frac {1}{m}}\int _{t-m}^{t}f(x)\,dx}$