Time Series Forecasting: Tools and Techniques. Part 1
By: Tufail Waris
Tags: Time Series Forecasting, Arima, Time Series Analysis

In this article, I will be discussing various methods used for time series forecasting. In the beginning, I will be touching the basic terminologies associated with time-series data and forecasts. Then I will be discussing the methods used for time-series forecasting. Going ahead, I will be discussing each and every method in detail one by one.

Terminologies associated with Time-series data.

Time-series: Time Series data is a series of data points indexed in time order. The goal of time series forecasting is to predict the future values based on past behaviour of data points. The time-series with one y variable is called univariate. And the time series with more than one y variable is called multivariate.

Trend: The increasing or decreasing value on the series. (overall in the whole series). A trend exists when there is a long term increase or decrease in the data.

Seasonality: The repeating short term cycle in the series. A seasonal pattern occurs when a time series is affected by seasonal factors such as the time of the year or the day of the week.

Error or Noise: The random variation in the series which is not explained by Trend and seasonality.

For example, if you look at the below graph, the first plot represents the actual data plotted. There are three components of observed data, namely Trend, seasonal and residual (error). The trend shows that there is an overall increasing y variable during the observed period. The seasonal component shows that within each year, the data is having some seasonal behaviour i.e. sometimes it is reaching a peak and sometimes it is going down. The residual component shows the noise in the data which is random behaviour not captured in trend and seasonal components.

Libraries used for time-series forecasting

Some of the main methods/libraries used for time-series forecasting are listed below.

ARIMA SARIMA (belongs to ARIMA family) Prophet (by Facebook) Vector autoregression (VAR) and VARMA LSTM (RNN)

Let’s begin our discussion with ARIMA!

ARIMA: AutoRegressive integrated moving average.

ARIMA has three parts/components. (AR+I+MA=ARIMA).

AR (autoregressive): A model that uses the dependent relationship between an observation and some number of lagged observations.

I (integrated): This is basically done to make the data stationary. If the data is already stationary then this component is zero.

MA (Moving averages): It uses dependency between an observation and a residual error from a moving average model applied to lagged observation.

Here, ‘AR’ is denoted by ‘p’, ‘I’ is denoted by ‘d’ and ‘MA’ is denoted by ‘q’.

Note: ARIMA can be applied only for non-seasonal data. For seasonal data, we use SARIMA which we will discuss later.

Steps to apply ARIMA:

Plot the data: To get an idea and overview of the data.

Run an ETS decomposition: This is done to see the Trend, seasonal and error part of the data. See how much is the contribution of the seasonal component to the overall data. Accordingly, we decide if the data is seasonal or not. However, there are other methods to check if the data is seasonal or not, but ETS is the simplest way to check it.

Code: from statsmodels.tsa.seasonal import seasonal_decompose # ets = seasonal_decompose(df[‘A’], model=’additive’)

ets.plot();

Determine the order of ARIMA (p,d,q) using ‘pmdarima’: pmdarima is a library which gives you the value of p,d,q directly so that you can use those values to fit in the actual ARIMA model.

from pmdarima import auto_arima auto_arima(df[‘A’],seasonal=False).summary()

Run an augmented dickey-fuller(adfuller) test [optional]: This is done to check after how many differencing, our data is becoming stationary. it basically finds the value of ‘d’. We can verify the value of ‘d’ obtained on pmdarima using adfuller test.

You can visit this link to read more on adfuller test. It’s good to know how you can check if the data is stationary or not and the types of staionarity.

ACF and PACF plots [Optional]: ACF plots are used to find value of ‘q’ and PACF plots are used to find the value of ‘p’. We can use ACF and PACF plots to verify the value of p and q obtained from pmdarima.

code: from statsmodels.graphics.tsaplots import plot_acf,plot_pacf title = ‘ACB’

lags = 40

plot_acf(df[‘A’],title=title,lags=lags); title = ‘ABC’

lags = 40

plot_pacf(df[‘A’],title=title,lags=lags); To understand how to interprete ACF and PACF plots, visit here. I will write another article in future discusing only about ACF and PACF plots.

Step-wise pmdarima [optional]: It is very similar to grid-search which we used to do in machine learning. Basically, we give a different range of values of p,d,q into it, and gives us the best value. This is also used to cross-verify values of p,d,q obtained from step 3.

Code: stepwise_fit = auto_arima(df[‘A’], start_p=0, start_q=0,max_p=2, max_q=2, m=12, seasonal=False, d=None, trace=True, error_action=’ignore’,

suppress_warnings=True, stepwise=True) stepwise_fit.summary()

Split the data into train and test set: We cut out some part of available data as test set (note that, the test set has to be the last rows of data sorted by time. We can not split the data as we did using Sklearn because in time series we always predict the future. We can not go back in time and predict something).

code: train = df.iloc[:280]

test = df.iloc[280:]

Fit the ARIMA model into train test.

model = ARIMA(train[‘A’],order=(p,d,q))

results = model.fit()

results.summary() here replace p,d,q by the values which you obtained in step 3.

Predict on the test set.

code: start=len(train)

end=len(train)+len(test)-1

predictions = results.predict(start=start, end=end, dynamic=False, typ=’levels’).rename(‘ARIMA(p,d,q) Predictions’)

Compare actual test set values with the predicted ones. You can use some MSE or other metrics as per your need. You can also plot the comparison.

Train the model on whole data and predict the future!!!

This was all about ARIMA. If you have any questions, please put it in the comment section. I will be writing about SARIMA, prophet, etc in upcoming parts.

Consider hitting the clap button of this article helped you... Happy learning.

References: