carlomazzaferro / scikit-hts

Hierarchical Time Series Forecasting with a familiar API
MIT License
222 stars 39 forks source link

[BUG] Possible error in computation of WLSV??? #84

Open jiahao87 opened 2 years ago

jiahao87 commented 2 years ago

Describe the bug The diagonal matrix for the WLSV method currently is computed by the code below under hts.functions.optimal_combination function:

elif method == MethodT.WLSV.name:
    diag = [mse[key] for key in mse.keys()]
    diag = np.diag(np.flip(np.hstack(diag) + 0.0000001, 0))

Is there a reason for the np.flip? I am assuming that the mse.keys() are in the same order as the forecasts parameter to hts.functions.optimal_combination function. By flipping the array, the base forecast errors will be presented in the diagonal matrix in a reverse order.

Example:

>>> mse = {'total': 10, 'subtotal1': 5, 'subtotal2': 3}

>>> diag = [mse[key] for key in mse.keys()]

>>> diag = np.diag(np.flip(np.hstack(diag) + 0.0000001, 0))

>>> diag
array([[ 3.0000001,  0.       ,  0.       ],
       [ 0.       ,  5.0000001,  0.       ],
       [ 0.       ,  0.       , 10.0000001]])

However, referring to the formula here, I am expecting diagonal matrix to be:

array([[ 10.0000001,  0.       ,  0.       ],
       [ 0.       ,  5.0000001,  0.       ],
       [ 0.       ,  0.       , 3.0000001]])

To Reproduce Steps to reproduce the behavior

Please see example above.

Expected behavior A clear and concise description of what you expected to happen.

Please see example above.

Desktop (please complete the following information):

Additional context Add any other context about the problem here.

Much appreciation for any clarification and your patience if I misunderstood something. Thank you for your great work here.