JoaquinAmatRodrigo / skforecast

Time series forecasting with machine learning models
https://skforecast.org
BSD 3-Clause "New" or "Revised" License
996 stars 113 forks source link

Time Series Differentiation not working as expected #664

Open tomatoes-prog opened 2 months ago

tomatoes-prog commented 2 months ago

Hello, thanks for this great package.

I am having struggles forecasting a time series with trend, which have a almost weekly trend, i would like to use a 7 differentiation order in my ForecasterAutoreg object using this params

            forecaster = ForecasterAutoreg(
                regressor=model,
                lags=[15],
                differentiation=7

            )

Nevertheless, when using the dummy example with a 2nd order differentiation the results are unexpected

y = np.array([5, 8, 12, 10, 14, 17, 21, 19], dtype=float)
diffenciator = TimeSeriesDifferentiator(2)
diffenciator.fit(y)
y_diff = diffenciator.transform(y)

print(f"Original time series   : {y}")
print(f"Differenced time series: {y_diff}")
Original time series   : [ 5.  8. 12. 10. 14. 17. 21. 19.]
Differenced time series result: [nan nan  1. -6.  6. -1.  1. -6.]
Expected: [nan nan  7. 2.  2. 7.  7. 2.]

Is this only working on order 1

JoaquinAmatRodrigo commented 2 months ago

Hi @tomatoes-prog, Thanks for using skforecast!

I think the problem here is a misunderstanding of the term "order". In skforecast, a differentiation of order 2 means applying the differentiation process twice. We are following the same idea as numpy.

import numpy as np
y = np.array([5, 8, 12, 10, 14, 17, 21, 19], dtype=float)
y_diff_1 = np.diff(y)
y_diff_2 = np.diff(y_diff_1)
y_diff_2

array([ 1., -6., 6., -1., 1., -6.])

np.diff(y, n=2)

array([ 1., -6., 6., -1., 1., -6.])

Do you want to differentiate once with the value 2 steps before?

tomatoes-prog commented 2 months ago

Ohhh I got it now, yeah I thought about order as the distance between the steps that were going to be differentiated. Thanks for the explanation. And yes, I want to differentiate once with n-steps before, any suggestions? I am expecting this to have a better trend capture than 1 order differentiation

JoaquinAmatRodrigo commented 2 months ago

Unfortunately, Skforecast does not yet automate this type of differentiation. The other option at the moment is to differentiate the series before modeling. We will try to add this in the next releases.

Best