Consider the following non-equally spaced TimeSeries
:
ts = TimeSeries[
Prepend[ConstantArray[1, 99],
0], {Prepend[(100 + Range[99])/100, 0]}];
ListLinePlot[ts, PlotMarkers -> {Automatic, 12}]
Visually, geometry suggests the Mean
ought to be roughly 3/4. However,
Mean[ts]
(*99/100*)
This is the same as Mean[ts["Values"]]
. In other words, Mean
is computed using the underlying values, assuming they're equally spaced... even if they're not.
In contrast, Mean@TimeSeriesResample@ts
returns $149/200\approx 3/4$. By resampling with equal spacing, which roughly corresponds to applying Mean
to the underlying continuous interpolating function, we get the expected result.
As a result, my initial hypothesis of how functions are applied to TimeSeries
was f_[t_TimeSeries]:=f_[t["Values"]]
.
Now consider Differences
, which provides a counterexample:
Differences[ts]["Values"][[1 ;; 3]]
(*{1/101, 1/101, 0}*)
Differences[ts["Values"]][[1 ;; 3]]
(*{1, 0, 0}*)
Unlike Mean
, it appears Differences
of a TimeSeries
somehow takes into account the spacing of the datapoints.
So my question is:
- Is there a consistent rule for how functions (such as
Mean
orDifferences
) are computed forTimeSeries
with unequally-spaced data?
Personally, my intuition is that applying a function to a TimeSeries
ought to roughly correspond to applying that function to the underlying InterpolatingFunction
of the data. However, that's debatable, and Mean
clearly wasn't implemented that way.
This has caused some unexpected results while I was working with unequally-spaced TimeSeries
.