Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/Make reconcilliation order independent #1592

14 changes: 10 additions & 4 deletions darts/dataprocessing/transformers/reconciliation.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,14 @@ class BottomUpReconciliator(BaseDataTransformer):

@staticmethod
def get_projection_matrix(series):
n, m = series.n_components, len(series.bottom_level_components)
return np.concatenate([np.zeros((m, n - m)), np.eye(m)], axis=1)
leaves_seq = list(series.bottom_level_components)
n, m = series.n_components, len(leaves_seq)
leaves_indexes = {l: i for i, l in enumerate(leaves_seq)}
G = np.zeros((m, n))
for i, c in enumerate(series.components):
if c in leaves_indexes:
G[leaves_indexes[c], i] = 1.0
return G

@staticmethod
def ts_transform(series: TimeSeries, *args, **kwargs) -> TimeSeries:
Expand Down Expand Up @@ -128,9 +134,9 @@ def get_projection_matrix(series):

# compute proportions for each base component
proportions = sum_base / sum_total

top_level_index = list(series.components).index(series.top_level_component)
G = np.zeros((m, n))
G[:, 0] = proportions
G[:, top_level_index] = proportions

return G

Expand Down
41 changes: 41 additions & 0 deletions darts/tests/dataprocessing/transformers/test_reconciliation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import unittest

import numpy as np
from pandas import date_range

from darts import TimeSeries, concatenate
from darts.dataprocessing.transformers.reconciliation import (
Expand Down Expand Up @@ -179,3 +180,43 @@ def test_more_intricate_hierarchy(self):
recon = MinTReconciliator("wls_val")
recon.fit(self.series_complex)
self._assert_reconciliation_complex(recon)

def test_reconcilliation_is_order_independent(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks for adding the tests too 👍

dates = date_range("2020-01-01", "2020-12-31", freq="D")
nr_dates = len(dates)
t1 = TimeSeries.from_times_and_values(
dates, 2 * np.ones(nr_dates), columns=["T1"]
)
t2 = TimeSeries.from_times_and_values(
dates, 5 * np.ones(nr_dates), columns=["T2"]
)
t3 = TimeSeries.from_times_and_values(dates, np.ones(nr_dates), columns=["T3"])
tsum = TimeSeries.from_times_and_values(
dates, 9 * np.ones(nr_dates), columns=["T_sum"]
)
ts_1 = concatenate([t1, t2, t3, tsum], axis="component")
ts_2 = concatenate([tsum, t1, t2, t3], axis="component")

def assert_ts_are_equal(ts1, ts2):
for comp in ["T1", "T2", "T3", "T_sum"]:
self.assertEqual(ts1[comp], ts2[comp])

hierarchy = {"T1": ["T_sum"], "T2": ["T_sum"], "T3": ["T_sum"]}
ts_1 = ts_1.with_hierarchy(hierarchy)
ts_2 = ts_2.with_hierarchy(hierarchy)
assert_ts_are_equal(ts_1, ts_2)

ts_1_reconc = TopDownReconciliator().fit_transform(ts_1)
ts_2_reconc = TopDownReconciliator().fit_transform(ts_2)

assert_ts_are_equal(ts_1_reconc, ts_2_reconc)

ts_1_reconc = MinTReconciliator().fit_transform(ts_1)
ts_2_reconc = MinTReconciliator().fit_transform(ts_2)

assert_ts_are_equal(ts_1_reconc, ts_2_reconc)

ts_1_reconc = BottomUpReconciliator().transform(ts_1)
ts_2_reconc = BottomUpReconciliator().transform(ts_2)

assert_ts_are_equal(ts_1_reconc, ts_2_reconc)