quantlab.model.predefined.model_ensemble.ModelEnsemble#

class quantlab.model.predefined.model_ensemble.ModelEnsemble(members)[source]#

Bases: BaseEnsemble

An ensemble of models given one by one, of any classes and over any factors.

Every hook keeps BaseEnsemble’s default: each member collects and predicts on its own, and the predictions are combined label by label (averaged when shared, passed through otherwise). Subclass it and override _combine for another combination rule. train() writes an ensemble unit whose run.json records a null seed per member; train_cv() writes one such unit per walk-forward fold.

Parameters:
memberssequence of BaseModel

At least two untrained or trained models. A label two members predict must have the same config in both, and their test windows must overlap.

Attributes:
memberslist[BaseModel]

The models given, in order.

Raises:
ValueError

If fewer than two members are given, two members predict a label of one name with different configs, or the test windows do not overlap.

Examples

Given two models xgb and gats over different factors and the same forward-return label:

>>> ensemble = ModelEnsemble([xgb, gats])
>>> checkpoint = ensemble.collect().train()
>>> out = ensemble.predict_window("2024-02-12", "2024-03-11")
>>> list(out.data_vars)
['fwd_ret_1']
property config#

The member models; get_config() serialises it.

Examples

>>> config = ModelEnsemble([xgb, gats]).get_config()
>>> config["name"], [m["name"] for m in config["members"]]
('quantlab.model.predefined.model_ensemble.ModelEnsemble', ['quantlab.model.predefined.xgb.XGBoostRegressor', 'quantlab.model.predefined.gats.GATsRegressor'])
config_cls#

The config dataclass the ensemble is serialised and rebuilt with.

alias of ModelEnsembleConfig

classmethod from_config(config, run_dir=None)[source]#

Rebuild a model ensemble from the dict get_config() returned.

Each member is rebuilt by the component rule.

Parameters:
configdict

The dict get_config() returned, for example read back from a backtest run’s config.json.

run_dirstr or os.PathLike, optional

The run directory the config was read from.

Returns:
Self

An untrained ensemble; call load to restore a trained unit.

Examples

>>> rebuilt = ModelEnsemble.from_config(ensemble.get_config())
>>> [type(m).__name__ for m in rebuilt.members]
['XGBoostRegressor', 'GATsRegressor']