Quantcast
Channel: Machine Learning | Towards AI
Viewing all articles
Browse latest Browse all 786

Practical Nuances of Time Series Forecasting — Part II— Improving Forecast Accuracy

$
0
0
Author(s): Santoshkumarpuvvada Originally published on Towards AI. Practical Nuances of Time Series Forecasting — Part II— Improving Forecast Accuracy In continuation of enhancing our understanding of time series forecasting, let’s get started with part 2. (Check out the part 1 here). Many times, practitioners feel that they have done everything possible to achieve a good forecast estimate — capturing all available relevant data , proper data cleaning, applying all possible algorithms, and fine-tuning the algorithms & still, results sometimes may not be up to mark! Now, what if I told you there could be some simple yet efficient ways to bump up your accuracy by a few points? We are presenting you a popular technique — the Hybrid Forecasting Model! Hybrid Model — Lift your accuracy by a few points! Hybrid Forecasting Model: The basic premise of hybrid forecasting is to combine the strengths of complementary algorithms & overcome the weaknesses of individual algorithms. For example, the ETS family(or exponential smoothening family) of models excels at detecting trend & seasonality. However, they can’t include any external factors information like promotions or weather or holiday information, etc. Similarly, tree-based models like XG boost/ Random forest regressors excel at determining complicated relationships between dependent & independent variables but fail to detect the trend in data as they try to fit the forecast to a range of values seen in training data. Now, what if we can combine the best of both models? That will likely perform much better than individual models. Let’s see a demo on how the hybrid model works using the famous airplane passengers data set & then you will get a clear understanding of Hybrid forecasts. A brief on the data set: The dataset contains 3 columns — Date, Volume of Passengers and Promotion. The dataset indicates 12 years monthly volume of passengers from Jan 1949 to Dec 1960 Airline Passenger data Let’s plot the data before we go for the analysis. There are a few missing values. Since there is a trend in data as per the graph, replacing the null values with Zero or by average value will not make much sense as they won’t reflect the underlying data pattern. Still, let us see how data will look like when we replace missing values by mean (mean imputation) Let us also see what happens when missing values are replaced by zeros. Note that, unlike in machine learning where there is a feasibility to just drop the records with missing values, we can’t do that in time series data. Because time series data is an ordered data(ordered by date) where as order doesn't matter in a typical ML problem. So, the best method to deal with missing values in this case would be a linear interpolation of data. Train-Test Spilt : Since we have 12 years data, let us use first 10 years for training & last 2 years for evaluating the model performance. train_len = 120train = df[0:train_len] # Taking first 120 months as training settest = df[train_len:] # Taking last 24 months as out-of-time test sety_hat_hwa = test.copy() # y_hat_hwa is a data frame which is copy of test set # i intend to use it for storing forecast from holt winter additive model(hwa) Model 1 – Holt Winter Model (ETS family based model): As data contains clear trend & seasonality based on the graph, we can use Holt Winter model for forecasting the volume of passengers. (Note: I will use the ETS model/Holt Winter/exponential smoothening interchangeably in the article, although they are not exactly the same. Refer to Appendix for further explanation) model = ExponentialSmoothing(np.asarray(train['Volume']),seasonal_periods=12 ,trend='add', seasonal='add')model_fit = model.fit(optimized=True)print(model_fit.params)y_hat_hwa['hw_forecast'] = model_fit.forecast(24)# Since it is a monthly data , seasonal_periods is set to 12, if it is a quarterly data , # it could have been set as 4# We are using an additive trend, additive seasonality in the above modeltrain['ETS fit'] = model_fit.fittedvalues# Noting the ETS fitted values on the training data# Plotting Actuals Vs ETS Forecastplt.plot(y_hat_hwa['hw_forecast'], label = 'ETS forecast values')plt.plot(test['Volume'],label= ' Test actuals')plt.plot(train['Volume'],label= ' Train actuals')plt.plot()plt.legend()plt.show()# Calculate RMSE & MAPE of Holt winter forecastrmse = np.sqrt(mean_squared_error(test['Volume'], y_hat_hwa['hw_forecast'])).round(2)mape = np.round(np.mean(np.abs(test['Volume']-y_hat_hwa['hw_forecast'])/test['Volume'])*100,2)tempResults = pd.DataFrame({'Method':['Holt Winters\' additive method'], 'RMSE': [rmse],'MAPE': [mape] })results = pd.DataFrame()results = pd.concat([results, tempResults])results = results[['Method', 'RMSE', 'MAPE']]results Following is the actuals Vs ETS forecast & below is the RMSE, MAPE of the ETS model ETS forecast Vs Actuals in Test Period: Let's have a closer look at actuals Vs. ETS forecast in the test period. plt.plot(y_hat_hwa['hw_forecast'], label = 'ETS forecast values')plt.plot(test['Volume'],label= 'actuals')# Adding 'Promo' values with different markersplt.plot(test.index[test['Promo'] == 1], test.loc[test['Promo'] == 1, 'Volume'], 'ro', label='Promo = 1') # 'ro' means red circlesplt.legend()plt.show() Clearly difference between Actuals & forecast is highest when there is promotion & lesser when there is no promotion As we can see from the above graph, ETS is failing to capture the uplift that typically happens during promotions. This is expected as ETS model cant take external factors into consideration Model 2 – XG Boost Regression: XG Boost is a popular & go-to ML algorithm for many tasks for practitioners. It can easily understand complex relations between dependent(y) & independent(x) variables. However, XG Boost has certain limitations in time series forecasting( like the inability to extend trend in the forecast). Let’s see how it performs on this data set # We need to do feature engineering to apply XG boost(or any ML algorithm)# for applying to time series forecasting as ML algorithms cant understand# date as an input. I have done below simple feature engineering as it is just# for a demo. More features can be added as per requirementdf['Month'] = df['Date'].dt.monthdf['year'] = df['Date'].dt.yeardf['Sr no.'] =df.index Now, we can drop the date column as all useful information from date has been extracted. Month column should not be considered as a continuous variable but as a categorical variable. Hence, we add dummy variables for the same. # Creating dummy variables for Month as follows:train_Xgboost['Month'].replace({1:'Jan',2:'Feb',3:'March',4:'April',5:'May',6:'June',7:'July', 8:'Aug',9:'Sept', 10:'Oct', 11:'Nov', 12:'Dec'},inplace = True)test_XGboost['Month'].replace({1:'Jan',2:'Feb',3:'March',4:'April',5:'May',6:'June',7:'July', 8:'Aug',9:'Sept', 10:'Oct', 11:'Nov', 12:'Dec'},inplace = True)Months_train = pd.get_dummies(train_Xgboost['Month'], drop_first […]

Viewing all articles
Browse latest Browse all 786

Trending Articles