不同结果statsmodels(python)与R自相关

问题描述 投票:0回答:1

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

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

R来源

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

Autocorrelation functions from Time Series Analysis, 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()

输出:

Output of the code

python r statistics time-series statsmodels
1个回答
0
投票

由于Josef,我发现了我的错字(请参阅:here)。现在,它已更改,绘图看起来更相似:

New image with correction

由于过程的随机性,我不希望得到完全相同的结果,但定性相似。现在解决了错别字,它们看起来确实很相似。

[我在这里学到的教训很长一段时间后,与某人一起检查代码。

© www.soinside.com 2019 - 2024. All rights reserved.