Datasets:
File size: 4,633 Bytes
c0058c4 | 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 | """Rolling-origin backtests on data up to 2018 only.
For each origin year o, every model sees years <= o and forecasts o+1..o+7, scored with the
challenge formula against the 30-year average. Origins 1997-2004 are used to choose settings
("tune"); 2005-2011 are held out to check that the choice generalises ("check").
python code/backtest.py compare # every model we tried -> code/experiments.csv (~10 min)
python code/backtest.py grid # settings grid of the level + pooled-trend model (~5 min)
"""
import sys
import pandas as pd
import climate_lib as cl
TUNE = list(range(1997, 2005))
CHECK = list(range(2005, 2012))
def summarise(name, f, panel, meta):
rows = []
for split, origins in [("tune", TUNE), ("check", CHECK)]:
r = cl.backtest(f, panel, meta, origins).drop(columns="origin").mean()
rows.append({"model": name, "split": split, **r.to_dict()})
print(f"{name:34s} {split:5s} " + " ".join(f"{m}={r[m]:+.3f}" for m in cl.METRICS), flush=True)
return rows
def compare(panel, meta):
import learned
from forecast import PARAMS, model as final
B = {m: learned.FeatureBuilder(panel, meta, m) for m in cl.METRICS}
def lt(**kw):
return lambda p, mt, o, y: cl.f_level_trend(p, mt, o, y, **kw)
def ml(make, span=None):
return lambda p, mt, o, y: learned.f_learned(p, mt, o, y, builders=B, make_model=make, train_span=span)
T = PARAMS["annual_avg_temp"]
def cdd_temp(p, mt, o, y):
return cl.f_cdd_from_temp(p, mt, o, y, temp_params=T)
def temp(**kw):
return lambda p, mt, o, y: cl.f_level_trend(p, mt, o, y, metrics=["annual_avg_temp"],
params={"annual_avg_temp": {**T, **kw}})
def tmax_tmin(p, mt, o, y):
a = cl.f_level_trend(p, mt, o, y, metrics=["tmax", "tmin"], params={"tmax": T, "tmin": T})
a = a.groupby(["city_id", "year"], as_index=False)["predicted_value"].mean()
return a.assign(metric="annual_avg_temp")
def gbm_cdd(p, mt, o, y):
return learned.f_learned(p, mt, o, y, metrics=["cdd"], builders=B, make_model=learned.make_gbm, train_span=30)
models = [
("baseline: last year", lambda p, mt, o, y: cl.f_last(p, mt, o, y)),
("baseline: city linear trend 30y", lambda p, mt, o, y: cl.f_city_trend(p, mt, o, y, k=30)),
("mean of last 10 years", lambda p, mt, o, y: cl.f_mean(p, mt, o, y, k=10)),
("mean of last 15 years", lambda p, mt, o, y: cl.f_mean(p, mt, o, y, k=15)),
("level15 + pooled trend (one setting)", lt(level_k=15, trend_k=50, radius_km=1000)),
("level+trend, per-metric PARAMS", lt(params=PARAMS)),
("ridge, all origins since 1940", ml(learned.make_ridge)),
("ridge, last 30 origins", ml(lambda: learned.make_ridge(100), 30)),
("GBM, all origins since 1940", ml(learned.make_gbm)),
("GBM, last 30 origins", ml(learned.make_gbm, 30)),
("temp: trend fitted since 1975", temp(trend_start=1975)),
("temp: trend x1.5 (amplified)", temp(trend_start=1975, damp=1.5)),
("temp: Tmax and Tmin forecast separately", tmax_tmin),
("CDD from temperature forecast", cdd_temp),
("CDD: from temperature + GBM (previous final)", lambda *a: cl.blend(cdd_temp(*a), gbm_cdd(*a))),
("CDD: delta method, 15 years", lambda p, mt, o, y: cl.f_cdd_delta(p, mt, o, y, temp_params=T, k=15)),
("CDD: delta method, 30 years", lambda p, mt, o, y: cl.f_cdd_delta(p, mt, o, y, temp_params=T, k=30)),
("FINAL (forecast.py)", final),
]
rows = []
for name, f in models:
rows += summarise(name, f, panel, meta)
out = pd.DataFrame(rows)
out.round(4).to_csv(cl.ROOT / "code" / "experiments.csv", index=False)
return out
def grid(panel, meta):
rows = []
for adj in (False, True):
for lk in (10, 15, 30):
for tk in (30, 50):
for r in (500, 1000, 5000):
for d in (0.5, 1.0):
name = f"adj={adj:d} L={lk} T={tk} R={r} d={d}"
f = (lambda p, mt, o, y, kw=dict(level_k=lk, trend_k=tk, radius_km=r, damp=d,
adjust_stations=adj): cl.f_level_trend(p, mt, o, y, **kw))
rows += summarise(name, f, panel, meta)
pd.DataFrame(rows).round(4).to_csv(cl.ROOT / "code" / "grid_results.csv", index=False)
if __name__ == "__main__":
panel, meta = cl.load_panel()
{"compare": compare, "grid": grid}[sys.argv[1] if len(sys.argv) > 1 else "compare"](panel, meta)
|