mirror of
https://github.com/webclinic017/drift.git
synced 2026-08-18 05:18:09 +00:00
refactor(Naming): use new convention, added Ensemble model parameter back, support multiple Meta-Labeling models (#132)
* refactor(Naming): use `primary_models` & `meta_labeling_models` * refactor(Naming): using primary * meta_labeling across config and in pipeline * feat(Pipeline): added back Ensemble models * fix(Pipeline): compiler error * fix(Config): typo * chore(Pipeline): removed unused averaging step * revert the changes in discretizing * chore(Pipeline): remove sharpe improvement logging * fix(Pipeline): ensemble predictions should be a pd.Series instead of a DataFrame * fix(Pipeline): discard unnecessary ensemble_probabilities * fix(Pipeline): fixes regarding various meta-labeling ensemble bugs * fix(Reporting): use the new naming convention * fix(Reporting): use the right variable * feat(Sweep): new sweep for ensemble models * fix(Sweep): config reference * fix(Config): simplified dev config * fix(Models): use the faster LR model * fix(Models): use LGBM in the meta-labeling model for speed * fix(Selection): always use the first model for feature selection, commented out caching from select_features() as it's close to redundant in terms of speed
This commit is contained in:
@@ -1,25 +0,0 @@
|
||||
|
||||
import pandas as pd
|
||||
from utils.evaluate import evaluate_predictions
|
||||
|
||||
def average_and_evaluate_predictions(predictions: pd.DataFrame, y: pd.Series, target_returns: pd.Series, data_config: dict) -> tuple[pd.Series, pd.DataFrame]:
|
||||
averaged_predictions = predictions.mean(axis = 1)
|
||||
non_discretized_result = evaluate_predictions(
|
||||
model_name = 'Averaged - Non-discrete',
|
||||
target_returns = target_returns,
|
||||
y_pred = averaged_predictions,
|
||||
y_true = y,
|
||||
method = 'classification',
|
||||
no_of_classes = data_config['no_of_classes'],
|
||||
discretize=False
|
||||
)
|
||||
discretized_result = evaluate_predictions(
|
||||
model_name = 'Averaged - Discrete',
|
||||
target_returns = target_returns,
|
||||
y_pred = averaged_predictions,
|
||||
y_true = y,
|
||||
method = 'classification',
|
||||
no_of_classes = data_config['no_of_classes'],
|
||||
discretize=True
|
||||
)
|
||||
return averaged_predictions, pd.concat([non_discretized_result, discretized_result], axis = 1)
|
||||
+23
-14
@@ -1,20 +1,23 @@
|
||||
from utils.evaluate import discretize_threeway_threshold, evaluate_predictions
|
||||
from utils.helpers import random_string, equal_except_nan, drop_until_first_valid_index
|
||||
from training.training import run_single_asset_trainig
|
||||
from training.primary_model import train_primary_model
|
||||
from feature_selection.feature_selection import select_features
|
||||
import pandas as pd
|
||||
from models.model_map import default_feature_selector_regression, default_feature_selector_classification
|
||||
from models.base import Model
|
||||
|
||||
|
||||
def run_meta_labeling_training(
|
||||
def train_meta_labeling_model(
|
||||
target_asset: str,
|
||||
X_pca: pd.DataFrame,
|
||||
input_predictions: pd.Series,
|
||||
y: pd.Series,
|
||||
target_returns: pd.Series,
|
||||
models: list[tuple[str, Model]],
|
||||
data_config: dict,
|
||||
model_config: dict,
|
||||
training_config: dict
|
||||
training_config: dict,
|
||||
model_suffix: str
|
||||
) -> tuple[pd.Series, pd.Series, pd.DataFrame, dict]:
|
||||
|
||||
discretize = discretize_threeway_threshold(0.33)
|
||||
@@ -24,29 +27,34 @@ def run_meta_labeling_training(
|
||||
print("Feature Selection started")
|
||||
backup_model = default_feature_selector_regression if data_config['method'] == 'regression' else default_feature_selector_classification
|
||||
meta_feature_selection_input_X, meta_feature_selection_input_y = drop_until_first_valid_index(X_pca, meta_y)
|
||||
feature_selection_output = select_features(X = meta_feature_selection_input_X, y = meta_feature_selection_input_y, model = model_config['level_1_models'][0][1], n_features_to_select = training_config['n_features_to_select'], backup_model = backup_model, scaling = training_config['scaler'], dynamic_feature_selection = training_config['dynamic_feature_selection'], data_config_hash = random_string(10))
|
||||
feature_selection_output = select_features(X = meta_feature_selection_input_X, y = meta_feature_selection_input_y, model = models[0][1], n_features_to_select = training_config['n_features_to_select'], backup_model = backup_model, scaling = training_config['scaler'])
|
||||
meta_selected_features_X = X_pca[feature_selection_output.columns]
|
||||
|
||||
meta_X = pd.concat([meta_selected_features_X, input_predictions, discretized_predictions], axis = 1)
|
||||
|
||||
_, meta_preds, meta_probabilities, all_models_single_asset = run_single_asset_trainig(
|
||||
_, meta_preds, meta_probabilities, all_models_single_asset = train_primary_model(
|
||||
ticker_to_predict = "prediction_correct",
|
||||
original_X = meta_X,
|
||||
X = meta_X,
|
||||
y = meta_y,
|
||||
target_returns = target_returns,
|
||||
models = [model_config['level_2_model']],
|
||||
method = data_config['method'],
|
||||
expanding_window = training_config['expanding_window_level2'],
|
||||
sliding_window_size = training_config['sliding_window_size_level2'],
|
||||
models = models,
|
||||
method = 'classification',
|
||||
expanding_window = training_config['expanding_window_meta_labeling'],
|
||||
sliding_window_size = training_config['sliding_window_size_meta_labeling'],
|
||||
retrain_every = training_config['retrain_every'],
|
||||
scaler = training_config['scaler'],
|
||||
no_of_classes = 'two',
|
||||
level = 2
|
||||
level = 'meta_labeling',
|
||||
print_results = False
|
||||
)
|
||||
bet_size = meta_probabilities.iloc[:,1]
|
||||
if len(models) > 1:
|
||||
meta_preds = meta_preds.mean(axis = 1)
|
||||
bet_size = meta_probabilities[meta_probabilities.columns[1::2]].mean(axis = 1)
|
||||
else:
|
||||
bet_size = meta_probabilities.iloc[:,1]
|
||||
avg_predictions_with_sizing = input_predictions * bet_size
|
||||
avg_predictions_with_sizing.rename("model_" + target_asset + "_meta_lvl" + str(2), inplace=True)
|
||||
avg_predictions_with_sizing.rename("model_" + target_asset + "_" + model_suffix, inplace=True)
|
||||
|
||||
meta_result = evaluate_predictions(
|
||||
model_name = "Meta",
|
||||
@@ -54,9 +62,10 @@ def run_meta_labeling_training(
|
||||
y_pred = avg_predictions_with_sizing,
|
||||
y_true = y,
|
||||
method = 'classification',
|
||||
no_of_classes = data_config['no_of_classes'],
|
||||
no_of_classes = 'two',
|
||||
print_results = True,
|
||||
discretize=False
|
||||
)
|
||||
meta_result.rename("model_" + target_asset + "_meta_lvl" + str(2), inplace=True)
|
||||
meta_result.rename("model_" + target_asset + "_" + model_suffix, inplace=True)
|
||||
|
||||
return meta_result, avg_predictions_with_sizing, meta_probabilities, all_models_single_asset
|
||||
|
||||
@@ -6,7 +6,7 @@ from models.base import Model
|
||||
from utils.scaler import get_scaler
|
||||
from utils.types import ScalerTypes
|
||||
|
||||
def run_single_asset_trainig(
|
||||
def train_primary_model(
|
||||
ticker_to_predict: str,
|
||||
original_X: pd.DataFrame,
|
||||
X: pd.DataFrame,
|
||||
@@ -19,10 +19,10 @@ def run_single_asset_trainig(
|
||||
retrain_every: int,
|
||||
scaler: ScalerTypes,
|
||||
no_of_classes: Literal['two', 'three-balanced', 'three-imbalanced'],
|
||||
level: int
|
||||
level: str,
|
||||
print_results: bool
|
||||
) -> tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame, dict]:
|
||||
|
||||
|
||||
scaler = get_scaler(scaler)
|
||||
|
||||
results = pd.DataFrame()
|
||||
@@ -51,14 +51,15 @@ def run_single_asset_trainig(
|
||||
y_true = y,
|
||||
method = method,
|
||||
no_of_classes=no_of_classes,
|
||||
print_results = print_results,
|
||||
discretize=True
|
||||
)
|
||||
column_name = "model_" + ticker_to_predict + "_" + model_name + "_lvl" + str(level)
|
||||
column_name = "model_" + ticker_to_predict + "_" + model_name + "_" + level
|
||||
results[column_name] = result
|
||||
all_models_single_asset[model_name] = model_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
|
||||
probs_column_name = "probs_" + ticker_to_predict + "_" + model_name + "_lvl" + str(level)
|
||||
probs_column_name = "probs_" + ticker_to_predict + "_" + model_name + "_" + level
|
||||
probs.columns = [probs_column_name + "_" + c for c in probs.columns]
|
||||
probabilities = pd.concat([probabilities, probs], axis=1)
|
||||
|
||||
Reference in New Issue
Block a user