Re: Creating a Moving Average Function
- To: mathgroup at smc.vnet.net
- Subject: [mg51643] Re: Creating a Moving Average Function
- From: Bill Rowe <readnewsciv at earthlink.net>
- Date: Wed, 27 Oct 2004 23:42:57 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
On 10/27/04 at 1:54 AM, gregory.lypny at videotron.ca (Gregory Lypny) wrote: >I'd like to create some custom moving average functions that retain >the same length as the original series by padding the lost >observations with zeros. I can get the moving average part of the >function to work but not the padding part. >This part without padding works: >MA[theSeries_, theWindowSize_] = MovingAverage[theSeries, >theWindowSize]; >But adding the padding causes it to fail: >MA[theSeries_, theWindowSize_] = PadLeft[MovingAverage[theSeries, >theWindowSize], 1000]; >Any suggestions would be most appreciated. Why not use the built in functions ListConvolve or ListCorrelate to do this? For example, data = Table[Random[], {100}]; ker = Table[1/15, {15}]; (* length of the kernel specifies the window size *) More generally, a kernel for a moving average with window size n can be created with Table[1/n, {n}] Length[ListConvolve[ker, data]] 86 The above uses the default settings to compute the moving average of the data with a window length of 15. By specifying the amount of overhang, i.e., Length[ListConvolve[ker, data, {-1, -1}]] 100 you achieve what you want, that is a result having the same length as the original data. With the way I used ListConvolve above, the data is assumed cyclic. That is values from the left are used to pad on the right. But I can specify a specific padding Length[ListConvolve[ker, data, {-1, -1}, 0]] 100 which pads at the right with 0 Contrast ListPlot[ListConvolve[ker, data, {-1, -1}]]; with ListPlot[ListConvolve[ker, data, {-1, -1}, 0]]; to see the effects of padding. -- To reply via email subtract one hundred and four