关于autocorrelation_plot结果与autocorr结果的问题

时间:2019-01-03 07:15:44

标签: python pandas autocorrelation

我用autocorrelation_plot绘制了一条直线的自相关:

import numpy as np
import pandas as pd
from pandas.plotting import autocorrelation_plot
import matplotlib.pyplot as plt

dr = pd.date_range(start='1984-01-01', end='1984-12-31')

df = pd.DataFrame(np.arange(len(dr)), index=dr, columns=["Values"])
autocorrelation_plot(df)
plt.show()

enter image description here

然后,我尝试使用autocorr()来计算具有不同滞后的自相关:

for i in range(0,366):
    print(df['Values'].autocorr(lag=i))

对于所有滞后,输出均为1(或0.99)。但是从相关图可以清楚地看到,自相关是一条曲线,而不是固定为1的直线。

我是否正确解释了相关图,或者我使用了不正确的autocorr()函数?谢谢!

1 个答案:

答案 0 :(得分:1)

您正确使用了两个函数,但是... Autocorrelation_plot使用的计算自相关的方法与autocorr()使用的方法不同。

以下两篇文章详细说明了这些差异。不幸的是,我不知道哪种计算方法是正确的方法:

What's the difference between pandas ACF and statsmodel ACF?

Why NUMPY correlate and corrcoef return different values and how to "normalize" a correlate in "full" mode?

如果需要,可以按如下方式从自相关图中获得自相关:

ax = autocorrelation_plot(df)
ax.lines[5].get_data()[1]