-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
349 lines (282 loc) · 12.3 KB
/
model.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
import tensorflow as tf
from data import Data
import numpy as np
import logging
logging.getLogger('tensorflow').disabled = True
from statistics import mean, variance
from math import sqrt
import matplotlib.pyplot as plt
class Model:
def __init__(self, data: Data, combined=False, train=True, ticker="SPY"):
self.data = data
self.epochs = 250 # May cause overfitting if too high
self.vix_var, self.vix_mean = self.get_vix_params()
self.ticker = ticker
self.combined = combined
self.model_name = self.get_model_name()
# Standardize each sample --> this makes the most sense
self.training_input, self.testing_input = self.get_standardized_samples()
self.training_input_vol, self.testing_input_vol = self.create_vix_input()
# Get targets to predict change in vol
self.training_output, self.testing_output = self.target_data_delta()
if train:
if combined:
self.model = self.define_combined_lstm_model()
self.train_combined_model()
else:
self.model = self.define_lstm_model()
self.train_model()
self.load_model()
if combined:
self.eval_combined_model()
else:
self.eval_model()
def get_model_name(self):
'''
Finds the model name using the ticker and whether it is the combined model
'''
if self.combined:
filename = self.ticker + '_' + 'combined_lstm_model'
else:
filename = self.ticker + '_' + 'lstm_model'
return filename
def get_vix_params(self):
'''
Finds average and variance of the IV data
:return:
'''
vix_data = self.data.training_vix_data_split
vix_var = variance(vix_data)
vix_mean = mean(vix_data)
return vix_var, vix_mean
def create_vix_input(self):
training_input_vol = self._get_all_vol_input(self.data.training_data)
testing_input_vol = self._get_all_vol_input(self.data.testing_data)
return training_input_vol, testing_input_vol
def _get_all_vol_input(self, vix_list):
'''
Creates the set of initial IV values for the combined model
:param vix_list: list of samples where sample = Tuple([price],[IV])
:return: IV[0] for each sample
'''
input_vol = []
for sample in vix_list:
initial_vol = sample[1][0]
std_sample = self.standardize_vix_sample(initial_vol)
input_vol.append(std_sample)
return input_vol
def standardize_vix_sample(self, vix_val, epsilon=1e-8):
'''
Standardize vix values, assumes mean and var already found
'''
return (vix_val - self.vix_mean) / sqrt(self.vix_var + epsilon)
def unstandardize_vix_sample(self, prediction, epsilon=1e-8):
'''
Convert a standardized output back to an unstandardized output
'''
return prediction * sqrt(self.vix_var + epsilon) + - self.vix_mean
def eval_model(self):
'''
Evalutes the standard LSTM model
Plots predicted IV change and IV
'''
output_arr = []
actual_arr = []
vix_actual = self.data.testing_vix_data_split[0:9]
vix_predicted = self.data.testing_vix_data_split[0:9]
start_ind = 1
end_ind = 10
correct_sign = 0
for ind, sample in enumerate(self.testing_input):
reshaped_sample = sample.reshape(1, 1, 10)
prediction = self.model.predict(reshaped_sample)[0][0]
output_arr.append(prediction[0])
actual_arr.append(self.testing_output[ind])
if np.sign(self.testing_output[ind]) == np.sign(prediction[0]):
correct_sign = correct_sign + 1
vix_actual.append(self.data.testing_vix_data_split[start_ind] + self.testing_output[ind])
vix_predicted.append(self.data.testing_vix_data_split[start_ind] + prediction[0])
start_ind += 1
end_ind += 1
print('Correlation with IV change:', np.corrcoef(output_arr, actual_arr))
print('Correlation with IV:', np.corrcoef(vix_predicted, vix_actual))
print('Sign Percentage:', correct_sign/len(actual_arr))
self.plot_change_in_vol(output_arr, actual_arr, combined=False)
self.plot_vol(vix_predicted, vix_actual, combined=False)
def eval_combined_model(self):
'''
Evalutes the combined LSTM model (with additional non-sequential data)
Plots predicted IV change and IV
'''
output_arr = []
actual_arr = []
vix_actual = self.data.testing_vix_data_split[0:9]
vix_predicted = self.data.testing_vix_data_split[0:9]
start_ind = 1
end_ind = 10
correct_sign = 0
for ind, sample in enumerate(self.testing_input):
reshaped_sample = sample.reshape(1, 1, 10)
vix_input = self.testing_input_vol[ind]
std_vix_input = self.standardize_vix_sample(vix_input)
reshaped_vix_sample = std_vix_input.reshape(1, 1, 1)
prediction = self.model.predict({"vol_input": reshaped_vix_sample, "lstm_input":reshaped_sample})[0][0]
output_arr.append(prediction[0])
actual_arr.append(self.testing_output[ind])
if np.sign(self.testing_output[ind]) == np.sign(prediction[0]):
correct_sign = correct_sign + 1
vix_actual.append(self.data.testing_vix_data_split[start_ind] + self.testing_output[ind])
vix_predicted.append(self.data.testing_vix_data_split[start_ind] + prediction[0])
start_ind += 1
end_ind += 1
print('Correlation with IV change:', np.corrcoef(output_arr, actual_arr))
print('Correlation with IV:', np.corrcoef(vix_predicted, vix_actual))
print('Sign Percentage:', correct_sign / len(actual_arr))
plt.figure()
plt.plot(output_arr, label='Output')
plt.plot(actual_arr, label='Actual')
plt.ylabel('Predicted Change in Volatility', fontsize=18)
plt.title('Predicted vs Actual Change in Volatility, Combined '+ self.ticker, fontsize=18)
plt.legend()
plt.show()
self.plot_change_in_vol(output_arr, actual_arr, combined=True)
self.plot_vol(vix_predicted, vix_actual, combined=True)
def plot_change_in_vol(self, output_arr, actual_arr, combined=False):
'''
Plots predicted IV change vs actual IV change
'''
plt.figure()
plt.plot(output_arr, label='Output')
plt.plot(actual_arr, label='Actual')
plt.ylabel('Predicted Change in Volatility', fontsize=18)
if combined:
plt.title('Predicted vs Actual Change in Volatility, Combined ' + self.ticker, fontsize=18)
else:
plt.title('Predicted vs Actual Change in Volatility ' + self.ticker, fontsize=18)
plt.legend()
plt.show()
def plot_vol(self, vix_predicted, vix_actual, combined=False):
'''
Plots predicted IV vs actual IV
'''
plt.figure()
plt.plot(vix_predicted, label='Output')
plt.plot(vix_actual, label='Actual')
plt.ylabel('Predicted Value of Volatility', fontsize=18)
if combined:
plt.title('Predicted vs Actual Volatility, Combined ' + self.ticker, fontsize=18)
else:
plt.title('Predicted vs Actual Volatility ' + self.ticker, fontsize=18)
plt.legend()
plt.show()
def define_combined_lstm_model(self):
'''
LSTM with additional non-time dependent input
:return:
'''
# Step 1: Define LSTM input
lstm_inputs = tf.keras.Input(shape=(None, 10), name="lstm_input")
lstm_layer = tf.keras.layers.LSTM(10, return_sequences=True, return_state=True, activation='relu')
lstm_outputs, state_h, state_c = lstm_layer(lstm_inputs)
# Step 2: Define Vol input
init_vol_inputs = tf.keras.Input(shape=(None, 1), name="vol_input")
# Concatenate LSTM output with the initial volitility
concat_layer = tf.keras.layers.concatenate([init_vol_inputs, lstm_outputs])
# Output a single numerical value
output_layer = tf.keras.layers.Dense(1)
network_output = output_layer(concat_layer)
lstm_model = tf.keras.Model(inputs=[init_vol_inputs, lstm_inputs], outputs=network_output)
# Compile model
lstm_model.compile(optimizer="adam", loss="mse", metrics=["accuracy"])
print(lstm_model.summary())
return lstm_model
def define_lstm_model(self):
'''
This is set up for a single output value
:return:
'''
# Step 1: Define input
lstm_inputs = tf.keras.Input(shape=(None, 10))
lstm_layer = tf.keras.layers.LSTM(10, return_sequences=True, return_state=True, activation='relu')
lstm_outputs, state_h, state_c = lstm_layer(lstm_inputs)
# Output a single numerical value
output_layer = tf.keras.layers.Dense(1)
network_output = output_layer(lstm_outputs)
lstm_model = tf.keras.Model(inputs=lstm_inputs, outputs=network_output)
# Compile model
lstm_model.compile(optimizer="adam", loss="mse", metrics=["accuracy"])
print(lstm_model.summary())
return lstm_model
def load_model(self):
'''
Loads saved model
:return:
'''
self.model = tf.keras.models.load_model(self.model_name)
print('Loaded model:', self.model_name)
def train_model(self):
'''
Train standard LSTM model
:return:
'''
reshaped_input = np.asarray(self.training_input).reshape(len(self.training_input), 1, 10)
self.model.fit(reshaped_input, np.asarray(self.training_output), epochs=self.epochs, validation_split=0.2, batch_size=5)
self.model.save(self.model_name)
print("Saved:", self.model_name)
def train_combined_model(self):
'''
Trains the LSTM model that takes the initial volatility as an input
:return:
'''
reshaped_lstm_input = np.asarray(self.training_input).reshape(len(self.training_input), 1, 10)
reshaped_vol_input = np.asarray(self.training_input_vol).reshape(len(self.training_input_vol), 1, 1)
self.model.fit({"vol_input": reshaped_vol_input, "lstm_input": reshaped_lstm_input}, np.asarray(self.training_output), epochs=self.epochs, validation_split=0.2, batch_size=5)
self.model.save(self.model_name)
print("Saved:", self.model_name)
def target_data_delta(self):
'''
Defines target output as the change in vol over the given time period
:return:
'''
training_targets = []
for sample in self.data.training_data:
training_targets.append(self._vol_diff(sample))
testing_targets = []
for sample in self.data.testing_data:
testing_targets.append(self._vol_diff(sample))
return training_targets, testing_targets
def target_data_all(self):
'''
Returns training data consisting of the list of 10 IV values
:return:
'''
training_targets = []
for sample in self.data.training_data:
training_targets.append(sample[1])
testing_targets = []
for sample in self.data.testing_data:
testing_targets.append(sample[1])
return training_targets, testing_targets
def get_standardized_samples(self):
'''
Standardizes each sample individually
:return:
'''
training_samples = []
for sample in self.data.training_data:
std_sample = self._standardize_sample(sample[0])
training_samples.append(std_sample)
testing_samples = []
for sample in self.data.testing_data:
std_sample = self._standardize_sample(sample[0])
testing_samples.append(std_sample)
return training_samples, testing_samples
@staticmethod
def _standardize_sample(sample, epsilon=1e-8):
mean_val = mean(sample)
var = variance(sample)
standardized_sample = (sample - mean_val) / sqrt(var + epsilon)
return standardized_sample
@staticmethod
def _vol_diff(sample):
return sample[1][-1] - sample[1][0]