Time series
Last updated
Last updated
A time series is something that can be expressed through the following structural equation: Xt = Tt + St + Ct + Err Xt = the value at time t Tt = the trend; a long term general drection St = seasonal component; something that repeats at known intervals (i.e. annually, monthly, weekly patterns) Ct = cyclical component; a pattern that does not occurr at predictable time points (i.e. a bull market) Err = something extra, ideally random, that cannot be modeled (we can call if error term)
The composition of there elements is not necessarily additive. It can also be multiplicative: Xt = Tt * St * Ct * Err
Decomposition is easy to use in Python. All it takes is one line of code
#residuals should be plotted as a scatterplot though (it makes more sense that way)
The very basic approach to a time series problem is to apply exponential smoothing methods.
Exponential smoothing predicts the value at time t as a form of average of previous values, where the futher back in time the values, the smalled weight is applyed to them.
According to Forecasting: Principles and Practice (3rd ed), by Rob J Hyndman and George Athanasopoulos, here is a classification of popular exponential smoothing methods:
Exponential smoothing methods don't use probabilities to model a distribution of data.
The best explanation I've seen for the autoregressive model is in this video. - the second is the moving average part; the moving average is actually a weihgted sum of previous errors; q is the order of the MA component
A good explanation for the moving average model is in this video, from the same author as the video linked above. It also explains the disturbing name of 'moving average'.
Expects a stationary time series (test with the classic Augmented Dickey–Fuller before applying ARMA): -> does not allow for a trend in the data -> does not allow for seasonality in the data
ARIMA partially fixes this: it explicitly models the trend: ARIMA(p,d,q) p and q are same asfore d is the order of differentiation (to get rid of the trend)
In Python, one can use something like the auto_arima
method from the pmdarima
package. This would find the best values for p
, d
and q
.
Normally, when using this method, we should provide a range in which to look for the best values for each parameter.
How would we know which p
and q
values would make sense ? From the ACF (autocorrelation) and the PACF (partial autocorrelation) plots. Both of them are explained quite clearly in this video. A demonstration of how ACF translates into the order q
is also provided here.
SARIMA further integrates seasonality.
The next level is Prophet, developed by Facebook.
Prophet is based on an additive model where non-linear trends are fit with yearly, weekly, and daily seasonality, plus holiday effects. So, one of its greatest avantages is that it cah handle multiple seasonaliy ocmponents (unlike everything discussed before).
It works best with time series that have strong seasonal effects and several seasons of historical data.
Prophet is robust to missing data and shifts in the trend, and typically handles outliers well.
A very good resource for getting started with Prophet is this Notebook by Konrad Banakewicz. The same information is predented through a video workshop available on this link on YouTube.
Here is a passage extracted from Konrad's notebook, which articulates clearly the multi-seasonality advantange of Prophet: "When dealing with data in practical applications, it is frequently necessary to take into account multiple seasonal patterns occurring in parallel; a classic example would be data related to energy consumption: there are morning vs evening patterns (intraday), workdays vs weekend (weekly) and during the year (annual). Modeling them explicitly tends to be cumbersome (you need to add more equations in exponential smoothing or introduce dummies in ARIMA), which is one of the issues Prophet was designed to overcome."
Changes from Prophet:
Gradient Descent for optimisation via using PyTorch as the backend.
Modelling autocorrelation of time series using AR-Net
Modelling lagged regressors using a sepearate Feed-Forward Neural Network.
Configurable non-linear deep layers of the FFNNs.
Tuneable to specific forecast horizons (greater than 1).
Custom losses and metrics.
Components of the model:
trend - linear or piece-wise linear trend by using changepoints
seasonality - modelled through Fourrier terms
auto regression - Implementation of AR-NET
special events - e.g. holidays
future regressors - external variables whose future values are known; both special events and future regressors are modelled as covariates of the model
lagged regressors - through separate Feed-Forward Neural Networks.
Only univariate models at the moment. If predicting for multiple series, modeling must be done individually for each.
Example: Simple Exponential Smoothing (Brown method) , where
The out of sample forecast is given by: # which means it will be flat
ARMA(p,q): - the first part is the AR bit; the autoregression is a linear combination of laged values of the same time series; p is the order of the AR component.