Table of Contents

Introduction

The Dynamic Optimized Theta Model (DOTM) is a forecasting technique that is used to predict future values of a time series. It is a variant of the Theta method, which combines exponential smoothing and a linear trend to forecast future values.

The DOTM extends the Theta method by introducing a dynamic optimization process that selects the optimal smoothing parameters for the exponential smoothing component and the optimal weights for the linear trend component based on the historical data. This optimization process is performed iteratively using a genetic algorithm that searches for the combination of parameters that minimizes the forecast error.

The DOTM is designed to handle time series data that exhibit non-linear and non-stationary behavior over time. It is particularly useful for forecasting time series with complex patterns such as seasonality, trend, and cyclical fluctuations.

The DOTM has several advantages over other forecasting methods. First, it is a simple and easy-to-implement method that does not require extensive statistical knowledge. Second, it is a highly adaptable method that can be customized to fit a wide range of time series data. Third, it is a robust method that can handle missing data, outliers, and other anomalies in the time series.

Overall, the Dynamic Optimized Theta Model is a powerful forecasting technique that can be used to generate accurate and reliable predictions for a wide range of time series data.

Dynamic Optimized Theta Models (DOTM)

So far, we have set AnA_n and BnB_n as fixed coefficients for all tt. We will now consider these coefficients as dynamic functions; i.e., for updating the state tt to t+1t+1 we will only consider the prior information Y1,,YtY_1, \cdots, Y_t when computing AtA_t and BtB_t. Hence, We replace AnA_n and BnB_n in equations (3) and (4) of the notebook of the optimized theta model with AtA_t and BtB_t. Then, after applying the new Eq. (4) to the new Eq. (3) and rewriting the result at time tt with h=1h=1, we have

Y^t+1t=t+(11θ)((1α)tAt+[1(1α)t+1α]Bt) \begin{equation} \hat Y_{t+1|t}=\ell_{t} + \left(1 - \frac{1}{\theta} \right) \left( (1-\alpha)^t A_t + \left[ \frac{1 - ( 1 - \alpha)^{t+1}}{\alpha} \right] B_t \tag 1 \right) \end{equation}

Then, assuming additive one-step-ahead errors and rewriting Eqs. (3) (see AutoTheta Model), (1), we obtain

Yt=μt+εt \begin{equation} Y_t=\mu_t +\varepsilon_t \tag 2 \end{equation} μt=t1+(11θ)((1α)t1At1+(1(1α)tα)Bt1) \begin{equation} \mu_t=\ell_{t-1}+ \left(1-\frac{1}{\theta}\right) \left( \left(1-\alpha\right)^{t-1} A_{t-1} + \left(\frac{1-(1-\alpha)^{t}}{\alpha}\right) B_{t-1} \tag 3 \right) \end{equation} t=αYt+(1α)t1 \begin{equation} \ell_{t}=\alpha Y_t+ (1-\alpha) \ell_{t-1} \tag 4 \end{equation} At=Yˉtt+12Bt \begin{equation} A_t=\bar Y_t - \frac{t+1}{2} B_t \tag 5 \end{equation} Bt=1t+1((t2)Bt1+6t(YtYˉt1)) \begin{equation} B_t=\frac{1}{t+1} \left((t-2) B_{t-1} +\frac{6}{t} (Y_t - \bar Y_{t-1}) \right) \tag 6 \end{equation} Yˉt=1t((t1)Yˉt1+Yt) \begin{equation} \bar Y_t=\frac{1}{t} \left((t-1) \bar Y_{t-1} + Y_t \right) \tag 7 \end{equation}

for t=1,,nt=1, \cdots ,n. Eqs. (2), (3), (4), (5), (6), (7) configure a state space model with parameters 0R,α(0,1)\ell_{0} \in \mathbb{R}, \alpha \in (0,1), and θ[1,)\theta \in [1,\infty ). The initialisation of the states is performed assuming A0=B0=B1=Yˉ0=0A_0 =B_0=B_1=\bar Y_0 =0. From here on, we will refer to this model as the dynamic optimised Theta model (DOTM).

An important property of the DOTM is that when θ=1\theta=1, which implies that Zt(1)=YtZ_t(1)=Y_t, the forecasting vector given by Eq. (3)(See OTM) will be equal to Y^t+ht=Z~t+ht\hat Y_{t+h|t} = \tilde Z_{t+h|t}

Thus, when θ=1\theta=1, the DOTM is the SES method. When θ>1\theta>1, DOTM (as SES-d) acts as a extension of SES, by adding a long-term component.

The out-of-sample one-step-ahead forecasts produced by DOTM at origin are given by

Y^n+1n=E[Yn+1Y1,,Yn]=n+(11θ)((1α)nAn+(1(1α)n+1α)Bn) \begin{equation} \hat Y_{n+1|n}=E[Y_{n+1|Y_1, \cdots, Y_n} ]=\ell_{n} + \left(1-\frac{1}{\theta}\right) \left( (1-\alpha)^n A_n + \left(\frac{1-(1-\alpha)^{n+1}}{\alpha}\right) B_n \right) \tag 8 \end{equation}

for a horizon h2h \geq 2, the forecast Y^n+2n,,Y^n+hn\hat Y_{n+2|n}, \cdots , \hat Y_{n+h|n} are computed recursively using Eqs. (3), (4), (5), (6), (7), (8) by replacing the non-observed values Yn+1,,Yn+h1Y_{n+1}, \cdots , Y_{n+h-1} with their expected values Y^n+1n,,Y^n+h1n\hat Y_{n+1|n}, \cdots , \hat Y_{n+h-1|n}. The conditional variance Var[Yn+hY1,,Yn]Var[Y_{n+h}|Y_{1}, \cdots, Y_n ] is hard to write analytically. However, the variance and prediction intervals for Yn+hY_{n+h} can be estimated using the bootstrapping technique, where a (usually large) sample of possible values of Yn+hY_{n+h} is simulated from the estimated model.

Note that, in contrast to STheta, STM and OTM, the forecasts produced by DSTM and DOTM are not necessary linear. This is also a fundamental difference between DSTM/DOTM and SES-d: while the long-term trend () in SES-d is constant, this is not the case for DSTM/DOTM, for either the in-sample fit or the out-of-sample predictions.

Loading libraries and data

Tip

Statsforecast will be needed. To install, see instructions.

Next, we import plotting libraries and configure the plotting style.

import matplotlib.pyplot as plt
import seaborn as sns
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
plt.style.use('grayscale') # fivethirtyeight  grayscale  classic
plt.rcParams['lines.linewidth'] = 1.5
dark_style = {
    'figure.facecolor': '#008080',  # #212946
    'axes.facecolor': '#008080',
    'savefig.facecolor': '#008080',
    'axes.grid': True,
    'axes.grid.which': 'both',
    'axes.spines.left': False,
    'axes.spines.right': False,
    'axes.spines.top': False,
    'axes.spines.bottom': False,
    'grid.color': '#000000',  #2A3459
    'grid.linewidth': '1',
    'text.color': '0.9',
    'axes.labelcolor': '0.9',
    'xtick.color': '0.9',
    'ytick.color': '0.9',
    'font.size': 12 }
plt.rcParams.update(dark_style)


from pylab import rcParams
rcParams['figure.figsize'] = (18,7)

Read Data

import pandas as pd

df = pd.read_csv("https://raw.githubusercontent.com/Naren8520/Serie-de-tiempo-con-Machine-Learning/main/Data/milk_production.csv", usecols=[1,2])
df.head()
monthproduction
01962-01-01589
11962-02-01561
21962-03-01640
31962-04-01656
41962-05-01727

The input to StatsForecast is always a data frame in long format with three columns: unique_id, ds and y:

  • The unique_id (string, int or category) represents an identifier for the series.

  • The 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.

  • The y (numeric) represents the measurement we wish to forecast.

df["unique_id"]="1"
df.columns=["ds", "y", "unique_id"]
df.head()
dsyunique_id
01962-01-015891
11962-02-015611
21962-03-016401
31962-04-016561
41962-05-017271
print(df.dtypes)
ds           object
y             int64
unique_id    object
dtype: object

We can see that our time variable (ds) is in an object format, we need to convert to a date format

df["ds"] = pd.to_datetime(df["ds"])

Explore Data with the plot method

Plot some series using the plot method from the StatsForecast class. This method prints a random series from the dataset and is useful for basic EDA.

from statsforecast import StatsForecast

StatsForecast.plot(df)

Autocorrelation plots

fig, axs = plt.subplots(nrows=1, ncols=2)

plot_acf(df["y"],  lags=30, ax=axs[0],color="fuchsia")
axs[0].set_title("Autocorrelation");

plot_pacf(df["y"],  lags=30, ax=axs[1],color="lime")
axs[1].set_title('Partial Autocorrelation')

plt.show();

Decomposition of the time series

How to decompose a time series and why?

In time series analysis to forecast new values, it is very important to know past data. More formally, we can say that it is very important to know the patterns that values follow over time. There can be many reasons that cause our forecast values to fall in the wrong direction. Basically, a time series consists of four components. The variation of those components causes the change in the pattern of the time series. These components are:

  • Level: This is the primary value that averages over time.
  • Trend: The trend is the value that causes increasing or decreasing patterns in a time series.
  • Seasonality: This is a cyclical event that occurs in a time series for a short time and causes short-term increasing or decreasing patterns in a time series.
  • Residual/Noise: These are the random variations in the time series.

Combining these components over time leads to the formation of a time series. Most time series consist of level and noise/residual and trend or seasonality are optional values.

If seasonality and trend are part of the time series, then there will be effects on the forecast value. As the pattern of the forecasted time series may be different from the previous time series.

The combination of the components in time series can be of two types: * Additive * Multiplicative

Additive time series

If the components of the time series are added to make the time series. Then the time series is called the additive time series. By visualization, we can say that the time series is additive if the increasing or decreasing pattern of the time series is similar throughout the series. The mathematical function of any additive time series can be represented by: y(t)=level+Trend+seasonality+noisey(t) = level + Trend + seasonality + noise

Multiplicative time series

If the components of the time series are multiplicative together, then the time series is called a multiplicative time series. For visualization, if the time series is having exponential growth or decline with time, then the time series can be considered as the multiplicative time series. The mathematical function of the multiplicative time series can be represented as.

y(t)=LevelTrendseasonalityNoisey(t) = Level * Trend * seasonality * Noise

Additive

from statsmodels.tsa.seasonal import seasonal_decompose 
a = seasonal_decompose(df["y"], model = "additive", period=12)
a.plot();

Multiplicative

from statsmodels.tsa.seasonal import seasonal_decompose 
a = seasonal_decompose(df["y"], model = "Multiplicative", period=12)
a.plot();

Split the data into training and testing

Let’s divide our data into sets

  1. Data to train our Dynamic Optimized Theta Model(DOTM).
  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.

train = df[df.ds<='1974-12-01'] 
test = df[df.ds>'1974-12-01']
train.shape, test.shape
((156, 3), (12, 3))

Now let’s plot the training data and the test data.

sns.lineplot(train,x="ds", y="y", label="Train", linestyle="--")
sns.lineplot(test, x="ds", y="y", label="Test")
plt.title("Monthly Milk Production");
plt.show()

Implementation of DynamicOptimizedTheta with StatsForecast

Load libraries

from statsforecast import StatsForecast
from statsforecast.models import DynamicOptimizedTheta

Instantiating Model

Import and instantiate the models. Setting the argument is sometimes tricky. This article on Seasonal periods by the master, Rob Hyndmann, can be useful for season_length.

season_length = 12 # Monthly data 
horizon = len(test) # number of predictions

# We call the model that we are going to use
models = [DynamicOptimizedTheta(season_length=season_length, 
                decomposition_type="additive")] # multiplicative   additive

We fit the models by instantiating a new StatsForecast object with the following parameters:

models: a list of models. Select the models you want from models and import them.

  • 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.

Any settings are passed into the constructor. Then you call its fit method and pass in the historical data frame.

sf = StatsForecast(models=models, freq='MS')

Fit the Model

sf.fit(df=train)
StatsForecast(models=[DynamicOptimizedTheta])

Let’s see the results of our Dynamic Optimized Theta Model. We can observe it with the following instruction:

result=sf.fitted_[0,0].model_
print(result.keys())
print(result['fit'])
dict_keys(['mse', 'amse', 'fit', 'residuals', 'm', 'states', 'par', 'n', 'modeltype', 'mean_y', 'decompose', 'decomposition_type', 'seas_forecast', 'fitted'])
results(x=array([250.83207246,   0.75624902,   4.67964777]), fn=10.697567725248804, nit=55, simplex=array([[237.42075735,   0.75306547,   4.46023813],
       [250.83207246,   0.75624902,   4.67964777],
       [257.164453  ,   0.75229688,   4.42377059],
       [256.90854919,   0.75757957,   4.43171897]]))

Let us now visualize the residuals of our models.

As we can see, the result obtained above has an output in a dictionary, to extract each element from the dictionary we are going to use the .get() function to extract the element and then we are going to save it in a pd.DataFrame().

residual=pd.DataFrame(result.get("residuals"), columns=["residual Model"])
residual
residual Model
0-18.247131
1-88.625732
22.864929
153-59.747070
154-91.901550
155-43.503296
import scipy.stats as stats

fig, axs = plt.subplots(nrows=2, ncols=2)

residual.plot(ax=axs[0,0])
axs[0,0].set_title("Residuals");

sns.distplot(residual, ax=axs[0,1]);
axs[0,1].set_title("Density plot - Residual");

stats.probplot(residual["residual Model"], dist="norm", plot=axs[1,0])
axs[1,0].set_title('Plot Q-Q')

plot_acf(residual,  lags=35, ax=axs[1,1],color="fuchsia")
axs[1,1].set_title("Autocorrelation");

plt.show();

Forecast Method

If you want to gain speed in productive settings where you have multiple series or models we recommend using the 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.

The forecast object here is a new data frame that includes a column with the name of the model and the y hat values, as well as columns for the uncertainty intervals. Depending on your computer, this step should take around 1min.

# Prediction
Y_hat = sf.forecast(df=train, h=horizon, fitted=True)
Y_hat
unique_iddsDynamicOptimizedTheta
011975-01-01839.259705
111975-02-01801.399170
211975-03-01895.189148
911975-10-01821.271179
1011975-11-01792.530457
1111975-12-01829.854492
values=sf.forecast_fitted_values()
values.head()
unique_iddsyDynamicOptimizedTheta
011962-01-01589.0607.247131
111962-02-01561.0649.625732
211962-03-01640.0637.135071
311962-04-01656.0609.225830
411962-05-01727.0604.995300
StatsForecast.plot(values)

Adding 95% confidence interval with the forecast method

sf.forecast(df=train, h=horizon, level=[95])
unique_iddsDynamicOptimizedThetaDynamicOptimizedTheta-lo-95DynamicOptimizedTheta-hi-95
011975-01-01839.259705741.963501955.137634
111975-02-01801.399170641.886292946.029114
211975-03-01895.189148707.2107541066.337280
911975-10-01821.271179546.1135861088.162842
1011975-11-01792.530457494.6581731037.432129
1111975-12-01829.854492519.6970211108.182007

Predict method with confidence interval

To generate forecasts use the predict method.

The predict method takes two arguments: forecasts the next 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.

The forecast object here is a new data frame that includes a column with the name of the model and the y hat values, as well as columns for the uncertainty intervals.

This step should take less than 1 second.

sf.predict(h=horizon)
unique_iddsDynamicOptimizedTheta
011975-01-01839.259705
111975-02-01801.399170
211975-03-01895.189148
911975-10-01821.271179
1011975-11-01792.530457
1111975-12-01829.854492
forecast_df = sf.predict(h=horizon, level=[80,95]) 
forecast_df
unique_iddsDynamicOptimizedThetaDynamicOptimizedTheta-lo-80DynamicOptimizedTheta-hi-80DynamicOptimizedTheta-lo-95DynamicOptimizedTheta-hi-95
011975-01-01839.259705766.150513928.015259741.963501955.137634
111975-02-01801.399170702.992554899.872864641.886292946.029114
211975-03-01895.189148760.1414791008.321960707.2107541066.337280
911975-10-01821.271179617.415405996.678406546.1135861088.162842
1011975-11-01792.530457568.329285975.049255494.6581731037.432129
1111975-12-01829.854492598.1251831035.452637519.6970211108.182007
sf.plot(train, test.merge(forecast_df), level=[80, 95])

Cross-validation

In previous steps, we’ve taken our historical data to predict the future. However, to asses its accuracy we would also like to know how the model would have performed in the past. To assess the accuracy and robustness of your models on your data perform Cross-Validation.

With time series data, Cross Validation is done by defining a sliding window across the historical data and predicting the period following it. This form of cross-validation allows us to arrive at a better estimation of our model’s predictive abilities across a wider range of temporal instances while also keeping the data in the training set contiguous as is required by our models.

The following graph depicts such a Cross Validation Strategy:

Perform time series cross-validation

Cross-validation of time series models is considered a best practice but most implementations are very slow. The statsforecast library implements cross-validation as a distributed operation, making the process less time-consuming to perform. If you have big datasets you can also perform Cross Validation in a distributed cluster using Ray, Dask or Spark.

In this case, we want to evaluate the performance of each model for the last 5 months (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.

crossvalidation_df = sf.cross_validation(df=train,
                                         h=horizon,
                                         step_size=12,
                                         n_windows=3)

The crossvaldation_df object is a new data frame that includes the following columns:

  • unique_id: series identifier
  • ds: datestamp or temporal index
  • cutoff: the last datestamp or temporal index for the n_windows.
  • y: true value
  • "model": columns with the model’s name and fitted value.
crossvalidation_df
unique_iddscutoffyDynamicOptimizedTheta
011972-01-011971-12-01826.0828.692017
111972-02-011971-12-01799.0792.444092
211972-03-011971-12-01890.0883.122620
3311974-10-011973-12-01812.0810.342834
3411974-11-011973-12-01773.0781.845703
3511974-12-011973-12-01813.0818.855103

Model Evaluation

Now we are going to evaluate our model with the results of the predictions, we will use different types of metrics MAE, MAPE, MASE, RMSE, SMAPE to evaluate the accuracy.

from functools import partial

import utilsforecast.losses as ufl
from utilsforecast.evaluation import evaluate
evaluate(
    test.merge(Y_hat),
    metrics=[ufl.mae, ufl.mape, partial(ufl.mase, seasonality=season_length), ufl.rmse, ufl.smape],
    train_df=train,
)
unique_idmetricDynamicOptimizedTheta
01mae6.861954
11mape0.008045
21mase0.308595
31rmse8.647457
41smape0.004010

Acknowledgements

We would like to thank Naren Castellon for writing this tutorial.

References

  1. Kostas I. Nikolopoulos, Dimitrios D. Thomakos. Forecasting with the Theta Method-Theory and Applications. 2019 John Wiley & Sons Ltd.
  2. Jose A. Fiorucci, Tiago R. Pellegrini, Francisco Louzada, Fotios Petropoulos, Anne B. Koehler (2016). “Models for optimising the theta method and their relationship to state space models”. International Journal of Forecasting.
  3. Nixtla Parameters.
  4. Pandas available frequencies.
  5. Rob J. Hyndman and George Athanasopoulos (2018). “Forecasting principles and practice, Time series cross-validation”..
  6. Seasonal periods- Rob J Hyndman.