【问题标题】:Question about autocorrelation_plot result vs autocorr result关于 autocorrelation_plot 结果与 autocorr 结果的问题
【发布时间】:2019-05-29 18:45:50
【问题描述】:

我使用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()

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

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

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

我是否错误地解释了相关图,或者我是否错误地使用了 autocorr() 函数?谢谢!

【问题讨论】:

    标签: python pandas autocorrelation


    【解决方案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]
    

    【讨论】:

    • 感谢您的链接。据我所知,autocorr() 称为np.corrcoef(),它计算皮尔逊相关性,这与自动完成相关性不同。 autocorrelation_plot 的实现是正确的。我已提交an issue on github
    猜你喜欢
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多