Coverage for examples/aether_quant_models.py: 100%
30 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 12:55 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 12:55 +0000
1"""Models from the library: the example catalog, then a model folder loaded and scored with the numpy forward pass on the bundled sample.
2No torch, no network: the model here is a hand-made one-layer net in the library's export format (20-session momentum minus 60-session
3volatility), written to a temporary folder. Any exported model folder loads the same way, an installed example or your own retrain's
4`ml/versions/<id>/`; `aq models install example-crypto-mlp` fetches a trained example from a GitHub release.
6 python examples/aether_quant_models.py
7"""
9from __future__ import annotations
11import json
12import sys
13import tempfile
14from pathlib import Path
16sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
18from aether_quant import models, samples
20# a linear trunk over the example features (ret_1, ret_5, ret_20, ret_60, vol_20, vol_60) and one rank_20d head
21WEIGHTS = [0.0, 0.0, 1.0, 0.0, -0.5, 0.0]
24def write_model(folder: Path, names: list[str]) -> Path:
25 export = {"trunk": [{"type": "linear", "weight_key": "w", "bias_key": "b"}], "heads": {"rank_20d": [{"type": "linear", "weight_key": "h", "bias_key": "hb"}]},
26 "state_dict": {"w": [WEIGHTS], "b": [0.0], "h": [[1.0]], "hb": [0.0]}}
27 folder.mkdir(parents=True, exist_ok=True)
28 (folder / "multitask_model.json").write_text(json.dumps({"export": export}), encoding="utf-8")
29 (folder / "multitask_feature_schema.json").write_text(json.dumps({"model_input_names": names}), encoding="utf-8")
30 (folder / "card.md").write_text("# momentum-minus-volatility\nA hand-made example, not a trained model and not a trading model.\n", encoding="utf-8")
31 return folder
34def main() -> None:
35 print("example models published with each release:")
36 for entry in models.catalog():
37 print(f" {entry['name']} ({entry['kind']}): {entry['summary']}")
38 closes = samples.load_sample_prices()
39 names = [column for column in models.example_features(closes).columns if column not in ("date", "ticker")]
40 with tempfile.TemporaryDirectory() as tmp:
41 model = models.load(write_model(Path(tmp) / "momentum-minus-volatility", names))
42 scores = models.score(model, closes)
43 last = scores[scores["date"] == scores["date"].max()].sort_values("score", ascending=False)
44 print(f"\nloaded {model['name']} ({model['kind']}): {model['card'].splitlines()[1]}")
45 print(f"scored {len(scores)} rows, {scores['ticker'].nunique()} coins, {scores['date'].min()} to {scores['date'].max()}")
46 print(f"ranking on {last['date'].iloc[0]}: " + ", ".join(f"{row.ticker} {row.score:+.3f}" for row in last.itertuples()))
49if __name__ == "__main__":
50 main()