Prediction intervals in forecasting models
using
mlforecast
.
During this walkthrough, we will become familiar with the main
MlForecast
class and some relevant methods such as
MLForecast.fit
,
MLForecast.predict
and
MLForecast.cross_validation
in other.
Let’s start!!!
Percentage | Multiplier |
---|---|
50 | 0.67 |
55 | 0.76 |
60 | 0.84 |
65 | 0.93 |
70 | 1.04 |
75 | 1.15 |
80 | 1.28 |
85 | 1.44 |
90 | 1.64 |
95 | 1.96 |
96 | 2.05 |
97 | 2.17 |
98 | 2.33 |
99 | 2.58 |
Method | h-step forecast standard deviation |
---|---|
Mean forecasts | |
Naïve forecasts | |
Seasonal naïve forecasts | |
Drift forecasts |
pip install mlforecast
conda install -c conda-forge mlforecast
timestamp | value | |
---|---|---|
0 | 2014-07-01 00:00:00 | 10844 |
1 | 2014-07-01 00:30:00 | 8127 |
2 | 2014-07-01 01:00:00 | 6210 |
3 | 2014-07-01 01:30:00 | 4656 |
4 | 2014-07-01 02:00:00 | 3820 |
unique_id
(string, int or category) represents an identifier
for the series.
ds
(datestamp) column should be of a format expected by
Pandas, ideally YYYY-MM-DD for a date or YYYY-MM-DD HH:MM:SS for a
timestamp.
y
(numeric) represents the measurement we wish to forecast.
ds | y | unique_id | |
---|---|---|---|
0 | 2014-07-01 00:00:00 | 10844 | 1 |
1 | 2014-07-01 00:30:00 | 8127 | 1 |
2 | 2014-07-01 01:00:00 | 6210 | 1 |
3 | 2014-07-01 01:30:00 | 4656 | 1 |
4 | 2014-07-01 02:00:00 | 3820 | 1 |
XGBoost model
.
MLForecast.preprocess
method to explore different transformations.
If it is true that the series we are working with is a stationary series
see (Dickey fuller test), however for the sake of practice and
instruction in this guide, we will apply the difference to our series,
we will do this using the target_transforms parameter
and calling the
diff function like:
mlforecast.target_transforms.Differences
target_transforms=[Differences([1])]
in case the series is stationary
we can use a difference, or in the case that the series is not
stationary, we can use more than one difference so that the series is
constant over time, that is, that it is constant in mean and in
variance.
ds | y | unique_id | |
---|---|---|---|
1 | 2014-07-01 00:30:00 | -2717.0 | 1 |
2 | 2014-07-01 01:00:00 | -1917.0 | 1 |
3 | 2014-07-01 01:30:00 | -1554.0 | 1 |
4 | 2014-07-01 02:00:00 | -836.0 | 1 |
5 | 2014-07-01 02:30:00 | -947.0 | 1 |
… | … | … | … |
10315 | 2015-01-31 21:30:00 | 951.0 | 1 |
10316 | 2015-01-31 22:00:00 | 1051.0 | 1 |
10317 | 2015-01-31 22:30:00 | 1588.0 | 1 |
10318 | 2015-01-31 23:00:00 | -718.0 | 1 |
10319 | 2015-01-31 23:30:00 | -303.0 | 1 |
ds | y | unique_id | lag1 | lag24 | |
---|---|---|---|---|---|
25 | 2014-07-01 12:30:00 | -22.0 | 1 | 445.0 | -2717.0 |
26 | 2014-07-01 13:00:00 | -708.0 | 1 | -22.0 | -1917.0 |
27 | 2014-07-01 13:30:00 | 1281.0 | 1 | -708.0 | -1554.0 |
28 | 2014-07-01 14:00:00 | 87.0 | 1 | 1281.0 | -836.0 |
29 | 2014-07-01 14:30:00 | 1045.0 | 1 | 87.0 | -947.0 |
… | … | … | … | … | … |
10315 | 2015-01-31 21:30:00 | 951.0 | 1 | 428.0 | 4642.0 |
10316 | 2015-01-31 22:00:00 | 1051.0 | 1 | 951.0 | -519.0 |
10317 | 2015-01-31 22:30:00 | 1588.0 | 1 | 1051.0 | 2411.0 |
10318 | 2015-01-31 23:00:00 | -718.0 | 1 | 1588.0 | 214.0 |
10319 | 2015-01-31 23:30:00 | -303.0 | 1 | -718.0 | 2595.0 |
ds | y | unique_id | lag1 | lag24 | expanding_mean_lag1 | rolling_mean_lag24_window_size7 | |
---|---|---|---|---|---|---|---|
31 | 2014-07-01 15:30:00 | -836.0 | 1 | -1211.0 | -305.0 | 284.533325 | -1254.285767 |
32 | 2014-07-01 16:00:00 | -2316.0 | 1 | -836.0 | 157.0 | 248.387100 | -843.714294 |
33 | 2014-07-01 16:30:00 | -1215.0 | 1 | -2316.0 | -63.0 | 168.250000 | -578.857117 |
34 | 2014-07-01 17:00:00 | 2190.0 | 1 | -1215.0 | 357.0 | 126.333336 | -305.857147 |
35 | 2014-07-01 17:30:00 | 2322.0 | 1 | 2190.0 | 1849.0 | 187.029419 | 77.714287 |
… | … | … | … | … | … | … | … |
10315 | 2015-01-31 21:30:00 | 951.0 | 1 | 428.0 | 4642.0 | 1.248303 | 2064.285645 |
10316 | 2015-01-31 22:00:00 | 1051.0 | 1 | 951.0 | -519.0 | 1.340378 | 1873.428589 |
10317 | 2015-01-31 22:30:00 | 1588.0 | 1 | 1051.0 | 2411.0 | 1.442129 | 2179.000000 |
10318 | 2015-01-31 23:00:00 | -718.0 | 1 | 1588.0 | 214.0 | 1.595910 | 1888.714233 |
10319 | 2015-01-31 23:30:00 | -303.0 | 1 | -718.0 | 2595.0 | 1.526168 | 2071.714355 |
ds | y | unique_id | lag1 | lag24 | expanding_mean_lag1 | rolling_mean_lag24_window_size7 | year | month | day | hour | |
---|---|---|---|---|---|---|---|---|---|---|---|
31 | 2014-07-01 15:30:00 | -836.0 | 1 | -1211.0 | -305.0 | 284.533325 | -1254.285767 | 2014 | 7 | 1 | 15 |
32 | 2014-07-01 16:00:00 | -2316.0 | 1 | -836.0 | 157.0 | 248.387100 | -843.714294 | 2014 | 7 | 1 | 16 |
33 | 2014-07-01 16:30:00 | -1215.0 | 1 | -2316.0 | -63.0 | 168.250000 | -578.857117 | 2014 | 7 | 1 | 16 |
34 | 2014-07-01 17:00:00 | 2190.0 | 1 | -1215.0 | 357.0 | 126.333336 | -305.857147 | 2014 | 7 | 1 | 17 |
35 | 2014-07-01 17:30:00 | 2322.0 | 1 | 2190.0 | 1849.0 | 187.029419 | 77.714287 | 2014 | 7 | 1 | 17 |
… | … | … | … | … | … | … | … | … | … | … | … |
10315 | 2015-01-31 21:30:00 | 951.0 | 1 | 428.0 | 4642.0 | 1.248303 | 2064.285645 | 2015 | 1 | 31 | 21 |
10316 | 2015-01-31 22:00:00 | 1051.0 | 1 | 951.0 | -519.0 | 1.340378 | 1873.428589 | 2015 | 1 | 31 | 22 |
10317 | 2015-01-31 22:30:00 | 1588.0 | 1 | 1051.0 | 2411.0 | 1.442129 | 2179.000000 | 2015 | 1 | 31 | 22 |
10318 | 2015-01-31 23:00:00 | -718.0 | 1 | 1588.0 | 214.0 | 1.595910 | 1888.714233 | 2015 | 1 | 31 | 23 |
10319 | 2015-01-31 23:30:00 | -303.0 | 1 | -718.0 | 2595.0 | 1.526168 | 2071.714355 | 2015 | 1 | 31 | 23 |
XGBoost model
. We
can observe it with the following instruction:
Let us now visualize the fitted values of our models.
ds | y | XGBRegressor | |
---|---|---|---|
unique_id | |||
1 | 2014-07-01 15:30:00 | 18544.0 | 18441.443359 |
1 | 2014-07-01 16:00:00 | 16228.0 | 16391.152344 |
1 | 2014-07-01 16:30:00 | 15013.0 | 15260.714844 |
1 | 2014-07-01 17:00:00 | 17203.0 | 17066.148438 |
1 | 2014-07-01 17:30:00 | 19525.0 | 19714.404297 |
… | … | … | … |
1 | 2015-01-31 21:30:00 | 24670.0 | 24488.646484 |
1 | 2015-01-31 22:00:00 | 25721.0 | 25868.865234 |
1 | 2015-01-31 22:30:00 | 27309.0 | 27290.125000 |
1 | 2015-01-31 23:00:00 | 26591.0 | 27123.226562 |
1 | 2015-01-31 23:30:00 | 26288.0 | 26241.205078 |
unique_id | ds | XGBRegressor | XGBRegressor-lo-95 | XGBRegressor-lo-80 | XGBRegressor-hi-80 | XGBRegressor-hi-95 | |
---|---|---|---|---|---|---|---|
0 | 1 | 2015-02-01 00:00:00 | 26320.298828 | 25559.884241 | 25680.228369 | 26960.369287 | 27080.713416 |
1 | 1 | 2015-02-01 00:30:00 | 26446.472656 | 24130.429614 | 25195.461621 | 27697.483691 | 28762.515698 |
2 | 1 | 2015-02-01 01:00:00 | 24909.970703 | 23094.950537 | 23579.583398 | 26240.358008 | 26724.990869 |
3 | 1 | 2015-02-01 01:30:00 | 24405.402344 | 21548.628296 | 22006.662598 | 26804.142090 | 27262.176392 |
4 | 1 | 2015-02-01 02:00:00 | 22292.390625 | 20666.736963 | 21130.215430 | 23454.565820 | 23918.044287 |