Time series forecasting in a simple way

Vishnu Nandakumar
Analytics Vidhya
Published in
4 min readNov 27, 2020

--

Have you heard the word prophecy? If not let me tell you in a simple way. If some one tells you that you are the one who is going to save the world from a big destruction and it happened as such then that person is a prophet and he said a prophecy about you. Lets come to some real case where we are trying to forecast most of the demands in many sectors. One of them is the case of web traffic, where forecasting the demand of the traffic is a tricky one though. Lets look at this data and try to forecast one of its demand

I found this dataset from kaggle, here is the link for the same: kaggle data link

Dataset

  • Let us look at the data now, it has data from September 2014 to July 2020. Lets us take Pageloads as the target variable and others as independent variables
dataset
  • Let us decompose the “Page.Loads” variable and see how they look and what seasonality pattern they have. We can see that the time series has a yearly seasonality by looking at the trend and the observed component.
Time series

Modelling the data

I am gonna try three methods for modelling the data by using one of the commonly used auto regression model, Holt Winters Exponential Smoothing. This is a good algorithm for starters and it states that a value at time step t is given as an exponential linear function of observations prior to time step t. But again there is a way you can improve your model by changing the window for which you forecast and adding potential regressors.

Now lets fit the model from 2015 to 2019 and forecast it for 2020. Lets fix the parameters for default and add these parameters to our exponential smoothing API from statsmodels library. I have tried rolling training and forecast here i.e, Assuming our training period is till time step (t) and we want to forecast from (t+1) till (t+28) what we can do is forecast at one go till t+28 or forecast one week ahead by recurrently training till t+7, t+14… and forecasting for windows t+7 to t+14, t+14 to t + 21 … respectively. We can repeat the same process for one day and one month time windows also. So lets implement and check out how the model performs for each scenario

seasonal_periods = 365 # yearly seasonality
seasonal = 'add'
trend = 'add'

I have defined the rolling forecast function as below you can edit it as you want (i.e) changing variables and parameters etc,

  • Forecasting for the entire test window in a go
  • Forecasting for one month, I have taken 30 as rolling window for forecasting one month
df_om_es = rolling_forecast_es(df, df_test, 30)
  • Forecasting for one week, window is taken as 7
df_ow_es = rolling_forecast_es(df, df_test, 7)
  • Forecasting for one day, window =1
df_od_es = rolling_forecast_es(df, df_test, 1)

Analyzing the results

Let us look at the plots and metrics that we got for each of our methods.

  • Looking at the plots we can infer that forecasting one week ahead seems to have performed better than others. The worst one is eventually without doubt is forecasting for the whole window as the forecasted values missed the trend of the actuals completely. This is evident for all autoregressive models as they give more weights to recent past than the remote past.
  • Let us look at the error metrics and we will be able to get a good picture of what has happened
Error metrics

As evident from the plots the MAE(Median absolute error) is low in the case of one week ahead forecast and very high in the Whole window because of the model missing the trend in the later part of forecast. The weird one is with the one day ahead forecast where it is likely to perform better but have not in this case which might be due to the granularity of the time frame, I believe it would have performed better if it is hourly level.

Thanks for you time reading through it and you also try the same by tweaking the parameters and adding more customization, you can find the code from the Github link: Time series using Exponential smoothing

--

--

Vishnu Nandakumar
Analytics Vidhya

Machine Learning Engineer, Cloud Computing (AWS), Arsenal Fan. Have a look at my page: https://bit.ly/m/vishnunandakumar