-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathfunctions.py
492 lines (377 loc) · 15.3 KB
/
functions.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 30 19:31:07 2014
@author: francesco
"""
import numpy as np
import pandas as pd
from sklearn import preprocessing
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.ensemble import RandomForestRegressor
import matplotlib.pyplot as plt
#from sklearn.ensemble import BaggingRegressor
from sklearn.ensemble import AdaBoostRegressor
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.neighbors import KNeighborsRegressor
from sklearn.ensemble import RandomForestClassifier
from sklearn import neighbors
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.svm import SVR
from sklearn.svm import SVC
import operator
from sklearn.metrics import roc_auc_score
import pandas.io.data
from sklearn.qda import QDA
def loadDatasets(path_directory):
"""
import into dataframe all datasets saved in path_directory
"""
name = path_directory + '/procter.csv'
out = pd.read_csv(name, index_col=0, parse_dates=True)
#name = path_directory + '/sp.csv'
#sp = pd.read_csv(name, index_col=0, parse_dates=True)
name = path_directory + '/nasdaq.csv'
nasdaq = pd.read_csv(name, index_col=0, parse_dates=True)
name = path_directory + '/djia.csv'
djia = pd.read_csv(name, index_col=0, parse_dates=True)
#name = path_directory + '/treasury.csv'
#treasury = pd.read_csv(name, index_col=0, parse_dates=True)
name = path_directory + '/hkong.csv'
hkong = pd.read_csv(name, index_col=0, parse_dates=True)
name = path_directory + '/frankfurt.csv'
frankfurt = pd.read_csv(name, index_col=0, parse_dates=True)
name = path_directory + '/paris.csv'
paris = pd.read_csv(name, index_col=0, parse_dates=True)
name = path_directory + '/nikkei.csv'
nikkei = pd.read_csv(name, index_col=0, parse_dates=True)
name = path_directory + '/london.csv'
london = pd.read_csv(name, index_col=0, parse_dates=True)
name = path_directory + '/australia.csv'
australia = pd.read_csv(name, index_col=0, parse_dates=True)
#return [sp, nasdaq, djia, treasury, hkong, frankfurt, paris, nikkei, london, australia]
#return [out, nasdaq, djia, frankfurt, hkong, nikkei, australia]
return [out, nasdaq, djia, frankfurt, london, paris, hkong, nikkei, australia]
def getStock(symbol, start, end):
"""
downloads stock which is gonna be the output of prediciton
"""
out = pd.io.data.get_data_yahoo(symbol, start, end)
out.columns.values[-1] = 'AdjClose'
out.columns = out.columns + '_Out'
out['Return_Out'] = out['AdjClose_Out'].pct_change()
return out
def count_missing(dataframe):
"""
count number of NaN in dataframe
"""
return (dataframe.shape[0] * dataframe.shape[1]) - dataframe.count().sum()
def addFeatures(dataframe, adjclose, returns, n):
"""
operates on two columns of dataframe:
- n >= 2
- given Return_* computes the return of day i respect to day i-n.
- given AdjClose_* computes its moving average on n days
"""
return_n = adjclose[9:] + "Time" + str(n)
dataframe[return_n] = dataframe[adjclose].pct_change(n)
roll_n = returns[7:] + "RolMean" + str(n)
dataframe[roll_n] = pd.rolling_mean(dataframe[returns], n)
def mergeDataframes(datasets, index, target):
"""
merges datasets in the list
"""
subset = []
subset = [dataset.iloc[:, index:] for dataset in datasets[1:]]
if target == 'CLASSIFICATION':
return datasets[0].iloc[:, index:].join(subset, how = 'outer')
#elif target == 'REGRESSION':
# return datasets[0].iloc[:, index:].join(subset, how = 'outer')
def applyTimeLag(dataset, lags, delta, back, target):
"""
apply time lag to return columns selected according to delta.
Days to lag are contained in the lads list passed as argument.
Returns a NaN free dataset obtained cutting the lagged dataset
at head and tail
"""
if target == 'CLASSIFICATION':
maxLag = max(lags)
columns = dataset.columns[::(2*max(delta)-1)]
for column in columns:
for lag in lags:
newcolumn = column + str(lag)
dataset[newcolumn] = dataset[column].shift(lag)
return dataset.iloc[maxLag:-1,:]
# elif target == 'REGRESSION':
# maxLag = max(lags)
#
# columns = dataset.columns[::(2*max(delta)-1)]
# for column in columns:
# for lag in lags:
# newcolumn = column + str(lag)
# dataset[newcolumn] = dataset[column].shift(lag)
#
# return dataset.iloc[maxLag:-1,:]
def performCV(X_train, y_train, folds, method, parameters):
"""
given complete dataframe, number of folds, the % split to generate
train and test set and features to perform prediction --> splits
dataframein test and train set. Takes train set and splits in k folds.
- Train on fold 1, test on 2
- Train on fold 1-2, test on 3
- Train on fold 1-2-3, test on 4
....
returns mean of test accuracies
"""
print ''
print 'Parameters --------------------------------> ', parameters
print 'Size train set: ', X_train.shape
k = int(np.floor(float(X_train.shape[0])/folds))
print 'Size of each fold: ', k
acc = np.zeros(folds-1)
for i in range(2, folds+1):
print ''
split = float(i-1)/i
print 'Splitting the first ' + str(i) + ' chuncks at ' + str(i-1) + '/' + str(i)
data = X_train[:(k*i)]
output = y_train[:(k*i)]
print 'Size of train+test: ', data.shape
index = int(np.floor(data.shape[0]*split))
X_tr = data[:index]
y_tr = output[:index]
X_te = data[(index+1):]
y_te = output[(index+1):]
acc[i-2] = performClassification(X_tr, y_tr, X_te, y_te, method, parameters)
print 'Accuracy on fold ' + str(i) + ': ', acc[i-2]
return acc.mean()
def performTimeSeriesSearchGrid(X_train, y_train, folds, method, grid):
"""
parameters is a dictionary with: keys --> parameter , values --> list of values of parameter
"""
print ''
print 'Performing Search Grid CV...'
print 'Algorithm: ', method
param = grid.keys()
finalGrid = {}
if len(param) == 1:
for value_0 in grid[param[0]]:
parameters = [value_0]
accuracy = performCV(dataset, folds, split, features, method, parameters)
finalGrid[accuracy] = parameters
final = sorted(finalGrid.iteritems(), key=operator.itemgetter(0), reverse=True)
print ''
print 'Final CV Results: ', final
return final[0]
elif len(param) == 2:
for value_0 in grid[param[0]]:
for value_1 in grid[param[1]]:
parameters = [value_0, value_1]
accuracy = performCV(dataset, folds, split, features, method, parameters)
finalGrid[accuracy] = parameters
final = sorted(finalGrid.iteritems(), key=operator.itemgetter(0), reverse=True)
print ''
print 'Final CV Results: ', final
return final[0]
##################
################## MERGING SENTIMENT
def mergeSentimenToStocks(stocks):
df = pd.read_csv('/home/francesco/BigData/Project/CSV/sentiment.csv', index_col = 'date')
final = stocks.join(df, how='left')
return final
###############################################################################
###############################################################################
###############################################################################
######## CLASSIFICATION
#####IDEAS --> MULTIPLYEACH RETURN BY 100, QDA, AUC
#####
def prepareDataForClassification(dataset, start_test):
"""
generates categorical to be predicted column, attach to dataframe
and label the categories
"""
le = preprocessing.LabelEncoder()
dataset['UpDown'] = dataset['Return_Out']
dataset.UpDown[dataset.UpDown >= 0] = 'Up'
dataset.UpDown[dataset.UpDown < 0] = 'Down'
dataset.UpDown = le.fit(dataset.UpDown).transform(dataset.UpDown)
features = dataset.columns[1:-1]
X = dataset[features]
y = dataset.UpDown
X_train = X[X.index < start_test]
y_train = y[y.index < start_test]
X_test = X[X.index >= start_test]
y_test = y[y.index >= start_test]
return X_train, y_train, X_test, y_test
def prepareDataForModelSelection(X_train, y_train, start_validation):
"""
gets train set and generates a validation set splitting the train.
The validation set is mandatory for feature and model selection.
"""
X = X_train[X_train.index < start_validation]
y = y_train[y_train.index < start_validation]
X_val = X_train[X_train.index >= start_validation]
y_val = y_train[y_train.index >= start_validation]
return X, y, X_val, y_val
def performClassification(X_train, y_train, X_test, y_test, method, parameters):
"""
performs classification on returns using serveral algorithms
"""
#print ''
print 'Performing ' + method + ' Classification...'
print 'Size of train set: ', X_train.shape
print 'Size of test set: ', X_test.shape
if method == 'RF':
return performRFClass(X_train, y_train, X_test, y_test)
elif method == 'KNN':
return performKNNClass(X_train, y_train, X_test, y_test)
elif method == 'SVM':
return performSVMClass(X_train, y_train, X_test, y_test)
elif method == 'ADA':
return performAdaBoostClass(X_train, y_train, X_test, y_test, parameters)
elif method == 'GTB':
return performGTBClass(X_train, y_train, X_test, y_test)
elif method == 'QDA':
return performQDAClass(X_train, y_train, X_test, y_test)
def performRFClass(X_train, y_train, X_test, y_test):
"""
Random Forest Binary Classification
"""
clf = RandomForestClassifier(n_estimators=100, n_jobs=-1)
clf.fit(X_train, y_train)
accuracy = clf.score(X_test, y_test)
#auc = roc_auc_score(y_test, clf.predict(X_test))
return accuracy
def performKNNClass(X_train, y_train, X_test, y_test):
"""
KNN binary Classification
"""
clf = neighbors.KNeighborsClassifier()
clf.fit(X_train, y_train)
accuracy = clf.score(X_test, y_test)
#auc = roc_auc_score(y_test, clf.predict(X_test))
return accuracy
def performSVMClass(X_train, y_train, X_test, y_test):
"""
SVM binary Classification
"""
clf = SVC()
clf.fit(X_train, y_train)
accuracy = clf.score(X_test, y_test)
#auc = roc_auc_score(y_test, clf.predict(X_test))
return accuracy
def performAdaBoostClass(X_train, y_train, X_test, y_test, parameters):
"""
Ada Boosting binary Classification
"""
n = parameters[0]
l = parameters[1]
clf = AdaBoostClassifier(n_estimators = n, learning_rate = l)
clf.fit(X_train, y_train)
accuracy = clf.score(X_test, y_test)
#auc = roc_auc_score(y_test, clf.predict(X_test))
return accuracy
def performGTBClass(X_train, y_train, X_test, y_test):
"""
Gradient Tree Boosting binary Classification
"""
clf = GradientBoostingClassifier(n_estimators=100)
clf.fit(X_train, y_train)
accuracy = clf.score(X_test, y_test)
#auc = roc_auc_score(y_test, clf.predict(X_test))
return accuracy
def performQDAClass(X_train, y_train, X_test, y_test):
"""
Gradient Tree Boosting binary Classification
"""
clf = QDA()
clf.fit(X_train, y_train)
accuracy = clf.score(X_test, y_test)
#auc = roc_auc_score(y_test, clf.predict(X_test))
return accuracy
##############################################################################
##############################################################################
##############################################################################
##############################################################################
####### REGRESSION
def performRegression(dataset, split):
"""
performs regression on returns using serveral algorithms
"""
features = dataset.columns[1:]
index = int(np.floor(dataset.shape[0]*split))
train, test = dataset[:index], dataset[index:]
print 'Size of train set: ', train.shape
print 'Size of test set: ', test.shape
output = 'Return_SP500'
#print 'Accuracy RFC: ', performRFReg(train, test, features, output)
#print 'Accuracy SVM: ', performSVMReg(train, test, features, output)
#print 'Accuracy BAG: ', performBaggingReg(train, test, features, output)
#print 'Accuracy ADA: ', performAdaBoostReg(train, test, features, output)
#print 'Accuracy BOO: ', performGradBoostReg(train, test, features, output)
print 'Accuracy KNN: ', performKNNReg(train, test, features, output)
def performRFReg(train, test, features, output):
"""
Random Forest Regression
"""
forest = RandomForestRegressor(n_estimators=100, n_jobs=-1)
forest = forest.fit(train[features], train[output])
Predicted = forest.predict(test[features])
plt.plot(test[output])
plt.plot(Predicted, color='red')
plt.show()
return mean_squared_error(test[output], Predicted), r2_score(test[output], Predicted)
def performSVMReg(train, test, features, output):
"""
SVM Regression
"""
clf = SVR()
clf.fit(train[features], train[output])
Predicted = clf.predict(test[features])
plt.plot(test[output])
plt.plot(Predicted, color='red')
plt.show()
return mean_squared_error(test[output],Predicted), r2_score(test[output], Predicted)
def performBaggingReg(train, test, features, output):
"""
Bagging Regression
"""
clf = BaggingRegressor()
clf.fit(train[features], train[output])
Predicted = clf.predict(test[features])
plt.plot(test[output])
plt.plot(Predicted, color='red')
plt.show()
return mean_squared_error(test[output],Predicted), r2_score(test[output], Predicted)
def performAdaBoostReg(train, test, features, output):
"""
Ada Boost Regression
"""
clf = AdaBoostRegressor()
clf.fit(train[features], train[output])
Predicted = clf.predict(test[features])
plt.plot(test[output])
plt.plot(Predicted, color='red')
plt.show()
return mean_squared_error(test[output],Predicted), r2_score(test[output], Predicted)
def performGradBoostReg(train, test, features, output):
"""
Gradient Boosting Regression
"""
clf = GradientBoostingRegressor()
clf.fit(test[features], train[output])
Predicted = clf.predict(test[features])
plt.plot(test[output])
plt.plot(Predicted, color='red')
plt.show()
return mean_squared_error(test[output],Predicted), r2_score(test[output], Predicted)
def performKNNReg(train, test, features, output):
"""
KNN Regression
"""
clf = KNeighborsRegressor()
clf.fit(train[features], train[output])
Predicted = clf.predict(test[features])
plt.plot(test[output])
plt.plot(Predicted, color='red')
plt.show()
return mean_squared_error(test[output],Predicted), r2_score(test[output], Predicted)