In [2]:
import pandas_datareader.data as web
from pandas import Series,DataFrame
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import statsmodels.api as sm
from statsmodels.graphics.api import qqplot
import statsmodels.tsa.stattools as ts
import pywt
In [3]:
%matplotlib inline
In [4]:
import datetime
In [5]:
start = datetime.datetime(2015,11,1)
end = datetime.datetime(2016,12,15)
f = web.DataReader("F", "yahoo", start, end)
In [6]:
test = f[f.index < '2016-12-1']['Close']
In [50]:
fig = plt.figure(figsize=(8, 10))
ax = {}
for i in range(7):
ax[i+1] = fig.add_subplot('71'+str(i+1))
test.plot(ax=ax[5])
Data = f.drop(['Adj Close'], axis=1)
close = pywt.dwt(f['Close'], 'db4')
Close_db4=pd.Series(close[0])
Close_db4=Close_db4-14
Close_db4.index = pd.Index(sm.tsa.datetools.dates_from_range('2001','2145'))
Close_db4.plot(ax=ax[2])
Close=Close_db4.diff(4)
Close = Close[4:]
Close.plot(ax=ax[3])
sm.graphics.tsa.plot_acf(Close,lags=40,ax=ax[6]) #ARIMA,q
sm.graphics.tsa.plot_pacf(Close,lags=40,ax=ax[7]) #ARIMA,p
ts.adfuller(Close,4)
Arma = sm.tsa.ARMA(Close,order=(9,3)).fit(disp=-1, method='mle')
print(Arma.aic,Arma.bic,Arma.hqic)
Arma_stock=Arma.predict()
Arma_stock.plot(ax=ax[3])
predict_stock = Arma.predict('2122','2155',dynamic=True)
predict_stock.plot(ax=ax[3])
L=len(Arma_stock)
i=0
while i<L:
if(i<4):
Arma_stock[i]=Arma_stock[i]+Close_db4[i]
else:
Arma_stock[i] = Arma_stock[i]+Arma_stock[i-4]
i=i+1
Arma_stock.plot(ax=ax[4])
L=len(predict_stock)
i=0
while i<L:
if(i<4):
predict_stock[i] = predict_stock[i]+Arma_stock[-4+i]
else:
predict_stock[i] = predict_stock[i]+predict_stock[i-4]
i=i+1
predict_stock.plot(ax=ax[4])
Out[50]:
In [1]:
from __future__ import print_function
import numpy as np
from scipy import stats
import pandas as pd
import matplotlib.pyplot as plt
import statsmodels.api as sm
from statsmodels.graphics.api import qqplot
%matplotlib inline
In [2]:
print(sm.datasets.sunspots.NOTE)
In [28]:
dta = sm.datasets.sunspots.load_pandas().data
In [33]:
dta.index = pd.to_datetime(dta['YEAR'].astype(int), format='%Y')
In [34]:
dta = dta.drop('YEAR', axis=1)
dta.head()
Out[34]:
In [35]:
dta.plot()
Out[35]:
In [36]:
dta.diff(1).diff(1).plot()
Out[36]:
In [37]:
fig = plt.figure(figsize=(12,8))
ax1 = fig.add_subplot(211)
sm.graphics.tsa.plot_acf(dta.values, lags=40, ax=ax1)
ax2 = fig.add_subplot(212)
sm.graphics.tsa.plot_pacf(dta.values, lags=40, ax=ax2)
Out[37]:
In [40]:
arma_mod20 = sm.tsa.ARMA(dta, (2,0)).fit()
print(arma_mod20.params)
print(arma_mod20.aic, arma_mod20.bic, arma_mod20.hqic)
In [41]:
arma_mod30 = sm.tsa.ARMA(dta, (3,0)).fit()
print(arma_mod30.params)
print(arma_mod30.aic, arma_mod30.bic, arma_mod30.hqic)
In [42]:
sm.stats.durbin_watson(arma_mod30.resid.values)
Out[42]:
In [43]:
fig = plt.figure(figsize=(12,8))
arma_mod30.resid.plot();
In [70]:
resid = arma_mod30.resid
stats.normaltest( resid )
Out[70]:
In [71]:
qqplot(resid, line='q', fit=True)
Out[71]:
In [75]:
fig1 = plt.figure(figsize=(12,8))
ax11 = fig1.add_subplot(211)
sm.graphics.tsa.plot_acf(resid, lags=40, ax=ax11)
ax12 = fig1.add_subplot(212)
sm.graphics.tsa.plot_pacf(resid, lags=40, ax=ax12)
Out[75]:
In [76]:
r,q,p = sm.tsa.acf(resid.values.squeeze(), qstat=True)
data = np.c_[range(1,41), r[1:], q, p]
table = pd.DataFrame(data, columns=['lag', "AC", "Q", "Prob(>Q)"])
print(table.set_index('lag'))
In [86]:
predict_sunspots = arma_mod30.predict('1990-1-1', '2012-1-1', dynamic=True)
print(predict_sunspots)
In [91]:
fig, ax = plt.subplots(figsize=(12, 8))
ax = dta.loc['1950-1-1':].plot(ax=ax)
fig = arma_mod30.plot_predict('1990-1-1', '2012-1-1', dynamic=True, ax=ax, plot_insample=False)
In [92]:
def mean_forecast_err(y, yhat):
return y.sub(yhat).mean()
In [93]:
mean_forecast_err(dta.SUNACTIVITY, predict_sunspots)
Out[93]:
In [ ]:
sm.tsa.AR(dta).select_order(40, 'aic') #29
In [ ]:
sm.tsa.MA.