feat(Inference): Inference now runs on the entire pipeline, only train/predict one asset, adjust trading costs (#173)

* fix, feat: Fixed inference processing data. Add transformation attribute.

* feat: Added transformations step, refractored the loop to make more sense (divided the train and inference loop).

* feat: Truncated models over time and transformations over time. Fixed some typing aswell.

* fix: Fixed a number of out of array problems.

* feat: Inference now works!

* fix(Steps): runtime error not checking for None

* fix(Steps): preloaded transformers are not optional anymore, sped up training by temporary increasing the retrain_every

* fix(CI): disable ray memory monitoring

* refactor(Inference): removed truncate_models and replaced it with filling X with NaN until inference should start

* feat(Inference): added index_from parameter

* fix(Tests): walk_forward test

* refactor(Pipeline): only predict one asset

* refactor(Inference): removed select_models step, inference code moved to run_inference.py so it matches convention (similar to run_pipeline.py)

* fix(Evaluation): adjust transaction costs

* fix(Config): adjusted retrain_every

Co-authored-by: Daniel Szemerey <szemereydaniel@gmail.com>
Co-authored-by: Mark Aron Szulyovszky <mark.szulyovszky@gmail.com>
This commit is contained in:
Daniel Szemerey
2022-01-23 11:38:40 +01:00
committed by GitHub
parent 6b26643ece
commit 516c8bcc87
16 changed files with 141 additions and 154 deletions
+23 -9
View File
@@ -1,5 +1,5 @@
import pandas as pd
from typing import Literal, Union
from typing import Literal, Optional, Union
from training.walk_forward import walk_forward_train, walk_forward_inference
from utils.evaluate import evaluate_predictions
from models.base import Model
@@ -19,11 +19,12 @@ def train_primary_model(
expanding_window: bool,
sliding_window_size: int,
retrain_every: int,
from_index: Optional[int],
scaler: ScalerTypes,
no_of_classes: Literal['two', 'three-balanced', 'three-imbalanced'],
level: str,
print_results: bool,
preloaded_models: Union[list[Reporting.Single_Model], None] = None
preloaded_models: Optional[list[tuple[str, pd.Series, list[pd.Series]]]] = None
) -> tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame, list[Reporting.Single_Model]]:
results = pd.DataFrame()
@@ -31,13 +32,16 @@ def train_primary_model(
probabilities = pd.DataFrame(index=y.index)
all_models_single_asset: list[Reporting.Single_Model] = []
unified_models: list[tuple[str, pd.Series, list[pd.Series]]] = []
if preloaded_models is not None:
models = preloaded_models
unified_models = preloaded_models
transformations_over_time = None
for model_name, model in models:
if preloaded_models is None:
if preloaded_models is None:
train_unified_models=[]
for model_name, model in models:
model_over_time, transformations_over_time = walk_forward_train(
model_name=model_name,
model = model,
@@ -47,6 +51,7 @@ def train_primary_model(
expanding_window = expanding_window,
window_size = sliding_window_size,
retrain_every = retrain_every,
from_index = from_index,
transformations= [
get_scaler(scaler),
PCATransformation(ratio_components_to_keep=0.5, sliding_window_size=sliding_window_size),
@@ -54,13 +59,19 @@ def train_primary_model(
],
preloaded_transformations=transformations_over_time,
)
train_unified_models.append((model_name, model_over_time, transformations_over_time))
unified_models = train_unified_models
for model_tuples in unified_models:
model_name, model_over_time, transformations_over_time = model_tuples[0], model_tuples[1], model_tuples[2]
preds, probs = walk_forward_inference(
model_name = model_name,
model_over_time= model_over_time if preloaded_models is None else pd.Series(model),
model_over_time= pd.Series(model_over_time),
transformations_over_time = transformations_over_time,
X = X,
expanding_window = expanding_window,
window_size = sliding_window_size
window_size = sliding_window_size,
from_index = from_index,
)
assert len(preds) == len(y)
@@ -75,11 +86,14 @@ def train_primary_model(
discretize=True
)
levelname=("_" + level) if level=='metalabeling' else ""
column_name = "model_" + model_name + "_" + ticker_to_predict + levelname
if preloaded_models is None:
column_name = "model_" + model_name + "_" + ticker_to_predict + levelname
else:
column_name = model_name
results[column_name] = result
all_models_single_asset.append(Reporting.Single_Model(model_name=column_name, model_over_time=model_over_time.tolist()))
all_models_single_asset.append(Reporting.Single_Model(model_name=column_name, model_over_time=model_over_time, transformations_over_time=transformations_over_time))
# column names for model outputs should be different, so we can differentiate between original data and model predictions later, where necessary
predictions[column_name] = preds