Download code/audit_forecasts.py from Tonic/protostar-climate-2026: direct link, hf CLI and curl.
- Browser
- Download file 3.43 kB
-
https://huggingface.co/datasets/Tonic/protostar-climate-2026/resolve/main/code/audit_forecasts.py
- Command line
-
hf download hf://datasets/Tonic/protostar-climate-2026/code/audit_forecasts.py
-
curl -L -o audit_forecasts.py https://huggingface.co/datasets/Tonic/protostar-climate-2026/resolve/main/code/audit_forecasts.py
3.43 kB
| """Plausibility audit: every forecast vs the city's own observed history. | |
| For each city-metric: historical (valid, <=2018) min/max/mean/sd and count, | |
| then forecast exceedance over the historical record, z-score of the 2025 | |
| forecast, and the implied 2019->2025 change. Flags rows worth hand-checking. | |
| Writes outputs/audit_forecast_plausibility.csv and prints the worst flags. | |
| """ | |
| from __future__ import annotations | |
| import sys | |
| from pathlib import Path | |
| import numpy as np | |
| import pandas as pd | |
| sys.path.insert(0, str(Path(__file__).parent)) | |
| from panel import Panel | |
| ROOT = Path(__file__).resolve().parent.parent | |
| def main() -> None: | |
| panel = Panel(ROOT) | |
| fc = pd.read_csv(ROOT / "submission.csv", dtype={"city_id": str}) | |
| rows = [] | |
| for metric, vals in panel.values.items(): | |
| with np.errstate(invalid="ignore"): | |
| hist_min = np.nanmin(np.where(np.isfinite(vals), vals, np.nan), axis=1) | |
| hist_max = np.nanmax(np.where(np.isfinite(vals), vals, np.nan), axis=1) | |
| hist_mean = np.nanmean(np.where(np.isfinite(vals), vals, np.nan), axis=1) | |
| hist_sd = np.nanstd(np.where(np.isfinite(vals), vals, np.nan), axis=1) | |
| n_hist = np.isfinite(vals).sum(axis=1) | |
| sub = fc[fc["metric"] == metric] | |
| piv = sub.pivot(index="city_id", columns="year", values="predicted_value") \ | |
| .reindex(index=panel.cities) | |
| f19, f25 = piv[2019].to_numpy(), piv[2025].to_numpy() | |
| rows.append(pd.DataFrame({ | |
| "city_id": panel.cities, "metric": metric, "n_hist": n_hist, | |
| "hist_min": hist_min, "hist_max": hist_max, "hist_mean": hist_mean, | |
| "hist_sd": hist_sd, "fc_2019": f19, "fc_2025": f25, | |
| "fc2025_minus_histmax": f25 - hist_max, | |
| "fc2025_z": (f25 - hist_mean) / np.where(hist_sd > 0, hist_sd, np.nan), | |
| "fc_change_19_25": f25 - f19, | |
| })) | |
| out = pd.concat(rows, ignore_index=True) | |
| out.to_csv(ROOT / "outputs" / "audit_forecast_plausibility.csv", index=False) | |
| print("=== forecast ranges by metric (all cities/years) ===") | |
| print(fc.groupby("metric")["predicted_value"].describe().round(2).to_string()) | |
| print("\n=== worst record-exceedance: fc2025 - historical max (per metric) ===") | |
| for metric, sub in out.groupby("metric"): | |
| top = sub.reindex(sub["fc2025_minus_histmax"].sort_values(ascending=False).index).head(6) | |
| print(f"\n-- {metric} --") | |
| print(top[["city_id", "n_hist", "hist_max", "hist_mean", "hist_sd", | |
| "fc_2019", "fc_2025", "fc2025_minus_histmax", "fc2025_z"]] | |
| .round(2).to_string(index=False)) | |
| print("\n=== largest |z| of 2025 forecast vs own history ===") | |
| for metric, sub in out.groupby("metric"): | |
| top = sub.reindex(sub["fc2025_z"].abs().sort_values(ascending=False).index).head(4) | |
| print(f"-- {metric}: " + "; ".join( | |
| f"{r.city_id} z={r.fc2025_z:+.1f} (hist {r.hist_mean:.0f}±{r.hist_sd:.0f}, n={int(r.n_hist)})" | |
| for r in top.itertuples())) | |
| print("\n=== sparsest histories (n_hist < 15) and their 2025 forecasts ===") | |
| sparse = out[out["n_hist"] < 15].sort_values("n_hist") | |
| print(sparse[["city_id", "metric", "n_hist", "hist_mean", "fc_2025"]].round(1) | |
| .to_string(index=False) if len(sparse) else "none") | |
| if __name__ == "__main__": | |
| main() | |