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
+43
View File
@@ -0,0 +1,43 @@
import pickle
import datetime
from typing import Optional, Union
import os
import warnings
from reporting.types import Reporting
def save_models(all_models: Reporting.Asset, data_config:dict, training_config:dict, model_config:dict) -> None:
dict_for_pickle = dict()
dict_for_pickle['training_config'] = training_config
dict_for_pickle['data_config'] = data_config
dict_for_pickle['model_config'] = model_config
dict_for_pickle['all_models'] = all_models
date_string = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M")
if not os.path.exists('output/models'):
warnings.warn("No folder exists, creating one.")
os.makedirs('output/models')
pickle.dump( dict_for_pickle, open( "output/models/{}.p".format(date_string), "wb" ) )
def load_models(file_name:Union[str, None]) -> tuple[Reporting.Asset, dict, dict, dict]:
if file_name is None:
warnings.warn("No file name provided, will load latest models and configurations.")
files_in_directory:list = os.listdir('output/models')
assert len(files_in_directory) > 0, "No models found in output/models."
file_name = sorted(files_in_directory)[-1]
packacked_dict = pickle.load( open( "output/models/{}".format(file_name), "rb" ) )
data_config = packacked_dict.pop("data_config", None)
training_config = packacked_dict.pop("training_config", None)
model_config = packacked_dict.pop("model_config", None)
all_models = packacked_dict.pop("all_models", None)
return all_models, data_config, training_config, model_config
+14 -12
View File
@@ -1,6 +1,7 @@
from __future__ import annotations
import pandas as pd
from models.base import Model
from typing import Optional, Union
class Reporting:
@@ -8,16 +9,17 @@ class Reporting:
self.results: pd.DataFrame = pd.DataFrame()
self.all_predictions: pd.DataFrame = pd.DataFrame()
self.all_probabilities: pd.DataFrame = pd.DataFrame()
self.all_assets:list[Reporting.Asset] = []
self.asset: Reporting.Asset
def get_results(self)->tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame, list[Reporting.Asset]]:
return self.results, self.all_predictions, self.all_probabilities, self.all_assets
def get_results(self) -> tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame, Reporting.Asset]:
return self.results, self.all_predictions, self.all_probabilities, self.asset
class Single_Model:
def __init__(self, model_name: str, model_over_time: list[Model]):
def __init__(self, model_name: str, model_over_time: pd.Series, transformations_over_time: list[pd.Series]):
self.model_name: str = model_name
self.model_over_time: list[Model] = model_over_time
self.model_over_time: pd.Series = model_over_time
self.transformations_over_time: list[pd.Series] = transformations_over_time
class Training_Step:
@@ -26,14 +28,14 @@ class Reporting:
self.base: list[Reporting.Single_Model] = []
self.metalabeling: list[list[Reporting.Single_Model]] = []
def convert_step_to_tuple(self, step:str)->list[tuple[str, list[Model]]]:
if step == 'base':
return [(x.model_name, x.model_over_time) for x in self.base ]
elif step == 'metalabeling':
return [(x.model_name, x.model_over_time) for sub in self.metalabeling for x in sub]
else:
raise ValueError('Unknown step: {}'.format(step))
def get_base(self) -> list[tuple[str, pd.Series, list[pd.Series]]]:
return [(x.model_name, x.model_over_time, x.transformations_over_time) for x in self.base ]
def get_metalabeling(self) -> dict:
structured_dict = dict()
for i, model in enumerate(self.base):
structured_dict[model.model_name] = [(x.model_name, x.model_over_time, x.transformations_over_time) for x in self.metalabeling[i]]
return structured_dict
class Asset():
def __init__(self, ticker: str, primary: Reporting.Training_Step, secondary: Reporting.Training_Step):