How to calculate a 6-month backward looking moving average matrix?
Show older comments
I have monthly data and would like to calculate a 6-month backward moving average. I'm looking at movavg function. I tried the following:
[Short, Long] = movavg(returns_list, 0, 6);
I got this error "Lead and lag arguments must be positive <= 588."
Should I use "[Short, Long] = movavg(returns_list, 1, 6);" instead?
Thanks, J
Answers (2)
Image Analyst
on 17 Sep 2012
I don't have whatever toolbox has movagv() in it, but I think you could use convolution:
filteredSignal = conv(signal, [0 0 0 0 0 1 1 1 1 1 1]/6, 'valid')
Remember, convolution flips the kernel so that's why you want to have the 1's on the right side of the array to get the average of the current and 5 prior values (assuming each element is one month).
Julio
on 17 Sep 2012
0 votes
3 Comments
Andrei Bobrov
on 17 Sep 2012
As by IA
filteredSignal = conv(signal, [0 0 0 0 0 1 1 1 1 1 1]/6, 'same')
Julio
on 17 Sep 2012
Image Analyst
on 17 Sep 2012
We don't know what your data are. Months have different numbers of days in them. If you have missing days or missing months, you should first run interp1 on it to fill them in. Conv() has options 'same', 'valid', and 'full' depending on how you want to handle the "edge effects" where your window reaches the beginning and end of your data where you don't have the full 6 months of data.
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!