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


{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


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.


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.

  • $\begingroup$ yep, that helps a lot! $\endgroup$
    – iav
    Mar 16 '18 at 16:50
twomonthavg = MovingAverage[ts, 2]

works properly.


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.