File size: 4,498 Bytes
ff4becd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"""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())

    # Panel sizes
    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

    # Scenario count
    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)

    # ── Pretty-print ──
    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,
    }