Re: Creating a Moving Average Function
- To: mathgroup at smc.vnet.net
- Subject: [mg51642] Re: [mg51615] Creating a Moving Average Function
- From: Sseziwa Mukasa <mukasa at jeol.com>
- Date: Wed, 27 Oct 2004 23:42:55 -0400 (EDT)
- References: <200410270554.BAA24573@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On Oct 27, 2004, at 1:54 AM, Gregory Lypny wrote: > Hello Everyone, > > 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. I would think you'd want to pad your data on both ends by half the window size then compute the moving average over the original data. I don't know what function you are using to compute the Moving average but movingAverage[theSeries_, theWindowSize_] := Plus[##]/theWindowSize &@@@Partition[theSeries,theWindowSize,Quotient[theWindowSize, 2], {-Ceiling[theWindowSize/2], Ceiling[theWindowSize/2]}, 0] will pad the data with zeroes on both ends and compute the moving average. Of course if your data size is large and the window is small this expression will not be memory efficient. In that case if you have a memory efficient moving average routine just pad the data before computing the averages: MA[theSeries_,theWindowSize_]: =MovingAverage[PadRight[PadLeft[theSeries,Ceiling[theWindowSize/ 2],Ceiling[theWindowSize/2]],theWindowSize] Incidentally you want to use := not =. Regards, Ssezi
- References:
- Creating a Moving Average Function
- From: Gregory Lypny <gregory.lypny@videotron.ca>
- Creating a Moving Average Function