Moving Average

Author

Eric W. Weisstein
October 21, 2004

This notebook downloaded from http://mathworld.wolfram.com/notebooks/Statistics/MovingAverage.nb.

For more information, see Eric's MathWorld entry http://mathworld.wolfram.com/MovingAverage.html.

©2005 Wolfram Research, Inc. except for portions noted otherwise

Initialization

<<Statistics`DataSmoothing`
<<Graphics`Colors`

Usage

?MovingAverage

Code

MovingAverage1[a_List,n_]:=Plus@@@Partition[a,n,1]/n
MovingAverage2[a_List,n_]:=ListCorrelate[Table[1,{n}],a]/n

Examples

Through[{MovingAverage,MovingAverage1,MovingAverage2}[{a,b,c,d,e},2]]//ColumnForm

Table[MovingAverage[Array[a,5],n],{n,5}]/.a[n_]:>Subscript[a,n]//ColumnForm

Sample Plot

l=With[{n=100},
Table[Sin[2Pi i/n]+(i-.5)/n+Random[],{i,n}]
];

colors={Red,Yellow,Green,Blue};
Show[{
ListPlot[l,PlotStyle->PointSize[.02],DisplayFunction->Identity],
Table[
ListPlot[MovingAverage[l,2n],DisplayFunction->Identity,PlotJoined->True,PlotStyle->colors[[n]]],
{n,4}]},DisplayFunction->$DisplayFunction]