"""Backtest harness on pre-2019 data only: forecast origin+1..origin+H from data up to origin.""" import itertools, sys import numpy as np, pandas as pd from common import load_annual, long_valid, score, METRICS import models A = load_annual() LV = long_valid(A) COORDS = A.groupby("ID")[["Lat", "Lon"]].first() ORIGINS = [1990, 1994, 1998, 2002, 2006, 2011] H = 7 def run(fn, metric, origins=ORIGINS, h=H, **kw): rows = [] for o in origins: yrs = list(range(o + 1, min(o + h, 2018) + 1)) p = fn(LV, o, yrs, COORDS, metric, **kw) r = score(p, LV[LV.metric == metric], o, yrs).loc[metric] rows.append(dict(origin=o, mae=r.e, base=r.eb, S=r.S)) return pd.DataFrame(rows) if __name__ == "__main__": # sanity check vs. the organizers' practice run (train to 2010, predict 2011-2018) for name, fn in [("30yr", models.baseline30_model), ("trend", models.linear30_model)]: print(name, run(fn, "cdd", origins=[2010], h=8).mae.round(1).tolist())