不同的结果 statsmodels(python) 与 R 自相关
Different results statsmodels(python) vs. R autocorrelation
问题:为什么 R 和 python 的统计模型在 ACF 中给出如此不同的结果?
我试图从 python 中的时间序列分析书中复制一个示例,不仅没有得到相同的平滑形状,而且我只看到不稳定的行为。我检查了我是否在编码错误(例如参数),但我似乎没有找到任何解决方案。有什么提示吗?
R 来源
这是我试图复制的 R 示例,摘自时间序列分析,Chan 和 Cryer 2008:
Python 来源
我对统计模型的尝试:
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()
输出:
感谢 Josef, I found out my typo (see: )。现在它已经改变了,情节看起来更相似了:
由于过程的随机性,我没想到会得到完全相同的结果,但质量相似。现在错字已经解决了,它们看起来确实很相似。
在同一件事上工作了很长时间后与某人检查代码是我在这里学到的教训。
问题:为什么 R 和 python 的统计模型在 ACF 中给出如此不同的结果?
我试图从 python 中的时间序列分析书中复制一个示例,不仅没有得到相同的平滑形状,而且我只看到不稳定的行为。我检查了我是否在编码错误(例如参数),但我似乎没有找到任何解决方案。有什么提示吗?
R 来源
这是我试图复制的 R 示例,摘自时间序列分析,Chan 和 Cryer 2008:
Python 来源
我对统计模型的尝试:
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()
输出:
感谢 Josef, I found out my typo (see:
由于过程的随机性,我没想到会得到完全相同的结果,但质量相似。现在错字已经解决了,它们看起来确实很相似。
在同一件事上工作了很长时间后与某人检查代码是我在这里学到的教训。