简体   繁体   中英

Different results statsmodels(python) vs. R autocorrelation

Question: why are R and python's statsmodels giving such different results in the ACF?

I tried to replicate an example from a Time Series Analysis book in python and not only I don't get the same smooth shape but I see only erratic behavior. I have checked if I was coding something wrong (eg the parameters) but I don't seem to find any solution. Any hints?

R source

This is the example from R that I'm trying to replicate, extractred from Time Series Analysis, Chan and Cryer 2008:

时间序列分析中的自相关函数,Chan 和 Cryer 2008

Python source

My attempt with 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()

The output:

代码的输出

Thanks to Josef , I found out my typo (see: here ). Now that it is changed, the plots look way more similar:

带校正的新图像

Due to the random nature of the processes, I was not expecting to get exactly the same results but qualitative similar. Now that the typo is solved, they do look similar.

Checking the code with someone after having worked long on the same thing is my lesson learnt here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM