-
Notifications
You must be signed in to change notification settings - Fork 10
/
functions_kit.py
299 lines (240 loc) · 11 KB
/
functions_kit.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
from imports import *
def get_exchange_rates(api):
fred = Fred(api_key=api)
#Australia
AUS_exchange = fred.get_series('EXUSAL')
AUS_exchange = pd.DataFrame(AUS_exchange)
AUS_exchange = AUS_exchange.rename(columns = {0:"AUS"})
AUS_exchange.AUS = 1/AUS_exchange.AUS
#Brazil
BRA_exchange = fred.get_series('EXBZUS')
BRA_exchange = pd.DataFrame(BRA_exchange)
BRA_exchange = BRA_exchange.rename(columns = {0:"BRA"})
#CANADA
CAN_exchange = fred.get_series('EXCAUS')
CAN_exchange = pd.DataFrame(CAN_exchange)
CAN_exchange = CAN_exchange.rename(columns = {0:"CAN"})
#CHILE
CHI_exchange = fred.get_series('CCUSSP02CLM650N')
CHI_exchange = pd.DataFrame(CHI_exchange)
CHI_exchange = CHI_exchange.rename(columns = {0:"CHI"})
#CHINA
CHN_exchange = fred.get_series('EXCHUS')
CHN_exchange = pd.DataFrame(CHN_exchange)
CHN_exchange = CHN_exchange.rename(columns = {0:"CHN"})
#COLOMBIA
COL_exchange = fred.get_series('COLCCUSMA02STM')
COL_exchange = pd.DataFrame(COL_exchange)
COL_exchange = COL_exchange.rename(columns = {0:"COL"})
#CZECH REPUBLIC
CZR_exchange = fred.get_series('CCUSMA02CZM618N')
CZR_exchange = pd.DataFrame(CZR_exchange)
CZR_exchange = CZR_exchange.rename(columns = {0:"CZR"})
#EURO
EUR_exchange = fred.get_series('EXUSEU')
EUR_exchange = pd.DataFrame(EUR_exchange)
EUR_exchange = EUR_exchange.rename(columns = {0:"EUR"})
EUR_exchange.EUR = 1/EUR_exchange.EUR
#Hungary
HUN_exchange = fred.get_series('CCUSMA02HUM618N')
HUN_exchange = pd.DataFrame(HUN_exchange)
HUN_exchange = HUN_exchange.rename(columns = {0:"HUN"})
#INDONESIA
INDO_exchange = fred.get_series('CCUSSP02IDM650N')
INDO_exchange = pd.DataFrame(INDO_exchange)
INDO_exchange = INDO_exchange.rename(columns = {0:"INDO"})
#JAPAN
JAP_exchange = fred.get_series('EXJPUS')
JAP_exchange = pd.DataFrame(JAP_exchange)
JAP_exchange = JAP_exchange.rename(columns = {0:"JAP"})
#MALAYA
#No Data for it on Fred
#MEXICO
MEX_exchange = fred.get_series('EXMXUS')
MEX_exchange = pd.DataFrame(MEX_exchange)
MEX_exchange = MEX_exchange.rename(columns = {0:"MEX"})
#NORWAY
NOR_exchange = fred.get_series('EXNOUS')
NOR_exchange = pd.DataFrame(NOR_exchange)
NOR_exchange = NOR_exchange.rename(columns = {0:"NOR"})
#New Zealand
NZ_exchange = fred.get_series('EXUSNZ')
NZ_exchange = pd.DataFrame(NZ_exchange)
NZ_exchange = NZ_exchange.rename(columns = {0:"NZ"})
NZ_exchange.NZ = 1/NZ_exchange.NZ
#PERU
#No Data for it on Fred
#Philippines
#No Data for it on Fred
#POLAND
PO_exchange = fred.get_series('CCUSMA02PLM618N')
PO_exchange = pd.DataFrame(PO_exchange)
PO_exchange = PO_exchange.rename(columns = {0:"PO"})
#South Africa
SA_exchange = fred.get_series('EXSFUS')
SA_exchange = pd.DataFrame(SA_exchange)
SA_exchange = SA_exchange.rename(columns = {0:"SA"})
#Singapore
SNG_exchange = fred.get_series('EXSIUS')
SNG_exchange = pd.DataFrame(SNG_exchange)
SNG_exchange = SNG_exchange.rename(columns = {0:"SNG"})
#SWEDEN
SWE_exchange = fred.get_series('EXSDUS')
SWE_exchange = pd.DataFrame(SWE_exchange)
SWE_exchange = SWE_exchange.rename(columns = {0:"SWE"})
#SWITZERLAND
SWI_exchange = fred.get_series('EXSZUS')
SWI_exchange = pd.DataFrame(SWI_exchange)
SWI_exchange = SWI_exchange.rename(columns = {0:"SWI"})
#UNITED KINGDOMS
UK_exchange = fred.get_series('EXUSUK')
UK_exchange = pd.DataFrame(UK_exchange)
UK_exchange = UK_exchange.rename(columns = {0:"UK"})
UK_exchange.UK = 1/UK_exchange.UK
data_frames = [AUS_exchange, BRA_exchange, CAN_exchange, CHI_exchange, CHN_exchange,
COL_exchange, CZR_exchange, EUR_exchange, HUN_exchange, INDO_exchange, MEX_exchange,
NOR_exchange, NZ_exchange, PO_exchange, SA_exchange, SNG_exchange, SWE_exchange,
SWI_exchange, UK_exchange, JAP_exchange]
df_merged = reduce(lambda left,right: pd.merge(left,right,left_index = True, right_index = True,
how='inner'), data_frames)
return df_merged
#Standardize Data
def standardize_data(df):
column_names = list(df.columns)
x = StandardScaler().fit_transform(df.values)
result = pd.DataFrame(data = x, columns = column_names)
result.index = df.index
return result
#Calcuate PCA
def PCA_analysis(df, type, standardize = True):
if standardize == True:
data = standardize_data(df)
else:
data = df
pca = PCA(n_components = 3)
principalComponents = pca.fit_transform(data)
principalDf = pd.DataFrame(data = principalComponents
, columns = [type + '_level', type + '_slope', type+ '_curvature'])
principalDf.index = df.index
return principalDf
#Merge Datasets
def merge_datasets(ylds_df, exp_df, tp_df, exchange = True, exchange_rates_df = None):
if exchange == True:
data_frames = [ylds_df, exp_df, tp_df, exchange_rates_df]
else:
data_frames = [ylds_df, exp_df, tp_df]
result = reduce(lambda left,right: pd.merge(left,right,left_index = True, right_index = True,
how='outer'), data_frames)
return result
#Calculate Exchange Rate returns
def calculate_returns(df_input, country_name, nlag = 1):
df = df_input.copy()
remaining_data = df.iloc[:,:-1]
remaining_data.drop(remaining_data.index[(-1*nlag):], inplace = True)
begin_data = df[country_name][:(-1*nlag)]
end_data = df[country_name][nlag:]
dates = list(begin_data.index)
begin_data.reset_index(drop = True, inplace = True)
end_data.reset_index(drop = True, inplace = True)
returns = ((begin_data-end_data)/end_data)*100
result = {'dates': dates, 'er_return': returns}
result = pd.DataFrame.from_dict(result)
result.set_index('dates', inplace = True)
final_result = pd.merge(remaining_data, result, left_index=True, right_index = True, how = 'inner')
return final_result
#Calcuate YLDS, EXP, and TP Differential with US
def calculate_differential(df, US_df, type = 'forex'):
data = df.iloc[:,:-1]
US_data = US_df.copy()
result = data.subtract(US_data, axis = 'index')
if type == 'forex':
result.columns = ['ylds_level_diff','ylds_slope_diff','ylds_curvature_diff','exp_level_diff',
'exp_slope_diff','exp_curvature_diff',
'tp_level_diff', 'tp_slope_diff',
'tp_curvature_diff']
final_result = pd.merge(result, df, left_index=True, right_index = True, how = 'outer')
return final_result
else:
result.columns = ['ylds_level_diff','ylds_slope_diff','ylds_curvature_diff','shortRun_interest_diff','exp_level_diff',
'exp_slope_diff','exp_curvature_diff',
'tp_level_diff', 'tp_slope_diff',
'tp_curvature_diff']
final_result = pd.merge(result, df, left_index=True, right_index = True, how = 'outer')
return final_result
#########################################
#Create Buy/Sell column
#########################################
def buy_classifier_setup(df):
data = df.copy()
buy = [1 if exchange_return > 0 else 0 for exchange_return in data['er_return']]
data['buy'] = buy
data_distribution = {"one": round(len(data[data['buy'] == 1])/len(data),2),
"zero": round(len(data[data['buy'] == 0])/len(data),2)}
return data, data_distribution
#############################################################
#Support Vector Classifier with Grid Search for all countries
############################################################
def support_vectorClassifier(train_df, test_df):
#Split dataset
train_features = train_df.iloc[:, 0:9]
train_target = train_df.iloc[:, -1]
test_features = test_df.iloc[:, 0:9]
test_target = test_df.iloc[:, -1]
#Create parameters
parameter_candidates = [
{'C': [0.001, 0.01, 0.1, 1, 10, 100, 1000], 'kernel': ['linear']},
{'C': [0.0001,0.001, 0.01, 0.1, 1, 10, 100, 1000], 'gamma': [0.00001,0.0001, 0.001, 0.01, 0.1, 1], 'kernel': ['rbf']},
]
#Fit Data
clf = GridSearchCV(estimator=SVC(), param_grid=parameter_candidates, n_jobs=-1)
clf.fit(train_features, train_target)
#Organize Results
results = {"model": clf,
"C": clf.best_estimator_.C,
"gamma": clf.best_estimator_.gamma,
"kernel": clf.best_estimator_.kernel,
"train_accuracy": accuracy_score(train_target, clf.predict(train_features)),
"test_accuracy": accuracy_score(test_target, clf.predict(test_features)),
"train_F1score": f1_score(train_target, clf.predict(train_features)),
"test_F1score": f1_score(test_target, clf.predict(test_features))}
return results
##################################################
#Create Neural Network Classifier
##################################################
def neural_networkClassifier(train_df, test_df):
#Split dataset
train_features = train_df.iloc[:, 0:9]
train_target = train_df.iloc[:, -1]
test_features = test_df.iloc[:, 0:9]
test_target = test_df.iloc[:, -1]
#Creating Neural Network
tag_classifier = Sequential()
#first layer
tag_classifier.add(Dense(64, activation='relu', kernel_initializer='random_normal', input_dim=9))
tag_classifier.add(Dropout(.2))
#second layer
tag_classifier.add(Dense(32, activation='relu', kernel_initializer='random_normal'))
tag_classifier.add(Dropout(.2))
#output layer
#softmax sums predictions to 1, good for multi-classification
tag_classifier.add(Dense(2, activation ='sigmoid', kernel_initializer='random_normal'))
#Compiling
#adam optimizer adjusts learning rate throughout training
#loss function categorical crossentroy for classification
tag_classifier.compile(optimizer ='adam',loss = 'categorical_crossentropy', metrics = ['accuracy'])
early_stop = EarlyStopping(monitor = 'loss', patience = 1, verbose = 2)
train_target = to_categorical(train_target) #First column is for 0 second is for 1
tag_classifier.fit(train_features, train_target, epochs = 1000,
batch_size = 10000, verbose = 2,
callbacks = [early_stop])
train_y_pred=tag_classifier.predict(train_features)
train_y_pred =[1 if prediction[1] > 0.5 else 0 for prediction in train_y_pred]
test_y_pred=tag_classifier.predict(test_features)
test_y_pred =[1 if prediction[1] > 0.5 else 0 for prediction in test_y_pred]
#Organize Results
results = {"model": tag_classifier,
"train_accuracy": accuracy_score(train_df.iloc[:, -1], train_y_pred),
"test_accuracy": accuracy_score(test_df.iloc[:, -1], test_y_pred),
"train_F1score": f1_score(train_df.iloc[:, -1], train_y_pred),
"test_F1score": f1_score(test_df.iloc[:, -1], test_y_pred)}
return results