({{, (RefLink[TimeSeriesAggregate, paclet:ref/TimeSeriesAggregate])[tseries,dt] 
computes the mean value of tseries over non-overlapping windows of width dt.}, {, (RefLink[TimeSeriesAggregate, paclet:ref/TimeSeriesAggregate])[tseries,dt,f] 
applies the function f to the values of tseries in non-overlapping windows of width dt.}})

OpenersNotesSection(Spacer1)Details

(RefLink[TimeSeriesAggregate, paclet:ref/TimeSeriesAggregate]) is often used in time series analysis to compute aggregated statistics like yearly averages or monthly totals.

(RefLink[TimeSeriesAggregate, paclet:ref/TimeSeriesAggregate]) breaks the time series tseries into disjoint left-closed and right-open windows of equal width dt and applies a function f to the values in each segment.

If there are no values in a segment, the segment is ignored.

(RowDefault[, (Labeled[, 𝒻]), ])

The time series tseries can be a list of values {(Subscript[x, 1]),(Subscript[x, 2]),…}, a list of time-value pairs {{(Subscript[t, 1]),(Subscript[x, 1])},{(Subscript[t, 2]),(Subscript[x, 2])},…}, a (RefLink[TimeSeries, paclet:ref/TimeSeries]), (RefLink[EventSeries, paclet:ref/EventSeries]), or (RefLink[TemporalData, paclet:ref/TemporalData]).

The window width dt can be given as a positive number, a (RefLink[Quantity, paclet:ref/Quantity]), or as a date increment.

The window specification {dt,align} can be used to determine the alignment of new times within each window.

Settings for window alignment align include (RefLink[Left, paclet:ref/Left]), (RefLink[Center, paclet:ref/Center]) (default), and (RefLink[Right, paclet:ref/Right]).

(RefLink[TimeSeriesAggregate, paclet:ref/TimeSeriesAggregate]) threads pathwise for multipath (RefLink[TemporalData, paclet:ref/TemporalData]).

OpenersPrimaryExamplesSection(Spacer1)Examples  (26)

OpenersExampleSection0(Spacer1)Basic Examples  (3)

Average successive pairs of values:

TimeSeriesAggregate[{(Subscript[x, 1]),(Subscript[x, 2]),(Subscript[x, 3]),(Subscript[x, 4]),(Subscript[x, 5]),(Subscript[x, 6])},2]

Compute the quarterly standard deviation for a financial time series:

data=FinancialData["GOOGL",{(DateObject["Day: ""Sat 1 Jan 2005", DateObject[{2005,1,1},"Day","Gregorian",-6.`]]),(DateObject["Day: ""Tue 31 Dec 2013", DateObject[{2013,12,31},"Day","Gregorian",-6.`]])}];

tot=TimeSeriesAggregate[data,"Quarter",StandardDeviation];

DateListPlot[tot,GridLines->{True,None}]

Aggregate multiple paths simultaneously:

data=RandomFunction[BrownianBridgeProcess[{0,-1},{2,1}],{0,2,.01},3];

Average over blocks of width 0.15:

avg=TimeSeriesAggregate[data,.15];

Show[ListPlot[data],ListLinePlot[avg,PlotStyle->Thick]]

OpenersExampleSection1(Spacer1)Scope  (20)

OpenersExampleSubsection0(Spacer1)Basic Uses  (3)

Map a function f over data with blocks of width 2:

data=TimeSeries[{(Subscript[x, 1]),(Subscript[x, 2]),(Subscript[x, 3]),(Subscript[x, 4]),(Subscript[x, 5])},{1}];

TimeSeriesAggregate[data,2,f]["Path"]

Compute the 95(^th) quantile for windows of width 0.05:

data=RandomFunction[WienerProcess[],{0,1,.01}];

mq=TimeSeriesAggregate[data,0.05,Quantile[#,95/100]&];

ListLinePlot[{data,mq}]

Find a quartile envelope for blocks of size 10:

data=TimeSeries[Accumulate@RandomReal[{-1,1},250]];

q=TimeSeriesAggregate[data,10,Quartiles];

ListLinePlot[q]

OpenersExampleSubsection1(Spacer1)Data Types  (8)

Aggregate a vector in blocks of 4 using a function f:

v=RandomInteger[{-10,10},10]

TimeSeriesAggregate[v,4,f]

Find the two-year average for a list of time-value pairs:

data={{"2005",1},{"2006",-1},{"2007",3},{"2008",6},{"2009",-2},{"2010",4},{"2011",5},{"2012",4},{"2013",6}};

mavg=TimeSeriesAggregate[data,{2,"Year"}];

DateListPlot[{data,mavg},PlotTheme->"Detailed"]

Compute the maximum for width-10 segments of a (RefLink[TimeSeries, paclet:ref/TimeSeries]):

ts=TimeSeries[];

mmx=TimeSeriesAggregate[ts,10,Max];

ListLinePlot[{ts,mmx}]

Compute a width-10 median for (RefLink[TemporalData, paclet:ref/TemporalData]):

td=TemporalData[];

mmed=TimeSeriesAggregate[td,10,Median];

ListLinePlot[{td,mmed}]

Compute a width-5 total for an (RefLink[EventSeries, paclet:ref/EventSeries]):

es=EventSeries[];

tot=TimeSeriesAggregate[es,5,Total];

Show[ListLinePlot[tot,InterpolationOrder->0],ListPlot[es,Filling->Axis]]

Compute a six-month variance for multiple paths simultaneously:

td=TemporalData[{FinancialData["HP",All],FinancialData["IBM",All]}];

mvar=TimeSeriesAggregate[td,{6,"Month"},Variance];

DateListPlot[mvar,GridLines->{All,None},PlotRange->All,PlotLegends->{"HP","IBM"}]

Aggregate over vector-valued data:

α={{.2,.1},{-.3,.2}};
β={{.2,.5},{-.2,.6}};
Σ={{1,0},{0,.3}};
arima=RandomFunction[SARIMAProcess[{α},1,{},{6,{β},1,{}},Σ],{1,10^3}];

Componentwise width-5 range:

rng=TimeSeriesAggregate[arima,5,Map[Max[#]-Min[#]&,Transpose[#]]&];

Visualize the range:

ts=TimeSeries[rng,ResamplingMethod->Automatic];
fn[t_]:=Join[{t},ts["PathFunction"][t]]

ListPointPlot3D[Table[fn[t],{t,4,998}],BoxRatios->1,ColorFunction->Function[{t,x,y},(ColorData["BlueGreenYellow"][t])],AxesLabel->{"t","x","y"}]

Aggregate over a time series involving quantities:

ts=TimeSeries[Quantity[{0,16,9,3,7,2,17,10,6,12},"Meters"]];

res=TimeSeriesAggregate[ts,2];

ListLinePlot[{ts,res},PlotLegends->{"original time series","aggregated time series"},Filling->Axis,AxesLabel->Automatic]

OpenersExampleSubsection2(Spacer1)Window Widths  (5)

Use windows with width 2:

data=TimeSeries[{(Subscript[x, 1]),(Subscript[x, 2]),(Subscript[x, 3]),(Subscript[x, 4])},{1}];

TimeSeriesAggregate[data,2,f]["Path"]

Equivalently:

TimeSeriesAggregate[data,{2,Center},f]["Path"]

Specify the window width in calendar time:

data=FinancialData["APPLX","2010"];

w={5,"Day"};

Average over windows spanning five days:

mvar=TimeSeriesAggregate[data,w];

DateListPlot[mvar,GridLines->{All,None}]

Specify a window width using (RefLink[Quantity, paclet:ref/Quantity]):

data=FinancialData["APPLX","2010"];

w=(QuantityUnit[2]);

Average over windows spanning two quarters:

mvar=TimeSeriesAggregate[data,w];

DateListPlot[mvar,GridLines->{All,None}]

Specify a mixed radix width:

data=FinancialData["APPLX","2010"];

w={{3,"Week"},{2,"Day"}};

Average over windows spanning three weeks and two days:

mvar=TimeSeriesAggregate[data,w];

DateListPlot[mvar,GridLines->{All,None}]

Larger windows smooth more:

data=FinancialData["SP500","2000"];

avg[w_]:=TimeSeriesAggregate[data,w]

Moving averages with increasing window width:

wspec={{1,"Week"},{1,"Month"},{1,"Quarter"}};

Map[DateListPlot[avg[#],GridLines->{All,None},PlotLabel->Last[#]]&,wspec]

OpenersExampleSubsection3(Spacer1)Alignment  (4)

Align new times with the center of the windows:

data=TimeSeries[{(Subscript[x, 1]),(Subscript[x, 2]),(Subscript[x, 3]),(Subscript[x, 4]),(Subscript[x, 5]),(Subscript[x, 6]),(Subscript[x, 7])},{1}];

TimeSeriesAggregate[data,{3,Center},f]["Path"]

Center alignment is used by default:

TimeSeriesAggregate[data,3,f]["Path"]

Align new times with the right side of the windows:

data=TimeSeries[{(Subscript[x, 1]),(Subscript[x, 2]),(Subscript[x, 3]),(Subscript[x, 4]),(Subscript[x, 5]),(Subscript[x, 6]),(Subscript[x, 7])},{1}];

TimeSeriesAggregate[data,{3,Right},f]["Path"]

Align new times with the left side of the windows:

data=TimeSeries[{(Subscript[x, 1]),(Subscript[x, 2]),(Subscript[x, 3]),(Subscript[x, 4]),(Subscript[x, 5]),(Subscript[x, 6]),(Subscript[x, 7])},{1}];

TimeSeriesAggregate[data,{3,Left},f]["Path"]

Compare window alignments:

data=TimeSeries[Accumulate[RandomReal[{-1,1},100]]];

mavg=Table[TimeSeriesAggregate[data,{10,align}],{align,{Right,Center,Left}}];

The values are all equivalent:

Equal@@(#["PathStates"]&/@mavg)

The times are not:

Equal@@(#["PathTimes"]&/@mavg)

A visual comparison:

Show[ListPlot[data],ListLinePlot[mavg,PlotStyle->Thick,PlotLegends->{Right,Center,Left}]]

OpenersExampleSection2(Spacer1)Applications  (2)

OpenersExampleSubsubsection0(Spacer1)Market Volatility  (1)

Identify periods of high volatility in the S&P 500:

data=FinancialData["SP500",All];

Five-year standard deviation:

smooth=TimeSeriesAggregate[data,{5*365,"Day"},StandardDeviation];

DateListPlot[smooth,GridLines->{All,None},PlotStyle->Thick]

Annual interquartile range:

smooth=TimeSeriesAggregate[data,{365,"Day"},InterquartileRange];

DateListPlot[smooth,GridLines->{All,None},PlotStyle->Thick]

Two-year range:

smooth=TimeSeriesAggregate[data,{2*365,"Day"},Max[#]-Min[#]&];

DateListPlot[smooth,GridLines->{All,None},PlotStyle->Thick]

OpenersExampleSubsubsection1(Spacer1)Labor Force  (1)

Visualize the growth of labor force in California based on monthly data:

CAMonthlyLaborForce=EntityValue[Entity["AdministrativeDivision",{"California","UnitedStates"}],EntityProperty["AdministrativeDivision","CivilianLaborForce",{"Date"->All,"Frequency"->"Monthly","SeasonalAdjustment"->"NotSeasonallyAdjusted"}]]

Aggregate the data yearly:

growth=TimeSeriesAggregate[CAMonthlyLaborForce,{1,"Year"}]

DateListPlot[growth,GridLines->{True,None},FrameLabel->Automatic,PlotLabel->"Annual average labor force in California"]

OpenersExampleSection3(Spacer1)Properties & Relations  (1)

Independently of the alignment the windows are closed on the left and open on the right:

data=TimeSeries[{a,b,c}];
data["Path"]

left=TimeSeriesAggregate[data,{2,Left},Total];
left//Normal

It is possible that more time stamps are needed:

center=TimeSeriesAggregate[data,{2,Center},Total];
center//Normal

right=TimeSeriesAggregate[data,{2,Right},Total];
right//Normal

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[MovingMap, paclet:ref/MovingMap])  ▪  (RefLink[TimeSeriesResample, paclet:ref/TimeSeriesResample])  ▪  (RefLink[TimeSeriesThread, paclet:ref/TimeSeriesThread])  ▪  (RefLink[RegularlySampledQ, paclet:ref/RegularlySampledQ])  ▪  (RefLink[MinimumTimeIncrement, paclet:ref/MinimumTimeIncrement])  ▪  (RefLink[TimeSeriesShift, paclet:ref/TimeSeriesShift])  ▪  (RefLink[TimeSeriesRescale, paclet:ref/TimeSeriesRescale])  ▪  (RefLink[TimeSeriesMap, paclet:ref/TimeSeriesMap])  ▪  (RefLink[TimeSeriesMapThread, paclet:ref/TimeSeriesMapThread])  ▪  (RefLink[TimeSeriesInsert, paclet:ref/TimeSeriesInsert])  ▪  (RefLink[TimeSeriesWindow, paclet:ref/TimeSeriesWindow])  ▪  (RefLink[TimeSeriesModelFit, paclet:ref/TimeSeriesModelFit])  ▪  (RefLink[TemporalData, paclet:ref/TemporalData])  ▪  (RefLink[TimeSeries, paclet:ref/TimeSeries])  ▪  (RefLink[EventSeries, paclet:ref/EventSeries])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Time Series Processing, paclet:guide/TimeSeries])}, {▪(RefLinkPlain[Signal Processing, paclet:guide/SignalProcessing])}})}})