Coverage for notebooks/src/05_model_lab.py: 100%

28 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-22 12:55 +0000

1# %% [markdown] 

2# # The model lab: build, train, judge and export a model 

3# 

4# [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/leon1706-lol/Aether-quant/blob/main/notebooks/05_model_lab.ipynb) 

5# 

6# `aether_quant.lab` is the library behind `aq lab`: a lab folder, a dataset in the production layout, model templates (linear, and with torch the project's own networks), training runs that each count as a trial, and the project's statistics to judge them. This notebook stays offline: the bundled sample and the `linear` template, which needs no torch. The [model lab guide](../guide/model-lab.md) shows the same steps as commands. 

7 

8# %% 

9import sys 

10from pathlib import Path 

11 

12if "google.colab" in sys.modules: # Colab: install the library (numpy and pandas come with it) 

13 get_ipython().run_line_magic("pip", 'install -q "aether-quant[notebooks]"') # noqa: F821 

14else: # a clone: run against the checkout's code 

15 for parent in (Path.cwd(), *Path.cwd().parents): 

16 if (parent / "aether_quant" / "__init__.py").is_file(): 

17 sys.path.insert(0, str(parent)) 

18 break 

19 

20# %% [markdown] 

21# ## A lab and its dataset 

22# 

23# `init_lab` makes the folder: `lab.toml`, `models/`, `runs/` and `data/`. `build_dataset` turns a close table into features, forward rank targets and split labels, in the columns of the project's own training dataset. Rows whose 20-session label reaches into the next split are labelled `purged`: never fitted or scored. 

24 

25# %% 

26import tempfile 

27 

28from aether_quant import lab, samples 

29 

30folder = Path(tempfile.mkdtemp()) / "mylab" 

31print(lab.init_lab(folder)) 

32closes = samples.load_sample_prices() 

33dataset = lab.build_dataset(closes, features="core", targets=(5, 20)) 

34dataset.to_csv(folder / "data" / "dataset.csv", index=False) 

35print(dataset.groupby("split").size()) 

36 

37# %% [markdown] 

38# ## A model and a training run 

39# 

40# `new_model` copies a template into `models/<name>/`. Its `config.toml` names the features, the target and the training settings, and `overrides` change one run. Each seed of the run is a trial in the lab's `trials.jsonl`: the count N that every report judges against. 

41 

42# %% 

43print(lab.new_model(folder, "ridge", "linear")) 

44record = lab.train_run(folder, "ridge", seeds=[1, 2], log=lambda line: None) 

45print(record["id"], "out-of-sample IC", round(record["metrics"]["oof_ic_mean"], 4), "N", record["trials_n"]) 

46 

47# %% [markdown] 

48# ## Judging it 

49# 

50# `evaluate_run` reads only the run's out-of-sample scores: the IC by split and year, its decay, the quintile spreads, the gap to the fitted IC, and with `book=True` a cost-aware quintile book with its deflated Sharpe at the lab's N. 

51 

52# %% 

53run = lab.find_run(folder, "latest") 

54report = lab.evaluate_run(run, lab_dir=folder, book=True) 

55print(lab.render_eval(report)) 

56 

57# %% [markdown] 

58# ## Against a baseline, and what it learned 

59# 

60# `compare_runs` sets runs and a plain baseline signal on the rows they all scored, each tested against the best with a HAC t-statistic. `explain_run` is out-of-sample permutation importance: how much the IC drops when one feature is shuffled. 

61 

62# %% 

63print(lab.render_compare(lab.compare_runs([run], baseline="momentum_20"))) 

64print(lab.render_mapping("permutation importance", lab.explain_run(run)["importance"])) 

65 

66# %% [markdown] 

67# ## Export 

68# 

69# `export_run` writes the run in the `aq models` format: the weights as JSON, the features it reads, a model card and a sha256 manifest. `aq models install NAME --from ZIP` installs it, and scoring needs no torch. 

70 

71# %% 

72import zipfile 

73 

74card = lab.model_card(run, report) 

75result = lab.export_run(run, folder / "ridge.zip", card=card) 

76print(result["zip"], sorted(zipfile.ZipFile(result["zip"]).namelist()))