Files
drift/training/training.py
T
Mark Aron Szulyovszky 9488e92597 feature(MetaLabeling): replaced previous non-functional Ensembling method with Meta-labeling method available for both lvl1 and lvl2 models (#110)
* feature(MetaLabeling): added hacky prototype

* fix(MetaLabeling): drop index until first valid X & y

* fix(MetaLabeling): transform both X & y before feature selection

* fix(MetaLabeling): got feature selection to work

* fix(MetaLabeling): correct values for meta_y

* feat(MetaLabeling): created predictions multiplied by bet sizes

* feat(Pipeline): print out averaged result

* fix(Evaluation): correctly deal with non-discretized data

* fix(Pipeline): use the right column names

* refactor(Pipeline): move out meta-labeling

* refactor(Pipeline): complete refactoring

* feat(CI): post results to PR

* fix(Pipeline): use the correct filename

* chore(Config): removed now redundant feature_selection flag

* feat(Models): added SVC

* fix(Pipeline): accidentally switched two return values

* feat(Sweep): prepared sweep_meta.yaml, moved report_results() into a separate file

* fix(Pipeline): wrong function name

* fix(Sweep): yaml + run_sweep

* fix(Sweep): typo in name

* fix(Reporting): only save averaged results

* feat(MetaLabeling): use optional meta-labeling step for every lvl1 models, before averaging

* feat(Reporting): print out sharpe improvement in meta-labeling step

* fix(Sweep): adjusted config, defaulted to good defaults

* fix(Sweep): adjusted sweep
2022-01-06 16:36:45 +01:00

63 lines
2.5 KiB
Python

import pandas as pd
from typing import Literal
from training.walk_forward import walk_forward_train_test
from utils.evaluate import evaluate_predictions
from models.base import Model
from utils.scaler import get_scaler
from utils.types import ScalerTypes
def run_single_asset_trainig(
ticker_to_predict: str,
original_X: pd.DataFrame,
X: pd.DataFrame,
y: pd.Series,
target_returns: pd.Series,
models: list[tuple[str, Model]],
method: Literal['regression', 'classification'],
expanding_window: bool,
sliding_window_size: int,
retrain_every: int,
scaler: ScalerTypes,
no_of_classes: Literal['two', 'three-balanced', 'three-imbalanced'],
level: int
) -> tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame]:
scaler = get_scaler(scaler)
results = pd.DataFrame()
predictions = pd.DataFrame(index=y.index)
probabilities = pd.DataFrame(index=y.index)
for model_name, model in models:
model_over_time, preds, probs = walk_forward_train_test(
model_name=model_name,
model = model,
X = X if model.feature_selection == 'on' else original_X,
y = y,
target_returns = target_returns,
expanding_window = expanding_window,
window_size = sliding_window_size,
retrain_every = retrain_every,
scaler = scaler
)
assert len(preds) == len(y)
result = evaluate_predictions(
model_name = model_name,
target_returns = target_returns,
y_pred = preds,
y_true = y,
method = method,
no_of_classes=no_of_classes,
discretize=True
)
column_name = "model_" + ticker_to_predict + "_" + model_name + "_lvl" + str(level)
results[column_name] = result
# 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
probs_column_name = "probs_" + ticker_to_predict + "_" + model_name + "_lvl" + str(level)
probs.columns = [probs_column_name + "_" + c for c in probs.columns]
probabilities = pd.concat([probabilities, probs], axis=1)
return results, predictions, probabilities