Exponential moving average doesn't seem to work properly
39 views (last 30 days)
Show older comments
Hi,
I have a question about the moving average function:
ma = movavg(Data,type,windowSize)
In the following example:
movavg([1;2;3;4;5;6;7;8],'exponential',3)
gives 7.0078 as the last moving average.
Removing the first two numbers:
movavg([3;4;5;6;7;8],'exponential',3)
gives 7.0313 as the last moving average.
I was expecting the answer to be identical between the two examples since I specified to look at only the current and preceding 3 data. The answer is identical for simple moving average. See below:
windowSize — Number of observations of the input series to include in moving average
positive integer
How do I calculate an exponential to only use exclusive the the data in the lookback window? Using a loop takes too long.
0 Comments
Answers (2)
Ashwin
on 18 Jun 2020
The general equation of the Exponential Moving Average is given as follows:
EMA = (Current value x Multiplier) + (Prev. EMA x (1-Multiplier))
Where Multiplier = (2/(windowsize+1))
Thus, when we apply the movavg function to the two set of values, we naturally see a discrepancy in final averages, because the first EMAs calculated are not the same.
In the first case, the first actual EMA value is calculated at the third value, ie, 3, thus making the EMA = 2.25 as compared to the 3 in the other distribution (because of the lack of previous values)
Thus, this is not a mistake of the function, and the same function can be used to calculate the moving averages that you require to.
0 Comments
Image Analyst
on 18 Jun 2020
If you want a filter that looks just at the numbers within the filter window ONLY, and not be affected by numbers prior to the window location, you can use nlfilter() in the Image Processing Toolbox. It slides a window along and gives you the window values which are passed to your function. Your function can be whatever you want. I'm attaching a demo for it for an image (not your function - sorry).
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!