quantlab.model.predefined.seed_ensemble.SeedEnsemble#
- class quantlab.model.predefined.seed_ensemble.SeedEnsemble(model, seeds)[source]#
Bases:
BaseEnsembleAn ensemble of one model trained under several random seeds.
Member k is
type(model)built onmodel.configwithrandom_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_windowrequests features once, through the first member (its warm-up included), and hands them to every member’spredict_panel.train(),load()andcheck_checkpoint()work on an ensemble unit (seeBaseEnsemble) whoserun.jsonrecords each member’s seed;train_cv()writes one such unit per walk-forward fold, inside a walk-forward unit that a backtester’srun_cvreplays.- 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_configrecords.- 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
modelover 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’sconfig.json.- run_dirstr or os.PathLike, optional
The run directory the config was read from.
- Returns:
- Self
An untrained ensemble; call
loadto restore a trained unit.
Examples
>>> rebuilt = SeedEnsemble.from_config(ensemble.get_config()) >>> rebuilt.seeds, type(rebuilt.members[0]).__name__ ((0, 1, 2), 'SeededHead')