Moving Average/Cumulative Moving Average

Cumulative moving average - discrete
In a cumulative moving average, the data arrive in an ordered datum stream with $$t \in \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 $$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 $$t$$. The starting point of data collection is $$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 $$x_0 ,x_1 \ldots, x_{t}$$ up to the current time $$t$$:


 * $$\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, $$x_{t}$$ becomes available, using the formula:


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

Thus the current cumulative average $$\textit{CMA}(t)$$ for a new datum point $$x_{t}$$ is equal to the previous cumulative average $$\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 ($$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


 * $$x_0 + x_1 + \cdots + x_t = (t+1) \cdot \textit{CMA}(t)$$

and similarly for t + 1, it is seen that



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

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



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

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



p_{t,m}(x)=\begin{cases} \frac{1}{m} & \mathrm{for}\ t-m \le x \le t, \\[8pt] 0 & \mathrm{for}\ xt \end{cases} $$. Application on the cumulative moving average definition in time $$t$$ for continuous probability distriubtions we get:
 * $$SMA_m(t):= \int_{\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 $$