-
Notifications
You must be signed in to change notification settings - Fork 0
/
returnEstimator.py
164 lines (153 loc) · 6.15 KB
/
returnEstimator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.gaussian_process import GaussianProcessClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
from sklearn.gaussian_process.kernels import RBF
from sklearn.model_selection import KFold
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
from sklearn.utils import shuffle
from dataCleanUp import removeNoReturn
from dateUtil import greaterThanDate, equalDate
from correlation import getCorrelation
import warnings
from time import perf_counter
from dataGraphing import plotTimeAccuracy
warnings.filterwarnings("ignore")
def getInOutSeries(prices, sentiment, pricesColumns, sentimentColumns):
x = []
y = []
j = 0
prices = removeNoReturn(prices, pricesColumns)
for i in range(len(prices) - 1):
entry = []
for c in pricesColumns:
entry.append(prices[i][c])
while(j < len(sentiment) and greaterThanDate(prices[i]['date'], sentiment[j]['date'])):
j += 1
if(j < len(sentiment) and equalDate(sentiment[j]['date'], prices[i]['date'])):
for c in sentimentColumns:
entry.append(sentiment[j][c])
else:
for c in sentimentColumns:
entry.append(0)
x.append(entry)
y.append(prices[i + 1]['return1Day'] > 0)
return x, y
def singlePointEstimator(prices, sentiment, pricesColumns, sentimentColumns):
X, y = getInOutSeries(prices, sentiment, pricesColumns, sentimentColumns)
X = np.array(X)
y = np.array(y)
X, y = shuffle(X, y)
highestAccuracy = 0
highestAccuracyModel = None
kf = KFold(n_splits=2)
processed = []
startTime = perf_counter()
K = range(1, 10)
highestAccuracyK = 0
highestKNNAccuracy = 0
for k in K:
accuracies = []
for train, test in kf.split(X):
model = KNeighborsClassifier(n_neighbors=k).fit(X[train], y[train])
pY = model.predict(X[test])
accuracies.append(accuracy_score(y[test], pY))
accuracies = np.array(accuracies)
accuracy = np.mean(accuracies)
if(accuracy > highestKNNAccuracy):
highestKNNAccuracy = accuracy
highestAccuracyK = k
name = model.__class__.__name__
time = round((perf_counter() - startTime) * 1000)
accuracy = highestKNNAccuracy
processed.append((name, time, accuracy))
print("Processed:", name)
print("Time:", time, "ms")
print("Accuracy:", accuracy)
if(highestKNNAccuracy > highestAccuracy):
highestAccuracy = highestKNNAccuracy
highestAccuracyModel = KNeighborsClassifier(n_neighbors=highestAccuracyK).fit(X, y)
startTime = perf_counter()
accuracies = []
for train, test in kf.split(X):
model = DecisionTreeClassifier().fit(X[train], y[train])
pY = model.predict(X[test])
accuracies.append(accuracy_score(y[test], pY))
accuracies = np.array(accuracies)
decisionTreeAccuracy = np.mean(accuracies)
name = model.__class__.__name__
time = round((perf_counter() - startTime) * 1000)
accuracy = decisionTreeAccuracy
processed.append((name, time, accuracy))
print("Processed:", name)
print("Time:", time, "ms")
print("Accuracy:", accuracy)
if(decisionTreeAccuracy > highestAccuracy):
highestAccuracy = decisionTreeAccuracy
highestAccuracyModel = DecisionTreeClassifier().fit(X, y)
startTime = perf_counter()
accuracies = []
kernel = 1.0 * RBF(length_scale=1.0, length_scale_bounds=(1e-1, 10.0))
for train, test in kf.split(X):
model = GaussianProcessClassifier(kernel).fit(X[train], y[train])
pY = model.predict(X[test])
accuracies.append(accuracy_score(y[test], pY))
accuracies = np.array(accuracies)
gaussianProcessAccuracy = np.mean(accuracies)
name = model.__class__.__name__
time = round((perf_counter() - startTime) * 1000)
accuracy = gaussianProcessAccuracy
processed.append((name, time, accuracy))
print("Processed:", name)
print("Time:", time, "ms")
print("Accuracy:", accuracy)
if(gaussianProcessAccuracy > highestAccuracy):
highestAccuracy = gaussianProcessAccuracy
highestAccuracyModel = GaussianProcessClassifier(kernel).fit(X, y)
startTime = perf_counter()
accuracies = []
for train, test in kf.split(X):
model = AdaBoostClassifier().fit(X[train], y[train])
pY = model.predict(X[test])
accuracies.append(accuracy_score(y[test], pY))
accuracies = np.array(accuracies)
adaBoostAccuracy = np.mean(accuracies)
name = model.__class__.__name__
time = round((perf_counter() - startTime) * 1000)
accuracy = adaBoostAccuracy
processed.append((name, time, accuracy))
print("Processed:", name)
print("Time:", time, "ms")
print("Accuracy:", accuracy)
if(adaBoostAccuracy > highestAccuracy):
highestAccuracy = adaBoostAccuracy
highestAccuracyModel = AdaBoostClassifier().fit(X, y)
startTime = perf_counter()
N = range(1, 20)
highestAccuracyN = 0
highestRandomForestAccuracy = 0
for n in N:
accuracies = []
for train, test in kf.split(X):
model = RandomForestClassifier(n_estimators=n).fit(X[train], y[train])
pY = model.predict(X[test])
accuracies.append(accuracy_score(y[test], pY))
accuracies = np.array(accuracies)
accuracy = np.mean(accuracies)
if(accuracy > highestRandomForestAccuracy):
highestRandomForestAccuracy = accuracy
highestAccuracyN = n
name = model.__class__.__name__
time = round((perf_counter() - startTime) * 1000)
accuracy = highestRandomForestAccuracy
processed.append((name, time, accuracy))
print("Processed:", name)
print("Time:", time, "ms")
print("Accuracy:", accuracy)
if(highestRandomForestAccuracy > highestAccuracy):
highestAccuracy = highestRandomForestAccuracy
highestAccuracyModel = RandomForestClassifier(n_estimators=highestAccuracyN).fit(X, y)
plotTimeAccuracy(processed)
return highestAccuracyModel, highestAccuracy