1import pandas as pd, numpy as np, runpy, sys, io, contextlib2# masked re-estimation: for each origin, set future y (>t-2) and e (>t-1) to NaN and confirm forecast identical3fc = pd.read_csv("out/forecasts.csv", parse_dates=["origin_date"])4d = pd.read_csv("data/canada_gdp_employment.csv", parse_dates=["month"]).set_index("month")5mx=06for t in sorted(fc.origin_date.unique()):7 t=pd.Timestamp(t); dm=d.copy()8 dm.loc[dm.index>t-pd.DateOffset(months=2),["gdp_level","gdp_yoy"]]=np.nan9 dm.loc[dm.index>t-pd.DateOffset(months=1),["emp_level","emp_yoy"]]=np.nan10 covid=lambda m:(m>=pd.Timestamp("2020-03-01"))&(m<=pd.Timestamp("2021-12-01"))11 for h in [1,3]:12 y=dm.gdp_yoy; e=dm.emp_yoy; s=dm.index13 Y=y.reindex(s+pd.DateOffset(months=h)).values; A=y.reindex(s-pd.DateOffset(months=2)).values; B=e.reindex(s-pd.DateOffset(months=1)).values14 k=~np.isnan(Y)&~np.isnan(A)&~np.isnan(B)&~np.asarray(covid(s+pd.DateOffset(months=h))|covid(s-pd.DateOffset(months=2))|covid(s-pd.DateOffset(months=1)))15 X=np.column_stack([np.ones(k.sum()),A[k],B[k]]); b=np.linalg.lstsq(X,Y[k],rcond=None)[0]16 f=b@[1,y[t-pd.DateOffset(months=2)],e[t-pd.DateOffset(months=1)]]17 g=fc[(fc.origin_date==t)&(fc.horizon==h)&(fc.model!="naive")].forecast.iloc[0]18 mx=max(mx,abs(f-g))19print("max abs diff masked vs ledger:",mx)20