mirror of
https://github.com/webclinic017/drift.git
synced 2026-08-18 05:18:09 +00:00
feat(WalkForward): added regression/classification switch, archived old experiments, wrapped the process into run_whole_pipeline() (#10)
* refactor(WalkForward): cleaned up training & evaluation code * refactor: added run_whole_pipeline(), moved all previous models to archive
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -0,0 +1,102 @@
|
||||
import keras
|
||||
|
||||
def create_basic_lstm_model(input_shape, num_classes):
|
||||
model = keras.Sequential()
|
||||
model.add(keras.layers.LSTM(units = 10, return_sequences = True, activation = 'sigmoid', input_shape=input_shape))
|
||||
model.add(keras.layers.Flatten())
|
||||
model.add(keras.layers.Dropout(0.3))
|
||||
model.add(keras.layers.Dense(units = 64, activation = 'sigmoid'))
|
||||
model.add(keras.layers.Dropout(0.3))
|
||||
model.add(keras.layers.Dense(units = 32, activation = 'sigmoid'))
|
||||
model.add(keras.layers.Dropout(0.3))
|
||||
model.add(keras.layers.Dense(units = num_classes, activation = 'linear'))
|
||||
return model
|
||||
|
||||
def create_basic_cnn_model(input_shape, num_classes):
|
||||
model = keras.Sequential()
|
||||
model.add(keras.layers.Conv1D(filters=32, kernel_size=3, padding="same", input_shape=input_shape))
|
||||
model.add(keras.layers.BatchNormalization())
|
||||
model.add(keras.layers.ReLU())
|
||||
model.add(keras.layers.Conv1D(filters=32, kernel_size=3, padding="same"))
|
||||
model.add(keras.layers.BatchNormalization())
|
||||
model.add(keras.layers.ReLU())
|
||||
model.add(keras.layers.Conv1D(filters=32, kernel_size=3, padding="same"))
|
||||
model.add(keras.layers.BatchNormalization())
|
||||
model.add(keras.layers.ReLU())
|
||||
model.add(keras.layers.GlobalAveragePooling1D())
|
||||
model.add(keras.layers.Dense(num_classes, activation="linear"))
|
||||
return model
|
||||
|
||||
|
||||
def create_resnet_cnn_model(input_shape, num_classes):
|
||||
n_feature_maps = 24
|
||||
|
||||
input_layer = keras.layers.Input(input_shape)
|
||||
|
||||
|
||||
conv_x = keras.layers.Conv1D(filters=n_feature_maps, kernel_size=8, padding='same')(input_layer)
|
||||
conv_x = keras.layers.BatchNormalization()(conv_x)
|
||||
conv_x = keras.layers.Activation('relu')(conv_x)
|
||||
|
||||
conv_y = keras.layers.Conv1D(filters=n_feature_maps, kernel_size=5, padding='same')(conv_x)
|
||||
conv_y = keras.layers.BatchNormalization()(conv_y)
|
||||
conv_y = keras.layers.Activation('relu')(conv_y)
|
||||
|
||||
conv_z = keras.layers.Conv1D(filters=n_feature_maps, kernel_size=3, padding='same')(conv_y)
|
||||
conv_z = keras.layers.BatchNormalization()(conv_z)
|
||||
|
||||
# expand channels for the sum
|
||||
shortcut_y = keras.layers.Conv1D(filters=n_feature_maps, kernel_size=1, padding='same')(input_layer)
|
||||
shortcut_y = keras.layers.BatchNormalization()(shortcut_y)
|
||||
|
||||
output_block_1 = keras.layers.add([shortcut_y, conv_z])
|
||||
output_block_1 = keras.layers.Activation('relu')(output_block_1)
|
||||
|
||||
# BLOCK 2
|
||||
|
||||
conv_x = keras.layers.Conv1D(filters=n_feature_maps * 2, kernel_size=8, padding='same')(output_block_1)
|
||||
conv_x = keras.layers.BatchNormalization()(conv_x)
|
||||
conv_x = keras.layers.Activation('relu')(conv_x)
|
||||
|
||||
conv_y = keras.layers.Conv1D(filters=n_feature_maps * 2, kernel_size=5, padding='same')(conv_x)
|
||||
conv_y = keras.layers.BatchNormalization()(conv_y)
|
||||
conv_y = keras.layers.Activation('relu')(conv_y)
|
||||
|
||||
conv_z = keras.layers.Conv1D(filters=n_feature_maps * 2, kernel_size=3, padding='same')(conv_y)
|
||||
conv_z = keras.layers.BatchNormalization()(conv_z)
|
||||
|
||||
# expand channels for the sum
|
||||
shortcut_y = keras.layers.Conv1D(filters=n_feature_maps * 2, kernel_size=1, padding='same')(output_block_1)
|
||||
shortcut_y = keras.layers.BatchNormalization()(shortcut_y)
|
||||
|
||||
output_block_2 = keras.layers.add([shortcut_y, conv_z])
|
||||
output_block_2 = keras.layers.Activation('relu')(output_block_2)
|
||||
|
||||
# BLOCK 3
|
||||
|
||||
conv_x = keras.layers.Conv1D(filters=n_feature_maps * 2, kernel_size=8, padding='same')(output_block_2)
|
||||
conv_x = keras.layers.BatchNormalization()(conv_x)
|
||||
conv_x = keras.layers.Activation('relu')(conv_x)
|
||||
|
||||
conv_y = keras.layers.Conv1D(filters=n_feature_maps * 2, kernel_size=5, padding='same')(conv_x)
|
||||
conv_y = keras.layers.BatchNormalization()(conv_y)
|
||||
conv_y = keras.layers.Activation('relu')(conv_y)
|
||||
|
||||
conv_z = keras.layers.Conv1D(filters=n_feature_maps * 2, kernel_size=3, padding='same')(conv_y)
|
||||
conv_z = keras.layers.BatchNormalization()(conv_z)
|
||||
|
||||
# no need to expand channels because they are equal
|
||||
shortcut_y = keras.layers.BatchNormalization()(output_block_2)
|
||||
|
||||
output_block_3 = keras.layers.add([shortcut_y, conv_z])
|
||||
output_block_3 = keras.layers.Activation('relu')(output_block_3)
|
||||
|
||||
# FINAL
|
||||
|
||||
gap_layer = keras.layers.GlobalAveragePooling1D()(output_block_3)
|
||||
|
||||
output_layer = keras.layers.Dense(num_classes, activation='linear')(gap_layer)
|
||||
model = keras.models.Model(inputs=input_layer, outputs=output_layer)
|
||||
|
||||
|
||||
return model
|
||||
@@ -0,0 +1,41 @@
|
||||
from keras import layers
|
||||
import keras
|
||||
|
||||
def transformer_encoder(inputs, head_size, num_heads, ff_dim, dropout=0):
|
||||
# Normalization and Attention
|
||||
x = layers.LayerNormalization(epsilon=1e-6)(inputs)
|
||||
x = layers.MultiHeadAttention(
|
||||
key_dim=head_size, num_heads=num_heads, dropout=dropout
|
||||
)(x, x)
|
||||
x = layers.Dropout(dropout)(x)
|
||||
res = x + inputs
|
||||
|
||||
# Feed Forward Part
|
||||
x = layers.LayerNormalization(epsilon=1e-6)(res)
|
||||
x = layers.Conv1D(filters=ff_dim, kernel_size=1, activation="relu")(x)
|
||||
x = layers.Dropout(dropout)(x)
|
||||
x = layers.Conv1D(filters=inputs.shape[-1], kernel_size=1)(x)
|
||||
return x + res
|
||||
|
||||
def create_basic_transformer_model(
|
||||
input_shape,
|
||||
n_classes,
|
||||
head_size,
|
||||
num_heads,
|
||||
ff_dim,
|
||||
num_transformer_blocks,
|
||||
mlp_units,
|
||||
dropout=0,
|
||||
mlp_dropout=0,
|
||||
):
|
||||
inputs = keras.Input(shape=input_shape)
|
||||
x = inputs
|
||||
for _ in range(num_transformer_blocks):
|
||||
x = transformer_encoder(x, head_size, num_heads, ff_dim, dropout)
|
||||
|
||||
x = layers.GlobalAveragePooling1D(data_format="channels_first")(x)
|
||||
for dim in mlp_units:
|
||||
x = layers.Dense(dim, activation="relu")(x)
|
||||
x = layers.Dropout(mlp_dropout)(x)
|
||||
outputs = layers.Dense(n_classes, activation="softmax")(x)
|
||||
return keras.Model(inputs, outputs)
|
||||
@@ -0,0 +1,64 @@
|
||||
import pytorch_lightning as pl
|
||||
from pytorch_lightning.callbacks import EarlyStopping, LearningRateMonitor
|
||||
|
||||
from pytorch_forecasting import TimeSeriesDataSet, TemporalFusionTransformer
|
||||
from pytorch_forecasting.data import GroupNormalizer
|
||||
from pytorch_forecasting.metrics import QuantileLoss
|
||||
|
||||
import sys
|
||||
sys.path.insert(0, '..')
|
||||
|
||||
from load_data import load_files
|
||||
|
||||
print("success")
|
||||
|
||||
def load_format_data(data_dir):
|
||||
print("Data Starting ===>", end=" ")
|
||||
# load data
|
||||
data = load_files(data_dir, add_features=True, log_returns=False, narrow_format=True)
|
||||
# need to treat time as an independent column
|
||||
data = data.reset_index().rename({'index':'time'}, axis = 'columns')
|
||||
# we need a `time_idx` column for pytorch-forecasting, so we convert the time column to a time index by encoding the dates as consecutive days from the first date.
|
||||
data['time_idx'] = (data['time']-data['time'].min()).astype('timedelta64[D]').astype(int)+1
|
||||
# volume needs some love before we can use it
|
||||
data.drop(columns=['volume'], inplace=True)
|
||||
|
||||
data['month'] = data['month'].astype(str)
|
||||
data['day_month'] = data['day_month'].astype(str)
|
||||
data['day_week'] = data['day_week'].astype(str)
|
||||
|
||||
|
||||
print("<=== Data Loaded")
|
||||
print(data.head(3))
|
||||
print(data.describe())
|
||||
print("")
|
||||
|
||||
|
||||
|
||||
|
||||
return data
|
||||
|
||||
|
||||
def create_dataloaders(data, kwargs):
|
||||
print("DataLoader Starting ===>", end=" ")
|
||||
# training_cutoff = "YYYY-MM-DD" # day for cutoff
|
||||
# training_cutoff = data["time_idx"].max() - max_prediction_length
|
||||
batch_size = kwargs['batch_size']
|
||||
del kwargs['batch_size']
|
||||
|
||||
training_dataset = TimeSeriesDataSet(
|
||||
data, # data[lambda x: x.date < training_cutoff],
|
||||
**kwargs
|
||||
)
|
||||
|
||||
#%%
|
||||
# create validation and training dataset
|
||||
validation = TimeSeriesDataSet.from_dataset(training_dataset, data, predict=True, stop_randomization=True)
|
||||
train_dataloader = training_dataset.to_dataloader(train=True, batch_size=batch_size, num_workers=0)
|
||||
val_dataloader = validation.to_dataloader(train=False, batch_size=batch_size, num_workers=0)
|
||||
|
||||
|
||||
print("<=== DataLoader Created")
|
||||
|
||||
|
||||
return training_dataset, train_dataloader, val_dataloader
|
||||
@@ -0,0 +1,19 @@
|
||||
from pytorch_forecasting import TemporalFusionTransformer
|
||||
from pytorch_forecasting.metrics import QuantileLoss
|
||||
|
||||
def create_TemporalFusionTransformer(training_dataset, model_options):
|
||||
# create the model
|
||||
tft = TemporalFusionTransformer.from_dataset(
|
||||
training_dataset,
|
||||
learning_rate=0.03,
|
||||
hidden_size=32,
|
||||
attention_head_size=1,
|
||||
dropout=0.1,
|
||||
hidden_continuous_size=16,
|
||||
output_size=7,
|
||||
loss=QuantileLoss(),
|
||||
log_interval=2,
|
||||
reduce_on_plateau_patience=4
|
||||
)
|
||||
print(f"Number of parameters in network: {tft.size()/1e3:.1f}k")
|
||||
return tft
|
||||
@@ -0,0 +1,98 @@
|
||||
#%%
|
||||
import warnings
|
||||
from typing import Dict
|
||||
|
||||
warnings.filterwarnings("ignore")
|
||||
|
||||
import torch
|
||||
from torch import nn
|
||||
|
||||
from pytorch_forecasting.models import BaseModel
|
||||
from pytorch_forecasting import TimeSeriesDataSet
|
||||
|
||||
|
||||
#%%
|
||||
class FullyConnectedModule(nn.Module):
|
||||
def __init__(self, input_size: int, output_size: int, hidden_size: int, n_hidden_layers: int):
|
||||
super().__init__()
|
||||
|
||||
# input layer
|
||||
module_list = [nn.Linear(input_size, hidden_size), nn.ReLU()]
|
||||
# hidden layers
|
||||
for _ in range(n_hidden_layers):
|
||||
module_list.extend([nn.Linear(hidden_size, hidden_size), nn.ReLU()])
|
||||
# output layer
|
||||
module_list.append(nn.Linear(hidden_size, output_size))
|
||||
|
||||
self.sequential = nn.Sequential(*module_list)
|
||||
|
||||
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
||||
# x of shape: batch_size x n_timesteps_in
|
||||
# output of shape batch_size x n_timesteps_out
|
||||
return self.sequential(x)
|
||||
|
||||
|
||||
|
||||
#%%
|
||||
class FullyConnectedModel(BaseModel):
|
||||
def __init__(self, input_size: int, output_size: int, hidden_size: int, n_hidden_layers: int, **kwargs):
|
||||
# saves arguments in signature to `.hparams` attribute, mandatory call - do not skip this
|
||||
self.save_hyperparameters()
|
||||
# pass additional arguments to BaseModel.__init__, mandatory call - do not skip this
|
||||
super().__init__(**kwargs)
|
||||
self.network = FullyConnectedModule(
|
||||
input_size=self.hparams.input_size,
|
||||
output_size=self.hparams.output_size,
|
||||
hidden_size=self.hparams.hidden_size,
|
||||
n_hidden_layers=self.hparams.n_hidden_layers,
|
||||
)
|
||||
|
||||
def forward(self, x: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]:
|
||||
# x is a batch generated based on the TimeSeriesDataset
|
||||
network_input = x["encoder_cont"].squeeze(-1)
|
||||
prediction = self.network(network_input)
|
||||
|
||||
# rescale predictions into target space
|
||||
prediction = self.transform_output(prediction, target_scale=x["target_scale"])
|
||||
|
||||
# We need to return a dictionary that at least contains the prediction
|
||||
# The parameter can be directly forwarded from the input.
|
||||
# The conversion to a named tuple can be directly achieved with the `to_network_output` function.
|
||||
return self.to_network_output(prediction=prediction)
|
||||
|
||||
@classmethod
|
||||
def from_dataset(cls, dataset: TimeSeriesDataSet, **kwargs):
|
||||
new_kwargs = {
|
||||
"output_size": dataset.max_prediction_length,
|
||||
"input_size": dataset.max_encoder_length,
|
||||
}
|
||||
new_kwargs.update(kwargs) # use to pass real hyperparameters and override defaults set by dataset
|
||||
# example for dataset validation
|
||||
assert dataset.max_prediction_length == dataset.min_prediction_length, "Decoder only supports a fixed length"
|
||||
assert dataset.min_encoder_length == dataset.max_encoder_length, "Encoder only supports a fixed length"
|
||||
assert (
|
||||
len(dataset.time_varying_known_categoricals) == 0
|
||||
and len(dataset.time_varying_known_reals) == 0
|
||||
and len(dataset.time_varying_unknown_categoricals) == 0
|
||||
and len(dataset.static_categoricals) == 0
|
||||
and len(dataset.static_reals) == 0
|
||||
and len(dataset.time_varying_unknown_reals) == 1
|
||||
and dataset.time_varying_unknown_reals[0] == dataset.target
|
||||
), "Only covariate should be the target in 'time_varying_unknown_reals'"
|
||||
|
||||
return super().from_dataset(dataset, **new_kwargs)
|
||||
|
||||
def calculate_prediction_actual_by_variable(x, train):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# %%
|
||||
def create_FullyConnectedModel(training_dataset, kwargs):
|
||||
model = FullyConnectedModel.from_dataset(training_dataset, **kwargs)
|
||||
model.summarize("full") # print model summary
|
||||
model.hparams
|
||||
|
||||
return model
|
||||
@@ -0,0 +1,63 @@
|
||||
from pytorch_forecasting.data import GroupNormalizer
|
||||
from pytorch_forecasting.models.temporal_fusion_transformer import TemporalFusionTransformer
|
||||
|
||||
from lightning.models.custom_model import FullyConnectedModel
|
||||
|
||||
training_options = dict(
|
||||
max_epochs=100,
|
||||
gpus=0,
|
||||
gradient_clip_val=0.1,
|
||||
limit_train_batches=30
|
||||
)
|
||||
|
||||
model_options_fcn = dict(
|
||||
hidden_size=64,
|
||||
n_hidden_layers=2,
|
||||
)
|
||||
|
||||
model_options_tft = dict(
|
||||
hidden_size=64,
|
||||
n_hidden_layers=2,
|
||||
)
|
||||
|
||||
dataset_options_tft = dict(
|
||||
time_idx= 'time_idx',
|
||||
target= 'returns',
|
||||
# weight="weight",
|
||||
group_ids=[ 'ticker' ],
|
||||
min_encoder_length = 36, # this is the look-back window, see https://github.com/jdb78/pytorch-forecasting/issues/448
|
||||
max_encoder_length = 36,
|
||||
min_prediction_length = 1,
|
||||
max_prediction_length = 1,
|
||||
static_categoricals=[ ],
|
||||
static_reals=[ ],
|
||||
time_varying_known_categoricals=[ 'day_month', 'day_week', 'month' ],
|
||||
time_varying_known_reals=[ 'vol_10', 'vol_20', 'vol_30', 'vol_60', 'mom_10', 'mom_20', 'mom_30', 'mom_60', 'mom_90'],
|
||||
time_varying_unknown_categoricals=[ ],
|
||||
time_varying_unknown_reals=[ 'returns' ],
|
||||
allow_missing_timesteps=True,
|
||||
target_normalizer=GroupNormalizer(
|
||||
groups=['ticker'], transformation="softplus"),
|
||||
batch_size=128
|
||||
)
|
||||
|
||||
dataset_options_fcn = dict(
|
||||
time_idx= 'time_idx',
|
||||
target= 'returns',
|
||||
# weight="weight",
|
||||
group_ids=[ 'ticker' ],
|
||||
min_encoder_length = 36, # this is the look-back window, see https://github.com/jdb78/pytorch-forecasting/issues/448
|
||||
max_encoder_length = 36,
|
||||
min_prediction_length = 1,
|
||||
max_prediction_length = 1,
|
||||
static_categoricals=[ ],
|
||||
static_reals=[ ],
|
||||
time_varying_known_categoricals=[ ],
|
||||
time_varying_known_reals=[ ],
|
||||
time_varying_unknown_categoricals=[ ],
|
||||
time_varying_unknown_reals=[ 'returns' ],
|
||||
allow_missing_timesteps=True,
|
||||
target_normalizer=GroupNormalizer(
|
||||
groups=['ticker'], transformation="softplus"),
|
||||
batch_size=128
|
||||
)
|
||||
@@ -0,0 +1,41 @@
|
||||
|
||||
#%%
|
||||
from create_dataset import load_format_data, create_dataloaders
|
||||
from train_predict import train_model, predict
|
||||
from models.built_in_models import create_TemporalFusionTransformer
|
||||
from models.custom_model import create_FullyConnectedModel
|
||||
from options import training_options, model_options_tft, model_options_fcn, dataset_options_tft, dataset_options_fcn
|
||||
|
||||
import warnings
|
||||
warnings.filterwarnings("ignore")
|
||||
|
||||
#%%
|
||||
def run_pipeline(model_name, data_dir):
|
||||
data = load_format_data(data_dir)
|
||||
|
||||
dataset_options, model_options, _create_model = select_model(model_name)
|
||||
training_dataset, train_dataloader, val_dataloader = create_dataloaders(data, dataset_options)
|
||||
|
||||
model = _create_model( training_dataset, model_options )
|
||||
trainer = train_model(model, train_dataloader, val_dataloader, training_options)
|
||||
predict(trainer, model, val_dataloader)
|
||||
|
||||
#%%
|
||||
|
||||
def select_model(model_name):
|
||||
if model_name == "FullyConnectedLayer":
|
||||
return dataset_options_fcn, model_options_fcn, create_FullyConnectedModel
|
||||
elif model_name == "TemporalFusionTransformer":
|
||||
return dataset_options_tft, model_options_tft, create_TemporalFusionTransformer
|
||||
else:
|
||||
assert False, "No such model exists."
|
||||
|
||||
|
||||
#%%
|
||||
run_pipeline("FullyConnectedLayer", '../data/')
|
||||
|
||||
# #%%
|
||||
# if __name__ == '__main__':
|
||||
# run_pipeline("FullyConnectedLayer", '../data/')
|
||||
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
import pytorch_lightning as pl
|
||||
from pytorch_lightning.callbacks import EarlyStopping, LearningRateMonitor
|
||||
import torch
|
||||
from torch import nn
|
||||
|
||||
|
||||
import warnings
|
||||
warnings.filterwarnings("ignore")
|
||||
|
||||
import sys
|
||||
sys.path.insert(0, '..')
|
||||
|
||||
from load_data import load_files
|
||||
|
||||
|
||||
|
||||
|
||||
def train_model(model, train_dataloader, val_dataloader, kwargs):
|
||||
print()
|
||||
print("Creating Trainer ===>", end=" ")
|
||||
# define trainer with early stopping
|
||||
early_stop_callback = EarlyStopping(monitor="val_loss", min_delta=1e-4, patience=1, verbose=False, mode="min")
|
||||
lr_logger = LearningRateMonitor()
|
||||
|
||||
trainer = pl.Trainer(
|
||||
**kwargs,
|
||||
callbacks=[lr_logger, early_stop_callback],
|
||||
)
|
||||
print("<=== Trainer Created")
|
||||
print("Finding Optimal LR ===>", end=" ")
|
||||
# find optimal learning rate (set limit_train_batches to 1.0 and log_interval = -1)
|
||||
res = trainer.tuner.lr_find(
|
||||
model, train_dataloader=train_dataloader, val_dataloaders=val_dataloader, early_stop_threshold=1000.0, max_lr=0.3,
|
||||
)
|
||||
|
||||
print(f"<=== suggested learning rate: {res.suggestion()}")
|
||||
fig = res.plot(show=True, suggest=True)
|
||||
fig.show()
|
||||
|
||||
print("Training the model ===>", end=" ")
|
||||
# fit the model
|
||||
trainer.fit(
|
||||
model, train_dataloader=train_dataloader, val_dataloaders=val_dataloader,
|
||||
)
|
||||
|
||||
print("<=== Training Finished")
|
||||
return trainer
|
||||
|
||||
|
||||
def predict(trainer, model, val_dataloader):
|
||||
pass
|
||||
# best_model_path = trainer.checkpoint_callback.best_model_path
|
||||
# best_model = model.load_from_checkpoint(best_model_path)
|
||||
|
||||
# # calcualte mean absolute error on validation set
|
||||
# actuals = torch.cat([y[0] for x, y in iter(val_dataloader)])
|
||||
# predictions = best_model.predict(val_dataloader)
|
||||
# (actuals - predictions).abs().mean()
|
||||
|
||||
# raw_predictions, x = best_model.predict(val_dataloader, mode="raw", return_x=True)
|
||||
# for idx in range(10): # plot 10 examples
|
||||
# best_model.plot_prediction(x, raw_predictions, idx=idx, add_loss_to_title=True)
|
||||
|
||||
# predictions, x = best_model.predict(val_dataloader, return_x=True)
|
||||
# predictions_vs_actuals = best_model.calculate_prediction_actual_by_variable(x, predictions)
|
||||
# best_model.plot_prediction_actual_by_variable(predictions_vs_actuals)
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
#%% Import all the stuff, load data, define constants
|
||||
from sklearn.utils import shuffle
|
||||
from load_data import load_files, create_target_classes
|
||||
import pandas as pd
|
||||
from tensorflow import keras
|
||||
from utils.normalize import normalize
|
||||
import tensorflow as tf
|
||||
from utils.visualize import visualize_loss
|
||||
from sklearn.preprocessing import MinMaxScaler
|
||||
from utils.evaluate import print_classification_metrics
|
||||
import numpy as np
|
||||
from utils.rolling import rolling_window
|
||||
from keras_models.classification import create_basic_cnn_model, create_basic_lstm_model, create_resnet_cnn_model
|
||||
from keras_models.classification_transformer import create_basic_transformer_model
|
||||
|
||||
data = load_files(path='data/',
|
||||
own_asset='BTC_ETH',
|
||||
own_asset_lags=[1,2,3,4,5,6,8,10,15],
|
||||
load_other_assets=True,
|
||||
other_asset_lags=[1,2,3,4],
|
||||
log_returns=False,
|
||||
add_date_features=True,
|
||||
own_technical_features='level2',
|
||||
other_technical_features='level2',
|
||||
exogenous_features='none',
|
||||
index_column='int'
|
||||
)
|
||||
|
||||
|
||||
target_col = 'target'
|
||||
data = create_target_classes(data, 'BTC_ETH_returns', 1, 'two')
|
||||
|
||||
num_classes = 2
|
||||
learning_rate = 0.002
|
||||
batch_size = 64
|
||||
epochs = 100
|
||||
|
||||
split_fraction = 0.8
|
||||
train_split = int(split_fraction * int(data.shape[0]))
|
||||
|
||||
past = 60
|
||||
future = 10
|
||||
|
||||
start = past + future
|
||||
end = start + train_split
|
||||
|
||||
#%% split data into training - validation sets
|
||||
train_data = data.loc[0 : train_split - 1]
|
||||
val_data = data.loc[train_split:]
|
||||
|
||||
#%% create features and target for training set & keras dataset
|
||||
feature_scaler = MinMaxScaler(feature_range= (-1, 1))
|
||||
|
||||
x_train = feature_scaler.fit_transform(train_data.drop(target_col, axis=1).values) # you get the mean and std
|
||||
y_train = keras.utils.to_categorical(data.iloc[start:end][target_col].values)
|
||||
|
||||
dataset_train = keras.preprocessing.timeseries_dataset_from_array(
|
||||
x_train,
|
||||
y_train,
|
||||
sequence_length=past,
|
||||
batch_size=batch_size,
|
||||
)
|
||||
|
||||
|
||||
#%% create features and target for validation set & keras dataset
|
||||
x_end = len(val_data) - past - future
|
||||
label_start = train_split + past + future
|
||||
|
||||
x_val = feature_scaler.transform(val_data.drop(target_col, axis=1).iloc[:x_end].values) # you use the training data's mean and std
|
||||
y_val = keras.utils.to_categorical(data.iloc[label_start:][target_col].values)
|
||||
|
||||
dataset_val = keras.utils.timeseries_dataset_from_array(
|
||||
x_val,
|
||||
y_val,
|
||||
sequence_length=past,
|
||||
batch_size=batch_size,
|
||||
shuffle=False,
|
||||
)
|
||||
|
||||
|
||||
#%%
|
||||
|
||||
for batch in dataset_train.take(1):
|
||||
batch_inputs, batch_targets = batch
|
||||
|
||||
print("Input shape:", batch_inputs.shape)
|
||||
print("Target shape:", batch_targets.shape)
|
||||
|
||||
n_timestamps = batch_inputs.shape[1]
|
||||
n_features = batch_inputs.shape[2]
|
||||
# print(batch_inputs)
|
||||
# print(batch_targets)
|
||||
|
||||
# %%
|
||||
# model = create_basic_lstm_model(input_shape=(n_timestamps, n_features), num_classes=num_classes)
|
||||
# model = create_basic_cnn_model(input_shape=(n_timestamps, n_features), num_classes=num_classes)
|
||||
model = create_resnet_cnn_model(input_shape=(n_timestamps, n_features), num_classes=num_classes)
|
||||
# model = create_basic_transformer_model(
|
||||
# input_shape=(n_timestamps, n_features),
|
||||
# n_classes=num_classes,
|
||||
# head_size=64,
|
||||
# num_heads=4,
|
||||
# ff_dim=4,
|
||||
# num_transformer_blocks=4,
|
||||
# mlp_units=[64],
|
||||
# mlp_dropout=0.4,
|
||||
# dropout=0.25,
|
||||
# )
|
||||
|
||||
optimizer = keras.optimizers.Adam(learning_rate=learning_rate)
|
||||
loss = keras.losses.CategoricalCrossentropy(from_logits=True)
|
||||
model.compile(optimizer=optimizer, loss=loss, metrics=['accuracy'])
|
||||
model.summary()
|
||||
|
||||
# %%
|
||||
|
||||
path_checkpoint = "model_checkpoint.h5"
|
||||
|
||||
history = model.fit(
|
||||
dataset_train,
|
||||
epochs=epochs,
|
||||
validation_data=dataset_val,
|
||||
)
|
||||
|
||||
#%%
|
||||
|
||||
# pred = model.predict(rolling_window(x_val, 11))
|
||||
# pred = pred.reshape(pred.shape[0], 1)
|
||||
|
||||
|
||||
#%%
|
||||
# print_classification_metrics(y_val, pred)
|
||||
|
||||
visualize_loss(history, "Training and Validation Loss")
|
||||
@@ -0,0 +1,158 @@
|
||||
#%% Import all the stuff, load data, define constants
|
||||
from sklearnex import patch_sklearn
|
||||
patch_sklearn()
|
||||
|
||||
from load_data import create_target_cum_forward_returns, create_target_classes, load_files
|
||||
from sktime.forecasting.model_selection import temporal_train_test_split
|
||||
from sklearn.metrics import accuracy_score
|
||||
from sklearn.metrics import confusion_matrix
|
||||
# from utils.evaluate import print_classification_metrics, format_data_for_backtest
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from sklearn.model_selection import train_test_split, KFold, cross_val_score, GridSearchCV
|
||||
from sklearn.linear_model import LogisticRegression
|
||||
from sklearn.tree import DecisionTreeClassifier
|
||||
from sklearn.neighbors import KNeighborsClassifier
|
||||
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
|
||||
from sklearn.naive_bayes import GaussianNB
|
||||
from sklearn.svm import SVC
|
||||
from sklearn.neural_network import MLPClassifier
|
||||
from sklearn.pipeline import Pipeline
|
||||
from sklearn.ensemble import AdaBoostClassifier, GradientBoostingClassifier, RandomForestClassifier, ExtraTreesClassifier
|
||||
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
|
||||
from sklearn.preprocessing import MinMaxScaler
|
||||
|
||||
from utils.sliding_window import sliding_window_and_flatten
|
||||
|
||||
ticket_to_predict = 'BTC_ETH'
|
||||
print('Predicting: ', ticket_to_predict)
|
||||
|
||||
data = load_files(path='data/',
|
||||
own_asset=ticket_to_predict,
|
||||
own_asset_lags=[1,2,3,4,5,6,8,10,15],
|
||||
load_other_assets=False,
|
||||
other_asset_lags=[1,2,3,4],
|
||||
log_returns=True,
|
||||
add_date_features=True,
|
||||
own_technical_features='level2',
|
||||
other_technical_features='none',
|
||||
exogenous_features='none',
|
||||
index_column='int'
|
||||
)
|
||||
|
||||
target_col = 'target'
|
||||
returns_col = ticket_to_predict + '_returns'
|
||||
data = create_target_classes(data, returns_col, 1, 'three')
|
||||
|
||||
X = data.drop(columns=['target'])
|
||||
y = data[target_col]
|
||||
|
||||
X_train, X_test, y_train, y_test = temporal_train_test_split(X, y, test_size=0.2)
|
||||
feature_scaler = MinMaxScaler(feature_range= (-1, 1))
|
||||
X_test_orig = X_test.copy()
|
||||
X_train = feature_scaler.fit_transform(X_train)
|
||||
X_test = feature_scaler.transform(X_test)
|
||||
|
||||
|
||||
#%%
|
||||
|
||||
sliding_window_size = 10
|
||||
X_train = sliding_window_and_flatten(X_train, sliding_window_size)
|
||||
X_test = sliding_window_and_flatten(X_test, sliding_window_size)
|
||||
X_test_orig = X_test_orig.iloc[sliding_window_size-1:]
|
||||
y_train = y_train[sliding_window_size-1:]
|
||||
y_test = y_test[sliding_window_size-1:]
|
||||
|
||||
assert X_train.shape[0] == y_train.shape[0]
|
||||
assert X_test.shape[0] == y_test.shape[0]
|
||||
scoring = 'accuracy'
|
||||
|
||||
# %%
|
||||
num_folds = 10
|
||||
|
||||
models = []
|
||||
models.append(('LR', LogisticRegression(n_jobs=-1)))
|
||||
models.append(('LDA', LinearDiscriminantAnalysis()))
|
||||
models.append(('KNN', KNeighborsClassifier()))
|
||||
models.append(('CART', DecisionTreeClassifier()))
|
||||
models.append(('NB', GaussianNB()))
|
||||
# models.append(('NN', MLPClassifier(hidden_layer_sizes=[200, 100, 50], shuffle=False)))
|
||||
models.append(('AB', AdaBoostClassifier()))
|
||||
# models.append(('GBM', GradientBoostingClassifier()))
|
||||
models.append(('RF', RandomForestClassifier(n_jobs=-1)))
|
||||
|
||||
results = []
|
||||
names = []
|
||||
for name, model in models:
|
||||
kfold = KFold(n_splits=num_folds, shuffle=False)
|
||||
cv_results = cross_val_score(model, X_train, y_train, cv=kfold, scoring=scoring)
|
||||
results.append(cv_results)
|
||||
names.append(name)
|
||||
msg = "%s: %f (%f)" % (name, cv_results.mean(), cv_results.std())
|
||||
print(msg)
|
||||
|
||||
# # compare algorithms
|
||||
# fig = plt.figure()
|
||||
# fig.suptitle('Algorithm Comparison')
|
||||
# ax = fig.add_subplot(111)
|
||||
# plt.boxplot(results)
|
||||
# ax.set_xticklabels(names)
|
||||
# fig.set_size_inches(15,8)
|
||||
# plt.show()
|
||||
|
||||
#%%
|
||||
# n_estimators = [20,80]
|
||||
# max_depth= [5,10, 15]
|
||||
# criterion = ["gini","entropy"]
|
||||
# param_grid = dict(n_estimators=n_estimators, max_depth=max_depth, criterion = criterion )
|
||||
# model = RandomForestClassifier(n_jobs=-1)
|
||||
# kfold = KFold(n_splits=10, shuffle=False)
|
||||
# grid = GridSearchCV(estimator=model, param_grid=param_grid, scoring=scoring, cv=kfold)
|
||||
# grid_result = grid.fit(X_train, y_train)
|
||||
|
||||
# #Print Results
|
||||
# print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
|
||||
# means = grid_result.cv_results_['mean_test_score']
|
||||
# stds = grid_result.cv_results_['std_test_score']
|
||||
# params = grid_result.cv_results_['params']
|
||||
# ranks = grid_result.cv_results_['rank_test_score']
|
||||
# for mean, stdev, param, rank in zip(means, stds, params, ranks):
|
||||
# print("#%d %f (%f) with: %r" % (rank, mean, stdev, param))
|
||||
|
||||
|
||||
#%% prepare model
|
||||
model = RandomForestClassifier(criterion='entropy', n_estimators=80, max_depth=5, n_jobs=-1)
|
||||
# model = LogisticRegression()
|
||||
# model = MLPClassifier(hidden_layer_sizes=[200, 100, 50], shuffle=False, max_iter=1000)
|
||||
model = GaussianNB()
|
||||
model.fit(X_train, y_train)
|
||||
|
||||
|
||||
#%%
|
||||
# estimate accuracy on validation set
|
||||
predictions = model.predict(X_test)
|
||||
print(accuracy_score(y_test, predictions))
|
||||
print(confusion_matrix(y_test, predictions))
|
||||
print(classification_report(y_test, predictions))
|
||||
|
||||
|
||||
#%%
|
||||
# feat_importance = pd.DataFrame({'Importance':model.feature_importances_*100}, index=X.columns)
|
||||
# feat_importance.sort_values('Importance', axis=0, ascending=True)
|
||||
# feat_importance.plot(kind='barh', color='r' )
|
||||
# plt.xlabel('Variable Importance')
|
||||
# print(feat_importance)
|
||||
|
||||
#%% Create column for Strategy Returns by multiplying the daily returns by the position that was held at close of business the previous day
|
||||
backtestdata = pd.DataFrame(index= X_test_orig.index)
|
||||
backtestdata['signal_pred'] = predictions
|
||||
backtestdata['signal_actual'] = y_test
|
||||
backtestdata['returns'] = X_test_orig[returns_col]
|
||||
backtestdata['only_positive_returns'] = backtestdata['returns'] * backtestdata['signal_actual'].shift(1)
|
||||
backtestdata['strategy_returns'] = backtestdata['returns'] * backtestdata['signal_pred'].shift(1)
|
||||
|
||||
# %%
|
||||
print(backtestdata.cumsum().apply(np.exp).tail(1))
|
||||
|
||||
# %%
|
||||
@@ -0,0 +1,142 @@
|
||||
#%% Import all the stuff, load data, define constants
|
||||
from sklearnex import patch_sklearn
|
||||
patch_sklearn()
|
||||
|
||||
from load_data import create_target_classes, load_files
|
||||
from sktime.forecasting.model_selection import temporal_train_test_split
|
||||
from sklearn.metrics import accuracy_score
|
||||
from sklearn.metrics import confusion_matrix
|
||||
# from utils.evaluate import print_classification_metrics, format_data_for_backtest
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from sklearn.model_selection import train_test_split, KFold, cross_val_score, GridSearchCV
|
||||
from sklearn.linear_model import LogisticRegression
|
||||
from sklearn.tree import DecisionTreeClassifier
|
||||
from sklearn.neighbors import KNeighborsClassifier
|
||||
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
|
||||
from sklearn.naive_bayes import GaussianNB
|
||||
from sklearn.svm import SVC
|
||||
from sklearn.neural_network import MLPClassifier
|
||||
from sklearn.pipeline import Pipeline
|
||||
from sklearn.ensemble import AdaBoostClassifier, GradientBoostingClassifier, RandomForestClassifier, ExtraTreesClassifier
|
||||
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
|
||||
from sklearn.preprocessing import MinMaxScaler
|
||||
|
||||
from utils.sliding_window import sliding_window_and_flatten
|
||||
|
||||
ticket_to_predict = 'BTC_ETH'
|
||||
print('Predicting: ', ticket_to_predict)
|
||||
|
||||
data = load_files(path='data/',
|
||||
own_asset=ticket_to_predict,
|
||||
own_asset_lags=[1,2,3,4,5,6,8,10,15],
|
||||
load_other_assets=False,
|
||||
other_asset_lags=[1,2,3,4],
|
||||
log_returns=True,
|
||||
add_date_features=True,
|
||||
own_technical_features='level1',
|
||||
other_technical_features='none',
|
||||
exogenous_features='none',
|
||||
index_column='int'
|
||||
)
|
||||
|
||||
target_col = 'target'
|
||||
returns_col = ticket_to_predict + '_returns'
|
||||
data = create_target_classes(data, returns_col, 1, 'two')
|
||||
|
||||
X = data.drop(columns=[target_col])
|
||||
y = data[target_col]
|
||||
|
||||
X_train, X_test, y_train, y_test = temporal_train_test_split(X, y, test_size=0.1)
|
||||
feature_scaler = MinMaxScaler(feature_range= (-1, 1))
|
||||
X_test_orig = X_test.copy()
|
||||
X_train = feature_scaler.fit_transform(X_train)
|
||||
X_test = feature_scaler.transform(X_test)
|
||||
|
||||
|
||||
#%%
|
||||
|
||||
sliding_window_size = 120
|
||||
retrain_every = 60
|
||||
X_train = sliding_window_and_flatten(X_train, sliding_window_size)
|
||||
# X_test = sliding_window_and_flatten(X_test, sliding_window_size)
|
||||
X_test_orig = X_test_orig.iloc[sliding_window_size-1:]
|
||||
y_train = y_train[sliding_window_size-1:]
|
||||
# y_test = y_test[sliding_window_size-1:]
|
||||
|
||||
|
||||
|
||||
|
||||
def evaluate_predictions(model_name: str, y: pd.Series, preds: pd.Series, sliding_window_size: int):
|
||||
print("Model: ", model_name)
|
||||
evaluate_from = sliding_window_size*2
|
||||
print(accuracy_score(y[evaluate_from:-1], preds[evaluate_from:]))
|
||||
print(confusion_matrix(y[evaluate_from:-1], preds[evaluate_from:]))
|
||||
print(classification_report(y[evaluate_from:-1], preds[evaluate_from:]))
|
||||
|
||||
|
||||
def walk_forward_train_test(
|
||||
create_model,
|
||||
X_train: pd.DataFrame,
|
||||
y_train: pd.Series,
|
||||
window_size: int,
|
||||
retrain_every: int
|
||||
):
|
||||
predictions = [None] * (len(y_train)-1)
|
||||
models = [None] * len(predictions)
|
||||
|
||||
train_from = sliding_window_size+1
|
||||
train_till = len(y_train)-2
|
||||
|
||||
iterations_since_retrain = 0
|
||||
|
||||
for i in range(train_from, train_till):
|
||||
# if i % 20 == 0: print('Fold: ', i)
|
||||
iterations_since_retrain += 1
|
||||
window_start = i - window_size
|
||||
window_end = i
|
||||
X_train_slice = X_train[window_start:window_end]
|
||||
y_train_slice = y_train[window_start:window_end]
|
||||
|
||||
if iterations_since_retrain >= retrain_every or models[i-1] is None:
|
||||
model = create_model()
|
||||
model.fit(X_train_slice, y_train_slice)
|
||||
iterations_since_retrain = 0
|
||||
else:
|
||||
model = models[i-1]
|
||||
models[window_end] = model
|
||||
|
||||
predictions[window_end+1] = model.predict(X_train[window_end+1].reshape(1, -1)).item()
|
||||
return models, predictions
|
||||
|
||||
|
||||
#%%
|
||||
for model_name, create_model in models_to_try:
|
||||
|
||||
model_over_time, preds = walk_forward_train_test(
|
||||
create_model = create_model,
|
||||
X_train = X_train,
|
||||
y_train = y_train,
|
||||
window_size = sliding_window_size,
|
||||
retrain_every = retrain_every
|
||||
)
|
||||
|
||||
evaluate_predictions(model_name, y_train, preds, sliding_window_size)
|
||||
|
||||
|
||||
#%%
|
||||
|
||||
|
||||
#%% Create column for Strategy Returns by multiplying the daily returns by the position that was held at close of business the previous day
|
||||
# backtestdata = pd.DataFrame(index= X_test_orig.index)
|
||||
# backtestdata['signal_pred'] = predictions
|
||||
# backtestdata['signal_actual'] = y_test
|
||||
# backtestdata['returns'] = X_test_orig[returns_col]
|
||||
# backtestdata['only_positive_returns'] = backtestdata['returns'] * backtestdata['signal_actual'].shift(1)
|
||||
# backtestdata['strategy_returns'] = backtestdata['returns'] * backtestdata['signal_pred'].shift(1)
|
||||
|
||||
# %%
|
||||
# print(backtestdata.cumsum().apply(np.exp).tail(1))
|
||||
|
||||
# %%
|
||||
@@ -0,0 +1,97 @@
|
||||
#%% Import all the stuff, load data, define constants
|
||||
from load_data import load_files, create_target_classes
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
from utils.sktime import from_df_to_sktime_data
|
||||
from sktime.utils.plotting import plot_series
|
||||
from sktime.forecasting.model_selection import temporal_train_test_split
|
||||
from sklearn.metrics import accuracy_score
|
||||
from sklearn.pipeline import Pipeline
|
||||
from sktime.classification.interval_based import (
|
||||
TimeSeriesForestClassifier,
|
||||
SupervisedTimeSeriesForest,
|
||||
|
||||
)
|
||||
from sktime.forecasting.model_selection import SlidingWindowSplitter
|
||||
from sktime.forecasting.model_selection import ForecastingRandomizedSearchCV
|
||||
|
||||
from sktime.forecasting.compose import make_reduction
|
||||
|
||||
from sklearn.tree import DecisionTreeClassifier
|
||||
from sklearn.metrics import confusion_matrix
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
from sktime.forecasting.model_selection import (
|
||||
SlidingWindowSplitter,
|
||||
ForecastingGridSearchCV,
|
||||
)
|
||||
from utils.evaluate import print_classification_metrics, format_data_for_backtest
|
||||
from sklearn.ensemble import RandomForestRegressor
|
||||
from sklearnex import patch_sklearn
|
||||
patch_sklearn()
|
||||
|
||||
|
||||
ticket_to_predict = 'BTC_USD'
|
||||
print('Predicting: ', ticket_to_predict)
|
||||
|
||||
data = load_files(path='data/',
|
||||
own_asset=ticket_to_predict,
|
||||
own_asset_lags=[1,2,3,4,5,6,8,10,15],
|
||||
load_other_assets=False,
|
||||
other_asset_lags=[1,2,3,4],
|
||||
log_returns=True,
|
||||
add_date_features=True,
|
||||
own_technical_features='level2',
|
||||
other_technical_features='none',
|
||||
exogenous_features='none',
|
||||
index_column='int'
|
||||
)
|
||||
|
||||
target_col = 'target'
|
||||
returns_col = ticket_to_predict + '_returns'
|
||||
data = create_target_classes(data, returns_col, 1, 'two')
|
||||
|
||||
X = data.drop(columns=[target_col])
|
||||
y = data[target_col]
|
||||
|
||||
X_train, X_test, y_train, y_test = temporal_train_test_split(X, y, test_size=0.2)
|
||||
X_train = from_df_to_sktime_data(X_train)
|
||||
X_test = from_df_to_sktime_data(X_test)
|
||||
|
||||
#%%
|
||||
|
||||
# pipe = RecursiveTabularRegressionForecaster(steps=[
|
||||
# # ("deseasonalizer", OptionalPassthrough(Deseasonalizer())),
|
||||
# ("scaler", StandardScaler()),
|
||||
# ("classifier", TimeSeriesForestClassifier(n_estimators=200, random_state=1)),
|
||||
# ])
|
||||
|
||||
# pipe.fit(X_train, y_train)
|
||||
# regressor = RandomForestRegressor(n_estimators=20)
|
||||
# model = DecisionTreeClassifier(random_state=1)
|
||||
model = TimeSeriesForestClassifier(n_estimators=50, random_state=1)
|
||||
model.fit(y = y_train, X = X_train)
|
||||
# forecaster = make_reduction(model, scitype="tabular-regressor")
|
||||
# nested_params = {"window_length": list(range(2,30)),
|
||||
# "estimator__max_depth": list(range(5,16))}
|
||||
# # "estimator__n_estimators": list(range(10,200))}
|
||||
#%%
|
||||
|
||||
# cv = SlidingWindowSplitter(initial_window=40, window_length=30)
|
||||
# nrcv = ForecastingRandomizedSearchCV(forecaster, strategy="refit", cv=cv,
|
||||
# param_distributions=nested_params,
|
||||
# n_iter=5, random_state=42)
|
||||
# nrcv.fit(y = y_train, X = X_train, fh=np.array([1]))
|
||||
# print(nrcv.best_params_)
|
||||
# print(nrcv.best_score_)
|
||||
|
||||
# model = DecisionTreeClassifier(random_state=1)
|
||||
# model.fit(X_train, y_train)
|
||||
|
||||
# preds = nrcv.best_forecaster_.predict( X=X_test)
|
||||
# # print(preds)
|
||||
preds = model.predict(X_test)
|
||||
print(print_classification_metrics(y_test, preds))
|
||||
|
||||
# backtest_data = format_data_for_backtest(data, returns_col, X_test, preds)
|
||||
# print(backtest_data)
|
||||
# %%
|
||||
@@ -0,0 +1,116 @@
|
||||
#%% Import all the stuff, load data, define constants
|
||||
from sklearn.utils import shuffle
|
||||
from load_data import load_files, create_target_cum_forward_returns
|
||||
import pandas as pd
|
||||
from tensorflow import keras
|
||||
from utils.normalize import normalize
|
||||
import tensorflow as tf
|
||||
from utils.visualize import visualize_loss
|
||||
from sklearn.preprocessing import MinMaxScaler
|
||||
from utils.evaluate import print_regression_metrics
|
||||
import numpy as np
|
||||
from utils.rolling import rolling_window
|
||||
|
||||
|
||||
data = load_files('data/', add_features=True, log_returns=False)
|
||||
data.reset_index(drop=True, inplace=True)
|
||||
data = data[[column for column in data.columns if not column.endswith('volume')]]
|
||||
data = data[["BTC_returns", "BTC_mom_10", "BTC_mom_20", "BTC_mom_30", "BTC_mom_60", "BTC_vol_10", "BTC_vol_20", "BTC_vol_60", "day_month", "day_week", "month"]]
|
||||
|
||||
target_col = 'target'
|
||||
data = create_target_cum_forward_returns(data, 'BTC_returns', 10)
|
||||
|
||||
learning_rate = 0.002
|
||||
batch_size = 64
|
||||
epochs = 100
|
||||
|
||||
split_fraction = 0.715
|
||||
train_split = int(split_fraction * int(data.shape[0]))
|
||||
|
||||
past = 10
|
||||
future = 1
|
||||
|
||||
start = past + future
|
||||
end = start + train_split
|
||||
|
||||
#%% split data into training - validation sets
|
||||
train_data = data.loc[0 : train_split - 1]
|
||||
val_data = data.loc[train_split:]
|
||||
|
||||
#%% create features and target for training set & keras dataset
|
||||
feature_scaler = MinMaxScaler(feature_range= (-1, 1))
|
||||
target_scaler = MinMaxScaler(feature_range= (-1, 1))
|
||||
|
||||
x_train = feature_scaler.fit_transform(train_data.drop(target_col, axis=1).values) # you get the mean and std
|
||||
y_train = target_scaler.fit_transform(data.iloc[start:end][target_col].values.reshape(-1, 1))
|
||||
|
||||
dataset_train = keras.preprocessing.timeseries_dataset_from_array(
|
||||
x_train,
|
||||
y_train,
|
||||
sequence_length=past,
|
||||
batch_size=batch_size,
|
||||
)
|
||||
|
||||
|
||||
#%% create features and target for validation set & keras dataset
|
||||
x_end = len(val_data) - past - future
|
||||
label_start = train_split + past + future
|
||||
|
||||
x_val = feature_scaler.transform(val_data.drop(target_col, axis=1).iloc[:x_end].values) # you use the training data's mean and std
|
||||
y_val = target_scaler.transform(data.iloc[label_start:][target_col].values.reshape(-1, 1))
|
||||
|
||||
dataset_val = keras.utils.timeseries_dataset_from_array(
|
||||
x_val,
|
||||
y_val,
|
||||
sequence_length=past,
|
||||
batch_size=batch_size,
|
||||
shuffle=False,
|
||||
)
|
||||
|
||||
|
||||
#%%
|
||||
|
||||
for batch in dataset_train.take(10):
|
||||
batch_inputs, batch_targets = batch
|
||||
|
||||
print("Input shape:", batch_inputs.shape)
|
||||
print("Target shape:", batch_targets.shape)
|
||||
|
||||
# print(batch_inputs)
|
||||
# print(batch_targets)
|
||||
|
||||
# %%
|
||||
model = keras.Sequential()
|
||||
model.add(keras.layers.LSTM(units = 10, return_sequences = True, activation = 'sigmoid', input_shape=(batch_inputs.shape[1], batch_inputs.shape[2])))
|
||||
model.add(keras.layers.Dropout(0.4))
|
||||
model.add(keras.layers.Dense(units = 64, activation = 'sigmoid'))
|
||||
model.add(keras.layers.Dropout(0.4))
|
||||
model.add(keras.layers.Dense(units = 32, activation = 'sigmoid'))
|
||||
model.add(keras.layers.Dropout(0.4))
|
||||
model.add(keras.layers.Dense(units = 1))
|
||||
|
||||
optimizer = keras.optimizers.Adam(learning_rate=learning_rate)
|
||||
model.compile(optimizer=optimizer, loss="mean_squared_error")
|
||||
model.summary()
|
||||
|
||||
# %%
|
||||
|
||||
path_checkpoint = "model_checkpoint.h5"
|
||||
|
||||
history = model.fit(
|
||||
dataset_train,
|
||||
epochs=epochs,
|
||||
validation_data=dataset_val,
|
||||
)
|
||||
|
||||
#%%
|
||||
|
||||
pred = model.predict(rolling_window(x_val, 11))
|
||||
pred = pred.reshape(pred.shape[0], 1)
|
||||
pred = target_scaler.inverse_transform(pred)
|
||||
|
||||
print_regression_metrics(y_val, pred)
|
||||
|
||||
#%%
|
||||
|
||||
visualize_loss(history, "Training and Validation Loss")
|
||||
Reference in New Issue
Block a user