问题
I need a little help with creating a loop in Python. Here's the code that I'm using:
import pandas as pd
import numpy as np
import math
import matplotlib.pyplot as plt
from statsmodels.graphics.tsaplots import plot_acf
from statsmodels.graphics.tsaplots import plot_pacf
%matplotlib inline
df= pd.read_csv('original_data.csv', index_col='1')
from statsmodels.tsa.stattools import adfuller
def adf_test(timeseries):
#Perform Dickey-Fuller test:
test_types = ['nc','c','ct']
for tests in test_types:
print ('\n Results of Dickey-Fuller Test type = {}:\n'.format(tests))
dftest = adfuller(timeseries,regression=tests,autolag='AIC')
dfoutput = pd.Series(dftest[0:4],index=['Test Statistic','p-value','#Lags used','#Observations used'])
# for key, value in dftest[4].items():
# dfoutput['Critical value (%s)'%key] = value
print(dfoutput)
p_value = dftest[1]
if p_value > 0.05:
print('Unit root!')
elif p_value < 0.05:
print('No unit root!')
adf_test(df['2']) #2 is the number of the column in the cvs file
n = 1
df['rates_difference'] = df['2']-df['2'].shift(n)
adf_test(df['rates_difference'].dropna())
n = 1
df['rates_difference2'] = df['rates_difference']-df['rates_difference'].shift(n)
adf_test(df['rates_difference2'].dropna())
The idea is that the code is checking if the data is stationary (p-value < 0.05) using 3 versions of ADF test. If it is not, then we have to do first difference and check again. If it is not stationary again, we have to do second difference and check p-value. So this process has to loop until the data is stationary.
Thanks in advance.
来源:https://stackoverflow.com/questions/60003381/how-to-create-a-loop-in-python-adf-test-with-p-value-check