-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiple_ticker_models.py
More file actions
380 lines (295 loc) · 13.4 KB
/
multiple_ticker_models.py
File metadata and controls
380 lines (295 loc) · 13.4 KB
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import math
from functools import lru_cache
from typing import List, Dict
import pandas as pd
import copy
from APIs.close_price import get_all_adjusted_prices
from sklearn.preprocessing import MinMaxScaler
import numpy as np
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import seaborn as sns
import random
from tqdm import tqdm
model_path = './models'
# 'LSTM' or 'GRU'
# If randomize testing is True, randomly choose test tickers from ticker list
# Otherwise, training and testing tickers must be specified.
randomize_testing = False
ticker_list = ['AAPL', 'MSFT', 'GOOGL', 'NVDA', 'META', 'UBER', 'TSLA', 'ORCL', 'CRM', 'NFLX']
training_split = 0.8
test_split = 1 - training_split
# If randomize testing is false, use existing training and testing list
training_tickers = ['MSFT', 'GOOGL', 'META', 'TLSA', 'ORCL', 'CRM', 'NFLX']
# testing_tickers = ['NVDA', 'UBER', 'AAPL']
testing_tickers = ['HUBS']
scalers = {}
dates = {}
input_dim = 1
# nodes per layer
output_dim = 1
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# sns.set_style("darkgrid")
class LSTM(nn.Module):
def __init__(self, input_dim, hidden_dim, num_layers, output_dim):
super(LSTM, self).__init__()
self.hidden_dim = hidden_dim
self.num_layers = num_layers
self.lstm = nn.LSTM(input_dim, hidden_dim, num_layers, batch_first=True)
self.fc = nn.Linear(hidden_dim, output_dim)
def forward(self, x):
h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_dim, device=device).requires_grad_()
c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_dim, device=device).requires_grad_()
# out, (hn, cn) = self.lstm(x, (h0.detach(), c0.detach()))
out, _ = self.lstm(x, (h0.detach(), c0.detach()))
out = self.fc(out[:, -1, :])
return out
def save_model(self, path=f"{model_path}/lstm_multiple_ticker.pt"):
torch.save(self.state_dict(), path)
class GRU(nn.Module):
def __init__(self, input_dim, hidden_dim, num_layers, output_dim):
super(GRU, self).__init__()
self.hidden_dim = hidden_dim
self.num_layers = num_layers
self.gru = nn.GRU(input_dim, hidden_dim, num_layers, batch_first=True)
self.fc = nn.Linear(hidden_dim, output_dim)
def forward(self, x):
h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_dim, device=device).requires_grad_()
out, (hn) = self.gru(x, (h0.detach()))
out = self.fc(out[:, -1, :])
return out
def save_model(self, path=f"{model_path}/gru_multiple_ticker.pt"):
torch.save(self.state_dict(), path)
# @lru_cache(maxsize=None)
def get_data_frames(tickers, existing_training, existing_testing, randomize):
if not randomize:
tickers = existing_training + existing_testing
# Mappings of ticker to data frame
result = dict()
for i in tqdm(range(len(tickers))):
ticker = tickers[i]
data = get_all_adjusted_prices(ticker, verbose=False)
df = pd.DataFrame(data, columns=["Date", "Close"])
# dates = df["Date"].values
dates[ticker] = list(df["Date"])
df["Date"] = pd.to_datetime(df["Date"])
df["Close"] = df["Close"].astype(float)
df = df.set_index("Date")
result[ticker] = df
return result
def preprocess(ticker_dataframes_map):
ticker_prices_map = dict()
for ticker in ticker_dataframes_map:
df = ticker_dataframes_map[ticker]
scaler = MinMaxScaler(feature_range=(-1, 1))
prices = df.copy()[["Close"]]
prices["Close"] = scaler.fit_transform(prices["Close"].values.reshape(-1, 1))
scalers[ticker] = scaler
# dates[ticker] = df.copy()[["Date"]]
ticker_prices_map[ticker] = prices
return ticker_prices_map
def sequence_and_split(ticker_prices_map, sequence_length, randomize):
x_train = dict()
y_train = dict()
x_test = dict()
y_test = dict()
complete_x = dict()
complete_y = dict()
for ticker in ticker_prices_map:
stock = ticker_prices_map[ticker]
raw = stock.to_numpy()[::-1]
data = []
for index in range(len(raw) - sequence_length):
data.append(raw[index: index + sequence_length])
data = np.array(data)
x = data[:, :-1]
y = data[:, -1]
complete_x[ticker] = x
complete_y[ticker] = y
# x_train[i] = some sequence of prices
# y_train[i] = the next price
if randomize:
test_stock_size = max(round(len(complete_x.keys()) * test_split), 1)
test_stock_tickers = random.sample(list(complete_x.keys()), test_stock_size)
for ticker in complete_x:
if ticker in test_stock_tickers:
x_test[ticker] = complete_x[ticker]
y_test[ticker] = complete_y[ticker]
else:
x_train[ticker] = complete_x[ticker]
y_train[ticker] = complete_y[ticker]
else:
for ticker in complete_x:
if ticker in training_tickers:
x_train[ticker] = complete_x[ticker]
y_train[ticker] = complete_y[ticker]
else:
x_test[ticker] = complete_x[ticker]
y_test[ticker] = complete_y[ticker]
return [x_train, y_train, x_test, y_test]
def train(model, ticker_x_map, ticker_y_map, num_epochs, lr, verbose=True):
for ticker in ticker_x_map:
if verbose:
print(f"Training using {ticker}...")
x_train = torch.from_numpy(ticker_x_map[ticker]).type(torch.Tensor).to(device)
y_train = torch.from_numpy(ticker_y_map[ticker]).type(torch.Tensor).to(device)
criterion = torch.nn.MSELoss(reduction='mean')
optimizer = torch.optim.Adam(model.parameters(), lr=lr)
history = np.zeros(num_epochs)
for t in range(num_epochs):
prediction = model(x_train)
loss = criterion(prediction, y_train)
if verbose:
print(f"Epoch: {t}, MSE: {loss.item()}")
history[t] = loss.item()
optimizer.zero_grad()
loss.backward()
optimizer.step()
if verbose:
print()
model.save_model()
return model
def eval_and_plot_model(model, x_train_map, y_train_map, x_test_map, y_test_map, model_type, sequence_length):
for ticker in x_test_map:
x_test = torch.from_numpy(x_test_map[ticker]).type(torch.Tensor).to(device)
# y_test = torch.from_numpy(y_test_map[ticker]).type(torch.Tensor).to(device)
y_test = y_test_map[ticker]
testing_predictions = model(x_test)
testing_predictions = testing_predictions.cpu().detach().numpy()
test_rmse = mean_squared_error(y_test[:, 0], testing_predictions[:, 0])
print(f"MSE for {model_type} with {ticker}: {test_rmse}")
plot_singular_complete(testing_predictions, y_test, ticker, model_type, sequence_length)
def average_mse_test(model, x_test_map, y_test_map):
total_mse = 0
for ticker in x_test_map:
x_test = torch.from_numpy(x_test_map[ticker]).type(torch.Tensor).to(device)
# y_test = torch.from_numpy(y_test_map[ticker]).type(torch.Tensor).to(device)
y_test = y_test_map[ticker]
testing_predictions = model(x_test)
testing_predictions = testing_predictions.cpu().detach().numpy()
total_mse += mean_squared_error(y_test[:, 0], testing_predictions[:, 0])
return total_mse / len(x_test_map)
def plot_singular_complete(predicted, actual, ticker, model_info, sequence_length):
scaler_predicted = copy.deepcopy(scalers[ticker])
scaler_actual = copy.deepcopy(scalers[ticker])
predicted = scaler_predicted.inverse_transform(predicted)
actual = scaler_actual.inverse_transform(actual)
dates_x = dates[ticker]
dates_x.reverse()
dates_x = dates_x[sequence_length:]
dates_x = pd.to_datetime(dates_x)
df = pd.DataFrame({'Actual price': actual.flatten(),
'Predicted price': predicted.flatten()},
index=dates_x)
fig = plt.figure()
fig.subplots_adjust(hspace=0.4, wspace=0.2)
fig.set_figheight(8)
fig.set_figwidth(16)
sns.set_style("darkgrid")
plt.subplot(2, 1, 1)
sns.lineplot(data=df, x=df.index, y='Actual price', label='Actual')
sns.lineplot(data=df, x=df.index, y='Predicted price', label='Predicted')
n = len(dates_x)
step_size = max(n // 4, 1) # Divide into roughly 4 sections
display_dates = dates_x[::-step_size]
plt.xticks(display_dates)
plt.xlabel("Date")
plt.ylabel("Stock Price")
plt.title(f"{ticker} Predictions - {model_info} All dates")
plt.legend()
recent_days_back = 365
predicted_recent = predicted[-recent_days_back:]
actual_recent = actual[-recent_days_back:]
dates_x_recent = dates_x[-recent_days_back:]
df_recent = pd.DataFrame({'Actual price': actual_recent.flatten(),
'Predicted price': predicted_recent.flatten()},
index=dates_x_recent)
sns.set_style("darkgrid")
plt.subplot(2, 1, 2)
sns.lineplot(data=df_recent, x=df_recent.index, y='Actual price', label='Actual')
sns.lineplot(data=df_recent, x=df_recent.index, y='Predicted price', label='Predicted')
n = len(dates_x_recent)
step_size = max(n // 4, 1)
display_dates_recent = dates_x_recent[::-step_size]
plt.xticks(display_dates_recent)
plt.xlabel("Date")
plt.ylabel("Stock Price")
plt.title(f"{ticker} Predictions - {model_info} Recent {recent_days_back} weekdays")
plt.legend()
plt.show()
def run_with_training(training, testing, use_model, sequence_length,
hidden_dim, num_layers, num_epochs, lr):
ticker_dataframes_map = get_data_frames(tuple(ticker_list), tuple(training), tuple(testing), False)
prices = preprocess(ticker_dataframes_map)
x_train, y_train, x_test, y_test = sequence_and_split(prices, sequence_length, False)
print(f"Training tickers: {str(list(x_train.keys()))}")
print(f"Testing tickers: {str(list(x_test.keys()))}")
x_train_copy = copy.deepcopy(x_train)
y_train_copy = copy.deepcopy(y_train)
if use_model == 'LSTM':
lstm = train(LSTM(input_dim, hidden_dim, num_layers, output_dim)
.to(device), x_train, y_train, num_epochs, lr)
# UNCOMMENT FOR LOADING SAVED MODEL
# lstm = LSTM(input_dim, hidden_dim, num_layers, output_dim).to(device)
# lstm.load_state_dict(torch.load(f"{model_path}/lstm_multiple_ticker.pt"))
# lstm.eval()
eval_and_plot_model(lstm, x_train_copy, y_train_copy, x_test, y_test, "LSTM", sequence_length)
elif use_model == 'GRU':
# gru = train(GRU(input_dim, hidden_dim, num_layers, output_dim)
# .to(device), x_train, y_train, num_epochs, lr)
# UNCOMMENT FOR LOADING SAVED MODEL
gru = GRU(input_dim, hidden_dim, num_layers, output_dim).to(device)
gru.load_state_dict(torch.load(f"{model_path}/gru_multiple_ticker.pt", map_location=torch.device('cpu')))
gru.eval()
eval_and_plot_model(gru, x_train_copy, y_train_copy, x_test, y_test, "GRU", sequence_length)
def run_and_eval(training, testing, use_model, sequence_length,
hidden_dim, num_layers, num_epochs, lr, verbose=False):
ticker_dataframes_map = get_data_frames(tuple(ticker_list), tuple(training), tuple(testing), False)
prices = preprocess(ticker_dataframes_map)
x_train, y_train, x_test, y_test = sequence_and_split(prices, sequence_length, False)
x_train_copy = copy.deepcopy(x_train)
y_train_copy = copy.deepcopy(y_train)
model = None
if use_model == 'LSTM':
model = train(LSTM(input_dim, hidden_dim, num_layers, output_dim).to(device), x_train, y_train, num_epochs, lr, verbose)
elif use_model == 'GRU':
model = train(GRU(input_dim, hidden_dim, num_layers, output_dim).to(device), x_train, y_train, num_epochs, lr, verbose)
avg_mse = average_mse_test(model, x_train_copy, y_train_copy)
return model, avg_mse
if __name__ == '__main__':
run_with_training(training_tickers, testing_tickers, 'GRU', 20, 32, 2, 100, 0.01)
# ticker_dataframes_map = get_data_frames(ticker_list, training_tickers, testing_tickers)
# prices = preprocess(ticker_dataframes_map)
# x_train, y_train, x_test, y_test = sequence_and_split(prices)
#
# print(f"Training tickers: {str(list(x_train.keys()))}")
# print(f"Testing tickers: {str(list(x_test.keys()))}")
#
# x_train_copy = copy.deepcopy(x_train)
# y_train_copy = copy.deepcopy(y_train)
#
# LOAD_SAVED_MODEL = False
#
# if LOAD_SAVED_MODEL:
# if use_model == 'LSTM':
# lstm = LSTM().to(device)
# lstm.load_state_dict(torch.load(f"{model_path}/lstm_multiple_ticker.pt"))
# lstm.eval()
#
# eval_model(lstm, x_train_copy, y_train_copy, x_test, y_test, "LSTM")
#
# elif use_model == 'GRU':
# gru = GRU().to(device)
# gru.load_state_dict(torch.load(f"{model_path}/gru_multiple_ticker.pt"))
# gru.eval()
# else:
# if use_model == 'LSTM':
# lstm = train(LSTM().to(device), x_train, y_train)
#
# eval_model(lstm, x_train_copy, y_train_copy, x_test, y_test, "LSTM")
# elif use_model == 'GRU':
# gru = train(GRU().to(device), x_train, y_train)
#
# eval_model(gru, x_train_copy, y_train_copy, x_test, y_test, "GRU")