The Theta method (Assimakopoulos & Nikolopoulos, 2000, hereafter
A&N)
is applied to non-seasonal or deseasonalised time series, where the
deseasonalisation is usually performed via the multiplicative classical
decomposition. The method decomposes the original time series into two
new lines through the so-called theta coefficients, denoted by
θ1 and θ2 for
θ1,θ2∈R, which are applied to the second
difference of the data. The second differences are reduced when
θ<1, resulting in a better approximation of the long-term
behaviour of the series (Assimakopoulos, 1995). If θ is equal
to zero, the new line is a straight line. When θ>1 the local
curvatures are increased, magnifying the short-term movements of the
time series (A&N). The new lines produced are called theta lines,
denoted here by Z(θ1) and Z(θ2). These
lines have the same mean value and slope as the original data, but the
local curvatures are either filtered out or enhanced, depending on the
value of the θ coefficient.In other words, the decomposition process has the advantage of
exploiting information in the data that usually cannot be captured and
modelled completely through the extrapolation of the original time
series. The theta lines can be regarded as new time series and are
extrapolated separately using an appropriate forecasting method. Once
the extrapolation of each theta line has been completed, recomposition
takes place through a combination scheme in order to calculate the point
forecasts of the original time series. Combining has long been
considered as a useful practice in the forecasting literature (for
example, Clemen, 1989, Makridakis and Winkler, 1983, Petropoulos et
al., 2014), and therefore its application to the Theta method is
expected to result in more accurate and robust forecasts.The Theta method is quite versatile in terms of choosing the number of
theta lines, the theta coefficients and the extrapolation methods, and
combining these to obtain robust forecasts. However, A&N proposed a
simplified version involving the use of only two theta lines with
prefixed θ coefficients that are extrapolated over time using a
linear regression (LR) model for the theta line with θ1=0 and
simple exponential smoothing (SES) for the theta line with
θ2=2. The final forecasts are produced by combining the
forecasts of the two theta lines with equal weights.The performance of the Theta method has also been confirmed by other
empirical studies (for example Nikolopoulos et al., 2012, Petropoulos
and Nikolopoulos, 2013). Moreover, Hyndman and Billah (2003), hereafter
H&B, showed that the simple exponential smoothing with drift model
(SES-d) is a statistical model for the simplified version of the Theta
method. More recently, Thomakos and Nikolopoulos (2014) provided
additional theoretical insights, while Thomakos and Nikolopoulos (2015)
derived new theoretical formulations for the application of the method
to multivariate time series, and investigated the conditions under which
the bivariate Theta method is expected to forecast better than the
univariate one. Despite these advances, we believe that the Theta method
deserves more attention from the forecasting community, given its
simplicity and superior forecasting performance.One key aspect of the Theta method is that, by definition, it is
dynamic. One can choose different theta lines and combine the produced
forecasts using either equal or unequal weights. However, AN limit this
important property by fixing the theta coefficients to have predefined
values.
Assimakopoulos and Nikolopoulo for standard theta model proposed the
Theta line as the solution of the equationD2ζt(θ)=θD2Yt,t=1,⋯,T(1)where Y1,⋯,YT represent the original time series data and
DXt=(Xt−Xt−1). The initial values ζ1 and ζ2 are
obtained by minimizing ∑i=1T[Yt−ζt(θ)]2.
However, the analytical solution of (1) is given byζt(θ)=θYt+(1−θ)(AT+BTt),t=1,⋯,T,(2)where AT and BT are the minimum square coefficients of a simple
linear regression over Y1,⋯,YT against 1,⋯,T which
are only dependent on the original data and given as followAT=T1i=1∑TYt−2T+1BT(3)BT=T2−16(T2t=1∑TtYt−TT+1t=1∑TYt(4)Theta lines can be understood as functions of the linear regression
model directly applied to the data from this perspective. Indeed, the
Theta method’s projections for h steps ahead are an ad hoc combination
(50 percent - 50 percent) of the linear extrapolations of ζ(0) and
ζ(2).
When θ<1 is applied to the second differences of the data,
the decomposition process is defined by a theta coefficient, which
reduces the second differences and improves the approximation of
series behavior.
If θ=0, the deconstructed line is turned into a constant
straight line. (see Fig)
If θ>1 then the short term movements of the analyzed series
show more local curvatures (see fig)
We will refer to the above setup as the standard Theta method. The steps
for building the theta method are as follows:
Deseasonalisation: Firstly, the time series data is tested for
statistically significant seasonal behaviour. A time series is
seasonal if
∣ρm∣>q1−2αT1+2∑i=1m−1ρi2where ρk denotes the lag k autocorrelation function, m is the number
of the periods within a seasonal cycle (for example, 12 for monthly
data), T is the sample size, q is the quantile function of the
standard normal distribution, and (1−a)% is the confidence level.
Assimakopoulos and Nikolopoulo [Standar Theta model] opted for a 90%
confidence level. If the time series is identified as seasonal, then it
is deseasonalised via the classical decomposition method, assuming the
seasonal component to have a multiplicative relationship.
Decomposition: The second step consits for the decomposition of
the seasonally adjusted time series into two Theta lines, the
linear regression line ζ(0) and the theta line ζ(2).
Extrapolation:ζ(2) is extrapolated using
simple exponential smoothing (SES), while ζ(0) is
extrapolated as a normal linear regression line.
Combination: the final forecast is a combination of the
forecasts of the two θ lines using equal weights.
Reseasonalisation: In the presence of seasonality in first step,
then the final forecasts are multiplied by the respective seasonal
indices.
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 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 = [Theta(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.
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