Skip to content

Instantly share code, notes, and snippets.

@nmolivo
Created March 20, 2018 20:34
Show Gist options
  • Save nmolivo/15c2753f38b6d6ac94862d888a527243 to your computer and use it in GitHub Desktop.
Save nmolivo/15c2753f38b6d6ac94862d888a527243 to your computer and use it in GitHub Desktop.
adfuller-test
from statsmodels.tsa.stattools import adfuller
def test_stationarity(timeseries):
#Determing rolling statistics
rolmean = pd.rolling_mean(timeseries, window=96)
rolstd = pd.rolling_std(timeseries, window=96)
#I use 96 as my window because that is how many of my observations are contained in one 24-hr-period
#Plot rolling statistics:
fig = plt.figure(figsize=(12, 8))
orig = plt.plot(timeseries, color='blue',label='Original')
mean = plt.plot(rolmean, color='red', label='Rolling Mean')
std = plt.plot(rolstd, color='black', label = 'Rolling Std')
plt.legend(loc='best')
plt.title('Rolling Mean & Standard Deviation')
plt.show()
#Perform Dickey-Fuller test:
print('Results of Dickey-Fuller Test:')
dftest = adfuller(timeseries, autolag='AIC')
dfoutput = pd.Series(dftest[0:4], index=['Test Statistic','p-value','#Lags Used','Number of Observations Used'])
for key,value in list(dftest[4].items()):
dfoutput['Critical Value (%s)'%key] = value
print(dfoutput)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment