Home

Awesome

pyhts

pypi version license

A python package for hierarchical forecasting, inspired by the hts package in R.

Features

Quick Demo

from pyhts import load_tourism

tourism_data = load_tourism()
train = tourism_data.iloc[:, 4:-12].T.values
test = tourism_data.iloc[:, -12:].T.values
from pyhts import Hierarchy

hierarchy = Hierarchy.new(tourism_data, [('state', 'region', 'city')])
print(hierarchy.node_name)
from pyhts import HFModel

model_ols = HFModel(hierarchy=hierarchy, base_forecasters="arima",
                    hf_method="comb", comb_method="ols")
model_ols.fit(train)
ols = model_ols.predict(horizon=12)
all_level_ols = hierarchy.aggregate_ts(ols)
model_wlss = HFModel(hierarchy, base_forecasters=model_ols.base_forecasters,
                     hf_method="comb", comb_method="wls", weights="structural")
model_wlss.fit(train)
wlss = model_wlss.predict(horizon=12)

model_wlsv = HFModel(hierarchy, base_forecasters=model_ols.base_forecasters,
                     hf_method="comb", comb_method="mint", weights="variance")
model_wlsv.fit(train)
wlsv = model_wlsv.predict(horizon=12)

model_shrink = HFModel(hierarchy, base_forecasters=model_ols.base_forecasters,
                       hf_method="comb", comb_method="mint", weights="shrinkage")
model_shrink.fit(train)
shrink = model_shrink.predict(horizon=12)
# accuracy of reconciled forecasts
accuracy = [hierarchy.accuracy(test, fcast, hist=train, measure=['mase', 'rmse'])
            for fcast in (ols, wlss, wlsv, shrink)]

# accuracy of base forecasts
base_forecasts = model_ols.generate_base_forecast(horizon=12)
accuracy_base = hierarchy.accuracy_base(test, base_forecasts, hist=train, measure=['mase', 'rmse'])

Because of the incoherence of base forecasts, base_forecasts are forecasts of all time series in the hierarchy, while coherent forecasts are forecasts of the bottom-level time series.

Documentation

See documentation here.