Last active
June 1, 2018 02:44
-
-
Save kevincdurand1/e8307dfb3e370ca15bdbb97300037c71 to your computer and use it in GitHub Desktop.
test_stationarity.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pandas import Series | |
from statsmodels.tsa.stattools import adfuller | |
def test_stationarity(timeseries, windowroll = 12, cutoff = 0.05): | |
#Determing rolling statistics | |
rolmean = pd.rolling_mean(timeseries, window=windowroll) | |
rolstd = pd.rolling_std(timeseries, window=windowroll) | |
#Plot rolling statistics: | |
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(block=False) | |
#Perform Dickey-Fuller test: | |
# H_0 in adfuller is unit root exists (Non-Stationary) | |
# We must observe significant p-value to convince ourselves tha the series is stationary | |
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']) | |
pvalue = dftest[1] | |
if pvalue <= cutoff: | |
print('p-value = '+ str(pvalue) + ' The series '+ timeseries.name + ' is likely stationary.') | |
#return True | |
else: | |
print('p-value = '+ str(pvalue) + ' The series '+ timeseries.name + ' is likely non-stationary.') | |
#return False | |
for key,value in dftest[4].items(): | |
dfoutput['Critical Value (%s)'%key] = value | |
print (dfoutput) | |
#%% | |
series = Series.from_csv('C:\\Users\\kdura\\Desktop\\eurusdclose.csv', header=0) | |
test_stationarity(series,12) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment