| """Benchmark metadata and ``info()`` display.""" |
|
|
| from __future__ import annotations |
|
|
| import json |
| from typing import Any |
|
|
| from ._compat import config |
|
|
| BENCHMARK_NAME = "MacroLens" |
| BENCHMARK_VERSION = "1.0" |
|
|
| _TASK_DESCRIPTIONS: dict[str, dict[str, str]] = { |
| "TSF": { |
| "name": "Time-Series Forecasting", |
| "target": "Close price", |
| "metrics": "MSE, RMSE, MAE, DA", |
| "horizons": "5, 21, 63 (daily) | 4, 13, 26 (weekly)", |
| }, |
| "A": { |
| "name": "Equity Valuation (Val-PT)", |
| "target": "Market capitalization", |
| "metrics": "MAPE, MedAPE, Spearman rho", |
| }, |
| "B": { |
| "name": "Statement Generation (Stmt-Gen)", |
| "target": "XBRL financial fields", |
| "metrics": "Per-field MAPE, Balance-equation accuracy", |
| }, |
| "C": { |
| "name": "Scenario-Conditioned Return (Scen-Ret)", |
| "target": "Post-event return (%)", |
| "metrics": "MAE (%), DA", |
| }, |
| "D": { |
| "name": "Private Company Valuation (Priv-Val)", |
| "target": "Market capitalization (from financials + sector only)", |
| "metrics": "MAPE, MedAPE, Spearman rho", |
| "note": "PE simulation: no price-derived inputs", |
| }, |
| "E": { |
| "name": "Generator Evaluation (Gen-Eval)", |
| "target": "Financial statement fields (Generator output vs XBRL actual)", |
| "metrics": "Per-field MAPE, Balance-equation accuracy", |
| }, |
| "F": { |
| "name": "Real Estate Valuation (RE-Val)", |
| "target": "Property rent and price", |
| "metrics": "Rent MAPE, Price MAPE", |
| }, |
| } |
|
|
|
|
| def _load_metadata(granularity: str = "daily") -> dict[str, Any]: |
| """Load benchmark metadata from disk.""" |
| bench_dir = config.get_benchmark_dir(granularity) |
| meta: dict[str, Any] = {"granularity": granularity} |
|
|
| for name in ("metadata.json", "task_definition.json", "valuation_tasks.json"): |
| p = bench_dir / name |
| if p.exists(): |
| meta[name.replace(".json", "")] = json.loads(p.read_text()) |
|
|
| |
| for split in ("train", "test"): |
| p = bench_dir / f"panel_{split}.parquet" |
| if p.exists(): |
| try: |
| import pandas as pd |
|
|
| meta[f"panel_{split}_rows"] = len(pd.read_parquet(p, columns=["date"])) |
| except Exception: |
| pass |
|
|
| |
| sc_path = bench_dir / "scenario_forecast_ground_truth.parquet" |
| if sc_path.exists(): |
| try: |
| import pandas as pd |
|
|
| sc = pd.read_parquet(sc_path, columns=["scenario_id"]) |
| meta["n_scenarios"] = int(sc["scenario_id"].nunique()) |
| meta["n_scenario_rows"] = len(sc) |
| except Exception: |
| pass |
|
|
| return meta |
|
|
|
|
| def info(granularity: str = "daily") -> dict[str, Any]: |
| """Print and return a summary of the MacroLens benchmark. |
| |
| Parameters |
| ---------- |
| granularity : str |
| ``"daily"`` (default), ``"weekly"``, or ``"monthly"``. |
| |
| Returns |
| ------- |
| dict |
| Benchmark metadata. |
| |
| Example |
| ------- |
| >>> import macrolens |
| >>> macrolens.info() |
| MacroLens v1.0 — Multi-Task Financial Forecasting Benchmark |
| ... |
| """ |
| meta = _load_metadata(granularity) |
| horizons = config.get_horizons(granularity) |
| lookbacks = config.get_lookback_windows(granularity) |
|
|
| |
| print(f"\n{'='*60}") |
| print(f" {BENCHMARK_NAME} v{BENCHMARK_VERSION}") |
| print(f" Multi-Task Financial Forecasting Benchmark") |
| print(f"{'='*60}") |
| print(f" Granularity : {granularity}") |
| print(f" Horizons : {horizons}") |
| print(f" Lookbacks : {lookbacks}") |
|
|
| train_rows = meta.get("panel_train_rows") |
| test_rows = meta.get("panel_test_rows") |
| if train_rows or test_rows: |
| print(f" Panel : {train_rows:,} train / {test_rows:,} test rows") |
|
|
| n_sc = meta.get("n_scenarios") |
| if n_sc: |
| print(f" Scenarios : {n_sc} unique events, {meta.get('n_scenario_rows', 0):,} GT rows") |
|
|
| print(f"\n Tasks:") |
| for task_id, desc in _TASK_DESCRIPTIONS.items(): |
| print(f" {task_id:>3} {desc['name']}") |
| print(f" Target : {desc['target']}") |
| print(f" Metrics: {desc['metrics']}") |
|
|
| print(f"{'='*60}\n") |
|
|
| return { |
| "name": BENCHMARK_NAME, |
| "version": BENCHMARK_VERSION, |
| "granularity": granularity, |
| "horizons": horizons, |
| "lookbacks": lookbacks, |
| "tasks": _TASK_DESCRIPTIONS, |
| **meta, |
| } |
|
|