Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*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 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


  • 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