forked from jaydu1/Stock-Selection-a-Framework
-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_pipeline
235 lines (196 loc) · 6.8 KB
/
data_pipeline
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
"""
A Machine Learning Framework for Stock Selection
Authors:
XingYu Fu; JinHong Du; YiFeng Guo; MingWen Liu; Tao Dong; XiuWen Duan;
Institutions:
AI&Fintech Lab of Likelihood Technology;
Gradient Trading;
Sun Yat-sen University;
Contact:
All Rights Reserved.
"""
"""Import Modules"""
# Numerical Computation
import numpy as np
from math import sqrt
# Load Data
import pandas as pd
import os
"""Load Factor-Data from Disk"""
def load_tail( start, sample_num, F, Q, Sequential = False):
"""Specify the data path"""
path_factor = r"./database/factor"
path_price = r"./database/price"
"""Loading Data"""
Factor = []
Price = []
factor_begin = 0
factor_count = 0
flag = 1
for data_name in os.listdir(path_factor):
if data_name == (start+".csv"):
factor_begin = 1
if factor_begin == 1:
if flag % (F+1) == 1: # Load Factor
x = pd.read_csv( path_factor + '/'+ data_name, header = None)
Factor.append(x)
factor_count += 1
else:
y = pd.read_csv( path_price + '/'+ data_name, header = None)
Price.append(y)
flag += 1
if factor_count == sample_num+1:
Factor = Factor[:-1]
break
"""Data Regularization"""
X_return = []
Y_return = []
for t in range( len(Factor) ):
X = np.array( Factor[t], dtype=object )
P = [ np.array( Price[i], dtype=object ) for i in range(F*t,F*t+F) ]
# Step1: Find the stocks that appear in factor matrix and price vectors at the same time.
n_x = set( X[:,0] )
name_p = [ set(pp[:,0]) for pp in P]
for n_p in name_p:
n_x = n_p & n_x
XX = []
YY = []
for row in X:
if row[0] in n_x:
XX.append( row[1:] )
y = []
for pp in P:
n_p = pp[:,0]
index = np.where( n_p == row[0] )[0][0]
y.append( pp[index][1] )
y = np.array(y)
YY.append( y )
else:
continue
XX = np.array(XX, dtype = np.float)
YY = np.array(YY, dtype = np.float)
# Step2: Replace nan with column average
XX = np.where(np.isnan(XX), np.ma.array(XX, mask=np.isnan(XX)).mean(axis=0), XX)
# Step3: Factor Normalization
for j in range( len( XX[0] ) ):
max_j = max( XX[:,j] )
min_j = min( XX[:,j] )
for i in range( len( XX ) ):
XX[i][j] = (XX[i][j]-min_j)/(max_j-min_j)
# Step4: Standard Deviation; Return_Rate; Anomaly Filtering;
Filter = [True for y in YY]
return_std = []
for i in range( len(YY) ):
y = YY[i]
mean = sum( y )/np.float( len(y) )
std = sqrt( sum([ (price-mean)**2 for price in y])/np.float( len(y) ) )
if std == 0:
Filter[i] = False
if y[0]!=0:
return_rate = y[-1]/y[0]
else:
return_rate = np.nan
Filter[i] = False
return_std.append( ( return_rate, std) )
XX = XX[ Filter ]
return_std = np.array( return_std )
return_std = return_std[ Filter ]
YY = np.array( [ pair[0]/pair[1] for pair in return_std] )
# Step5: Tail Set Construction
XX = XX[ (-YY).argsort() ]
bd = np.int(np.round( len(XX)*Q ))
Top = np.array( XX[0:bd,:] )
Bottom = np.array( XX[-bd:,:] )
XX = np.concatenate( (Top,Bottom) )
YY = np.concatenate( (np.ones(bd),np.zeros(bd)) )
permutation = np.random.permutation(2*bd)
XX = XX[permutation]
YY = YY[permutation]
if t == 0:
X_return = XX
Y_return = YY
else:
X_return = np.concatenate((X_return, XX))
Y_return = np.concatenate((Y_return, YY))
# Shuffle
if Sequential:
pass
else:
permutation = np.random.permutation(X_return.shape[0])
X_return = X_return[permutation]
Y_return = Y_return[permutation]
return X_return, Y_return
def load_whole( start, F):
"""Specify the data path"""
path_factor = r"./database/factor"
path_price = r"./database/price"
"""Loading Data"""
Factor = []
Price = []
begin = 0
flag = 1
for data_name in os.listdir(path_factor):
if data_name == (start+".csv"):
begin = 1
if begin == 1:
if flag == 1: # Load Factor
Factor = pd.read_csv( path_factor + '/'+ data_name, header = None)
elif flag <= F+1:
y = pd.read_csv( path_price + '/'+ data_name, header = None)
Price.append(y)
else:
break
flag += 1
else:
continue
"""Data Regularization"""
Factor = np.array( Factor, dtype = object)
Price = [ np.array( Price[t], dtype=object ) for t in range(F) ]
# Step1: Find the stocks that appear in factor matrix and price vectors at the same time.
n_x = set( Factor[:,0] )
n_p_list = [ set(p_t[:,0]) for p_t in Price]
for n_p in n_p_list:
n_x = n_p & n_x
XX = []
YY = []
for row in Factor:
if row[0] in n_x:
XX.append( row[1:] )
y = []
for p_t in Price:
n_p = p_t[:,0]
index = np.where( n_p == row[0] )[0][0]
y.append( p_t[index][1] )
y = np.array(y)
YY.append( y )
else:
continue
XX = np.array(XX, dtype = np.float)
YY = np.array(YY, dtype = np.float)
# Step2: Replace nan with column average
XX = np.where(np.isnan(XX), np.ma.array(XX, mask=np.isnan(XX)).mean(axis=0), XX)
# Step3: Factor Normalization
for j in range( len( XX[0] ) ):
max_j = max( XX[:,j] )
min_j = min( XX[:,j] )
for i in range( len( XX ) ):
XX[i][j] = (XX[i][j]-min_j)/(max_j-min_j)
# Step4: Standard Deviation; Return_Rate; Anomaly Filtering;
Filter = [True for y in YY]
for i in range(len(YY)):
price_vector = YY[i]
if 0.0 in price_vector:
Filter[i] = False
continue
mean = sum(price_vector)/len(price_vector)
variance = sum( [ (p-mean)**2 for p in price_vector] )
if variance == 0:
Filter[i] = False
XX = XX[ Filter ]
YY = YY[ Filter ]
# Step5: Shuffle
permutation = np.random.permutation(XX.shape[0])
XX = XX[permutation]
YY = YY[permutation]
return XX, YY