-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
250 lines (215 loc) · 10.2 KB
/
main.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
import itertools
import matplotlib.pyplot as plt
import os
import pandas as pd
from real_eval import real_eval
from dataset import FeatureDataset
from framework import BaseFramework
import parser
from preprocessing import DataPreprocessor, get_ticker_symbols
from architectures import (
ShallowRegressionLSTM,
DoubleRegressionLSTM,
QuadRegressionLSTM,
DeepRegressionLSTM,
ShallowLSTM,
ShallowMovementLSTM,
)
from constants import PRICE_MODEL_PATH, VAL_MODEL_FILE_NAME
import torch
from torch.utils.data import DataLoader
# -----------------------------------------------------------------------------------------#
# Collect Hyperparameters #
# -----------------------------------------------------------------------------------------#
args = parser.parse_args()
training_batch_sizes = args.batch_size
validation_split = args.val_split[0]
learning_rates = args.learning_rate
weight_decays = args.weight_decay
epochs_arr = args.epochs
sequence_lengths = args.days_prior
sequence_seps = args.sequence_sep
use_pretrained = args.use_pretrained
num_hidden_units_arr = args.num_hidden_units
norm_hist_lengths = args.norm_hist_length
predict_movement = args.predict_movement
architectures = []
# architectures.append(ShallowRegressionLSTM)
architectures.append(ShallowLSTM)
# architectures.append(DoubleRegressionLSTM)
# architectures.append(QuadRegressionLSTM)
# architectures.append(DeepRegressionLSTM)
def get_hyperparameter_combos(*hyperparameters):
return list(itertools.product(*hyperparameters[0]))
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Using {device} device")
starting_value = 100
ticker_symbols = get_ticker_symbols(1)
preprocessors = {}
train_dfs = {}
val_dfs = {}
# -----------------------------------------------------------------------------------------#
# Preprocess the Data #
# -----------------------------------------------------------------------------------------#
for ticker_symbol in ticker_symbols:
preprocessors[ticker_symbol] = DataPreprocessor(
ticker_symbol=ticker_symbol,
validation_split=validation_split,
predict_movement=predict_movement
)
preprocessors[ticker_symbol].pre_process_data()
train_dfs[ticker_symbol], val_dfs[ticker_symbol] = preprocessors[ticker_symbol].get_dfs()
if ticker_symbol == ticker_symbols[0]:
feature_columns = preprocessors[ticker_symbol].get_feature_columns()
target_column = preprocessors[ticker_symbol].get_target_column()
num_features = len(feature_columns)
# plt.ion()
for i, (
training_batch_size,
learning_rate,
weight_decay,
epochs,
sequence_length,
sequence_sep,
architecture,
num_hidden_units,
norm_hist_length,
) in enumerate(get_hyperparameter_combos([
training_batch_sizes,
learning_rates,
weight_decays,
epochs_arr,
sequence_lengths,
sequence_seps,
architectures,
num_hidden_units_arr,
norm_hist_lengths,
])):
print(f"""
-------------------------------------------------------------------------------------
Hyperparameters for Version {i+1}:
Training Batch Size: {training_batch_size} Examples
Learning Rate: {learning_rate}
Number of Epochs: {epochs} Epochs
History Considered: {sequence_length} Days
History Considered in Norm: {norm_hist_length} Days
Sequence Seperation: {sequence_sep} Days
Number of Hidden Units: {num_hidden_units} Units
Architecture: {architecture.__name__}
-------------------------------------------------------------------------------------
""")
train_acc_sum = 0.0
val_acc_sum = 0.0
hold_value_sum = 0.0
model_value_sum = 0.0
for ticker_symbol in ticker_symbols:
train_df = train_dfs[ticker_symbol]
val_df = val_dfs[ticker_symbol]
norm_train_df, norm_val_df = preprocessors[ticker_symbol].normalize_pre_processed_data(norm_hist_length)
# -----------------------------------------------------------------------------------------#
# Create the datasets and dataloaders #
# -----------------------------------------------------------------------------------------#
training_dataset = FeatureDataset(
dataframe=norm_train_df, features=feature_columns, target=target_column, sequence_length=sequence_length, sequence_sep=sequence_sep)
val_dataset = FeatureDataset(
dataframe=norm_val_df, features=feature_columns, target=target_column, sequence_length=sequence_length, sequence_sep=sequence_sep)
training_loader = DataLoader(
training_dataset, batch_size=training_batch_size, shuffle=False)
val_loader = DataLoader(
val_dataset, batch_size=1, shuffle=False)
# -----------------------------------------------------------------------------------------#
# Model, Optimizer, Loss #
# -----------------------------------------------------------------------------------------#
if use_pretrained:
model = torch.load(
os.path.join(PRICE_MODEL_PATH, os.path.join(ticker_symbol, VAL_MODEL_FILE_NAME))
)
else:
model = architecture(
sequence_length=sequence_length, num_features=num_features, hidden_units=num_hidden_units)
model.to(device)
loss_fn = torch.nn.MSELoss()
if predict_movement:
loss_fn = torch.nn.BCEWithLogitsLoss()
optimizer = torch.optim.Adam(
model.parameters(), lr=learning_rate, weight_decay=weight_decay)
# -----------------------------------------------------------------------------------------#
# Train the model #
# -----------------------------------------------------------------------------------------#
framework = BaseFramework(
model=model, loss_function=loss_fn, ticker_symbol=ticker_symbol)
if not use_pretrained:
losses = framework.train(
train_loader=training_loader, epochs=epochs, optimizer=optimizer)
# -----------------------------------------------------------------------------------------#
# Evaluate the model #
# -----------------------------------------------------------------------------------------#
train_data = framework.eval(
training_loader, is_training_data=True)
train_acc_sum += train_data['accuracy']
# print(
# f"Training for {ticker_symbol} done. Accuracy: {train_data['accuracy'] * 100:.2f}%"
# )
val_data = framework.eval(
val_loader)
val_acc_sum += val_data["accuracy"]
# print(
# f"Validation for {ticker_symbol} done. Accuracy: {val_data['accuracy'] * 100:.2f}%"
# )
regular_strat = 0
model_based_strat = 0
if predict_movement:
regular_strat = 1
model_based_strat = 1
else:
regular_strat, model_based_strat = real_eval(model, val_df, norm_val_df, feature_columns, target_column, starting_value=starting_value, sequence_length=sequence_length, norm_hist_length=norm_hist_length, sequence_sep=sequence_sep)
hold_value_sum += regular_strat
model_value_sum += model_based_strat
# -----------------------------------------------------------------------------------------#
# Plot results #
# -----------------------------------------------------------------------------------------#
name = f"Version {i+1}"
plt.figure()
plt.plot(norm_train_df.index.values[:len(train_data["targets"])],
train_data["targets"], label=f"{name} Target")
plt.plot(norm_train_df.index.values[:len(train_data["predictions"])],
train_data["predictions"], label=f"{name} Prediction",)
plt.xlabel("Date")
plt.ylabel("Predicted Values")
plt.suptitle(f"Training Data Predictions for {ticker_symbol}")
plt.legend()
plt.grid()
plt.show()
# plt.pause(0.1)
plt.figure()
plt.plot(norm_val_df.index.values[:len(val_data["targets"])],
val_data["targets"], label=f"{name} Target", marker=".")
plt.plot(norm_val_df.index.values[:len(val_data["predictions"])],
val_data["predictions"], label=f"{name} Prediction", marker=".")
plt.xlabel("Date")
plt.ylabel("Predicted Values")
plt.title(f"Validation Data Predictions for {ticker_symbol}")
plt.legend()
plt.grid()
plt.show()
# plt.pause(0.1)
# -----------------------------------------------------------------------------------------#
# Update best stats and weights #
# -----------------------------------------------------------------------------------------#
framework.save_model(sequence_length, num_hidden_units, sequence_sep, is_training=True, predict_movement=predict_movement)
framework.save_model(sequence_length, num_hidden_units, sequence_sep, is_training=False, predict_movement=predict_movement)
train_acc = train_acc_sum / len(ticker_symbols) * 100
val_acc = val_acc_sum / len(ticker_symbols) * 100
start_date = val_df.index[0].strftime("%B %-d, %Y")
end_date = val_df.index[-1].strftime("%B %-d, %Y")
hold_value = hold_value_sum / len(ticker_symbols)
model_value = model_value_sum / len(ticker_symbols)
improvement = (model_value - hold_value) / hold_value
print(f"""
Training done.
Average training accuracy: {train_acc:.2f}%.
Average Validation Accuracy: {val_acc:.2f}%.
If you started with ${starting_value}:
Buying equal weights on {start_date} and holding would result in ${hold_value:.2f} on {end_date}.
Buying and Selling equal weights based on the model starting on {start_date} would result in ${model_value:.2f} on {end_date}.
This is is an average improvement of {improvement * 100:.2f}%.""")