Continue work on project 3.

This commit is contained in:
2020-09-22 17:01:07 -04:00
parent 9697add7a6
commit f823029a50
3 changed files with 53 additions and 21 deletions

View File

@@ -4,7 +4,8 @@ This is my solution to the ML4T course exercises. The main page for the course
is [here](http://quantsoftware.gatech.edu/Machine_Learning_for_Trading_Course). is [here](http://quantsoftware.gatech.edu/Machine_Learning_for_Trading_Course).
The page contains a link to the The page contains a link to the
[assignments](http://quantsoftware.gatech.edu/CS7646_Spring_2020#Projects:_73.25). [assignments](http://quantsoftware.gatech.edu/CS7646_Spring_2020#Projects:_73.25).
There are eight projects in total. There are eight projects in total. The summer 2020 page is
[here](http://lucylabs.gatech.edu/ml4t/summer2020/).
To set up the environment I have installed the following packages on my Linux To set up the environment I have installed the following packages on my Linux
Manjaro based system. Manjaro based system.
@@ -15,18 +16,16 @@ sudo pacman -S python-pandas --asdeps python-pandas-datareader python-numexpr \
python-numpy python-numpy
``` ```
I am also using the wonderful I am also using [mplfinance](https://github.com/matplotlib/mplfinance) to plot
[mplfinance](https://github.com/matplotlib/mplfinance). You can install candlestick-charts. You can install mplfinance via pip and find the tutorial
mplfinance via pip and find the tutorial
[here](https://github.com/matplotlib/mplfinance#tutorials). [here](https://github.com/matplotlib/mplfinance#tutorials).
``` ```
pip install mplfinance --user pip install mplfinance --user
``` ```
Use unzip with the `-n` flag to extract the archives for the different I have included the archived version of the exercise. To extract them run the
exercises. This makes sure that you do not override any of the existing files. I following command. The `-n` flag makes unzip never overwrite existing files.
might add a makefile to automize this later.
``` ```
unzip -n zips/*.zip -d ./ unzip -n zips/*.zip -d ./

View File

@@ -1,28 +1,51 @@
import numpy as np import numpy as np
class DTLearner(object): class DTLearner(object):
LEAF = -1
NA = -1
def __init__(self, leaf_size = 1, verbose = False): def __init__(self, leaf_size = 1, verbose = False):
pass # move along, these aren't the drones you're looking for self.leaf_size = leaf_size
self.verbose = verbose
def author(self): def author(self):
return 'felixm' # replace tb34 with your Georgia Tech username return 'felixm' # replace tb34 with your Georgia Tech username
def addEvidence(self, dataX, dataY): def create_node(self, factor: int, split: int, left: int, right: int):
return np.array((factor, split, left, right))
def build_tree(self, xs, y):
assert(xs.shape[0] == y.shape[0])
assert(xs.shape[0] > 0) # If this is 0 something went wrong.
if xs.shape[0] == 1:
return self.create_node(self.LEAF, y[0], self.NA, self.NA)
if np.all(y[0] == y):
return self.create_node(self.LEAV, y[0], self.NA, self.NA)
# XXX: continue here
y = np.array([y])
correlations = np.corrcoef(xs, y, rowvar=True)
print(f"{correlations=}")
return 0
def addEvidence(self, data_x, data_y):
""" """
@summary: Add training data to learner @summary: Add training data to learner
@param dataX: X values of data to add @param dataX: X values of data to add
@param dataY: the Y training values @param dataY: the Y training values
""" """
if self.verbose:
print(data_x)
print(data_y)
self.tree = self.build_tree(data_x, data_y)
# slap on 1s column so linear regression finds a constant term
newdataX = np.ones([dataX.shape[0], dataX.shape[1]+1])
newdataX[:,0:dataX.shape[1]] = dataX
# build and save the model
self.model_coefs, residuals, rank, s = np.linalg.lstsq(newdataX,
dataY,
rcond=None)
def query(self,points): def query(self,points):
""" """
@@ -30,7 +53,8 @@ class DTLearner(object):
@param points: should be a numpy array with each row corresponding to a specific query. @param points: should be a numpy array with each row corresponding to a specific query.
@returns the estimated values according to the saved model. @returns the estimated values according to the saved model.
""" """
return (self.model_coefs[:-1] * points).sum(axis = 1) + self.model_coefs[-1] return
# return (self.model_coefs[:-1] * points).sum(axis = 1) + self.model_coefs[-1]
if __name__=="__main__": if __name__=="__main__":
print("the secret clue is 'zzyzx'") print("the secret clue is 'zzyzx'")

View File

@@ -33,8 +33,14 @@ if __name__=="__main__":
print("Usage: python testlearner.py <filename>") print("Usage: python testlearner.py <filename>")
sys.exit(1) sys.exit(1)
inf = open(sys.argv[1]) inf = open(sys.argv[1])
# data = np.array([list(map(float,s.strip().split(',')[1:]))
# for s in inf.readlines()[1:]])
data = np.array([list(map(float,s.strip().split(',')[1:])) data = np.array([list(map(float,s.strip().split(',')[1:]))
for s in inf.readlines()[1:]]) for s in inf.readlines()])
# XXX: Get rid of some rows and columns for easier development.
# XXX: Remove later for testing!
# data = data[:10,5:]
# compute how much of the data is training and testing # compute how much of the data is training and testing
train_rows = int(0.6* data.shape[0]) train_rows = int(0.6* data.shape[0])
@@ -46,15 +52,18 @@ if __name__=="__main__":
testX = data[train_rows:,0:-1] testX = data[train_rows:,0:-1]
testY = data[train_rows:,-1] testY = data[train_rows:,-1]
print(f"{testX.shape}") # print(f"{testX.shape}")
print(f"{testY.shape}") # print(f"{testY.shape}")
# create a learner and train it # create a learner and train it
# learner = lrl.LinRegLearner(verbose = True) # create a LinRegLearner # learner = lrl.LinRegLearner(verbose = True) # create a LinRegLearner
learner = dtl.DTLearner(verbose = True) # create a LinRegLearner learner = dtl.DTLearner(verbose = True) # create a LinRegLearner
learner.addEvidence(trainX, trainY) # train it # learner.addEvidence(trainX, trainY) # train it #XXX split back into test and non-test
learner.addEvidence(data[:,0:-1], data[:,-1])
print(learner.author()) print(learner.author())
sys.exit(0)
# evaluate in sample # evaluate in sample
predY = learner.query(trainX) # get the predictions predY = learner.query(trainX) # get the predictions
rmse = math.sqrt(((trainY - predY) ** 2).sum()/trainY.shape[0]) rmse = math.sqrt(((trainY - predY) ** 2).sum()/trainY.shape[0])