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

Réponses (2)

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 le 17 Sep 2012
Thank you.. I actually figured out like this
[Short, Long] = movavg(return_list, 6, 6);

3 commentaires

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.

Connectez-vous pour commenter.

Catégories

Question posée :

le 17 Sep 2012

Community Treasure Hunt

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

Start Hunting!

Translated by