File size: 6,899 Bytes
029e02e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
"""Pydantic v2 result schemas for MacroLens task runners.

These models replace the legacy ``TypedDict`` shapes used pre-Phase-4. The
orchestrator (:mod:`experiments.run_all`) persists results as
:class:`macrolens.RunRecord` (full reproducibility envelope); these
per-task models capture the *metric content* of a single record's
``metrics`` field and are used by post-hoc tools (``gen_tables.py``,
``analysis.py``) that need a typed handle on the per-task metric set.

Every model:

* Uses ``model_config = ConfigDict(extra="forbid", frozen=True)`` so unknown
  keys raise at construction and instances are hashable.
* Allows every metric to be ``None`` — runners that legitimately skip a
  metric (e.g., a deterministic naive method that does not report CRPS)
  emit ``None``, not a sentinel string.
* Adds T5/T6/T7 (the legacy schema was missing T5/T6/T7).
"""

from __future__ import annotations

import pydantic


# ── Shared sub-schemas ────────────────────────────────────────────────────


class BootstrapCI(pydantic.BaseModel):
    """Bootstrap 95% CI for a scalar metric (matches ``MetricValue``)."""

    model_config = pydantic.ConfigDict(extra="forbid", frozen=True)

    mean: float | None = None
    ci_lo: float | None = None
    ci_hi: float | None = None
    std: float | None = None


class MultiSeedStats(pydantic.BaseModel):
    """Mean +/- std over the headline T1 multi-seed subset."""

    model_config = pydantic.ConfigDict(extra="forbid", frozen=True)

    seed_mean: float | None = None
    seed_std: float | None = None
    per_seed: dict[int, float] | None = None


# ── Per-task metric schemas ───────────────────────────────────────────────


class T1Metrics(pydantic.BaseModel):
    """T1 — Contextual Time-Series Forecasting."""

    model_config = pydantic.ConfigDict(extra="forbid", frozen=True)

    method_id: str
    task: str = "T1"
    horizon: int | None = None
    granularity: str = "daily"
    seed: int = 42
    mse: float | None = None
    mae: float | None = None
    rmse: float | None = None
    directional_accuracy: float | None = None
    mse_ci: BootstrapCI | None = None
    mae_ci: BootstrapCI | None = None
    da_ci: BootstrapCI | None = None
    multiseed: MultiSeedStats | None = None
    n_instances: int | None = None
    inference_time_sec: float | None = None
    train_time_sec: float | None = None


class T2Metrics(pydantic.BaseModel):
    """T2 — Point-in-Time Equity Valuation."""

    model_config = pydantic.ConfigDict(extra="forbid", frozen=True)

    method_id: str
    task: str = "T2"
    granularity: str = "daily"
    seed: int = 42
    mape: float | None = None
    median_ape: float | None = None
    rank_correlation: float | None = None
    rank_p_value: float | None = None
    mape_ci: BootstrapCI | None = None
    n_predictions: int | None = None
    n_tickers: int | None = None
    inference_time_sec: float | None = None


class T3Metrics(pydantic.BaseModel):
    """T3 — Statement Generation (per-field MAPE + balance equation)."""

    model_config = pydantic.ConfigDict(extra="forbid", frozen=True)

    method_id: str
    task: str = "T3"
    granularity: str = "daily"
    seed: int = 42
    overall_mape: float | None = None
    per_field_mape: dict[str, float] | None = None
    balance_equation_accuracy: float | None = None
    balance_equation_checked: int | None = None
    success_rate: float | None = None
    n_fields_matched: int | None = None
    n_field_misses: int | None = None
    n_tickers: int | None = None
    inference_time_sec: float | None = None


class T4Metrics(pydantic.BaseModel):
    """T4 — Scenario-Conditioned Return Forecasting."""

    model_config = pydantic.ConfigDict(extra="forbid", frozen=True)

    method_id: str
    task: str = "T4"
    granularity: str = "daily"
    seed: int = 42
    return_mae_pct: float | None = None
    directional_accuracy: float | None = None
    ci_calibration_95: float | None = None
    return_mae_ci: BootstrapCI | None = None
    n_predictions: int | None = None
    n_scenarios: int | None = None
    inference_time_sec: float | None = None


class T5Metrics(pydantic.BaseModel):
    """T5 — Private-Company Valuation (no market prices)."""

    model_config = pydantic.ConfigDict(extra="forbid", frozen=True)

    method_id: str
    task: str = "T5"
    granularity: str = "daily"
    seed: int = 42
    mape: float | None = None
    median_ape: float | None = None
    rank_correlation: float | None = None
    rank_p_value: float | None = None
    mape_ci: BootstrapCI | None = None
    n_predictions: int | None = None
    n_tickers: int | None = None
    gap_vs_t2: float | None = None
    inference_time_sec: float | None = None


class T6Metrics(pydantic.BaseModel):
    """T6 — Generator Evaluation (NL description -> XBRL)."""

    model_config = pydantic.ConfigDict(extra="forbid", frozen=True)

    method_id: str
    task: str = "T6"
    granularity: str = "daily"
    seed: int = 42
    overall_mape: float | None = None
    per_field_mape: dict[str, float] | None = None
    success_rate: float | None = None
    n_fields_matched: int | None = None
    n_field_misses: int | None = None
    n_tickers: int | None = None
    inference_time_sec: float | None = None


class T7Metrics(pydantic.BaseModel):
    """T7 — Real-Estate Valuation."""

    model_config = pydantic.ConfigDict(extra="forbid", frozen=True)

    method_id: str
    task: str = "T7"
    granularity: str = "daily"
    seed: int = 42
    rent_MAPE: float | None = None
    price_MAPE: float | None = None
    rent_median_APE: float | None = None
    price_median_APE: float | None = None
    rent_n_valid: int | None = None
    price_n_valid: int | None = None
    n_predictions: int | None = None
    inference_time_sec: float | None = None


# ── Family-level container ────────────────────────────────────────────────


class FamilyResults(pydantic.BaseModel):
    """Container emitted by each family's ``run_all_*()`` function."""

    model_config = pydantic.ConfigDict(extra="forbid", frozen=True)

    family: str
    panel_version: str | None = None
    methods: dict[str, list[dict]] = pydantic.Field(default_factory=dict)


# ── Task dispatch map ─────────────────────────────────────────────────────


TASK_RESULT_TYPES: dict[str, type[pydantic.BaseModel]] = {
    "T1": T1Metrics,
    "T2": T2Metrics,
    "T3": T3Metrics,
    "T4": T4Metrics,
    "T5": T5Metrics,
    "T6": T6Metrics,
    "T7": T7Metrics,
}