quantlab.model.predefined.seed_ensemble.SeedEnsemble#

class quantlab.model.predefined.seed_ensemble.SeedEnsemble(model, seeds)[source]#

Bases: BaseEnsemble

An ensemble of one model trained under several random seeds.

Member k is type(model) built on model.config with random_seed=seeds[k]. collect() collects once, on the first member, and the other members share its data backend, so the panel is held once. predict_window requests features once, through the first member (its warm-up included), and hands them to every member’s predict_panel. train(), load() and check_checkpoint() work on an ensemble unit (see BaseEnsemble) whose run.json records each member’s seed; train_cv() writes one such unit per walk-forward fold, inside a walk-forward unit that a backtester’s run_cv replays.

Parameters:
modelBaseModel

The model to replicate. It is not trained itself; its config is the template of every member.

seedssequence of int

At least two distinct seeds, one per member, in member order.

Attributes:
modelBaseModel

The model given, whose config get_config records.

seedstuple[int, …]

The members’ seeds, in member order.

memberslist[BaseModel]

One model per seed.

Raises:
TypeError

If a seed is not an integer.

ValueError

If fewer than two seeds are given or a seed repeats.

Examples

Given a model model over one factor and one forward-return label:

>>> ensemble = SeedEnsemble(model, [0, 1, 2])
>>> [member.config.random_seed for member in ensemble.members]
[0, 1, 2]
>>> checkpoint = ensemble.collect().train()
>>> out = ensemble.predict_window("2024-02-12", "2024-03-11")
>>> list(out.data_vars), out.sizes["timestamp"]
(['fwd_ret_1'], 21)
property config#

The wrapped model and the seeds; get_config() serialises it.

Examples

>>> config = SeedEnsemble(model, [0, 1]).get_config()
>>> config["name"], config["seeds"], config["model"]["name"]
('quantlab.model.predefined.seed_ensemble.SeedEnsemble', [0, 1], 'tests.backtest_fixtures.SeededHead')
config_cls#

The config dataclass the ensemble is serialised and rebuilt with.

alias of SeedEnsembleConfig

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

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

The wrapped model is rebuilt by the component rule, and the seeds are applied to it.

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 = SeedEnsemble.from_config(ensemble.get_config())
>>> rebuilt.seeds, type(rebuilt.members[0]).__name__
((0, 1, 2), 'SeededHead')