Skip to content

Instantly share code, notes, and snippets.

View hrzn's full-sized avatar

Julien Herzen hrzn

View GitHub Profile
measure_mae(reconcilied_preds)
plot_forecast_sums(reconcilied_preds)
reconciliator = MinTReconciliator(method="wls_val")
reconciliator.fit(train)
reconcilied_preds = reconciliator.transform(pred)
def plot_forecast_sums(pred_series):
plt.figure(figsize=(10, 5))
pred_series["Total"].plot(label="total", lw=6, alpha=0.3, color="grey")
sum([pred_series[r] for r in regions]).plot(label="sum of regions")
sum([pred_series[r] for r in reasons]).plot(label="sum of reasons")
sum([pred_series[t] for t in regions_reasons_comps]).plot(
label="sum of (region, reason) series"
)
sum([pred_series[t] for t in regions_reasons_city_comps]).plot(
# we pre-generate some of the components' names
regions_reasons_comps = list(
map(lambda t: "{} - {}".format(t[0], t[1].lower()), product(regions, reasons))
)
regions_reasons_city_comps = list(
map(
lambda t: "{} - {} - {}".format(t[0], t[1].lower(), t[2]),
product(regions, reasons, city_labels),
)
components_to_show = ["Total", "NSW", "NSW - bus", "NSW - hol", "NSW - bus - city"]
plt.figure(figsize=(14, 8))
tourism_series[components_to_show].plot(lw=5)
pred[components_to_show].plot(lw=2)
model = LinearRegressionModel(lags=12)
model.fit(train)
pred = model.predict(n=len(val))
train, val = tourism_series[:-12], tourism_series[-12:]
tourism_series = tourism_series.with_hierarchy(hierarchy)
for component in ["Hol", "NSW - hol", "NSW - bus - city"]:
print(f"{component} -> {hierarchy[component]}")