Factorialize a Number using ES6 Syntax

Description of the Algorithm. “Factorialize a Number using ES6 Syntax” is published by Tatenda Marufu.

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Time Series Forecasting using TBATS Model

Multi seasonalities Time series forecasting model with python and R codes

Time-series forecasting refers to the use of a model to predict future values based on previously observed values. Many researchers are familiar with time-series forecasting yet they struggle with specific types of time-series data. One such type of data is data with seasonality. There can be many types of seasonalities present (e.g., time of day, daily, weekly, monthly, yearly).

TBATS is a forecasting method to model time series data. The main aim of this is to forecast time series with complex seasonal patterns using exponential smoothing

TBATS: Trigonometric seasonality, Box-Cox transformation, ARMA errors, Trend and Seasonal components.

In order to start forecasting, we need to first install tbats package. The following steps should be implemented to create the model:

TBATS will consider various alternatives and fit quite a few models. It will consider models:

In particular, auto ARIMA is used to decide if residuals need modeling and what p and q values are suitable.

For a new implementation of TBATS in Python, available at GitHub. In the rest of the article, we will provide the example usage and compare the performance of this implementation with the other methods.

From PyPI:

Import via:

Reading model details

BATS and TBATS try the multitude of models under the hood and may appear slow when fitting too long time series. In order to speed it up, you can start with constrained model search space. It is recommended to run it without Box-Cox transformation and ARMA errors modeling that are the slowest model elements:

In some environment configurations parallel computation of models freezes. The reason for this is unclear yet. If the process appears to be stuck you can try running it on a single core:

R forecast package comparison tests. Those DO NOT RUN with the default test command, you need the R forecast package installed:

Python implementation is meant to be as much as possible equivalent to R implementation in the forecast package.

Fig 1: Daily sales of Item 1 at Store 1

Sales data contains daily observations. It exhibits weekly and yearly seasonal patterns. It means we are dealing with time series containing multiple seasonal effects. One of those seasonalities is long and contains 365 (366 for leap year) observations. This is something TBATS was designed for.

In order to start forecasting, we need to install tbats package and fit the model. The only thing that we have to manually provide to the model are season lengths:

You may have noticed that the yearly season length is not an integer. It equals 365.25 to account for leap years, a feature TBATS is able to handle.

TBATS seems to have done a pretty good job in modeling both seasonal effects:

Fig 2: 3 last years of sales data. TBATS is modeling yearly seasonal effects.

Fig 3: 12 weeks of data. The weekly seasonal effect is also being modeled by TBATS.

If we take a look under the hood and review model parameters we will discover that 3 seasonal harmonics are used to model the weekly pattern and 11 harmonics are used to model the yearly pattern. TBATS has chosen to use Box-Cox transformation with lambda of 0.234955. The trend is not being modeled and ARMA is not used to model residuals as p, q is 0.

Auto arima has chosen SARIMA(0, 1, 1)x(1, 0, 1, 7) model. As the expected yearly pattern is not modeled (see Fig 4).

Fig 4: SARIMA models only weekly patterns. Compare to Fig 2.

We will keep modeling the weekly pattern with the seasonal part of SARIMA. For the yearly seasonal pattern, we will use the above-mentioned trick. I have compared multiple choices for the number of Fourier terms and 2 provides the most accurate forecasts. Therefore we shall use 2 Fourier terms as exogenous variables.

With the help of Fourier terms, SARIMAX is able to model both seasonal patterns (Fig 5).

Fig 5: SARIMAX with Fourier terms models both weekly and yearly patterns

So far we have been only looking at plots. Let us compare the models’ performance using the 365-days-ahead forecast. We will use Mean Absolute Error as our metric:

As expected SARIMA provides a poor model as it is unable to model yearly seasonality. TBATS and SARIMAX with Fourier Terms provide much better models.

Many time series exhibit complex and multiple seasonal patterns (e.g., hourly data that contains a daily pattern, weekly pattern, and an annual pattern). The most popular models (e.g. ARIMA and exponential smoothing) can only account for one seasonality.

TBATS model has the capability to deal with complex seasonalities (e.g., non-integer seasonality, non-nested seasonality, and large-period seasonality) with no seasonality constraints, making it possible to create detailed, long-term forecasts.

Unfortunately BATS and TBATS capabilities do not come for free. The method is very generic. Under the hood, it builds and evaluates many model candidates. This results in the slowness of the computation. It may be crucial when one needs to train models for lots of parallel time series.

TBATS model (Exponential smoothing state-space model with Box-Cox transformation, ARMA errors, Trend and Seasonal components)

Fits a TBATS model applied to y, as described in De Livera, Hyndman & Snyder (2011). Parallel processing is used by default to speed up the computations.

TBATS makes it easy for users to handle data with multiple seasonal patterns. This model is preferable when the seasonality changes over time.

Add a comment

Related posts:

Server Boots into Black Screen after Upgrading to Ubuntu 18.04

Recently I upgraded a few servers from Ubuntu 16.04 LTS to the latest version of 18.04 LTS. However, I ran into a strange issue that the system will always boot into black screen immediately after…

Fixed to Growth Mindset Transition.

The common belief is that, a growth mindset helps a person to learn new capbilities and grow. But, there is a twist to this, learning comes later-It is "Unlearning" that comes first!

ICO insights. Part 1. Money compensation for the team and board

Being involved in the blockchain and crypto world as a practitioner and speaker since 2016, I was refusing to join any token-based project for a long period of time because of the following reasons…