The optimized Theta model is a time series forecasting method that is
based on the decomposition of the time series into three components:
trend, seasonality and noise. The model then forecasts the long-term
trend and seasonality, and uses the noise to adjust the short-term
forecasts. The optimized Theta model has been shown to be more accurate
than other time series forecasting methods, especially for time series
with complex trends and seasonality.The optimized Theta model was developed by Athanasios N. Antoniadis and
Nikolaos D. Tsonis in 2013. The model is based on the Theta forecasting
method, which was developed by George E. P. Box and Gwilym M. Jenkins in
1976. Theta method is a time series forecasting method that is based on
the decomposition of the time series into three components: trend,
seasonality, and noise. The Theta model then forecasts the long-term
trend and seasonality, and uses the noise to adjust the short-term
forecasts.The Theta Optimized model improves on the Theta method by using an
optimization algorithm to find the best parameters for the model. The
optimization algorithm is based on the Akaike loss function (AIC), which
is a measure of the goodness of fit of a model to the data. The
optimization algorithm looks for the parameters that minimize the AIC
function.The optimized Theta model has been shown to be more accurate than other
time series forecasting methods, especially for time series with complex
trends and seasonality. The model has been used to forecast a variety of
time series, including sales, production, prices, and weather.Below are some of the benefits of the optimized Theta model:
It is more accurate than other time series forecasting methods.
It’s easy to use.
Can be used to forecast a variety of time series.
It is flexible and can be adapted to different scenarios.
If you are looking for an easy-to-use and accurate time series
forecasting method, the Optimized Theta model is a good choice.The optimized Theta model can be applied in a variety of areas,
including:
Sales: The optimized Theta model can be used to forecast sales
of products or services. This can help companies make decisions
about production, inventory, and marketing.
Production: The optimized Theta model can be used to forecast
the production of goods or services. This can help companies ensure
they have the capacity to meet demand and avoid overproduction.
Prices: The optimized Theta model can be used to forecast the
prices of goods or services. This can help companies make decisions
about pricing and marketing strategy.
Weather: The optimized Theta model can be used to forecast the
weather. This can help companies make decisions about agricultural
production, travel planning and risk management.
Other: The optimized Theta model can also be used to forecast
other types of time series, including traffic, energy demand, and
population.
The Optimized Theta model is a powerful tool that can be used to improve
the accuracy of time series forecasts. It is easy to use and can be
applied to a variety of areas. If you are looking for a tool to improve
your time series forecasts, the Optimized Theta model is a good choice.
Assume that either the time series Y1,⋯Yn is non-seasonal or
it has been seasonally adjusted using the multiplicative classical
decomposition approach.Let Xt be the linear combination of two theta lines,Xt=ωZt(θ1)+(1−ω)Zt(θ2)(1)where ω∈[0,1] is the weight parameter. Assuming that
θ1<1 and θ2≥1, the weight ω can be derived
asω:=ω(θ1,θ2)=θ2−θ1θ2−1(2)It is straightforward to see from Eqs. (1), (2) that
Xt=Yt,t=1,⋯n i.e., the weights are calculated properly in
such a way that Eq. (1) reproduces the original series.Theorem 1: Let θ1<1 and θ2≥1. We will prove
that
the linear system given by Xt=Yt for all t=1,⋯,n, where
Xt is given by Eq.(4), has the single solution
ω=(θ2−1)/(θ2−θ1)
the error of choosing a non-optimal weight
ωδ=ω+δ is proportional to the error for
a simple linear regression model.
In Theorem 1 , we prove that the solution is unique and that the error
from not choosing the optimal weights (ω and 1−ω) s
proportional to the error of a linear regression model. As a
consequence, the STheta method is given simply by setting θ1=0
and θ2=2 while from Eq. (2) we get ω=0.5. Thus, Eqs. (1),
(2) allow us to construct a generalisation of the Theta model that
maintains the re-composition propriety of the original time series for
any theta lines Zt(θ1) and Zt(θ2).In order to maintain the modelling of the long-term component and retain
a fair comparison with the STheta method, in this work we fix
θ1=0 and focus on the optimisation of the short-term component,
θ2=0 with θ≥1. Thus, θ is the only parameter
that requires estimation so far. The theta decomposition is now given byYt=(1−θ1)(An+Bnt)+θ1Zt(θ),t=1,⋯,nThe h -step-ahead forecasts calculated at origin are given byY^n+h∣n=(1−θ1)[An+Bn(n+h)]+θ1Z~n+h∣n(θ)(3)where
Z~n+h∣n(θ)=Z~n+1∣n(θ)=α∑i=0n−1(1−α)iZn−i(θ)+(1−α)nℓ0∗
is the extrapolation of Zt(θ) by an SES model with
ℓ0∗∈R as the initial level parameter and
α∈(0,1) as the smoothing parameter. Note that for θ=2
Eq. (3) corresponds to Step 4 of the STheta algorithm. After some
algebra, we can writeZ~n+1∣n(θ)=θℓn+(1−θ){An[1−(1−α)n]+Bn[n+(1−α1)[1−(1−α)n]]}(4)where ℓt=αYt+(1−α)ℓt−1 for t=1,⋯,n
and ℓ0=ℓ0∗/θ.In the light of Eqs. (3), (4), we suggest four stochastic approaches.
These approaches differ due to the parameter θ which may be
either fixed at two or optimised, and the coefficients An and
Bn, which can be either fixed or dynamic functions. To
formulate the state space models, it is helpful to adopt μt as
the one-step-ahead forecast at origin t−1 and εt as the
respective additive error, i.e., εt=Yt−μt if
μt=Y^t∣t−1. We assume {εt} to be a
Gaussian white noise process with mean zero and variance σ2.
Let An and Bn be fixed coefficients for all
t=1,⋯,n so that Eqs. (3), (4) configure the state space model
given byYt=μt+εt(5)μt=ℓt−1+(1−θ1){(1−α)t−1An+[α1−(1−α)tBn](6)ℓt=αYt+(1−α)ℓt−1(7)with parameters ℓ0∈R, α∈(0,1) and
θ∈[1,∞) . The parameter θ is to be estimated
along with α and ℓ0 We call this the optimised Theta
model (OTM).The h-step-ahead forecast at origin n is given byY^n+h∣n=E[Yn+h∣Y1,⋯,Yn]=ℓn+(1−θ1){(1−α)nAn+[(h−1)+α1−(1−α)n+1]Bn}which is equivalent to Eq. (3). The conditional variance
Var[Yn+h∣Y1,⋯,Yn]=[1+(h−1)α2]σ2 can be
computed easily from the state space model. Thus, the (1−α)%
prediction interval for Yn+h is given by
Y^n+h∣n±q1−α/2[1+(h−1)α2]σ2For θ=2 OTM reproduces the forecasts of the STheta method;
hereafter, we will refer to this particular case as the standard Theta
model (STM).Theorem 2: The SES-d (ℓ0∗∗,α,b) model, where
ℓ0∗∗∈R,α∈(0,1) and b∈R
is equivalent to OTM(ℓ0,α,θ) where
ℓ0∈R and θ≥1, ifℓ0∗∗=ℓ0+(1−θ1)Anandb=(1−θ1)BnIn Theorem 2, we show that OTM is mathematically equivalent to the SES-d
model. As a corollary of Theorem 2, STM is mathematically equivalent to
SES-d with b=21Bn. Therefore, for θ=2 the
corollary also re-confirms the H&B result on the relationship between
STheta and the SES-d model.
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
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+noise
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)=Level∗Trend∗seasonality∗Noise
Let’s divide our data into sets 1. Data to train our
Optimized Theta model. 2. Data to test our modelFor the test data we will use the last 12 months to test and evaluate
the performance of our 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 datahorizon = len(test) # number of predictionsmodels = [OptimizedTheta(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.
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().
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. (If you want to speed things up to a couple of seconds,
remove the AutoModels like ARIMA and Theta)
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.
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:
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.
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 partialimport utilsforecast.losses as uflfrom utilsforecast.evaluation import evaluate