Beat the Time Series Forecasting in a nutshell
By: Nhu Hoang
Tags: Time Series Forecasting, Data Science, Autocorrelation, Machine Learning, Arima

Beat the Time Series Forecasting in a nutshell

Reveal the proper forecasting method with Time series, denote characteristics of the Time series and forecast its future values using Retail sales data as example code Nhu Hoang Feb 6, 2020·6 min read

Source: ScienceSoft

1. Define Time Series

Time series is a sequence of value corresponding with time and this stands as the biggest point to differentiate the a Series with other Cross-sectional data.

Retail sales data, Daily temperature, production, demand, natural reserves are time series data because the current value depends on its historical values.

2. Constitute a Time Series

There are 4 components in Time Series: Level, Trend, Seasonality and Noise.

Level : The average value of the time series

: The average value of the time series Trend : The movement of the series values from 1 period to another period

: The movement of the series values from 1 period to another period Seasonality : The short-term cyclical behavior of the series that can be observed several times

: The short-term cyclical behavior of the series that can be observed several times Noise: The random variation that results from the measurement of error

It is not always the case that we are able to notice the first 3 elements from Noise because they are usually invisible if we don’t use some techniques to locate them.

To observe and identify the existence of these components, we can consider.

Plot the Time series (this is the best way to detect the characteristics of the series)

Zoom in a specify shorter period of time

Change scale of the series to observe the trend more clearly

Suppress seasonality: aggregate the time series to a bigger time scale (from hourly scale to daily scale, from monthly scale to yearly scale, etc.)

I used 3-year weekly sales of a Retail store as an illustration.

plt.plot("Date", "Weekly_Sales", data=Wal_sales);

plt.hlines(y=Wal_sales.Weekly_Sales.mean(), xmin=0,

xmax=len(Wal_sales), linestyles='dashed');

With this data, there are obviously 2 peaks which denotes quite a clear seasonality at the end of year (probably Christmas and New year period). There might be other seasonality but it is hard to observe it from the plot. Auto correlation can be used to confirm the seasonality.

3. Autocorrelation

Autocorrelation describes the connection between the value of time series and its neighbors. Thus, to compute Autocorrelation, we calculate the correlation of the series with its lagged versions.

Lag-n version is produced from the original data set by moving the series values forward n period. For example, lag-1 is moved forward 1 period, Lag-10 series is moved forward 10 periods.

By observing the correlation of the series and its lags, we can confirm the seasonality of the series.

Code to compute and plot Autocorrelation

Judging from the Autocorrelation plot above, there is a strong positive autocorrelation in lag-52 as well as lag-51. This implies a cyclical annual pattern at the end of the year .

. The second strong correlation is lag-1, which connotes as the second week of February, or Valentine period.

The autocorrelation reveals both Positive and Negative autocorrelation, which implies that the series does not move in the same direction but ups and downs.

I computed it from scratch but Autocorrelation can beplotted easily by using autocorrelation_plot function from pandas.plotting in Python or acf function from tseries package in R.

4. Forecasting Time series

There are several methods to forecast Time series.

Model-based method through multiple linear regression to explore the correlation of the series with other features. Alike other cross-sectional data, model-based method compute the dependence of the time series to other features, but in the other hand does not take into account the dependence between time series values among different periods.

to explore the correlation of the series with other features. Alike other cross-sectional data, model-based method compute the dependence of the time series to other features, but in the other hand does not take into account the dependence between time series values among different periods. Data-driven method in which the model learns the pattern from the data itself and estimate the next value of the time series in correspondence with its previous values. The data-driven method is important in time series given in the time series context, the values in adjoining period tend to be correlated with each other. Such correlation is denoted as Autocorrelation .

in which the model learns the pattern from the data itself and estimate the next value of the time series in correspondence with its previous values. The data-driven method is important in time series given in the time series context, the values in adjoining period tend to be correlated with each other. Such correlation is denoted as . Combining data-based methods by forecasting the future values of the series as well as the future values of residuals, and then combine the result of 2 forecasts together. The residual forecast acts as the correction for the first forecast.

by forecasting the future values of the series as well as the future values of residuals, and then combine the result of 2 forecasts together. The residual forecast acts as the correction for the first forecast. Ensembles method by averaging multiple methods to get the result.

In this article, I will present the Data-driven method.

Forecasting using Data-driven method:

ARIMA model is the most frequent choice to compute data-driven forecasting. You can find detail for ARIMA model in this post. Here I will apply the ARIMA to the data.

It is useful to try auto_arimafunction from pmdarima in Python or auto.arima function from forecast package in R.

There is one thing to note is that from the Autocorrelation above, there is a clear seasonality at lag 52 so we will need to include this into the ARIMA model.

Code to define the best ARIMA model based on AIC score

This function chose the lowest AIC-score model and embed it for further model usage.

Then, split train-test set, train the model and make prediction.

Code to split train-test sets, train and forecast a Time series data by ARIMA model

In overall, the model seem to work moderately on the data but there is still room to improve further.

The MAPE (mean absolute percentage error) score is 5.7%, which is not too high, not too low.

The ARIMA model seems to perform well in early predicted value and gets worse in later predicted values.

However, one question emerged, does this model truly capture the values of the time series data?

It is helpful to take a look at the Residual of the model (or the difference between predicted values and actual values). Examining the residuals of the forecasting model is suggested to evaluate whether the specified model has adequately captured the information of the data. This can be done through exploring the correlation of one period’s residual with other periods’ ones.

The Residuals of a good time series forecasting model have the following properties:

Residuals are uncorrelated

Residuals have zero or nearly-zero mean (which means the model is unbiased in any directions)

(which means the model is unbiased in any directions) Residuals should have normal distribution

Residuals should have constant variance

If the result is lack of any of the above attributes, the forecasting model can be further improved.

Let’s compute the Residuals Autocorrelation and judge the result.

Code to compute and plot Autocorrelation for Residuals

Residual mean: -6308833.905274585

Let’s judge the Autocorrelation of Residual based on the following criteria:

Residuals are uncorrelated: No . the Residual series is still observed some correlations with its lags.

. the Residual series is still observed some correlations with its lags. Residuals have zero or nearly-zero mean (which means the model is unbiased in any directions): the mean is -6308833.905274585 . So this criteria is not met.

. So this criteria is not met. Residuals should have normal distribution: Not quite a normal distribution

Residuals should have constant variance: No as consistent with mean does not equal to 0.

Hence, the forecasting model has a lot of rooms to improve further by finding the way to capture the correlation in the Residuals, adding the values that is currently staying in residuals to the prediction.

5. Data partitioning

One of the biggest characteristics of Time series distinguishing it with normal cross-sectional data is the dependence of the future values with their historical values. Therefore, the Data partitioning for Time series cannot be done randomly but instead, trim the series into 2 periods, the earlier to train set and the later to validation set.

The below code will help split the train-test sets with respect to time series structure.

Code to split train and test sets for time series data

Sales does not only correlated with its own past but also might be affected by other factors such as special occasions (i.e Holiday in this data set), weekday and weekend, etc… The method-driven models will be presented in the next article with feature extraction, feature selection.