Step-by-step guide on using the AutoCES Model
with Statsforecast
.
Tip Statsforecast will be needed. To install, see instructions.Next, we import plotting libraries and configure the plotting style.
year | value | |
---|---|---|
0 | 1960-01-01 | 69.123902 |
1 | 1961-01-01 | 69.760244 |
2 | 1962-01-01 | 69.149756 |
3 | 1963-01-01 | 69.248049 |
4 | 1964-01-01 | 70.311707 |
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 | 1960-01-01 | 69.123902 | 1 |
1 | 1961-01-01 | 69.760244 | 1 |
2 | 1962-01-01 | 69.149756 | 1 |
3 | 1963-01-01 | 69.248049 | 1 |
4 | 1964-01-01 | 70.311707 | 1 |
.tail()
function.
ds
from object
type to datetime.
season_length
Note
Automatically selects the best Complex Exponential Smoothing model
using an information criterion. Default is Akaike Information Criterion
(AICc), while particular models are estimated using maximum likelihood.
The state-space equations can be determined based on their simple,
parial, optimized or ommited components. The model string
parameter defines the kind of CES model: for simple CES (withous
seasonality), for simple seasonality (lagged CES), for partial
seasonality (without complex part), for full seasonality (lagged CES
with real and complex seasonal parts).
If the component is selected as , it operates as a placeholder to ask
the AutoCES model to figure out the best parameter.
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.
.get()
function to extract the element and then we are going to save
it in a pd.DataFrame()
.
residual Model | |
---|---|
0 | -0.727729 |
1 | 0.144552 |
2 | -0.762086 |
… | … |
51 | -0.073258 |
52 | -0.234578 |
53 | 0.109990 |
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.
ARIMA
and
Theta
)
unique_id | ds | CES | |
---|---|---|---|
0 | 1 | 2014-01-01 | 82.906075 |
1 | 1 | 2015-01-01 | 83.166687 |
2 | 1 | 2016-01-01 | 83.424744 |
3 | 1 | 2017-01-01 | 83.685760 |
4 | 1 | 2018-01-01 | 83.946213 |
5 | 1 | 2019-01-01 | 84.208359 |
unique_id | ds | y | CES | |
---|---|---|---|---|
0 | 1 | 1960-01-01 | 69.123902 | 69.851631 |
1 | 1 | 1961-01-01 | 69.760244 | 69.615692 |
2 | 1 | 1962-01-01 | 69.149756 | 69.911842 |
3 | 1 | 1963-01-01 | 69.248049 | 69.657822 |
4 | 1 | 1964-01-01 | 70.311707 | 69.601196 |
unique_id | ds | CES | CES-lo-95 | CES-hi-95 | |
---|---|---|---|---|---|
0 | 1 | 2014-01-01 | 82.906075 | 82.342483 | 83.454016 |
1 | 1 | 2015-01-01 | 83.166687 | 82.604029 | 83.717271 |
2 | 1 | 2016-01-01 | 83.424744 | 82.858573 | 83.975870 |
3 | 1 | 2017-01-01 | 83.685760 | 83.118946 | 84.239582 |
4 | 1 | 2018-01-01 | 83.946213 | 83.376905 | 84.501133 |
5 | 1 | 2019-01-01 | 84.208359 | 83.637738 | 84.765408 |
ds | y | unique_id | CES | |
---|---|---|---|---|
0 | 2014-01-01 | 83.090244 | 1 | 82.906075 |
1 | 2015-01-01 | 82.543902 | 1 | 83.166687 |
2 | 2016-01-01 | 83.243902 | 1 | 83.424744 |
3 | 2017-01-01 | 82.946341 | 1 | 83.685760 |
4 | 2018-01-01 | 83.346341 | 1 | 83.946213 |
5 | 2019-01-01 | 83.197561 | 1 | 84.208359 |
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 | CES | |
---|---|---|---|
0 | 1 | 2014-01-01 | 82.906075 |
1 | 1 | 2015-01-01 | 83.166687 |
2 | 1 | 2016-01-01 | 83.424744 |
3 | 1 | 2017-01-01 | 83.685760 |
4 | 1 | 2018-01-01 | 83.946213 |
5 | 1 | 2019-01-01 | 84.208359 |
unique_id | ds | CES | CES-lo-95 | CES-hi-95 | |
---|---|---|---|---|---|
0 | 1 | 2014-01-01 | 82.906075 | 82.342483 | 83.454016 |
1 | 1 | 2015-01-01 | 83.166687 | 82.604029 | 83.717271 |
2 | 1 | 2016-01-01 | 83.424744 | 82.858573 | 83.975870 |
3 | 1 | 2017-01-01 | 83.685760 | 83.118946 | 84.239582 |
4 | 1 | 2018-01-01 | 83.946213 | 83.376905 | 84.501133 |
5 | 1 | 2019-01-01 | 84.208359 | 83.637738 | 84.765408 |
(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 | CES | |
---|---|---|---|---|---|
0 | 1 | 1984-01-01 | 1983-01-01 | 75.389512 | 74.952705 |
1 | 1 | 1985-01-01 | 1983-01-01 | 75.470732 | 75.161736 |
2 | 1 | 1986-01-01 | 1983-01-01 | 75.770732 | 75.377945 |
3 | 1 | 1987-01-01 | 1983-01-01 | 76.219512 | 75.590378 |
4 | 1 | 1988-01-01 | 1983-01-01 | 76.370732 | 75.806343 |
unique_id | metric | CES | |
---|---|---|---|
0 | 1 | mae | 0.556314 |
1 | 1 | mape | 0.006699 |
2 | 1 | mase | 1.770512 |
3 | 1 | rmse | 0.630183 |
4 | 1 | smape | 0.003336 |