MacroLens / code /dataloader /budgets.py
itouchz's picture
Add code/macrolens/ + code/dataloader/ packages; fix README path prefixes
f02626b verified
Raw
History Blame Contribute Delete
4.07 kB
"""Canonical sample budgets per task.
Single source of truth. Every family runner reads from here so that
cross-method comparison on each task is fair (same N for every method,
same instances, same indices).
Normalization principle:
- Forecasting / regression-with-subsample tasks (T1, T4, T7):
N_eval = 1,000 / N_train = 10,000 (stratified subsample from larger pools)
- Ticker-holdout valuation tasks (T2, T5):
N_eval = 1,324 / N_train = 2,673 (full 30% holdout, no subsampling)
- Filing-level generation tasks (T3, T6):
N_eval = 1,058 (some holdout tickers lack complete XBRL); train varies
Sample sizes are intentionally conservative -- ~10x the median peer-benchmark
scale (CiK 125 / WIT 446 / EDINET 350 / SciTS 1,250) so reviewers cannot
claim small-sample noise, while keeping LLM eval (4 LLMs x 7 tasks x ~1K
samples = ~28K calls) tractable on 4xA100 within the wall-clock budget.
The values here are DEFAULTS; runners may override via the
`get_canonical_indices(task, split, n_eval=..., n_train=...)` keyword
arguments to regenerate (and re-cache) for a re-tune without rebuilding
any artifacts.
"""
from __future__ import annotations
from typing import Literal
Task = Literal["T1", "T2", "T3", "T4", "T5", "T6", "T7"]
# ── Canonical budgets ─────────────────────────────────────────────────────
EVAL_N_PER_TASK: dict[Task, int] = {
"T1": 1_000, # subsampled (full ~1.3M)
"T2": 1_324, # full 30% ticker holdout
"T3": 1_058, # filing-level holdout (subset of 1,324 with full XBRL)
"T4": 1_000, # subsampled (full ~3M scenario-ticker pairs)
"T5": 1_324, # full 30% ticker holdout
"T6": 1_058, # filing-level holdout
"T7": 1_000, # subsampled (full ~23K properties)
}
TRAIN_N_PER_TASK: dict[Task, int] = {
"T1": 10_000, # subsampled training windows, sector x mcap_q
"T2": 2_673, # latest snapshot per non-holdout ticker
"T3": 9_458, # prior fiscal years across non-holdout tickers
"T4": 10_000, # subsampled scenario-conditioned windows
"T5": 2_673, # latest snapshot per non-holdout ticker
"T6": 1_377, # prior fiscal years for filing-level holdout
"T7": 10_000, # subsampled training properties, property_type x state
}
# ── Seed + stratifier ─────────────────────────────────────────────────────
SEED: int = 42
# Bumped if the stratifier logic changes (forces cache invalidation
# without changing N values). Increment when:
# - the panel column used for stratification changes
# - the per-task stratifier columns change
# - the sampler's tie-breaking / fallback logic changes
STRATIFIER_VERSION: int = 1
# ── Cache key derivation ──────────────────────────────────────────────────
def cache_key(
*,
n_eval: dict[Task, int] | None = None,
n_train: dict[Task, int] | None = None,
seed: int | None = None,
stratifier_version: int | None = None,
) -> str:
"""Stable cache-directory name for the (budgets, seed, stratifier) tuple.
Defaults to the module-level canonical values. Override any subset to
generate a non-canonical cache (e.g. a re-tune at N_eval=2000 produces
its own cache dir leaving the canonical cache intact).
"""
ne = n_eval or EVAL_N_PER_TASK
nt = n_train or TRAIN_N_PER_TASK
s = SEED if seed is None else seed
sv = STRATIFIER_VERSION if stratifier_version is None else stratifier_version
# Compact, readable encoding -- avoids sha hashes so the directory
# contents are inspectable.
eval_str = "-".join(f"{t}={ne[t]}" for t in ("T1", "T2", "T3", "T4", "T5", "T6", "T7"))
train_str = "-".join(f"{t}={nt[t]}" for t in ("T1", "T2", "T3", "T4", "T5", "T6", "T7"))
return f"seed={s}_strat=v{sv}_eval[{eval_str}]_train[{train_str}]"