File size: 2,541 Bytes
49f3de8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Generate the v2 submission.csv (adopted after Phase-5 confirmation).



Package (selected on tune-era origins 1990-2000, confirmed on 2001-2011):

- annual_avg_temp: median of {L15+national-40yr-trend, L15+regional-40yr-trend,

  L15+national-40yr-trend on step-adjusted series}

- cdd: 50/50 blend of CDD delta method (30 historical years, median) and the

  linear L15+national-40yr-trend CDD model

- total_precip_in, hottest_day_temp: 30-year mean (all contested candidates

  were rejected in Phase 4)

Fitted on data <= 2018 only; predicts 2019-2025.

"""
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 dataset import METRICS
from leakage import assert_no_forbidden_artifacts
from run_v2_confirm import build_packages

ORIGIN = 2018
TARGET_YEARS = list(range(2019, 2026))


def main() -> None:
    root = Path(__file__).resolve().parent.parent
    assert_no_forbidden_artifacts(Path(__file__).parent)
    panel, packages = build_packages()
    assert int(panel.years.max()) <= ORIGIN

    frames = []
    for metric, cands in packages.items():
        fc = cands["v2"](panel, ORIGIN)  # (210, 7)
        if not np.isfinite(fc).all():
            raise RuntimeError(f"{metric}: non-finite forecasts")
        frames.append(pd.DataFrame({
            "city_id": np.repeat(panel.cities, 7),
            "year": np.tile(TARGET_YEARS, len(panel.cities)),
            "metric": metric,
            "predicted_value": fc.ravel(),
        }))
    out = pd.concat(frames, ignore_index=True)
    expected = {(c, y, m) for c in panel.cities for y in TARGET_YEARS for m in METRICS}
    got = set(zip(out["city_id"], out["year"], out["metric"]))
    if got != expected:
        raise RuntimeError(f"grid mismatch: missing {len(expected - got)}, extra {len(got - expected)}")
    out = out.sort_values(["city_id", "year", "metric"]).reset_index(drop=True)

    v1_path = root / "outputs" / "submission_v1.csv"
    sub_path = root / "submission.csv"
    if sub_path.exists() and not v1_path.exists():
        v1_path.write_bytes(sub_path.read_bytes())
        print(f"backed up v1 to {v1_path}")
    out.to_csv(sub_path, index=False)
    out.to_csv(root / "outputs" / "submission_v2.csv", index=False)
    print(f"wrote {sub_path}: {len(out)} rows")
    print(out.groupby("metric")["predicted_value"].describe().round(2).to_string())


if __name__ == "__main__":
    main()