【问题标题】:Different results statsmodels(python) vs. R autocorrelation不同的结果 statsmodels(python) 与 R 自相关
【发布时间】:2020-08-30 16:23:33
【问题描述】:

问题:为什么 R 和 python 的 statsmodels 在 ACF 中给出如此不同的结果?

我试图在 python 中复制时间序列分析书中的一个示例,但我不仅没有得到相同的平滑形状,而且只看到了不稳定的行为。我检查了我是否编码错误(例如参数),但我似乎没有找到任何解决方案。有什么提示吗?

R 源码

这是我试图复制的 R 示例,摘自时间序列分析,Chan and Cryer 2008:

Python 源码

我对 statsmodels 的尝试:

import matplotlib.pyplot as plt
import statsmodels.api as sm

from statsmodels.graphics import tsaplots

fig, ax = plt.subplots(2,2, figsize=(10, 10), sharey=True, sharex=True)
for n, i in enumerate([[0.5,0.25], [1, -0.25], [1.5, -0.75], [1, -0.6]]):
    y = sm.tsa.arma_generate_sample(ar=[1]+[-j in i for j in i], ma=[1, 0], nsample=100)
    tsaplots.plot_acf(y, ax[n//2][n%2], lags=20, fft=True)#lags=len(y)//2)
    if n//2: ax[n//2][n%2].set_xlabel('Lag [t]')
    if n in [0,2]: ax[n//2][n%2].set_ylabel(r'Correlation [$\rho$]')
    ax[n//2][n%2].legend(['AR(2)={}'.format(i)])
plt.show()

输出:

【问题讨论】:

  • 你检查过ar=[1]+[-j in i for j in i] 做了什么吗?
  • 哦,谢谢你指出那个错字!它应该是ar=[1]+[-j for j in i]。重点是改变值的符号,因为 statsmodels 要求 AR 系数与您期望的符号相反。
  • statsmodels 有一个 ArmaProcess 类,它应该具有 ARMA 的理论 acf

标签: python r statistics time-series statsmodels


【解决方案1】:

感谢Josef,我发现了我的错字(请参阅:here)。现在它被改变了,情节看起来更相似了:

由于过程的随机性,我没想到会得到完全相同的结果,但质量相似。现在错字已解决,它们看起来确实相似。

在同一件事上工作了很长时间后与某人检查代码是我在这里学到的教训。

【讨论】:

    猜你喜欢
    • 2014-01-09
    • 2020-04-02
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    • 2019-08-28
    • 2021-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多