1
$\begingroup$

Suppose there is a uniformly spaced time series ts:

ts = TimeSeries[{{{2015, 1}, a}, {{2015, 2}, b}, {{2015, 3}, 
c}, {{2015, 4}, d}, {{2015, 5}, e}}]

The task is to calculate a two-month moving average of this series.

However if we execute the following:

twomonthavg = MovingAverage[ts, {2, "Month"}]

It yields

%["Values"]

{1/3 (a + b + c), 1/3 (b + c + d), 1/3 (c + d + e)}

In general {n,"Month"} MovingAverage yields n+1 month moving average.

Is this the intended behavior?

Wolfram Desktop v11.2

$\endgroup$
0

2 Answers 2

3
$\begingroup$

MovingAverage is operating as expected.

Examine the dates in the TimeSeries object and you will see that each is on the first of the month at midnight.

ts["Dates"]

Mathematica graphics

You are requesting a 2 month average. Midnight 1-Jan to Midnight 1-Feb only spans one month (all of Jan) while midnight 1-Jan to midnight 1-Mar spans two months (all of Jan and all of Feb). There are three observations in the two month span so all three are included in the average.

It seems you want a two observation average. In that case MovingAverage[ts, 2] is what you should code.

Hope this helps.

$\endgroup$
1
  • $\begingroup$ yep, that helps a lot! $\endgroup$
    – iav
    Mar 16, 2018 at 16:50
2
$\begingroup$
twomonthavg = MovingAverage[ts, 2]
%["ValueList"]

works properly.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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