Importing libraries for statistical analysis and plotting
In [1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
In [2]:
eurusd = pd.read_csv("data/EURUSD_daily.csv", index_col='Date')
In [3]:
eurusd.index = pd.to_datetime(eurusd.index)
In [4]:
eurusd = eurusd['2007-01-01':'2008-05-01']
In [5]:
eurusd.head()
Out[5]:
In [6]:
eurusd.describe().transpose()
Out[6]:
In [7]:
eurusd.plot(figsize=(16,9))
Out[7]:
In [8]:
timeseries = eurusd['EUR/USD Close']
In [9]:
timeseries.rolling(12).mean()['2007-09-01':'2008-09-01'].plot(label='12 Month MA', figsize = (16,9))
#timeseries.rolling(12).std()['2007-09-01':'2008-09-01'].plot(label='12 Month rolling STD')
timeseries['2007-09-01':'2008-09-01'].plot()
plt.legend(loc='best')
Out[9]:
In [2]:
from statsmodels.tsa.stattools import adfuller
In [11]:
result = adfuller(timeseries)
In [12]:
def adf_test(time_series):
"""
Pass in a time series, returns ADF report
"""
result = adfuller(time_series)
print('Augmented Dickey-Fuller Test:')
labels = ['ADF Test Statistic','p-value','#Lags Used','Number of Observations Used']
for value,label in zip(result,labels):
print(label+' : '+str(value) )
if result[1] <= 0.05:
print("Data is stationary")
else:
print("Data is non-stationary ")
In [13]:
adf_test(timeseries)
In [14]:
eurusd['Fx rate first difference'] = eurusd['EUR/USD Close'] - eurusd['EUR/USD Close'].shift(1)
In [15]:
adf_test(eurusd['Fx rate first difference'].dropna())
In [16]:
eurusd['Fx rate first difference'].plot(figsize = (16,9))
Out[16]:
In [17]:
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
In [28]:
fig = plt.figure(figsize=(16,9))
ax = fig.add_axes([0,0,1,1])
first_diff_acf = plot_acf(eurusd['Fx rate first difference'].dropna(), ax=ax)
ax.set_xlim(-1,150)
Out[28]:
In [19]:
fig = plt.figure(figsize=(16,9))
ax = fig.add_axes([0,0,1,1])
first_diff_pacf = plot_pacf(eurusd['Fx rate first difference'].dropna(), lags=40, ax=ax)
In [20]:
from statsmodels.tsa.arima_model import ARIMA
In [21]:
model = ARIMA(timeseries,order=(5,1,0))
In [22]:
model_fit = model.fit()
In [23]:
print(model_fit.summary())
In [24]:
residuals = pd.DataFrame(model_fit.resid)
residuals.plot()
Out[24]:
In [25]:
residuals.describe().transpose()
Out[25]:
In [ ]:
In [ ]:
In [26]:
#fig = plt.figure(figsize=(16,9))
#ax = fig.add_axes([0,0,1,1])
#irst_diff_acf = plot_acf(eurusd['Fx rate first difference'].dropna(), ax=ax)
#ax.set_xlim(-1,150)
#x.set_ylim(-.1,.1)
In [32]:
eurusd['forecast'] = model_fit.predict(start = '2008-04-04', end = '2008-05-04', dynamic=True, typ='levels')
In [37]:
figFor = plt.figure(figsize = (16,9))
ax = figFor.add_axes([0,0,1,1])
ax.plot(eurusd['EUR/USD Close'])
ax.plot(eurusd['forecast'])
ax.set_xlim('2008-04-04', '2008-05-04')
ax.set_ylim(1.5,1.65)
Out[37]:
In [38]:
eurusd['forecast'].plot()
Out[38]:
In [ ]: