In [31]:
import numpy as np
import sys
if "../" not in sys.path:
sys.path.append("../")
from tsap.solver import Solver
from tsap.model import AR, MA
from tsap.gradient_check import eval_numerical_gradient, eval_numerical_gradient_array
import tsap.data_processor as dp
import tsap.inference
from tsap.ts_gen import ar1_gen
import matplotlib.pyplot as plt
# This is a bit of magic to make matplotlib figures appear inline in the notebook
# rather than in a new window.
%matplotlib inline
plt.rcParams['figure.figsize'] = (5.0, 4.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
# Some more magic so that the notebook will reload external python modules;
# see http://stackoverflow.com/questions/1907993/autoreload-of-modules-in-ipython
%load_ext autoreload
%autoreload 2
data = np.loadtxt("../data/GOOG.csv", delimiter=',')
X = np.array([data[0:480]])
Y = dp.get_return(X)
nrow=X.shape[0]
plt.plot(Y[0,:])
plt.xlabel('time')
plt.ylabel('price')
plt.title(' 2015-2016 Google stock price')
plt.show()
In [114]:
lag = 5
sigma = 1.0
intercept = 0.1
phi = np.random.randn(lag,1)
AR_model = AR(lag=lag, phi=phi, sigma=sigma, intercept=intercept)
AR_model.params
AR_solver = Solver(AR_model, Y,
update_rule='sgd_momentum',
optim_config={
'learning_rate': 1e-6,
},
num_epochs=3000, batch_size=1,print_every=10)
AR_solver.train()
print AR_model.params
In [121]:
predreturn=AR_model.predict(Y,5)
print predreturn
truereturn=dp.get_return(np.array([data[479:485]]))
print truereturn
plt.plot(predreturn.T,label="predicted return")
plt.plot(truereturn.T,label="true return")
plt.xlabel("time")
plt.ylabel("price")
plt.title(' predicted and true true for 5 following days')
plt.legend()
plt.show()
predprice = dp.get_price(data[479],pred)
print predprice
trueprice=np.array([data[480:485]])
print trueprice
plt.plot(predprice.T,label="predicted price")
plt.plot(trueprice.T,label="true price")
plt.xlabel('time')
plt.ylabel('price')
plt.title(' predicted and true price for 5 following days')
plt.legend()
plt.show()
In [ ]:
In [ ]:
In [ ]: