Exponential moving average doesn't seem to work properly

43 ビュー (過去 30 日間)
Azura Hashim
Azura Hashim 2020 年 3 月 29 日
回答済み: Image Analyst 2020 年 6 月 18 日
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.

回答 (2 件)

Ashwin
Ashwin 2020 年 6 月 18 日
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.

Image Analyst
Image Analyst 2020 年 6 月 18 日
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).

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by