Jump to content

Moving Average/Cumulative Moving Average

From Wikiversity


Cumulative moving average - discrete

[edit | edit source]

In a cumulative moving average, the data arrive in an ordered datum stream with tβˆˆβ„•0={0,1,2,3,…}, 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 x0,x1…,xt up to the current time t:

𝐢𝑀𝐴(t)=x0+x1+β‹―+xtt+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, xt becomes available, using the formula:

𝐢𝑀𝐴(t)=xt+t⋅𝐢𝑀𝐴(tβˆ’1)t+1

Thus the current cumulative average 𝐢𝑀𝐴(t) for a new datum point xt is equal to the previous cumulative average 𝐢𝑀𝐴(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

x0+x1+β‹―+xt=(t+1)⋅𝐢𝑀𝐴(t)

and similarly for t + 1, it is seen that

xt=(x0+x1+β‹―+xt)βˆ’(x0+x1+β‹―+xtβˆ’1)=(t+1)⋅𝐢𝑀𝐴(t)βˆ’t⋅𝐢𝑀𝐴(tβˆ’1)

Solving this equation for 𝐢𝑀𝐴(t) results in:

𝐢𝑀𝐴(t)=xt+t⋅𝐢𝑀𝐴(tβˆ’1)t+1

Cumulative moving average - continuous

[edit | edit source]

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:

pt,m(x)={1mfor tβˆ’m≀x≀t,0for x<tβˆ’m or x>t.

Application on the cumulative moving average definition in time t for continuous probability distriubtions we get:

SMAm(t):=βˆ«β„pt,m(x)β‹…f(x)dx=∫tβˆ’mtpt,m(x)β‹…f(x)dx=1m∫tβˆ’mtf(x)dx