mirror of
https://github.com/webclinic017/drift.git
synced 2026-08-14 03:18:06 +00:00
chore(Linter): reformatted code with black (#211)
* chore(Linter): reformatted code with black * Create black.yaml
This commit is contained in:
+63
-46
@@ -13,75 +13,92 @@ from transformations.scaler import get_scaler
|
||||
from transformations.rfe import RFETransformation
|
||||
from transformations.pca import PCATransformation
|
||||
|
||||
|
||||
def bet_sizing_with_meta_model(
|
||||
X: XDataFrame,
|
||||
input_predictions: pd.Series,
|
||||
y: ySeries,
|
||||
forward_returns: ForwardReturnSeries,
|
||||
model: Model,
|
||||
config: Config,
|
||||
model_suffix: str,
|
||||
from_index: Optional[pd.Timestamp],
|
||||
transformations_over_time: Optional[TransformationsOverTime] = None,
|
||||
preloaded_models: Optional[ModelOverTime] = None
|
||||
) -> BetSizingWithMetaOutcome:
|
||||
X: XDataFrame,
|
||||
input_predictions: pd.Series,
|
||||
y: ySeries,
|
||||
forward_returns: ForwardReturnSeries,
|
||||
model: Model,
|
||||
config: Config,
|
||||
model_suffix: str,
|
||||
from_index: Optional[pd.Timestamp],
|
||||
transformations_over_time: Optional[TransformationsOverTime] = None,
|
||||
preloaded_models: Optional[ModelOverTime] = None,
|
||||
) -> BetSizingWithMetaOutcome:
|
||||
|
||||
input_predictions.name = "model_predictions"
|
||||
discretized_predictions = input_predictions.apply(discretize_threeway_threshold(0.33))
|
||||
discretized_predictions = input_predictions.apply(
|
||||
discretize_threeway_threshold(0.33)
|
||||
)
|
||||
discretized_predictions.name = "model_discretized_predictions"
|
||||
|
||||
meta_y: pd.Series = pd.concat([discretized_predictions, y], axis=1).apply(equal_except_nan, axis = 1)
|
||||
meta_X = pd.concat([X, input_predictions, discretized_predictions], axis = 1)
|
||||
meta_y: pd.Series = pd.concat([discretized_predictions, y], axis=1).apply(
|
||||
equal_except_nan, axis=1
|
||||
)
|
||||
meta_X = pd.concat([X, input_predictions, discretized_predictions], axis=1)
|
||||
|
||||
if transformations_over_time is None:
|
||||
print("Preprocess transformations")
|
||||
transformations_over_time = walk_forward_process_transformations(
|
||||
X = meta_X,
|
||||
y = meta_y,
|
||||
forward_returns = forward_returns,
|
||||
expanding_window = config.expanding_window_meta,
|
||||
window_size = config.sliding_window_size_meta,
|
||||
retrain_every = config.retrain_every,
|
||||
from_index = from_index,
|
||||
transformations= [
|
||||
X=meta_X,
|
||||
y=meta_y,
|
||||
forward_returns=forward_returns,
|
||||
expanding_window=config.expanding_window_meta,
|
||||
window_size=config.sliding_window_size_meta,
|
||||
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_meta),
|
||||
RFETransformation(n_feature_to_select=40, model=default_feature_selector_classification)
|
||||
PCATransformation(
|
||||
ratio_components_to_keep=0.5,
|
||||
sliding_window_size=config.sliding_window_size_meta,
|
||||
),
|
||||
RFETransformation(
|
||||
n_feature_to_select=40,
|
||||
model=default_feature_selector_classification,
|
||||
),
|
||||
],
|
||||
)
|
||||
|
||||
meta_outcome = train_model(
|
||||
ticker_to_predict = "prediction_correct",
|
||||
X = meta_X,
|
||||
y = meta_y,
|
||||
forward_returns = forward_returns,
|
||||
model = model,
|
||||
expanding_window = config.expanding_window_meta,
|
||||
sliding_window_size = config.sliding_window_size_meta,
|
||||
retrain_every = config.retrain_every,
|
||||
from_index = from_index,
|
||||
no_of_classes = 'two',
|
||||
level = 'meta',
|
||||
output_stats = config.mode == 'training',
|
||||
transformations_over_time = transformations_over_time,
|
||||
model_over_time = preloaded_models,
|
||||
ticker_to_predict="prediction_correct",
|
||||
X=meta_X,
|
||||
y=meta_y,
|
||||
forward_returns=forward_returns,
|
||||
model=model,
|
||||
expanding_window=config.expanding_window_meta,
|
||||
sliding_window_size=config.sliding_window_size_meta,
|
||||
retrain_every=config.retrain_every,
|
||||
from_index=from_index,
|
||||
no_of_classes="two",
|
||||
level="meta",
|
||||
output_stats=config.mode == "training",
|
||||
transformations_over_time=transformations_over_time,
|
||||
model_over_time=preloaded_models,
|
||||
)
|
||||
|
||||
meta_predictions = meta_outcome.predictions
|
||||
bet_size = meta_outcome.probabilities.iloc[:,1]
|
||||
bet_size = meta_outcome.probabilities.iloc[:, 1]
|
||||
avg_predictions_with_sizing = input_predictions * meta_predictions * bet_size
|
||||
|
||||
if config.mode == 'training':
|
||||
if config.mode == "training":
|
||||
stats = evaluate_predictions(
|
||||
forward_returns = forward_returns,
|
||||
y_pred = avg_predictions_with_sizing,
|
||||
y_true = y,
|
||||
no_of_classes = 'three-balanced',
|
||||
discretize=False
|
||||
forward_returns=forward_returns,
|
||||
y_pred=avg_predictions_with_sizing,
|
||||
y_true=y,
|
||||
no_of_classes="three-balanced",
|
||||
discretize=False,
|
||||
)
|
||||
print(stats)
|
||||
else:
|
||||
stats = None
|
||||
model_id = "model_" + config.target_asset[1] + "_" + model_suffix
|
||||
|
||||
return BetSizingWithMetaOutcome(model_id, meta_outcome, transformations_over_time, avg_predictions_with_sizing, stats)
|
||||
return BetSizingWithMetaOutcome(
|
||||
model_id,
|
||||
meta_outcome,
|
||||
transformations_over_time,
|
||||
avg_predictions_with_sizing,
|
||||
stats,
|
||||
)
|
||||
|
||||
@@ -5,7 +5,7 @@ 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 config.types import Config
|
||||
from models.base import Model
|
||||
from models.model_map import default_feature_selector_classification
|
||||
|
||||
@@ -13,53 +13,61 @@ from transformations.scaler import get_scaler
|
||||
from transformations.rfe import RFETransformation
|
||||
from transformations.pca import PCATransformation
|
||||
|
||||
|
||||
def train_directional_model(
|
||||
X: pd.DataFrame,
|
||||
y: pd.Series,
|
||||
forward_returns: pd.Series,
|
||||
config: Config,
|
||||
model: Model,
|
||||
from_index: Optional[pd.Timestamp],
|
||||
preloaded_training_step: Optional[DirectionalTrainingOutcome] = None,
|
||||
) -> DirectionalTrainingOutcome:
|
||||
X: pd.DataFrame,
|
||||
y: pd.Series,
|
||||
forward_returns: pd.Series,
|
||||
config: Config,
|
||||
model: 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= [
|
||||
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)
|
||||
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_outcome = 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',
|
||||
output_stats= config.mode == 'training',
|
||||
transformations_over_time = transformations_over_time,
|
||||
model_over_time = preloaded_training_step.training.model_over_time if preloaded_training_step else None
|
||||
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",
|
||||
output_stats=config.mode == "training",
|
||||
transformations_over_time=transformations_over_time,
|
||||
model_over_time=preloaded_training_step.training.model_over_time
|
||||
if preloaded_training_step
|
||||
else None,
|
||||
)
|
||||
if config.mode == 'training':
|
||||
if config.mode == "training":
|
||||
print(training_outcome.stats)
|
||||
|
||||
return DirectionalTrainingOutcome(training_outcome, transformations_over_time)
|
||||
|
||||
|
||||
+52
-43
@@ -1,69 +1,78 @@
|
||||
import pandas as pd
|
||||
from typing import Literal, Optional
|
||||
from training.walk_forward import walk_forward_train, walk_forward_inference, walk_forward_inference_batched
|
||||
from training.walk_forward import (
|
||||
walk_forward_train,
|
||||
walk_forward_inference,
|
||||
walk_forward_inference_batched,
|
||||
)
|
||||
from utils.evaluate import evaluate_predictions
|
||||
from models.base import Model
|
||||
from .types import ModelOverTime, TransformationsOverTime, TrainingOutcome
|
||||
|
||||
|
||||
def train_model(
|
||||
ticker_to_predict: str,
|
||||
X: pd.DataFrame,
|
||||
y: pd.Series,
|
||||
forward_returns: pd.Series,
|
||||
model: Model,
|
||||
expanding_window: bool,
|
||||
sliding_window_size: int,
|
||||
retrain_every: int,
|
||||
from_index: Optional[pd.Timestamp],
|
||||
no_of_classes: Literal['two', 'three-balanced', 'three-imbalanced'],
|
||||
level: str,
|
||||
output_stats: bool,
|
||||
transformations_over_time: TransformationsOverTime,
|
||||
model_over_time: Optional[ModelOverTime]
|
||||
) -> TrainingOutcome:
|
||||
ticker_to_predict: str,
|
||||
X: pd.DataFrame,
|
||||
y: pd.Series,
|
||||
forward_returns: pd.Series,
|
||||
model: Model,
|
||||
expanding_window: bool,
|
||||
sliding_window_size: int,
|
||||
retrain_every: int,
|
||||
from_index: Optional[pd.Timestamp],
|
||||
no_of_classes: Literal["two", "three-balanced", "three-imbalanced"],
|
||||
level: str,
|
||||
output_stats: bool,
|
||||
transformations_over_time: TransformationsOverTime,
|
||||
model_over_time: Optional[ModelOverTime],
|
||||
) -> TrainingOutcome:
|
||||
|
||||
if model_over_time is None:
|
||||
print("Train model")
|
||||
model_over_time = walk_forward_train(
|
||||
model = model,
|
||||
X = X,
|
||||
y = y,
|
||||
forward_returns = forward_returns,
|
||||
expanding_window = expanding_window,
|
||||
window_size = sliding_window_size,
|
||||
retrain_every = retrain_every,
|
||||
from_index = from_index,
|
||||
transformations_over_time = transformations_over_time,
|
||||
model=model,
|
||||
X=X,
|
||||
y=y,
|
||||
forward_returns=forward_returns,
|
||||
expanding_window=expanding_window,
|
||||
window_size=sliding_window_size,
|
||||
retrain_every=retrain_every,
|
||||
from_index=from_index,
|
||||
transformations_over_time=transformations_over_time,
|
||||
)
|
||||
|
||||
levelname = ("_" + level) if level == 'meta' else ""
|
||||
levelname = ("_" + level) if level == "meta" else ""
|
||||
if model_over_time is None:
|
||||
model_id = "model_" + model.name + "_" + ticker_to_predict + levelname
|
||||
model_id = "model_" + model.name + "_" + ticker_to_predict + levelname
|
||||
else:
|
||||
model_id = model_over_time.name
|
||||
|
||||
inference_function = walk_forward_inference if from_index is not None else walk_forward_inference_batched
|
||||
predictions, probabilities = inference_function(
|
||||
model_name = model_id,
|
||||
model_over_time= model_over_time,
|
||||
transformations_over_time = transformations_over_time,
|
||||
X = X,
|
||||
expanding_window = expanding_window,
|
||||
window_size = sliding_window_size,
|
||||
retrain_every = retrain_every,
|
||||
from_index = from_index,
|
||||
inference_function = (
|
||||
walk_forward_inference
|
||||
if from_index is not None
|
||||
else walk_forward_inference_batched
|
||||
)
|
||||
|
||||
predictions, probabilities = inference_function(
|
||||
model_name=model_id,
|
||||
model_over_time=model_over_time,
|
||||
transformations_over_time=transformations_over_time,
|
||||
X=X,
|
||||
expanding_window=expanding_window,
|
||||
window_size=sliding_window_size,
|
||||
retrain_every=retrain_every,
|
||||
from_index=from_index,
|
||||
)
|
||||
|
||||
assert len(predictions) == len(y)
|
||||
if output_stats:
|
||||
stats = evaluate_predictions(
|
||||
forward_returns = forward_returns,
|
||||
y_pred = predictions,
|
||||
y_true = y,
|
||||
forward_returns=forward_returns,
|
||||
y_pred=predictions,
|
||||
y_true=y,
|
||||
no_of_classes=no_of_classes,
|
||||
discretize=True
|
||||
discretize=True,
|
||||
)
|
||||
else:
|
||||
stats = None
|
||||
|
||||
return TrainingOutcome(model_id, predictions, probabilities, stats, model_over_time)
|
||||
return TrainingOutcome(model_id, predictions, probabilities, stats, model_over_time)
|
||||
|
||||
+5
-2
@@ -19,6 +19,7 @@ class TrainingOutcome:
|
||||
stats: Optional[Stats]
|
||||
model_over_time: ModelOverTime
|
||||
|
||||
|
||||
@dataclass
|
||||
class BetSizingWithMetaOutcome:
|
||||
model_id: str
|
||||
@@ -27,11 +28,13 @@ class BetSizingWithMetaOutcome:
|
||||
weights: WeightsSeries
|
||||
stats: Optional[Stats]
|
||||
|
||||
|
||||
@dataclass
|
||||
class DirectionalTrainingOutcome:
|
||||
training: TrainingOutcome
|
||||
transformations: TransformationsOverTime
|
||||
|
||||
|
||||
|
||||
@dataclass
|
||||
class PipelineOutcome:
|
||||
directional_training: DirectionalTrainingOutcome
|
||||
@@ -41,4 +44,4 @@ class PipelineOutcome:
|
||||
return self.bet_sizing.weights
|
||||
|
||||
def get_output_stats(self) -> Stats:
|
||||
return self.bet_sizing.stats
|
||||
return self.bet_sizing.stats
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from .inference_batched import walk_forward_inference_batched
|
||||
from .inference import walk_forward_inference
|
||||
from .train import walk_forward_train
|
||||
from .process_transformations import walk_forward_process_transformations
|
||||
from .process_transformations import walk_forward_process_transformations
|
||||
|
||||
@@ -1,49 +1,79 @@
|
||||
import pandas as pd
|
||||
from training.types import ModelOverTime, TransformationsOverTime, PredictionsSeries, ProbabilitiesDataFrame
|
||||
from training.types import (
|
||||
ModelOverTime,
|
||||
TransformationsOverTime,
|
||||
PredictionsSeries,
|
||||
ProbabilitiesDataFrame,
|
||||
)
|
||||
from utils.helpers import get_first_valid_return_index
|
||||
from tqdm import tqdm
|
||||
from typing import Optional
|
||||
from data_loader.types import XDataFrame
|
||||
from utils.helpers import get_last_non_na_index
|
||||
|
||||
|
||||
def walk_forward_inference(
|
||||
model_name: str,
|
||||
model_over_time: ModelOverTime,
|
||||
transformations_over_time: TransformationsOverTime,
|
||||
X: XDataFrame,
|
||||
expanding_window: bool,
|
||||
window_size: int,
|
||||
retrain_every: int,
|
||||
from_index: Optional[pd.Timestamp],
|
||||
) -> tuple[PredictionsSeries, ProbabilitiesDataFrame]:
|
||||
predictions = pd.Series(index=X.index, dtype='object').rename(model_name)
|
||||
model_name: str,
|
||||
model_over_time: ModelOverTime,
|
||||
transformations_over_time: TransformationsOverTime,
|
||||
X: XDataFrame,
|
||||
expanding_window: bool,
|
||||
window_size: int,
|
||||
retrain_every: int,
|
||||
from_index: Optional[pd.Timestamp],
|
||||
) -> tuple[PredictionsSeries, ProbabilitiesDataFrame]:
|
||||
predictions = pd.Series(index=X.index, dtype="object").rename(model_name)
|
||||
probabilities = pd.DataFrame(index=X.index)
|
||||
|
||||
inference_from = max(get_first_valid_return_index(model_over_time), get_first_valid_return_index(X.iloc[:,0])) if from_index is None else X.index.to_list().index(from_index)
|
||||
inference_from = (
|
||||
max(
|
||||
get_first_valid_return_index(model_over_time),
|
||||
get_first_valid_return_index(X.iloc[:, 0]),
|
||||
)
|
||||
if from_index is None
|
||||
else X.index.to_list().index(from_index)
|
||||
)
|
||||
inference_till = X.shape[0]
|
||||
model_index_offset = get_last_non_na_index(model_over_time, inference_from) if pd.isna(model_over_time[inference_from]) else 0
|
||||
first_model = model_over_time[inference_from - model_index_offset] if pd.isna(model_over_time[inference_from]) else model_over_time[inference_from]
|
||||
model_index_offset = (
|
||||
get_last_non_na_index(model_over_time, inference_from)
|
||||
if pd.isna(model_over_time[inference_from])
|
||||
else 0
|
||||
)
|
||||
first_model = (
|
||||
model_over_time[inference_from - model_index_offset]
|
||||
if pd.isna(model_over_time[inference_from])
|
||||
else model_over_time[inference_from]
|
||||
)
|
||||
|
||||
if first_model.only_column is not None:
|
||||
X = X[[column for column in X.columns if first_model.only_column in column]]
|
||||
|
||||
if first_model.data_transformation == 'original':
|
||||
|
||||
if first_model.data_transformation == "original":
|
||||
transformations_over_time = []
|
||||
|
||||
for index in tqdm(range(inference_from, inference_till)):
|
||||
|
||||
last_model_index = index - ((index - inference_from) % retrain_every) - model_index_offset
|
||||
train_window_start = X.index[inference_from] if expanding_window else X.index[index - window_size - 1]
|
||||
|
||||
last_model_index = (
|
||||
index - ((index - inference_from) % retrain_every) - model_index_offset
|
||||
)
|
||||
train_window_start = (
|
||||
X.index[inference_from]
|
||||
if expanding_window
|
||||
else X.index[index - window_size - 1]
|
||||
)
|
||||
|
||||
current_model = model_over_time[X.index[last_model_index]]
|
||||
current_transformations = [transformation_over_time[X.index[last_model_index]] for transformation_over_time in transformations_over_time]
|
||||
current_transformations = [
|
||||
transformation_over_time[X.index[last_model_index]]
|
||||
for transformation_over_time in transformations_over_time
|
||||
]
|
||||
|
||||
if current_model.predict_window_size == 'window_size':
|
||||
next_timestep = X.loc[train_window_start:X.index[index]]
|
||||
else:
|
||||
if current_model.predict_window_size == "window_size":
|
||||
next_timestep = X.loc[train_window_start : X.index[index]]
|
||||
else:
|
||||
# we need to get a Dataframe out of it, since the transformation step always expects a 2D array, but it's equivalent to X.iloc[index]
|
||||
next_timestep = X.loc[X.index[index]:X.index[index]]
|
||||
|
||||
next_timestep = X.loc[X.index[index] : X.index[index]]
|
||||
|
||||
for transformation in current_transformations:
|
||||
next_timestep = transformation.transform(next_timestep)
|
||||
|
||||
@@ -54,7 +84,9 @@ def walk_forward_inference(
|
||||
|
||||
predictions[X.index[index]] = prediction
|
||||
if inference_from == index and len(probabilities.columns) != len(probs):
|
||||
probabilities = probabilities.reindex(columns = ["prob_" + str(num) for num in range(0, len(probs.T))])
|
||||
probabilities = probabilities.reindex(
|
||||
columns=["prob_" + str(num) for num in range(0, len(probs.T))]
|
||||
)
|
||||
probabilities.loc[X.index[index]] = probs
|
||||
|
||||
return predictions, probabilities
|
||||
|
||||
@@ -1,36 +1,64 @@
|
||||
import pandas as pd
|
||||
from training.types import ModelOverTime, TransformationsOverTime, PredictionsSeries, ProbabilitiesDataFrame
|
||||
from training.types import (
|
||||
ModelOverTime,
|
||||
TransformationsOverTime,
|
||||
PredictionsSeries,
|
||||
ProbabilitiesDataFrame,
|
||||
)
|
||||
from utils.helpers import get_first_valid_return_index
|
||||
from tqdm import tqdm
|
||||
from typing import Optional
|
||||
from data_loader.types import XDataFrame
|
||||
from tqdm import tqdm
|
||||
|
||||
def walk_forward_inference_batched(
|
||||
model_name: str,
|
||||
model_over_time: ModelOverTime,
|
||||
transformations_over_time: TransformationsOverTime,
|
||||
X: XDataFrame,
|
||||
expanding_window: bool,
|
||||
window_size: int,
|
||||
retrain_every: int,
|
||||
from_index: Optional[pd.Timestamp],
|
||||
) -> tuple[PredictionsSeries, ProbabilitiesDataFrame]:
|
||||
predictions = pd.Series(index=X.index, dtype='object').rename(model_name)
|
||||
probabilities = pd.DataFrame(index=X.index, columns=['0', '1'])
|
||||
|
||||
inference_from = max(get_first_valid_return_index(model_over_time), get_first_valid_return_index(X.iloc[:,0])) if from_index is None else X.index.to_list().index(from_index)
|
||||
def walk_forward_inference_batched(
|
||||
model_name: str,
|
||||
model_over_time: ModelOverTime,
|
||||
transformations_over_time: TransformationsOverTime,
|
||||
X: XDataFrame,
|
||||
expanding_window: bool,
|
||||
window_size: int,
|
||||
retrain_every: int,
|
||||
from_index: Optional[pd.Timestamp],
|
||||
) -> tuple[PredictionsSeries, ProbabilitiesDataFrame]:
|
||||
predictions = pd.Series(index=X.index, dtype="object").rename(model_name)
|
||||
probabilities = pd.DataFrame(index=X.index, columns=["0", "1"])
|
||||
|
||||
inference_from = (
|
||||
max(
|
||||
get_first_valid_return_index(model_over_time),
|
||||
get_first_valid_return_index(X.iloc[:, 0]),
|
||||
)
|
||||
if from_index is None
|
||||
else X.index.to_list().index(from_index)
|
||||
)
|
||||
inference_till = X.shape[0]
|
||||
first_model = model_over_time[inference_from]
|
||||
|
||||
if first_model.only_column is not None:
|
||||
X = X[[column for column in X.columns if first_model.only_column in column]]
|
||||
|
||||
if first_model.data_transformation == 'original':
|
||||
|
||||
if first_model.data_transformation == "original":
|
||||
transformations_over_time = []
|
||||
|
||||
batch_indices = range(inference_from, inference_till, retrain_every) if inference_till - inference_from > retrain_every else [inference_from]
|
||||
batched_results = [__inference_from_window(index, index + retrain_every, X, model_over_time, transformations_over_time, expanding_window, window_size) for index in tqdm(batch_indices)]
|
||||
batch_indices = (
|
||||
range(inference_from, inference_till, retrain_every)
|
||||
if inference_till - inference_from > retrain_every
|
||||
else [inference_from]
|
||||
)
|
||||
batched_results = [
|
||||
__inference_from_window(
|
||||
index,
|
||||
index + retrain_every,
|
||||
X,
|
||||
model_over_time,
|
||||
transformations_over_time,
|
||||
expanding_window,
|
||||
window_size,
|
||||
)
|
||||
for index in tqdm(batch_indices)
|
||||
]
|
||||
for batch in batched_results:
|
||||
for index, prediction, probs in batch:
|
||||
predictions[X.index[index]] = prediction
|
||||
@@ -38,12 +66,24 @@ def walk_forward_inference_batched(
|
||||
|
||||
return predictions, probabilities
|
||||
|
||||
def __inference_from_window(index_start: int, index_end: int, X: XDataFrame, model_over_time: ModelOverTime, transformations_over_time: TransformationsOverTime, expanding_window: bool, window_size: int) -> list[tuple[int, float, pd.Series]]:
|
||||
|
||||
def __inference_from_window(
|
||||
index_start: int,
|
||||
index_end: int,
|
||||
X: XDataFrame,
|
||||
model_over_time: ModelOverTime,
|
||||
transformations_over_time: TransformationsOverTime,
|
||||
expanding_window: bool,
|
||||
window_size: int,
|
||||
) -> list[tuple[int, float, pd.Series]]:
|
||||
current_model = model_over_time[X.index[index_start]]
|
||||
current_transformations = [transformation_over_time[X.index[index_start]] for transformation_over_time in transformations_over_time]
|
||||
current_transformations = [
|
||||
transformation_over_time[X.index[index_start]]
|
||||
for transformation_over_time in transformations_over_time
|
||||
]
|
||||
|
||||
input_data = X.iloc[index_start:index_end]
|
||||
|
||||
|
||||
for transformation in current_transformations:
|
||||
input_data = transformation.transform(input_data)
|
||||
|
||||
@@ -51,6 +91,9 @@ def __inference_from_window(index_start: int, index_end: int, X: XDataFrame, mod
|
||||
|
||||
predictions = current_model.predict(input_data)
|
||||
probs = current_model.predict_proba(input_data)
|
||||
results = [(index_start + index, predictions[index], probs[index]) for index in range(len(predictions))]
|
||||
|
||||
return results
|
||||
results = [
|
||||
(index_start + index, predictions[index], probs[index])
|
||||
for index in range(len(predictions))
|
||||
]
|
||||
|
||||
return results
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
import pandas as pd
|
||||
from models.base import Model
|
||||
from training.types import ModelOverTime, TransformationsOverTime, PredictionsSeries, ProbabilitiesDataFrame
|
||||
from training.types import (
|
||||
ModelOverTime,
|
||||
TransformationsOverTime,
|
||||
PredictionsSeries,
|
||||
ProbabilitiesDataFrame,
|
||||
)
|
||||
from transformations.base import Transformation
|
||||
from utils.helpers import get_first_valid_return_index
|
||||
from tqdm import tqdm
|
||||
@@ -8,31 +13,54 @@ from typing import Optional
|
||||
from data_loader.types import XDataFrame
|
||||
import ray
|
||||
|
||||
|
||||
def walk_forward_inference(
|
||||
model_name: str,
|
||||
model_over_time: ModelOverTime,
|
||||
transformations_over_time: TransformationsOverTime,
|
||||
X: XDataFrame,
|
||||
expanding_window: bool,
|
||||
window_size: int,
|
||||
retrain_every: int,
|
||||
from_index: Optional[pd.Timestamp],
|
||||
) -> tuple[PredictionsSeries, ProbabilitiesDataFrame]:
|
||||
predictions = pd.Series(index=X.index, dtype='object').rename(model_name)
|
||||
model_name: str,
|
||||
model_over_time: ModelOverTime,
|
||||
transformations_over_time: TransformationsOverTime,
|
||||
X: XDataFrame,
|
||||
expanding_window: bool,
|
||||
window_size: int,
|
||||
retrain_every: int,
|
||||
from_index: Optional[pd.Timestamp],
|
||||
) -> tuple[PredictionsSeries, ProbabilitiesDataFrame]:
|
||||
predictions = pd.Series(index=X.index, dtype="object").rename(model_name)
|
||||
probabilities = pd.DataFrame(index=X.index)
|
||||
|
||||
inference_from = max(get_first_valid_return_index(model_over_time), get_first_valid_return_index(X.iloc[:,0])) if from_index is None else X.index.to_list().index(from_index)
|
||||
inference_from = (
|
||||
max(
|
||||
get_first_valid_return_index(model_over_time),
|
||||
get_first_valid_return_index(X.iloc[:, 0]),
|
||||
)
|
||||
if from_index is None
|
||||
else X.index.to_list().index(from_index)
|
||||
)
|
||||
inference_till = X.shape[0]
|
||||
first_model = model_over_time[inference_from]
|
||||
|
||||
if first_model.only_column is not None:
|
||||
X = X[[column for column in X.columns if first_model.only_column in column]]
|
||||
|
||||
if first_model.data_transformation == 'original':
|
||||
|
||||
if first_model.data_transformation == "original":
|
||||
transformations_over_time = []
|
||||
|
||||
batch_size = int((inference_till - inference_from) / 10)
|
||||
batched_results = ray.get([__inference_from_window.remote(index, index + batch_size, inference_from, retrain_every, X, model_over_time, transformations_over_time, expanding_window, window_size) for index in range(inference_from, inference_till)])
|
||||
batched_results = ray.get(
|
||||
[
|
||||
__inference_from_window.remote(
|
||||
index,
|
||||
index + batch_size,
|
||||
inference_from,
|
||||
retrain_every,
|
||||
X,
|
||||
model_over_time,
|
||||
transformations_over_time,
|
||||
expanding_window,
|
||||
window_size,
|
||||
)
|
||||
for index in range(inference_from, inference_till)
|
||||
]
|
||||
)
|
||||
for batch in batched_results:
|
||||
for index, prediction, probs in batch:
|
||||
predictions[X.index[index]] = prediction
|
||||
@@ -40,23 +68,41 @@ def walk_forward_inference(
|
||||
|
||||
return predictions, probabilities
|
||||
|
||||
|
||||
@ray.remote
|
||||
def __inference_from_window(index_start: int, index_end: int, inference_from: int, retrain_every: int, X: XDataFrame, model_over_time: ModelOverTime, transformations_over_time: TransformationsOverTime, expanding_window: bool, window_size: int) -> list[tuple[int, float, pd.Series]]:
|
||||
|
||||
def __inference_from_window(
|
||||
index_start: int,
|
||||
index_end: int,
|
||||
inference_from: int,
|
||||
retrain_every: int,
|
||||
X: XDataFrame,
|
||||
model_over_time: ModelOverTime,
|
||||
transformations_over_time: TransformationsOverTime,
|
||||
expanding_window: bool,
|
||||
window_size: int,
|
||||
) -> list[tuple[int, float, pd.Series]]:
|
||||
|
||||
results = []
|
||||
for index in range(index_start, index_end):
|
||||
last_model_index = index - ((index - inference_from) % retrain_every)
|
||||
train_window_start = X.index[inference_from] if expanding_window else X.index[index - window_size - 1]
|
||||
train_window_start = (
|
||||
X.index[inference_from]
|
||||
if expanding_window
|
||||
else X.index[index - window_size - 1]
|
||||
)
|
||||
|
||||
current_model = model_over_time[X.index[last_model_index]]
|
||||
current_transformations = [transformation_over_time[X.index[last_model_index]] for transformation_over_time in transformations_over_time]
|
||||
current_transformations = [
|
||||
transformation_over_time[X.index[last_model_index]]
|
||||
for transformation_over_time in transformations_over_time
|
||||
]
|
||||
|
||||
if current_model.predict_window_size == 'window_size':
|
||||
next_timestep = X.loc[train_window_start:X.index[index]]
|
||||
else:
|
||||
if current_model.predict_window_size == "window_size":
|
||||
next_timestep = X.loc[train_window_start : X.index[index]]
|
||||
else:
|
||||
# we need to get a Dataframe out of it, since the transformation step always expects a 2D array, but it's equivalent to X.iloc[index]
|
||||
next_timestep = X.loc[X.index[index]:X.index[index]]
|
||||
|
||||
next_timestep = X.loc[X.index[index] : X.index[index]]
|
||||
|
||||
for transformation in current_transformations:
|
||||
next_timestep = transformation.transform(next_timestep)
|
||||
|
||||
@@ -64,5 +110,5 @@ def __inference_from_window(index_start: int, index_end: int, inference_from: in
|
||||
|
||||
prediction, probs = current_model.predict(next_timestep)
|
||||
results.append((index, prediction, probs))
|
||||
|
||||
return results
|
||||
|
||||
return results
|
||||
|
||||
@@ -8,40 +8,63 @@ from data_loader.types import ForwardReturnSeries, XDataFrame, ySeries
|
||||
|
||||
|
||||
def walk_forward_process_transformations(
|
||||
X: XDataFrame,
|
||||
y: ySeries,
|
||||
forward_returns: ForwardReturnSeries,
|
||||
expanding_window: bool,
|
||||
window_size: int,
|
||||
retrain_every: int,
|
||||
from_index: Optional[pd.Timestamp],
|
||||
transformations: list[Transformation],
|
||||
) -> TransformationsOverTime:
|
||||
transformations_over_time = [pd.Series(index=y.index, dtype='object').rename(t.get_name()) for t in transformations]
|
||||
X: XDataFrame,
|
||||
y: ySeries,
|
||||
forward_returns: ForwardReturnSeries,
|
||||
expanding_window: bool,
|
||||
window_size: int,
|
||||
retrain_every: int,
|
||||
from_index: Optional[pd.Timestamp],
|
||||
transformations: list[Transformation],
|
||||
) -> TransformationsOverTime:
|
||||
transformations_over_time = [
|
||||
pd.Series(index=y.index, dtype="object").rename(t.get_name())
|
||||
for t in transformations
|
||||
]
|
||||
|
||||
first_nonzero_return = max(get_first_valid_return_index(forward_returns), get_first_valid_return_index(X.iloc[:,0]), get_first_valid_return_index(y))
|
||||
train_from = first_nonzero_return + window_size + 1 if from_index is None else X.index.to_list().index(from_index)
|
||||
first_nonzero_return = max(
|
||||
get_first_valid_return_index(forward_returns),
|
||||
get_first_valid_return_index(X.iloc[:, 0]),
|
||||
get_first_valid_return_index(y),
|
||||
)
|
||||
train_from = (
|
||||
first_nonzero_return + window_size + 1
|
||||
if from_index is None
|
||||
else X.index.to_list().index(from_index)
|
||||
)
|
||||
train_till = len(y)
|
||||
iterations_before_retrain = 0
|
||||
|
||||
for index in tqdm(range(train_from, train_till)):
|
||||
train_window_start = X.index[first_nonzero_return] if expanding_window else X.index[index - window_size - 1]
|
||||
|
||||
if iterations_before_retrain <= 0 or pd.isna(transformations_over_time[0][index-1]):
|
||||
for index in tqdm(range(train_from, train_till)):
|
||||
train_window_start = (
|
||||
X.index[first_nonzero_return]
|
||||
if expanding_window
|
||||
else X.index[index - window_size - 1]
|
||||
)
|
||||
|
||||
if iterations_before_retrain <= 0 or pd.isna(
|
||||
transformations_over_time[0][index - 1]
|
||||
):
|
||||
|
||||
train_window_end = X.index[index - 1]
|
||||
|
||||
|
||||
X_expanding_window = X[train_window_start:train_window_end]
|
||||
y_expanding_window = y[train_window_start:train_window_end]
|
||||
|
||||
current_transformations = [t.clone() for t in transformations]
|
||||
for transformation_index, transformation in enumerate(current_transformations):
|
||||
X_expanding_window = transformation.fit_transform(X_expanding_window, y_expanding_window)
|
||||
for transformation_index, transformation in enumerate(
|
||||
current_transformations
|
||||
):
|
||||
X_expanding_window = transformation.fit_transform(
|
||||
X_expanding_window, y_expanding_window
|
||||
)
|
||||
|
||||
iterations_before_retrain = retrain_every
|
||||
|
||||
for transformation_index, transformation in enumerate(current_transformations):
|
||||
transformations_over_time[transformation_index][X.index[index]] = transformation
|
||||
transformations_over_time[transformation_index][
|
||||
X.index[index]
|
||||
] = transformation
|
||||
|
||||
iterations_before_retrain -= 1
|
||||
|
||||
|
||||
@@ -8,33 +8,72 @@ from data_loader.types import ForwardReturnSeries, XDataFrame, ySeries
|
||||
import ray
|
||||
from utils.parallel import parallel_compute_with_bar
|
||||
|
||||
def walk_forward_process_transformations(
|
||||
X: XDataFrame,
|
||||
y: ySeries,
|
||||
forward_returns: ForwardReturnSeries,
|
||||
expanding_window: bool,
|
||||
window_size: int,
|
||||
retrain_every: int,
|
||||
from_index: Optional[pd.Timestamp],
|
||||
transformations: list[Transformation],
|
||||
) -> TransformationsOverTime:
|
||||
transformations_over_time = [pd.Series(index=y.index).rename(t.get_name()) for t in transformations]
|
||||
|
||||
first_nonzero_return = max(get_first_valid_return_index(forward_returns), get_first_valid_return_index(X.iloc[:,0]), get_first_valid_return_index(y))
|
||||
train_from = first_nonzero_return + window_size + 1 if from_index is None else X.index.to_list().index(from_index)
|
||||
def walk_forward_process_transformations(
|
||||
X: XDataFrame,
|
||||
y: ySeries,
|
||||
forward_returns: ForwardReturnSeries,
|
||||
expanding_window: bool,
|
||||
window_size: int,
|
||||
retrain_every: int,
|
||||
from_index: Optional[pd.Timestamp],
|
||||
transformations: list[Transformation],
|
||||
) -> TransformationsOverTime:
|
||||
transformations_over_time = [
|
||||
pd.Series(index=y.index).rename(t.get_name()) for t in transformations
|
||||
]
|
||||
|
||||
first_nonzero_return = max(
|
||||
get_first_valid_return_index(forward_returns),
|
||||
get_first_valid_return_index(X.iloc[:, 0]),
|
||||
get_first_valid_return_index(y),
|
||||
)
|
||||
train_from = (
|
||||
first_nonzero_return + window_size + 1
|
||||
if from_index is None
|
||||
else X.index.to_list().index(from_index)
|
||||
)
|
||||
train_till = len(y)
|
||||
|
||||
processed_transformations = parallel_compute_with_bar([preprocess_transformations_window.remote(X, y, expanding_window, window_size, transformations, first_nonzero_return, index) for index in range(train_from, train_till, retrain_every)])
|
||||
|
||||
|
||||
processed_transformations = parallel_compute_with_bar(
|
||||
[
|
||||
preprocess_transformations_window.remote(
|
||||
X,
|
||||
y,
|
||||
expanding_window,
|
||||
window_size,
|
||||
transformations,
|
||||
first_nonzero_return,
|
||||
index,
|
||||
)
|
||||
for index in range(train_from, train_till, retrain_every)
|
||||
]
|
||||
)
|
||||
|
||||
for transformation, index_time in processed_transformations:
|
||||
for transformation_index, transformation in enumerate(transformation):
|
||||
transformations_over_time[transformation_index][X.index[index_time]] = transformation
|
||||
transformations_over_time[transformation_index][
|
||||
X.index[index_time]
|
||||
] = transformation
|
||||
|
||||
return transformations_over_time
|
||||
|
||||
|
||||
@ray.remote
|
||||
def preprocess_transformations_window(X: XDataFrame, y: ySeries, expanding_window: bool, window_size: int, transformations: list[Transformation], first_nonzero_return: int, index: int) -> tuple[list[Transformation], int]:
|
||||
train_window_start = X.index[first_nonzero_return] if expanding_window else X.index[index - window_size - 1]
|
||||
def preprocess_transformations_window(
|
||||
X: XDataFrame,
|
||||
y: ySeries,
|
||||
expanding_window: bool,
|
||||
window_size: int,
|
||||
transformations: list[Transformation],
|
||||
first_nonzero_return: int,
|
||||
index: int,
|
||||
) -> tuple[list[Transformation], int]:
|
||||
train_window_start = (
|
||||
X.index[first_nonzero_return]
|
||||
if expanding_window
|
||||
else X.index[index - window_size - 1]
|
||||
)
|
||||
train_window_end = X.index[index - 1]
|
||||
|
||||
X_expanding_window = X[train_window_start:train_window_end]
|
||||
@@ -42,6 +81,8 @@ def preprocess_transformations_window(X: XDataFrame, y: ySeries, expanding_windo
|
||||
|
||||
current_transformations = [t.clone() for t in transformations]
|
||||
for transformation in current_transformations:
|
||||
X_expanding_window = transformation.fit_transform(X_expanding_window, y_expanding_window)
|
||||
X_expanding_window = transformation.fit_transform(
|
||||
X_expanding_window, y_expanding_window
|
||||
)
|
||||
|
||||
return (current_transformations, index)
|
||||
return (current_transformations, index)
|
||||
|
||||
@@ -7,39 +7,55 @@ from typing import Optional
|
||||
from data_loader.types import ForwardReturnSeries, XDataFrame, ySeries
|
||||
from copy import deepcopy
|
||||
|
||||
def walk_forward_train(
|
||||
model: Model,
|
||||
X: XDataFrame,
|
||||
y: ySeries,
|
||||
forward_returns: ForwardReturnSeries,
|
||||
expanding_window: bool,
|
||||
window_size: int,
|
||||
retrain_every: int,
|
||||
from_index: Optional[pd.Timestamp],
|
||||
transformations_over_time: TransformationsOverTime,
|
||||
) -> ModelOverTime:
|
||||
models_over_time = pd.Series(index=y.index, dtype='object').rename(model.name)
|
||||
|
||||
first_nonzero_return = max(get_first_valid_return_index(forward_returns), get_first_valid_return_index(X.iloc[:,0]), get_first_valid_return_index(y))
|
||||
train_from = first_nonzero_return + window_size + 1 if from_index is None else X.index.to_list().index(from_index)
|
||||
def walk_forward_train(
|
||||
model: Model,
|
||||
X: XDataFrame,
|
||||
y: ySeries,
|
||||
forward_returns: ForwardReturnSeries,
|
||||
expanding_window: bool,
|
||||
window_size: int,
|
||||
retrain_every: int,
|
||||
from_index: Optional[pd.Timestamp],
|
||||
transformations_over_time: TransformationsOverTime,
|
||||
) -> ModelOverTime:
|
||||
models_over_time = pd.Series(index=y.index, dtype="object").rename(model.name)
|
||||
|
||||
first_nonzero_return = max(
|
||||
get_first_valid_return_index(forward_returns),
|
||||
get_first_valid_return_index(X.iloc[:, 0]),
|
||||
get_first_valid_return_index(y),
|
||||
)
|
||||
train_from = (
|
||||
first_nonzero_return + window_size + 1
|
||||
if from_index is None
|
||||
else X.index.to_list().index(from_index)
|
||||
)
|
||||
train_till = len(y)
|
||||
|
||||
if model.only_column is not None:
|
||||
X = X[[column for column in X.columns if model.only_column in column]]
|
||||
|
||||
if model.data_transformation == 'original':
|
||||
|
||||
if model.data_transformation == "original":
|
||||
transformations_over_time = []
|
||||
|
||||
|
||||
for index in tqdm(range(train_from, train_till, retrain_every)):
|
||||
train_window_start = X.index[first_nonzero_return] if expanding_window else X.index[index - window_size - 1]
|
||||
train_window_start = (
|
||||
X.index[first_nonzero_return]
|
||||
if expanding_window
|
||||
else X.index[index - window_size - 1]
|
||||
)
|
||||
|
||||
train_window_end = X.index[index - 1]
|
||||
current_transformations = [transformation_over_time[index] for transformation_over_time in transformations_over_time]
|
||||
current_transformations = [
|
||||
transformation_over_time[index]
|
||||
for transformation_over_time in transformations_over_time
|
||||
]
|
||||
X_slice = X[train_window_start:train_window_end]
|
||||
|
||||
for transformation in current_transformations:
|
||||
X_slice = transformation.transform(X_slice)
|
||||
|
||||
|
||||
X_slice = X_slice.to_numpy()
|
||||
y_slice = y[train_window_start:train_window_end].to_numpy()
|
||||
|
||||
@@ -48,4 +64,4 @@ def walk_forward_train(
|
||||
|
||||
models_over_time[X.index[index]] = current_model
|
||||
|
||||
return models_over_time
|
||||
return models_over_time
|
||||
|
||||
@@ -9,46 +9,86 @@ import ray
|
||||
from utils.parallel import parallel_compute_with_bar
|
||||
from copy import deepcopy
|
||||
|
||||
|
||||
def walk_forward_train(
|
||||
model: Model,
|
||||
X: XDataFrame,
|
||||
y: ySeries,
|
||||
forward_returns: ForwardReturnSeries,
|
||||
expanding_window: bool,
|
||||
window_size: int,
|
||||
retrain_every: int,
|
||||
from_index: Optional[pd.Timestamp],
|
||||
transformations_over_time: TransformationsOverTime,
|
||||
) -> ModelOverTime:
|
||||
model: Model,
|
||||
X: XDataFrame,
|
||||
y: ySeries,
|
||||
forward_returns: ForwardReturnSeries,
|
||||
expanding_window: bool,
|
||||
window_size: int,
|
||||
retrain_every: int,
|
||||
from_index: Optional[pd.Timestamp],
|
||||
transformations_over_time: TransformationsOverTime,
|
||||
) -> ModelOverTime:
|
||||
models_over_time = pd.Series(index=y.index).rename(model.name)
|
||||
|
||||
first_nonzero_return = max(get_first_valid_return_index(forward_returns), get_first_valid_return_index(X.iloc[:,0]), get_first_valid_return_index(y))
|
||||
train_from = first_nonzero_return + window_size + 1 if from_index is None else X.index.to_list().index(from_index)
|
||||
first_nonzero_return = max(
|
||||
get_first_valid_return_index(forward_returns),
|
||||
get_first_valid_return_index(X.iloc[:, 0]),
|
||||
get_first_valid_return_index(y),
|
||||
)
|
||||
train_from = (
|
||||
first_nonzero_return + window_size + 1
|
||||
if from_index is None
|
||||
else X.index.to_list().index(from_index)
|
||||
)
|
||||
train_till = len(y)
|
||||
|
||||
if model.only_column is not None:
|
||||
X = X[[column for column in X.columns if model.only_column in column]]
|
||||
|
||||
if model.data_transformation == 'original':
|
||||
|
||||
if model.data_transformation == "original":
|
||||
transformations_over_time = []
|
||||
|
||||
models = parallel_compute_with_bar([train_on_window.remote(index, first_nonzero_return, window_size, X, y, model, expanding_window, transformations_over_time) for index in tqdm(range(train_from, train_till, retrain_every))])
|
||||
for index, current_model in models:
|
||||
|
||||
models = parallel_compute_with_bar(
|
||||
[
|
||||
train_on_window.remote(
|
||||
index,
|
||||
first_nonzero_return,
|
||||
window_size,
|
||||
X,
|
||||
y,
|
||||
model,
|
||||
expanding_window,
|
||||
transformations_over_time,
|
||||
)
|
||||
for index in tqdm(range(train_from, train_till, retrain_every))
|
||||
]
|
||||
)
|
||||
for index, current_model in models:
|
||||
models_over_time[X.index[index]] = current_model
|
||||
|
||||
return models_over_time
|
||||
|
||||
|
||||
@ray.remote
|
||||
def train_on_window(index: int, first_nonzero_return: int, window_size: int, X: XDataFrame, y: ySeries, model: Model, expanding_window: bool, transformations_over_time: TransformationsOverTime) -> tuple[int, Model]:
|
||||
train_window_start = X.index[first_nonzero_return] if expanding_window else X.index[index - window_size - 1]
|
||||
def train_on_window(
|
||||
index: int,
|
||||
first_nonzero_return: int,
|
||||
window_size: int,
|
||||
X: XDataFrame,
|
||||
y: ySeries,
|
||||
model: Model,
|
||||
expanding_window: bool,
|
||||
transformations_over_time: TransformationsOverTime,
|
||||
) -> tuple[int, Model]:
|
||||
train_window_start = (
|
||||
X.index[first_nonzero_return]
|
||||
if expanding_window
|
||||
else X.index[index - window_size - 1]
|
||||
)
|
||||
|
||||
train_window_end = X.index[index - 1]
|
||||
current_transformations = [transformation_over_time[index] for transformation_over_time in transformations_over_time]
|
||||
current_transformations = [
|
||||
transformation_over_time[index]
|
||||
for transformation_over_time in transformations_over_time
|
||||
]
|
||||
X_slice = X[train_window_start:train_window_end]
|
||||
|
||||
for transformation in current_transformations:
|
||||
X_slice = transformation.transform(X_slice)
|
||||
|
||||
|
||||
X_slice = X_slice.to_numpy()
|
||||
y_slice = y[train_window_start:train_window_end].to_numpy()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user