【发布时间】: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