Files
drift/training/directional_training.py
T
Mark Aron Szulyovszky 3eb3ea94e3 Refactor(Training): new outcome types, representative pipeline steps, bet-sizing (#187)
* refactor(Training): added InferenceResult & TrainedModel types

* refactor(Pipeline): introduced TrainingOutcome, BetSizingWithMetaOutcome, etc.

* fix(Pipeline): getting it to compile

* refactor(WalkForward): separate preprocessing step

* feat(Pipeline): separate out transformations processing step

* refactor(Pipeline): use the Directional model terminology, put bet_sizing into pipeline instead of hiding it in a step

* refactor(WalkForward): moved functions to separate folder

* fix(WalkForward): use sparse array to store models, process transformations in parallel (lot faster)

* fix(Tests): and evaluation

* fix(Tests): for realz

* fix(Inference): preloading everything now, renamed primary models to directional models

* fix(BetSizing): was running transformations on the wrong data, oops

* fix(BetSizing): concatenated on the wrong axis accidentally

* fix(Reporting): able to use the new Stats type

* fix(BetSizing): renamed int column names

* fix(Portfolio): name the column properly

* fix(Reporting): rename the correct Series, lol

* fix(Inference): walk_forwad_inference() can deal with models not being aligned with the starting index

* fix(WalkForward): accidentally using the wrong index

* fix(WalkForward): use the correct indicies to fetch last model/transformations

* fix(CI): changed the name of the results
2022-01-29 06:41:40 +01:00

63 lines
2.5 KiB
Python

import pandas as pd
from .types import DirectionalTrainingOutcome
from training.train_model import train_model
from training.walk_forward import walk_forward_process_transformations
from typing import Optional
from config.types import Config
from models.base import Model
from models.model_map import default_feature_selector_classification
from transformations.scaler import get_scaler
from transformations.rfe import RFETransformation
from transformations.pca import PCATransformation
def train_directional_models(
X: pd.DataFrame,
y: pd.Series,
forward_returns: pd.Series,
config: Config,
models: list[Model],
from_index: Optional[pd.Timestamp],
preloaded_training_step: Optional[DirectionalTrainingOutcome] = None,
) -> DirectionalTrainingOutcome:
if preloaded_training_step is None:
print("Preprocess transformations")
transformations_over_time = walk_forward_process_transformations(
X = X,
y = y,
forward_returns = forward_returns,
expanding_window = config.expanding_window_base,
window_size = config.sliding_window_size_base,
retrain_every = config.retrain_every,
from_index = from_index,
transformations= [
get_scaler(config.scaler),
PCATransformation(ratio_components_to_keep=0.5, sliding_window_size=config.sliding_window_size_base),
RFETransformation(n_feature_to_select=40, model=default_feature_selector_classification)
],
)
else:
transformations_over_time = preloaded_training_step.transformations
training_outcomes = [train_model(
ticker_to_predict = config.target_asset[1],
X = X,
y = y,
forward_returns = forward_returns,
model = model,
expanding_window = config.expanding_window_base,
sliding_window_size = config.sliding_window_size_base,
retrain_every = config.retrain_every,
from_index = from_index,
no_of_classes = config.no_of_classes,
level = 'primary',
print_results= True,
transformations_over_time = transformations_over_time,
model_over_time = preloaded_training_step.training[index].model_over_time if preloaded_training_step else None
) for index, model in enumerate(models)]
return DirectionalTrainingOutcome(training_outcomes, transformations_over_time)