StackingEnsembleΒΆ

Defined in fynance.models.ensemble

class StackingEnsemble(direction_factory, magnitude_factory, meta_factory)[source]

Bases: object

Direction + magnitude stacking with an out-of-fold meta-model.

The two base models are evaluated with walk-forward cross-validation; their out-of-fold (OOF) predictions become the meta-features on which the meta-model is trained (e.g. with fynance.models.loss.SharpeLoss). Using OOF predictions avoids feeding the meta-model with in-sample base predictions, the classic stacking leakage.

Parameters:
direction_factory, magnitude_factorycallable

No-arg callables returning base models (the BaseNeuralNet interface). Typically the direction model is trained with DirectionalAccuracyLoss, the magnitude model with MSE or SortinoLoss.

meta_factorycallable

Callable meta_factory(n_features) -> model returning the meta-model sized for the stacked features.

fit_predict(X, y, train_period, test_period, roll_period, epochs=1)[source]

Fit the ensemble and return meta out-of-fold predictions.

Parameters:
X, ytorch.Tensor

Input and target, shapes (T, N) and (T, M).

train_period, test_period, roll_periodint

Walk-forward window parameters.

epochsint, optional

Training passes per fold and for the meta-model. Default 1.

Returns:
np.ndarray

Meta predictions of shape (T, M); rows before the first test fold (no OOF base features) are NaN.