5
$\begingroup$

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.

$\endgroup$
3
$\begingroup$

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

$\endgroup$
0
$\begingroup$

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]}}

$\endgroup$
  • $\begingroup$ ... doesn't work for vector-valued time series. $\endgroup$ – kglr Oct 11 '17 at 1:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.