For even numbered periods the centered moving average is calculated incorrectly. Suppose the period length is 5. Then the center of 5 periods is 3. However if the period length is 4 then the center of the period is 2.5. The value at index 3 should be the average of the values at 2.5 and 3.5. Pandas is showing the 2.5 value at 3 which is incorrect.
EXAMPLE: a=pd.Series(range(1,6), index=range(1,6)) a.rolling(4, center=True).mean()
1 NaN 2 NaN 3 2.5 4 3.5 5 NaN
Comment From: jreback
center is a very funny beast. The computation is done then the results are shifted. To be honest we should just remove this.
This has been this way for quite a long time.
Comment From: jreback
actually this is a duplicate of #2953. has been open a long time.
Comment From: simonm3
This is not the same issue. The one you refer to is apparently affecting only the edge values. I am saying all values are shifted half an index so wrong.
Also it is easy to fix. It is just an test for length even and then averaging each consecutive pair. And the suggestion it might be slower is bizarre. It is slower to calculate 2+2 =4 than to just set it to 1 but that would be incorrect.
On 14 Oct 2016 9:46 p.m., "Jeff Reback" notifications@github.com wrote:
Closed #14425.
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.
Comment From: simonm3
And SPSS, sas, and minitab all calculate centred moving averages correctly.
On 14 Oct 2016 11:25 p.m., "simon mackenzie" simonm3@gmail.com wrote:
This is not the same issue. The one you refer to is apparently affecting only the edge values. I am saying all values are shifted half an index so wrong.
Also it is easy to fix. It is just an test for length even and then averaging each consecutive pair. And the suggestion it might be slower is bizarre. It is slower to calculate 2+2 =4 than to just set it to 1 but that would be incorrect.
On 14 Oct 2016 9:46 p.m., "Jeff Reback" notifications@github.com wrote:
Closed #14425.
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.
Comment From: chris-b1
@simonm3 - there's a lot going on in that issue, but changing the centered mean was part of it - this reference, which I think is the approach you expect? http://www.itl.nist.gov/div898/handbook/pmc/section4/pmc422.htm
I'm guessing centered mean isn't used that often, but if there is a standard approach for double smoothing an even window, and you would like to submit a PR to support that, I think that would be great.
Comment From: chris-b1
I guess you can also fairly easily solve it like this?
s.rolling(4, center=True).mean().rolling(2).mean()
Comment From: pkch
It would have to be:
s.rolling(4, center=True).mean().rolling(2).mean().shift(-1)
The current calculation of s.rolling(k, center=True)
for an even k
is clearly wrong, since it's not centered.
Also, IMHO, the issue linked to this as a duplicate really is not about even-sized centered windows, it's about some more complex issues.
Given how easy it is to fix this, perhaps it's worth reopening it, so someone can fix it with a small PR?