Step-by-step guide on using the DynamicStandardTheta Model
with Statsforecast
.
Tip Statsforecast will be needed. To install, see instructions.Next, we import plotting libraries and configure the plotting style.
month | production | |
---|---|---|
0 | 1962-01-01 | 589 |
1 | 1962-02-01 | 561 |
2 | 1962-03-01 | 640 |
3 | 1962-04-01 | 656 |
4 | 1962-05-01 | 727 |
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 | 1962-01-01 | 589 | 1 |
1 | 1962-02-01 | 561 | 1 |
2 | 1962-03-01 | 640 | 1 |
3 | 1962-04-01 | 656 | 1 |
4 | 1962-05-01 | 727 | 1 |
(ds)
is in an object format, we need
to convert to a date format
Dynamic Standard Theta Model
2. Data to test our model
For the test data we will use the last 12 months to test and evaluate
the performance of our model.
season_length
.
freq:
a string indicating the frequency of the data. (See pandas’
available
frequencies.)
n_jobs:
n_jobs: int, number of jobs used in the parallel
processing, use -1 for all cores.
fallback_model:
a model to be used if a model fails.
Dynamic Standard Theta model
. We can
observe it with the following instruction:
.get()
function to extract the element and then we are going to save
it in a pd.DataFrame()
.
residual Model | |
---|---|
0 | -18.247131 |
1 | -46.247131 |
2 | 17.140198 |
… | … |
153 | -58.941711 |
154 | -91.055420 |
155 | -42.624939 |
StatsForecast.forecast
method instead of .fit
and .predict
.
The main difference is that the .forecast
doest not store the fitted
values and is highly scalable in distributed environments.
The forecast method takes two arguments: forecasts next h
(horizon)
and level
.
h (int):
represents the forecast h steps into the future. In this
case, 12 months ahead.
level (list of floats):
this optional parameter is used for
probabilistic forecasting. Set the level (or confidence percentile)
of your prediction interval. For example, level=[90]
means that
the model expects the real value to be inside that interval 90% of
the times.
unique_id | ds | DynamicTheta | |
---|---|---|---|
0 | 1 | 1975-01-01 | 838.531555 |
1 | 1 | 1975-02-01 | 800.154968 |
2 | 1 | 1975-03-01 | 893.430786 |
… | … | … | … |
9 | 1 | 1975-10-01 | 815.959351 |
10 | 1 | 1975-11-01 | 786.716431 |
11 | 1 | 1975-12-01 | 823.539368 |
unique_id | ds | y | DynamicTheta | |
---|---|---|---|---|
0 | 1 | 1962-01-01 | 589.0 | 607.247131 |
1 | 1 | 1962-02-01 | 561.0 | 607.247131 |
2 | 1 | 1962-03-01 | 640.0 | 622.859802 |
3 | 1 | 1962-04-01 | 656.0 | 606.987793 |
4 | 1 | 1962-05-01 | 727.0 | 605.021179 |
unique_id | ds | DynamicTheta | DynamicTheta-lo-95 | DynamicTheta-hi-95 | |
---|---|---|---|---|---|
0 | 1 | 1975-01-01 | 838.531555 | 741.237366 | 954.407166 |
1 | 1 | 1975-02-01 | 800.154968 | 640.697205 | 945.673096 |
2 | 1 | 1975-03-01 | 893.430786 | 703.900635 | 1065.418701 |
… | … | … | … | … | … |
9 | 1 | 1975-10-01 | 815.959351 | 536.422791 | 1086.643433 |
10 | 1 | 1975-11-01 | 786.716431 | 484.476593 | 1033.687134 |
11 | 1 | 1975-12-01 | 823.539368 | 509.187256 | 1104.107788 |
h
(for
horizon) and level
.
h (int):
represents the forecast h steps into the future. In this
case, 12 months ahead.
level (list of floats):
this optional parameter is used for
probabilistic forecasting. Set the level (or confidence percentile)
of your prediction interval. For example, level=[95]
means that
the model expects the real value to be inside that interval 95% of
the times.
unique_id | ds | DynamicTheta | |
---|---|---|---|
0 | 1 | 1975-01-01 | 838.531555 |
1 | 1 | 1975-02-01 | 800.154968 |
2 | 1 | 1975-03-01 | 893.430786 |
… | … | … | … |
9 | 1 | 1975-10-01 | 815.959351 |
10 | 1 | 1975-11-01 | 786.716431 |
11 | 1 | 1975-12-01 | 823.539368 |
unique_id | ds | DynamicTheta | DynamicTheta-lo-80 | DynamicTheta-hi-80 | DynamicTheta-lo-95 | DynamicTheta-hi-95 | |
---|---|---|---|---|---|---|---|
0 | 1 | 1975-01-01 | 838.531555 | 765.423828 | 927.285339 | 741.237366 | 954.407166 |
1 | 1 | 1975-02-01 | 800.154968 | 701.099854 | 899.316162 | 640.697205 | 945.673096 |
2 | 1 | 1975-03-01 | 893.430786 | 758.326416 | 1007.631165 | 703.900635 | 1065.418701 |
… | … | … | … | … | … | … | … |
9 | 1 | 1975-10-01 | 815.959351 | 608.699463 | 992.552673 | 536.422791 | 1086.643433 |
10 | 1 | 1975-11-01 | 786.716431 | 558.429810 | 970.648376 | 484.476593 | 1033.687134 |
11 | 1 | 1975-12-01 | 823.539368 | 588.706787 | 1031.564941 | 509.187256 | 1104.107788 |
(n_windows=5)
, forecasting every second months
(step_size=12)
. Depending on your computer, this step should take
around 1 min.
The cross_validation method from the StatsForecast class takes the
following arguments.
df:
training data frame
h (int):
represents h steps into the future that are being
forecasted. In this case, 12 months ahead.
step_size (int):
step size between each window. In other words:
how often do you want to run the forecasting processes.
n_windows(int):
number of windows used for cross validation. In
other words: what number of forecasting processes in the past do you
want to evaluate.
unique_id:
index. If you dont like working with index just run
crossvalidation_df.resetindex()ds:
datestamp or temporal indexcutoff:
the last datestamp or temporal index for the n_windows.y:
true value"model":
columns with the model’s name and fitted value.unique_id | ds | cutoff | y | DynamicTheta | |
---|---|---|---|---|---|
0 | 1 | 1972-01-01 | 1971-12-01 | 826.0 | 827.107239 |
1 | 1 | 1972-02-01 | 1971-12-01 | 799.0 | 789.924194 |
2 | 1 | 1972-03-01 | 1971-12-01 | 890.0 | 879.664429 |
… | … | … | … | … | … |
33 | 1 | 1974-10-01 | 1973-12-01 | 812.0 | 804.398560 |
34 | 1 | 1974-11-01 | 1973-12-01 | 773.0 | 775.329285 |
35 | 1 | 1974-12-01 | 1973-12-01 | 813.0 | 811.767639 |
unique_id | metric | DynamicTheta | |
---|---|---|---|
0 | 1 | mae | 8.182119 |
1 | 1 | mape | 0.009736 |
2 | 1 | mase | 0.367965 |
3 | 1 | rmse | 9.817624 |
4 | 1 | smape | 0.004874 |