简体   繁体   English

eviews和Python中的移动平均线

[英]Moving average in eviews and Python

I wanted to ask when doing moving average models in Time series trend analyze when we do moving average in eviews we do something like code below 我想问一下,在时间序列趋势分析中进行移动平均模型时,我们在eview中进行移动平均时,我们在下面执行类似代码的操作

moving average = @movavc(data, n)

However in python, we would do something like below: 但是在python中,我们将执行以下操作:

data["mov_avc"] = data.rolling(window=n).mean()

When doing simple moving average in eviews we lose first but also LAST few observations, in python we would only lose first observations. 在eview中进行简单移动平均时,我们首先丢失,但也丢失了最后几个观察值,在python中,我们只会丢失第一个观察值。

How is so? 怎么样了

If i got your question correctly, you want to understand why performing a moving average of window size n in python doesn't lose the last few points. 如果我正确地回答了您的问题,那么您想了解为什么在python中执行窗口大小n的移动平均值不会丢失最后几分。

Looking at the pandas.rolling() docs you see the note below: 查看pandas.rolling() 文档,您会看到以下注释:

By default, the result is set to the right edge of the window. 默认情况下,结果设置为窗口的右边缘。 This can be changed to the center of the window by setting center=True. 可以通过设置center = True将其更改为窗口的中心。

This means that the rolling window, by default, isn't centred on the value it is calculating the average for. 这意味着默认情况下,滚动窗口不以其计算平均值为中心。

Let's take a look at how this works with an example. 让我们来看一个例子。

We have a simple DataFrame: 我们有一个简单的DataFrame:

In [2]: ones_matrix = np.ones((5,1))
   ...: ones_matrix[:,0] = np.array([i+1 for i in range(ones_matrix.shape[0])])
   ...: index = [chr(ord('A')+i) for i in range(ones_matrix.shape[0])]
   ...: df = pd.DataFrame(data = ones_matrix,columns=['Value'],index=index)
   ...: df
Out[2]:
   Value
A    1.0
B    2.0
C    3.0
D    4.0
E    5.0

Now let's roll window with size 3 . 现在让我们滚动3号窗口。 (Notice that i explicitly wrote the argument center=False but that's the default value of calling df.rolling()) (请注意,我明确写了参数center = False,但这是调用df.rolling()的默认值)

In [3]: rolled_df = df.rolling(window=3,center=False).mean()
   ...: rolled_df
Out[3]:
   Value
A    NaN
B    NaN
C    2.0
D    3.0
E    4.0

The first two rows are NaN while the last points remain there. 前两行是NaN,而最后的点保留在那里。 If you notice for example at the row with index C it's value after rolling is 2 . 例如,如果您在索引为C的行上注意到,则滚动后的值为2 But before it was 3 . 但在3点之前。 This means that the new value for this index was the result of averaging the rows with indexes {A,B,C} whose values were respectively {1,2,3}. 这意味着该索引的新值是对索引分别为{1,2,3}的索引为{A,B,C}的行进行平均的结果。

Therefore you can see the window wasn't centred on the index C when calculating the average for that position, it was instead centred on the index B. 因此,在计算该位置的平均值时,您可以看到窗口不在索引C的中心,而是在索引B的中心。

You can change that by setting centered=True , thus outputing the expected behaviour: 您可以通过设置centered = True来更改它,从而输出预期的行为:

In [4]: centred_rolled_df = df.rolling(window=3,center=True).mean()
   ...: centred_rolled_df
Out[4]:
   Value
A    NaN
B    2.0
C    3.0
D    4.0
E    NaN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM