Mathematica 9 is now available
Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Creating a Moving Average Function

  • To: mathgroup at
  • Subject: [mg51642] Re: [mg51615] Creating a Moving Average Function
  • From: Sseziwa Mukasa <mukasa at>
  • Date: Wed, 27 Oct 2004 23:42:55 -0400 (EDT)
  • References: <>
  • Sender: owner-wri-mathgroup at

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  

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:


Incidentally you want to use := not =.



  • Prev by Date: Re: listplot and plot of the same function display in different scale...
  • Next by Date: Re: listplot and plot of the same function display in different scale...
  • Previous by thread: Re: Creating a Moving Average Function
  • Next by thread: Prefix Notation