In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import theano
import theano.tensor as T
In [38]:
import pandas.io.data as web
import datetime
start = datetime.datetime(2013, 1, 1)
end = datetime.datetime(2013, 1, 27)
f = web.DataReader("F", 'yahoo', start, end)
In [39]:
f
Out[39]:
In [3]:
f.Close.plot()
Out[3]:
In [4]:
f.Volume.plot()
Out[4]:
In [5]:
f.plot(kind='scatter', x='Close', y='Open')
Out[5]:
In [6]:
from pandas.tools.plotting import scatter_matrix
scatter_matrix(f, alpha=0.2, figsize=(6, 6), diagonal='kde');
In [7]:
f.describe()
Out[7]:
In [8]:
Xtr = f[['Open','Low','High']].values.astype(np.float32)
Xtr
Out[8]:
In [9]:
Ytr = f.Close.values.astype(np.float32)
Ytr
Out[9]:
In [10]:
rng = np.random
In [11]:
def define_fit_model(dataset_x, dataset_y, learning_rate = 1e-4, training_steps = 10000):
N = len(dataset_x[0])
x = T.matrix("x")
y = T.vector("y")
w = theano.shared(rng.randn(N), name="w")
b = theano.shared(0., name="b")
Hx = T.dot(x,w) + b
cost = T.mean(T.sqr(Hx - y))
gw, gb = T.grad(cost=cost, wrt=[w, b])
train = theano.function(inputs=[x,y],outputs=[Hx,cost], updates=((w, w - learning_rate * gw), (b, b - learning_rate * gb)))
predict = theano.function(inputs=[x], outputs=Hx)
for i in range(training_steps):
pred, err = train(dataset_x, dataset_y)
return predict, w, b
In [12]:
predict, w, b = define_fit_model(Xtr,Ytr)
In [13]:
"Model that was fit from data:\n {}, {}".format( w.get_value(), b.get_value() )
Out[13]:
In [23]:
Ypredict = predict(Xtr)
In [24]:
plt.plot(Ytr, label='real')
plt.plot(Ypredict, label='predict')
plt.legend();
In [34]:
start = datetime.datetime(2015, 5, 1)
end = datetime.datetime(2015, 5, 30)
f = web.DataReader("F", 'yahoo', start, end)
In [35]:
Xts = f[['Open','Low','High']].values.astype(np.float32)
Yts = f.Close.values.astype(np.float32)
In [36]:
Ytest_predict = predict(Xts)
In [37]:
plt.plot(Yts, label='test')
plt.plot(Ytest_predict, label='predict')
plt.legend();
In [ ]: