2022-01-29 06:41:40 +01:00
|
|
|
import pandas as pd
|
|
|
|
|
|
2022-02-19 14:44:49 +01:00
|
|
|
from transformations.base import Transformation
|
2022-03-03 17:40:17 +01:00
|
|
|
from utils.evaluate import evaluate_predictions
|
2022-02-19 14:44:49 +01:00
|
|
|
|
2022-03-02 00:26:33 +01:00
|
|
|
from .types import TrainingOutcome
|
2022-01-29 06:41:40 +01:00
|
|
|
from training.train_model import train_model
|
|
|
|
|
from training.walk_forward import walk_forward_process_transformations
|
|
|
|
|
|
|
|
|
|
from typing import Optional
|
2022-02-17 19:22:17 +01:00
|
|
|
from config.types import Config
|
2022-01-29 06:41:40 +01:00
|
|
|
from models.base import Model
|
2022-03-03 17:40:17 +01:00
|
|
|
import pprint
|
2022-01-29 06:41:40 +01:00
|
|
|
|
2022-02-17 19:22:17 +01:00
|
|
|
|
2022-02-17 16:36:35 +01:00
|
|
|
def train_directional_model(
|
2022-02-17 19:22:17 +01:00
|
|
|
X: pd.DataFrame,
|
|
|
|
|
y: pd.Series,
|
|
|
|
|
forward_returns: pd.Series,
|
|
|
|
|
config: Config,
|
|
|
|
|
model: Model,
|
2022-02-19 14:44:49 +01:00
|
|
|
transformations: list[Transformation],
|
2022-02-17 19:22:17 +01:00
|
|
|
from_index: Optional[pd.Timestamp],
|
2022-03-02 00:26:33 +01:00
|
|
|
preloaded_training_step: Optional[TrainingOutcome] = None,
|
|
|
|
|
) -> TrainingOutcome:
|
2022-01-29 06:41:40 +01:00
|
|
|
|
|
|
|
|
if preloaded_training_step is None:
|
|
|
|
|
print("Preprocess transformations")
|
|
|
|
|
transformations_over_time = walk_forward_process_transformations(
|
2022-02-17 19:22:17 +01:00
|
|
|
X=X,
|
|
|
|
|
y=y,
|
|
|
|
|
forward_returns=forward_returns,
|
2022-03-13 16:05:16 +01:00
|
|
|
window_size=config.initial_window_size,
|
2022-02-17 19:22:17 +01:00
|
|
|
retrain_every=config.retrain_every,
|
|
|
|
|
from_index=from_index,
|
2022-02-19 14:44:49 +01:00
|
|
|
transformations=transformations,
|
2022-01-29 06:41:40 +01:00
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
transformations_over_time = preloaded_training_step.transformations
|
|
|
|
|
|
2022-02-17 16:36:35 +01:00
|
|
|
training_outcome = train_model(
|
2022-03-10 20:00:06 +01:00
|
|
|
ticker_to_predict=config.target_asset.file_name,
|
2022-02-17 19:22:17 +01:00
|
|
|
X=X,
|
|
|
|
|
y=y,
|
|
|
|
|
forward_returns=forward_returns,
|
|
|
|
|
model=model,
|
2022-03-13 16:05:16 +01:00
|
|
|
initial_window_size=config.initial_window_size,
|
2022-02-17 19:22:17 +01:00
|
|
|
retrain_every=config.retrain_every,
|
|
|
|
|
from_index=from_index,
|
|
|
|
|
level="primary",
|
2022-03-15 14:43:16 +01:00
|
|
|
class_labels=config.labeling.get_labels(),
|
2022-02-17 19:22:17 +01:00
|
|
|
transformations_over_time=transformations_over_time,
|
2022-03-02 00:26:33 +01:00
|
|
|
model_over_time=preloaded_training_step.model_over_time
|
2022-02-17 19:22:17 +01:00
|
|
|
if preloaded_training_step
|
|
|
|
|
else None,
|
2022-02-17 16:36:35 +01:00
|
|
|
)
|
2022-03-03 17:40:17 +01:00
|
|
|
|
|
|
|
|
stats = (
|
|
|
|
|
evaluate_predictions(
|
|
|
|
|
forward_returns=forward_returns,
|
|
|
|
|
y_pred=training_outcome.predictions,
|
|
|
|
|
y_true=y,
|
|
|
|
|
discretize_func=config.labeling.get_discretize_function(),
|
|
|
|
|
labels=config.labeling.get_labels(),
|
|
|
|
|
transaction_costs=config.transaction_costs,
|
|
|
|
|
)
|
|
|
|
|
if config.mode == "training"
|
|
|
|
|
else None
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if stats is not None:
|
|
|
|
|
pp = pprint.PrettyPrinter(depth=2)
|
|
|
|
|
pp.pprint(stats)
|
2022-02-17 16:36:35 +01:00
|
|
|
|
2022-03-02 00:26:33 +01:00
|
|
|
return TrainingOutcome(
|
2022-03-03 17:40:17 +01:00
|
|
|
**vars(training_outcome), transformations=transformations_over_time, stats=stats
|
2022-03-02 00:26:33 +01:00
|
|
|
)
|