import numpy as np
import pandas as pd

URL = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/airline-passengers.csv"
y = pd.read_csv(URL, index_col="Month", parse_dates=True)["Passengers"].asfreq("MS")
train, test = y[:-12], y[-12:]
h, m = len(test), 12

forecasts = {
    "mean":           np.repeat(train.mean(), h),
    "naive":          np.repeat(train.iloc[-1], h),
    "seasonal naive": np.array([train.iloc[-m + (i % m)] for i in range(h)]),
    "drift":          train.iloc[-1] + np.arange(1, h + 1) * (train.iloc[-1] - train.iloc[0]) / (len(train) - 1),
}

# MASE scales errors by the in-sample seasonal-naive error: < 1 beats seasonal naive
scale = np.mean(np.abs(train.values[m:] - train.values[:-m]))
for name, f in forecasts.items():
    mae = np.mean(np.abs(test.values - f))
    print(f"{name:15s} MAE {mae:6.1f}   MASE {mae / scale:.2f}")
