-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIV_many2one_simpleDeeplearning.py
151 lines (129 loc) · 6.01 KB
/
IV_many2one_simpleDeeplearning.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
"""
Including features and labels engineering and imposing of simple neutral network.
"""
import paddle
import paddle.nn.functional as F
import warnings
# Using gpu
paddle.device.set_device('gpu:0')
class simpleNN(paddle.nn.Layer):
def __init__(self):
super(simpleNN, self).__init__()
# Only ONE layer with output dim to be 1
self.fc = paddle.nn.Linear(in_features=12, out_features=1)
# Define the net forward function
def forward(self, inputs):
outputs = self.fc(inputs)
return outputs
"""
First of all, the feature's date and label's date should not be in the same as my purpose is to predict trading
direction of the future. Thus I am adjusting them by lagging the feature by 2 days.
The feature and label are trying to be engineered by scikit-learn package sklearn's transformation functions.
"""
import numpy as np
import datetime as dt
import pandas as pd
from sklearn import preprocessing
class simpleDeeplearning:
def __init__(self, indexWanted, emailFeatures, weiboFeatures, featuresYields, labels):
self.indexWanted = indexWanted
self.emailFeatures = emailFeatures
self.weiboFeatures = weiboFeatures
self.featuresYields = featuresYields
self.labels = labels
self.results = pd.DataFrame(index=['_'.join([self.indexWanted[0], 'sdl'])],
columns=['VOLATILITY', 'STD', 'T+1 RESULT', 'T+2 RESULT'])
self.fnl_fd = self.fnl_reProcessing()
self.sdl_modeling()
#print(self.results)
def datetimeProcessing(self, dataToproc):
# functions to reprocess the DATE formate
dataToproc['DATE_'] = ''
for i in range(len(dataToproc)):
with warnings.catch_warnings():
warnings.simplefilter("ignore")
dataToproc['DATE_'].iloc[i] = dataToproc['DATE'].iloc[i].to_pydatetime().date()
dataToproc.index = dataToproc['DATE_']
return dataToproc
def fnl_reProcessing(self):
# function to combine the features and labels.
self.emailFeatures = self.datetimeProcessing(self.emailFeatures)
self.weiboFeatures = self.datetimeProcessing(self.weiboFeatures)
self.featuresYields = self.datetimeProcessing(self.featuresYields)
self.labels = self.datetimeProcessing(self.labels)
# join the emailFeatures and labels into one dataframe
fnl_fd = self.labels.join(self.emailFeatures, rsuffix = '_other')
# remove NAs
fnl_fd = fnl_fd.dropna()
fnl_fd = fnl_fd.drop(['DATE', 'DATE_other', 'DATE__other'], axis = "columns")
# join the weiboFeatures
fnl_fd = fnl_fd.join(self.weiboFeatures, rsuffix='_weibo')
fnl_fd = fnl_fd.dropna()
fnl_fd = fnl_fd.drop(['DATE', 'DATE__weibo'], axis="columns")
# more features yields
fnl_fd = fnl_fd.join(self.featuresYields, rsuffix = '_other')
fnl_fd = fnl_fd.dropna()
fnl_fd = fnl_fd.drop(['DATE', 'DATE__other'], axis = "columns")
return fnl_fd
def sdl_train(self, datas): # features and labels
sdl_model = simpleNN()
opt = paddle.optimizer.SGD(learning_rate=0.001, parameters=sdl_model.parameters())
EPOCH_NUM = 500 #500
features = datas[0]
labels = datas[1]
for epoch in range(EPOCH_NUM):
for i in range(labels.shape[0]):
feature = features[i].reshape((1, 12)).astype('float32')
label = labels[i].reshape((1, 1)).astype('float32')
# calculate forward
predict = sdl_model(feature)
# calculate loss
loss = F.square_error_cost(predict, label)
avg_loss = paddle.mean(loss)
# backwarding
avg_loss.backward()
opt.step()
opt.clear_grad()
#print("EPOCH_NUM: {}, loss is: {}".format(epoch, avg_loss.numpy()))
return sdl_model
def sdl_predict(self, datas):
# for day t+1
feature_t_1 = datas[0]
feature_t_1_tensor = paddle.to_tensor(feature_t_1).reshape((1, 12)).astype('float32')
result_t_1 = self.sdl_model(feature_t_1_tensor)
#print('Prediction for DAY T + 1:', result_t_1.numpy())
self.results['T+1 RESULT'] = result_t_1.numpy()
# for day t+2
feature_t_2 = datas[1]
feature_t_2_tensor = paddle.to_tensor(feature_t_2).reshape((1, 12)).astype('float32')
result_t_2 = self.sdl_model(feature_t_2_tensor)
#print('Prediction for DAY T + 2:', result_t_2.numpy())
self.results['T+2 RESULT'] = result_t_2.numpy()
def sdl_modeling(self):
fnl_fd = self.fnl_fd
# transform the dataset into more desired features and labels.
labels = self.fnl_fd['LABELS']
features = self.fnl_fd.drop(['DATE_', 'LABELS'], axis="columns")
self.X_predict_date = features.index[-1]
labels_scaled = labels
# using StandardScaler of preprocessing in sklearn to scale the features
self.scaler = preprocessing.StandardScaler().fit(features)
features_scaled = self.scaler.transform(features)
# in order to predict result in 2 days, I am lagging the features by 2.
labels_train = labels_scaled[2:]
features_train = features_scaled[:-2:]
# prepare the labels and features for Deep Learning.
labels_array = np.array(labels_train).reshape(len(labels_train), 1)
features_array = np.array(features_train)
labels_tensor = paddle.to_tensor(labels_array)
features_tensor = paddle.to_tensor(features_array)
# implement the training function.
datas = [features_tensor, labels_tensor]
self.sdl_model = self.sdl_train(datas)
# paddle.save(self.sdl_model.state_dict(), './sdl_model.pdparams')
# Try to predict from new features.
features_predict = features_scaled[-2:]
self.sdl_predict(features_predict)
# Other results
self.results['VOLATILITY'] = abs(labels).mean()
self.results['STD'] = labels.std()