How to calculate a 6-month backward looking moving average matrix?

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

답변 (2개)

Image Analyst
Image Analyst 2012년 9월 17일
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
Julio 2012년 9월 17일

0 개 추천

Thank you.. I actually figured out like this
[Short, Long] = movavg(return_list, 6, 6);

댓글 수: 3

As by IA
filteredSignal = conv(signal, [0 0 0 0 0 1 1 1 1 1 1]/6, 'same')
Will this work if I have missing data? Especially, since it will try to average always by 6...
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

질문:

2012년 9월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by