Download code/harness.py from Tonic/protostar-climate-2026: direct link, hf CLI and curl.
- Browser
- Download file 4.06 kB
-
https://huggingface.co/datasets/Tonic/protostar-climate-2026/resolve/main/code/harness.py
- Command line
-
hf download hf://datasets/Tonic/protostar-climate-2026/code/harness.py
-
curl -L -o harness.py https://huggingface.co/datasets/Tonic/protostar-climate-2026/resolve/main/code/harness.py
4.06 kB
| """Rolling-origin harness v2: origins 1990-2011, tune/check eras, lead bands. | |
| Detail rows are per (candidate, metric, origin, target_year): sum_abs_err, n, | |
| sum_abs_err_ref (30-yr mean at same origin). Skill = 1 - MAE/MAE_ref pooled. | |
| """ | |
| from __future__ import annotations | |
| import numpy as np | |
| import pandas as pd | |
| from candidates import baseline_mean30 | |
| from panel import Panel | |
| ORIGINS = list(range(1990, 2012)) | |
| TUNE_ORIGINS = [o for o in ORIGINS if o <= 2000] | |
| CHECK_ORIGINS = [o for o in ORIGINS if o > 2000] | |
| def evaluate(panel: Panel, metric: str, candidates: dict, origins=ORIGINS) -> pd.DataFrame: | |
| rows = [] | |
| for origin in origins: | |
| truth, mask = panel.truth(metric, origin) | |
| ref = baseline_mean30(panel, metric, origin) | |
| forecasts = {name: fn(panel, origin) for name, fn in candidates.items()} | |
| for h in range(7): | |
| m = mask[:, h] | |
| if not m.any(): | |
| continue | |
| t = truth[m, h] | |
| base = {"metric": metric, "origin": origin, | |
| "target_year": origin + h + 1, "lead": h + 1, | |
| "era": "tune" if origin in TUNE_ORIGINS else "check", | |
| "n": int(m.sum()), | |
| "sum_err_ref": float(np.abs(ref[m, h] - t).sum())} | |
| for name, fc in forecasts.items(): | |
| if not np.isfinite(fc[m, h]).all(): | |
| raise ValueError(f"{name} produced non-finite forecasts " | |
| f"for scored rows at origin {origin}, lead {h + 1}") | |
| rows.append({**base, "candidate": name, | |
| "sum_err": float(np.abs(fc[m, h] - t).sum())}) | |
| return pd.DataFrame(rows) | |
| def summarise(detail: pd.DataFrame, by=("candidate",)) -> pd.DataFrame: | |
| g = detail.groupby(list(by) + ["metric"] if "metric" in detail else list(by)) | |
| out = [] | |
| for keys, sub in detail.groupby(list(by)): | |
| keys = (keys,) if not isinstance(keys, tuple) else keys | |
| mae = sub["sum_err"].sum() / sub["n"].sum() | |
| mae_ref = sub["sum_err_ref"].sum() / sub["n"].sum() | |
| row = dict(zip(by, keys)) | |
| row.update({"mae": mae, "mae_ref": mae_ref, | |
| "skill": 1 - mae / mae_ref, "n": int(sub["n"].sum())}) | |
| out.append(row) | |
| return pd.DataFrame(out) | |
| def skill_table(detail: pd.DataFrame) -> pd.DataFrame: | |
| """Candidate x era skill (per metric detail retained in detail frame).""" | |
| rows = [] | |
| for (cand, metric, era), sub in detail.groupby(["candidate", "metric", "era"]): | |
| mae = sub["sum_err"].sum() / sub["n"].sum() | |
| mae_ref = sub["sum_err_ref"].sum() / sub["n"].sum() | |
| rows.append({"candidate": cand, "metric": metric, "era": era, | |
| "skill": 1 - mae / mae_ref, "mae": mae, "n": int(sub["n"].sum())}) | |
| return pd.DataFrame(rows) | |
| def bootstrap_skill_diff(detail: pd.DataFrame, cand_a: str, cand_b: str, | |
| metric: str, era: str | None = None, | |
| n_boot: int = 500, seed: int = 0) -> dict: | |
| """Block-bootstrap by target_year: skill(a)-skill(b) distribution.""" | |
| d = detail[(detail["metric"] == metric)] | |
| if era: | |
| d = d[d["era"] == era] | |
| a = d[d["candidate"] == cand_a].groupby("target_year")[["sum_err", "n", "sum_err_ref"]].sum() | |
| b = d[d["candidate"] == cand_b].groupby("target_year")[["sum_err", "n", "sum_err_ref"]].sum() | |
| j = a.join(b, lsuffix="_a", rsuffix="_b", how="inner") | |
| rng = np.random.default_rng(seed) | |
| years = j.index.to_numpy() | |
| diffs = [] | |
| for _ in range(n_boot): | |
| s = j.loc[rng.choice(years, size=len(years), replace=True)] | |
| skill_a = 1 - s["sum_err_a"].sum() / s["sum_err_ref_a"].sum() | |
| skill_b = 1 - s["sum_err_b"].sum() / s["sum_err_ref_b"].sum() | |
| diffs.append(skill_a - skill_b) | |
| diffs = np.array(diffs) | |
| return {"mean_diff": float(diffs.mean()), "se": float(diffs.std()), | |
| "p_diff_le_0": float((diffs <= 0).mean())} | |