unit8co / darts

A python library for user-friendly forecasting and anomaly detection on time series.
https://unit8co.github.io/darts/
Apache License 2.0
8.12k stars 885 forks source link

Plot in-sample forecast? #245

Closed aleave closed 3 years ago

aleave commented 4 years ago

Hello, is there a way to plot in-sample forecast after having fitted a model? The backtest() and historical_forecasts() are of course more rigorous for a proper backtesting but I would simply like sometimes to see how the model performs in sample before moving on to more sophisticate analysis.

LeoTafti commented 4 years ago

Hi @aleave,

The easiest way would be to first split your time series in two parts, train on the first part and forecast from there (much like what is done in the example notebooks with training and validation sets):

training, validation = series.split_before(pd.Timestamp('xxx'))
model.fit(training)
forecast = model.predict(len(validation))

You can then plot forecast against the original time series series:

import matplotlib.pyplot as plt

series.plot()
forecast.plot()

plt.show()

One important thing to note though is that this trains the model only on the points lying before the specified timestamp, not on the whole time series. Currently there is no easy way to make a prediction from an earlier time than the end of the time series the model was trained on.

–––

Also note for your convenience that there is a get_timestamp_at_point() method you can import from darts.utils, which lets you convert a ratio to a timestamp. E.g. to split training 70% – validation 30% you can do:

from darts.utils import get_timestamp_at_point

training, validation = series.split_before(get_timestamp_at_point(0.7, series)) 

Finally, allowing for a more natural series.split_before(0.7) is also already on our radar :)