Expanding window map for time series

5

2

Given a TimeSeries object ts, how do I implement ExpandingMap similar to MovingMap, where the window is not moving but expanding. In pseudocode,

tsNew[tf] = f[TimeSeriesWindow[ts,{t0,tf}]],

where t0=Min[ts["FirstTimes"]].

For example, the function Accumulate does exactly this with f=Total. I would like to have this functionality for an arbitrary function f.

freevillage

Posted 2017-10-10T23:07:59.680

Reputation: 147

Answers

3

You can obtain this behavior from MovingMap by using the padding parameter.

Here's some random data:

data = RandomFunction[WienerProcess[.3, .5], {0, 1, 0.01}, 2];
ListLinePlot[data]

enter image description here

As an example I compute the rolling Max. The final 2 arguments set the window size to be the length of the whole time series, and instruct the function to use no padding, i.e. to compute the function over incomplete intervals. By default it is right-aligned.

MovingMap[Max, data, data["PathLength"], None]
ListLinePlot[%]

enter image description here

mfvonh

Posted 2017-10-10T23:07:59.680

Reputation: 8 252

0

It should be easier/cleaner than

ClearAll[tsNew]
tsNew[s_, tf_, foo_] := Module[{tsw = TimeSeriesWindow[s, {s["FirstTime"], tf}], 
 bar, tsX, rules}, 
 tsX = TimeSeries[Array[x, {Length@tsw["Values"]}], Automatic]; 
 rules = Dispatch[Thread[tsX["Values"] -> tsw["Values"]]]; 
 Accumulate[TimeSeriesMap[bar, tsX]] /. Plus -> foo /. 
   foo[x__] :> foo[## & @@ ({x} /. bar -> Identity)] /. bar -> foo /. rules]

Example:

v = {2, 1, 6, 5, 7, 4};
t = {1, 2, 5, 10, 12, 15};
ts = TimeSeries[v, {t}];

ts["Path"]

{{1, 2}, {2, 1}, {5, 6}, {10, 5}, {12, 7}, {15, 4}}

tsNew[ts, 10, f]["Path"]

{{0, f[2]}, {1, f[2, 1]}, {2, f[2, 1, 6]}, {3, f[2, 1, 6, 5]}}

tsNew[ts, 20, f]["Path"]

{{0, f[2]}, {1, f[2, 1]}, {2, f[2, 1, 6]}, {3, f[2, 1, 6, 5]},
{4, f[2, 1, 6, 5, 7]}, {5, f[2, 1, 6, 5, 7, 4]}}

kglr

Posted 2017-10-10T23:07:59.680

Reputation: 302 076

... doesn't work for vector-valued time series. – kglr – 2017-10-11T01:28:00.123