-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathGP2.final.py
270 lines (235 loc) · 9.68 KB
/
GP2.final.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
# IE598 Machine Learning in Finance, Fall 2018
# University of Illinois at Urbana-Champaign
#
# Final Group Project
#
# Authors: Joseph Loss, Ruozhong Yang, Fengkai Xu, Biao Feng, and Yuchen Duan
#
# source code available at https://github.com/chicago-joe/Machine-Learning-in-Finance-Final-Project
# --------------------------------------------------------------------------------
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from sklearn.ensemble import GradientBoostingRegressor, RandomForestRegressor
from sklearn.feature_selection import SelectFromModel
from sklearn.linear_model import LinearRegression, Lasso, Ridge
from sklearn.metrics import mean_squared_error as MSE, r2_score, mean_squared_error
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
df = pd.read_excel('C:\\Users\\jloss\\PyCharmProjects\\Machine-Learning-in-Finance-Final-Project\\data\\GP2_EconCycle.xlsx', sep = ',')
cols = ['T1Y Index', 'T2Y Index', 'T3Y Index', 'T5Y Index', 'T7Y Index', 'T10Y Index', 'CP1M', 'CP3M', 'CP6M',
'CP1M_T1Y', 'CP3M_T1Y', 'CP6M_T1Y', 'USPHCI', 'PCT 3MO FWD', 'PCT 6MO FWD', 'PCT 9MO FWD']
## Exploratory Data Analysis
df.dropna(inplace = True)
print(df.shape, df.info(), df.describe(), df.head())
CPTcols = ['CP1M_T1Y', 'CP3M_T1Y', 'CP6M_T1Y', 'USPHCI']
sns.pairplot(df[CPTcols], dropna = True, )
# plt.tight_layout()
# plt.savefig('E:\Study\Courses\Fall 2018\IE 598\IE598 Homework\Group Project\scatter_GP2_.png',dpi = 500)
plt.show()
cm = np.corrcoef(df[cols].values.T)
hm = sns.heatmap(cm,
cbar = False,
annot = True,
square = False,
fmt = '.1f',
annot_kws = { 'size':8 },
yticklabels = cols,
xticklabels = cols)
# plt.tight_layout()
# plt.savefig('E:\Study\Courses\Fall 2018\IE 598\IE598 Homework\Group Project\heatmap_rate_GP2_.png',dpi = 15000)
plt.show()
## 3- month prediction and model
X = np.array(df.drop(['USPHCI', 'PCT 3MO FWD', 'PCT 6MO FWD', 'PCT 9MO FWD'], 1))
y = np.array(df['PCT 3MO FWD'])
sc_x = StandardScaler()
sc_y = StandardScaler()
X_std = sc_x.fit_transform(X)
y_std = sc_y.fit_transform(y[:, np.newaxis]).flatten()
X_train, X_test, y_train, y_test = train_test_split(X_std, y_std, test_size = 0.1, random_state = 42)
# feature importance
feat_labels = cols[:-4]
forest = RandomForestRegressor(n_estimators = 500, random_state = 1)
forest.fit(X_train, y_train)
importances = forest.feature_importances_
indices = np.argsort(importances)[::-1]
print("3MO FWD RATE - Feature Importance")
for f in range(X_train.shape[1]):
print("%2d) %-*s %f" % (f + 1, 30, feat_labels[indices[f]], importances[indices[f]]))
print('\n')
plt.title('Feature Importance PCT 3MO FWD')
plt.bar(range(X_train.shape[1]), importances[indices], align = 'center')
plt.xticks(range(X_train.shape[1]), feat_labels, rotation = 90)
plt.xlim([-1, X_train.shape[1]])
plt.show()
# Selection
model = SelectFromModel(forest, prefit = True)
X_train = model.transform(X_train)
X_test = model.transform(X_test)
print(X_test.shape)
print(X_train.shape)
# LinearRegression
reg = LinearRegression()
reg.fit(X_train, y_train)
y_train_pred = reg.predict(X_train)
y_test_pred = reg.predict(X_test)
plt.scatter(y_train_pred, y_train_pred - y_train,
c = 'steelblue', marker = 'o', edgecolor = 'white',
label = 'Training data')
plt.scatter(y_test_pred, y_test_pred - y_test,
c = 'limegreen', marker = 's', edgecolor = 'white',
label = 'Test data')
plt.xlabel('Predicted values')
plt.ylabel('Residuals')
plt.legend(loc = 'upper left')
plt.hlines(y = 0, xmin = 0, xmax = 1, color = 'black', lw = 2)
plt.xlim([0, 1])
plt.savefig('LinearRegression.png', dpi = 300)
plt.show()
print('(LR)MSE train: %.3f, test: %.3f' % (
mean_squared_error(y_train, y_train_pred),
mean_squared_error(y_test, y_test_pred)))
print('(LR)R^2 train: %.3f, test: %.3f' % (
r2_score(y_train, y_train_pred),
r2_score(y_test, y_test_pred)))
print('(LR)Slope: %.3f' % reg.coef_[0])
print('(LR)Intercept: %.3f' % reg.intercept_)
## 6 month prediction and model
X = np.array(df.drop(['USPHCI', 'PCT 3MO FWD', 'PCT 6MO FWD', 'PCT 9MO FWD'], 1))
y = np.array(df['PCT 6MO FWD'])
sc_x = StandardScaler()
sc_y = StandardScaler()
X_std = sc_x.fit_transform(X)
y_std = sc_y.fit_transform(y[:, np.newaxis]).flatten()
X_train, X_test, y_train, y_test = train_test_split(X_std, y_std, test_size = 0.1, random_state = 42)
feat_labels = cols[:-4]
forest = RandomForestRegressor(n_estimators = 500, random_state = 1)
forest.fit(X_train, y_train)
importances = forest.feature_importances_
print("6MO FWD RATE - Feature Importance")
indices = np.argsort(importances)[::-1]
for f in range(X_train.shape[1]):
print("%2d) %-*s %f" % (f + 1, 30, feat_labels[indices[f]], importances[indices[f]]))
print('\n')
plt.title('Feature Importance PCT 6MO FWD')
plt.bar(range(X_train.shape[1]), importances[indices], align = 'center')
plt.xticks(range(X_train.shape[1]), feat_labels, rotation = 90)
plt.xlim([-1, X_train.shape[1]])
plt.show()
# Selection
model = SelectFromModel(forest, prefit = True)
X_train = model.transform(X_train)
X_test = model.transform(X_test)
print(X_test.shape)
print(X_train.shape)
# RidgeRegression
alpha_space = np.logspace(-3, 0, 4)
ridge = Ridge(normalize = True)
# Compute scores over range of alphas
for alpha in alpha_space:
# Specify the alpha value to use: ridge.alpha
ridge.alpha = alpha
ridge.fit(X_train, y_train)
y_train_pred = ridge.predict(X_train)
y_test_pred = ridge.predict(X_test)
plt.scatter(y_train_pred, y_train_pred - y_train,
c = 'steelblue', marker = 'o', edgecolor = 'white',
label = 'Training data')
plt.scatter(y_test_pred, y_test_pred - y_test,
c = 'limegreen', marker = 's', edgecolor = 'white',
label = 'Test data')
plt.xlabel('Predicted values')
plt.ylabel('Residuals')
plt.legend(loc = 'upper left')
plt.hlines(y = 0, xmin = 0, xmax = 1, color = 'black', lw = 2)
plt.xlim([0, 1])
plt.savefig('Ridge(alpha=' + str(alpha) + ' ).png', dpi = 300)
plt.show()
print('Ridgealpha: %.3f' % (alpha))
print('MSE train: %.3f, test: %.3f' % (
mean_squared_error(y_train, y_train_pred),
mean_squared_error(y_test, y_test_pred)))
print('R^2 train: %.3f, test: %.3f' % (
r2_score(y_train, y_train_pred),
r2_score(y_test, y_test_pred)))
print('Slope: %.3f' % ridge.coef_[0])
print('Intercept: %.3f' % ridge.intercept_)
## 9-month prediction & model
X = np.array(df.drop(['USPHCI', 'PCT 3MO FWD', 'PCT 6MO FWD', 'PCT 9MO FWD'], 1))
y = np.array(df['PCT 9MO FWD'])
sc_x = StandardScaler()
sc_y = StandardScaler()
X_std = sc_x.fit_transform(X)
y_std = sc_y.fit_transform(y[:, np.newaxis]).flatten()
X_train, X_test, y_train, y_test = train_test_split(X_std, y_std, test_size = 0.1, random_state = 42)
feat_labels = cols[:-4]
forest = RandomForestRegressor(n_estimators = 500, random_state = 1)
forest.fit(X_train, y_train)
importances = forest.feature_importances_
print("9MO FWD RATE - Feature Importance")
indices = np.argsort(importances)[::-1]
for f in range(X_train.shape[1]):
print("%2d) %-*s %f" % (f + 1, 30, feat_labels[indices[f]], importances[indices[f]]))
print('\n')
plt.title('Feature Importance: PCT 9MO FWD ')
plt.bar(range(X_train.shape[1]), importances[indices], align = 'center')
plt.xticks(range(X_train.shape[1]), feat_labels, rotation = 90)
plt.xlim([-1, X_train.shape[1]])
plt.show()
# Selection
model = SelectFromModel(forest, prefit = True)
X_train = model.transform(X_train)
X_test = model.transform(X_test)
print(X_test.shape)
print(X_train.shape)
# LassoRegression
alpha_space = np.logspace(-6, -3, 4)
lasso = Lasso(normalize = True)
# Compute scores over range of alphas
for alpha in alpha_space:
# Specify the alpha value to use: ridge.alpha
lasso.alpha = alpha
lasso.fit(X_train, y_train)
y_train_pred = lasso.predict(X_train)
y_test_pred = lasso.predict(X_test)
plt.scatter(y_train_pred, y_train_pred - y_train,
c = 'steelblue', marker = 'o', edgecolor = 'white',
label = 'Training data')
plt.scatter(y_test_pred, y_test_pred - y_test,
c = 'limegreen', marker = 's', edgecolor = 'white',
label = 'Test data')
plt.xlabel('Predicted values')
plt.ylabel('Residuals')
plt.legend(loc = 'upper left')
plt.hlines(y = 0, xmin = 0, xmax = 1, color = 'black', lw = 2)
plt.savefig('Lasso(alpha=' + str(alpha) + ' ).png', dpi = 300)
plt.xlim([0, 1])
plt.show()
print('Lassoalpha: %.6f' % (lasso.alpha))
print('MSE train: %.3f, test: %.3f' % (
mean_squared_error(y_train, y_train_pred),
mean_squared_error(y_test, y_test_pred)))
print('R^2 train: %.3f, test: %.3f' % (
r2_score(y_train, y_train_pred),
r2_score(y_test, y_test_pred)))
print('Slope: %.3f' % lasso.coef_[0])
print('Intercept: %.3f' % lasso.intercept_)
## # Part 5 - Ensemble Learning
# Set seed for reproducibility
SEED = 1
# Split dataset into 90% train and 10% test
X_train, X_test, y_train, y_test = train_test_split(X_std, y_std, test_size = 0.1, random_state = 42)
# Instantiate a GradientBoostingRegressor 'gbr'
gbr = GradientBoostingRegressor(max_features = 4, learning_rate = 0.1, n_estimators = 500,
subsample = 0.3, random_state = 42)
gbr.fit(X_train, y_train)
# Predict the test set labels
y_pred = gbr.predict(X_test)
# Evaluate the test set RMSE
mse = MSE(y_test, y_pred)
rsquared = r2_score(y_test, y_pred)
# Print the test set RMSE
print('\n')
print('Test set MSE: {:.2f}'.format(mse))
print('Test set R-Squared: {:.2f}'.format(rsquared))