# pip install neuralforecast   (needs PyTorch)
import pandas as pd
from neuralforecast import NeuralForecast
from neuralforecast.models import NHITS, NBEATS

URL = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/airline-passengers.csv"
raw = pd.read_csv(URL)
df = pd.DataFrame({"unique_id": "air", "ds": pd.to_datetime(raw["Month"]), "y": raw["Passengers"]})
train, test = df.iloc[:-12], df.iloc[-12:]

models = [
    # three stacks pool the input at different rates (4, 2, 1) and forecast at matching resolutions
    NHITS(h=12, input_size=36, n_pool_kernel_size=[4, 2, 1], n_freq_downsample=[4, 2, 1],
          scaler_type="robust", max_steps=500, random_seed=1),
    NBEATS(h=12, input_size=36, scaler_type="robust", max_steps=500, random_seed=1),
]
nf = NeuralForecast(models=models, freq="MS")
nf.fit(df=train)
fc = nf.predict().merge(test, on=["unique_id", "ds"])
for m in ["NHITS", "NBEATS"]:
    print(f"{m:7s} MAE {(fc['y'] - fc[m]).abs().mean():.1f}")
