Step-by-step guide on using the DynamicOptimizedTheta Model
with Statsforecast
.
StatsForecast
is a variation of the classic Theta model. It combines key features of
two other extensions: the Optimized Theta Model (OTM) and the
Dynamic Standard Theta Model (DSTM).
DOTM introduces two main improvements over the standard Theta model:
optimization of the theta parameters and dynamic updating of
model components over time.
decomposition_type
parameter. You can choose between: -
'multiplicative'
(default), which assumes that seasonal effects scale
with the level of the series, or - 'additive'
, which assumes that
seasonal effects remain constant in absolute magnitude.
The Dynamic Optimized Theta Model is the most flexible of the Theta
family and is particularly effective when forecasting series with
changing trends and seasonalities.
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 Optimized Theta Model(DOTM)
.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 Optimized 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.247106 |
1 | -75.757706 |
2 | 6.001494 |
… | … |
153 | -59.747044 |
154 | -91.901521 |
155 | -43.503294 |
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 | DynamicOptimizedTheta | |
---|---|---|---|
0 | 1 | 1975-01-01 | 839.259705 |
1 | 1 | 1975-02-01 | 801.399170 |
2 | 1 | 1975-03-01 | 895.189148 |
… | … | … | … |
9 | 1 | 1975-10-01 | 821.271240 |
10 | 1 | 1975-11-01 | 792.530518 |
11 | 1 | 1975-12-01 | 829.854553 |
unique_id | ds | y | DynamicOptimizedTheta | |
---|---|---|---|---|
0 | 1 | 1962-01-01 | 589.0 | 607.247131 |
1 | 1 | 1962-02-01 | 561.0 | 636.757690 |
2 | 1 | 1962-03-01 | 640.0 | 633.998535 |
3 | 1 | 1962-04-01 | 656.0 | 608.461243 |
4 | 1 | 1962-05-01 | 727.0 | 604.808899 |
unique_id | ds | DynamicOptimizedTheta | DynamicOptimizedTheta-lo-95 | DynamicOptimizedTheta-hi-95 | |
---|---|---|---|---|---|
0 | 1 | 1975-01-01 | 839.259705 | 741.952332 | 955.151001 |
1 | 1 | 1975-02-01 | 801.399170 | 641.867920 | 946.045776 |
2 | 1 | 1975-03-01 | 895.189148 | 707.189087 | 1066.356812 |
… | … | … | … | … | … |
9 | 1 | 1975-10-01 | 821.271240 | 546.081726 | 1088.193481 |
10 | 1 | 1975-11-01 | 792.530518 | 494.623718 | 1037.459839 |
11 | 1 | 1975-12-01 | 829.854553 | 519.661133 | 1108.213867 |
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 | DynamicOptimizedTheta | |
---|---|---|---|
0 | 1 | 1975-01-01 | 839.259705 |
1 | 1 | 1975-02-01 | 801.399170 |
2 | 1 | 1975-03-01 | 895.189148 |
… | … | … | … |
9 | 1 | 1975-10-01 | 821.271240 |
10 | 1 | 1975-11-01 | 792.530518 |
11 | 1 | 1975-12-01 | 829.854553 |
unique_id | ds | DynamicOptimizedTheta | DynamicOptimizedTheta-lo-80 | DynamicOptimizedTheta-hi-80 | DynamicOptimizedTheta-lo-95 | DynamicOptimizedTheta-hi-95 | |
---|---|---|---|---|---|---|---|
0 | 1 | 1975-01-01 | 839.259705 | 766.142090 | 928.025513 | 741.952332 | 955.151001 |
1 | 1 | 1975-02-01 | 801.399170 | 702.981262 | 899.884216 | 641.867920 | 946.045776 |
2 | 1 | 1975-03-01 | 895.189148 | 760.125916 | 1008.335022 | 707.189087 | 1066.356812 |
… | … | … | … | … | … | … | … |
9 | 1 | 1975-10-01 | 821.271240 | 617.391724 | 996.698364 | 546.081726 | 1088.193481 |
10 | 1 | 1975-11-01 | 792.530518 | 568.303162 | 975.070312 | 494.623718 | 1037.459839 |
11 | 1 | 1975-12-01 | 829.854553 | 598.098267 | 1035.476196 | 519.661133 | 1108.213867 |
(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:
series identifierds:
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 | DynamicOptimizedTheta | |
---|---|---|---|---|---|
0 | 1 | 1972-01-01 | 1971-12-01 | 826.0 | 828.692017 |
1 | 1 | 1972-02-01 | 1971-12-01 | 799.0 | 792.444092 |
2 | 1 | 1972-03-01 | 1971-12-01 | 890.0 | 883.122620 |
… | … | … | … | … | … |
33 | 1 | 1974-10-01 | 1973-12-01 | 812.0 | 810.304688 |
34 | 1 | 1974-11-01 | 1973-12-01 | 773.0 | 781.804688 |
35 | 1 | 1974-12-01 | 1973-12-01 | 813.0 | 818.811096 |
unique_id | metric | DynamicOptimizedTheta | |
---|---|---|---|
0 | 1 | mae | 6.861949 |
1 | 1 | mape | 0.008045 |
2 | 1 | mase | 0.308595 |
3 | 1 | rmse | 8.647459 |
4 | 1 | smape | 0.004010 |