philipperemy / n-beats

Keras/Pytorch implementation of N-BEATS: Neural basis expansion analysis for interpretable time series forecasting.
MIT License
855 stars 163 forks source link

Trend coefficients in pytorch implementation #45

Closed meganset closed 3 years ago

meganset commented 3 years ago

I'm not able to match the trend coefficients used here with those in the paper's github repo (https://github.com/ElementAI/N-BEATS)

here, iI think the trend coefficients are calculated as:

>>> bn=15; fn=5; p=4  #15 backcasts,5 foreeasts, p=thetas
>>> b,f=linspace(bn,fn)
>>> f
array([0.78947368, 1.84210526, 2.89473684, 3.94736842, 5.        ])
>>> torch.tensor([f ** i for i in range(p)])
tensor([[  1.0000,   1.0000,   1.0000,   1.0000,   1.0000],
        [  0.7895,   1.8421,   2.8947,   3.9474,   5.0000],
        [  0.6233,   3.3934,   8.3795,  15.5817,  25.0000],
        [  0.4921,   6.2509,  24.2565,  61.5068, 125.0000]],
       dtype=torch.float64)

I think the N-Beats repository (and the paper, on page 5) calculates the trend coefficients as:

>>> torch.tensor(np.concatenate([np.power(np.arange(fn) / fn, i)[None, :] for i in range(p)]))
tensor([[1.0000, 1.0000, 1.0000, 1.0000, 1.0000],
        [0.0000, 0.2000, 0.4000, 0.6000, 0.8000],
        [0.0000, 0.0400, 0.1600, 0.3600, 0.6400],
        [0.0000, 0.0080, 0.0640, 0.2160, 0.5120]], dtype=torch.float64)

the main difference is factors from linpace are not divided by the number of forecast periods; there's a smaller discrepancy in the linspace periods vs the arange periods.

thanks for looking at this (and thanks for the useful repo on nbeats)

philipperemy commented 3 years ago

@meganset I changed it! Thank you for reporting :)