Crash course in Forecasting Worked Examples — Step-by-Step Guide for Time Series Analysis using Exponential Smoothing

Cibaca Khandelwal
AI Skunks
Published in
5 min readApr 10, 2023

Exponential smoothing is a popular technique for smoothing time series data and forecasting future values based on past observations. In this article, we will walk through an example of applying exponential smoothing to web traffic data using Python.

Advantages of Exponential Smoothing

  1. Simple and easy to understand: Exponential smoothing is a straightforward technique that does not require advanced statistical knowledge to apply. The technique is based on the idea that recent data points are more relevant than older ones, and it adjusts the weights of the observations accordingly.
  2. Flexibility: Exponential smoothing can handle various types of time series data, including trend, seasonality, and irregularity. By adjusting the smoothing parameters, the technique can be customized to fit a wide range of time series patterns.
  3. Efficient computation: Exponential smoothing requires less computation time than other time series techniques, making it well suited for large datasets.
  4. Accurate forecasting: Exponential smoothing can produce accurate forecasts for short-term horizons when the underlying time series has a stable pattern.

Disadvantages of Exponential Smoothing

  1. Limited accuracy: Exponential smoothing may not produce accurate forecasts when the underlying time series has a complex or non-linear pattern, such as sudden changes or extreme events.
  2. Overfitting: Exponential smoothing may overfit the data if the smoothing parameters are not selected correctly. The technique can be prone to producing spurious patterns or noise in the forecasts if the parameters are not well-tuned.
  3. Limited applicability: Exponential smoothing may not be suitable for all types of time series data, especially when the underlying patterns are difficult to identify or interpret.
  4. Lack of transparency: Exponential smoothing is often criticized for being a “black box” technique, as it can be difficult to interpret the results or understand how the smoothing parameters are affecting the forecasts.

Mathematics for Exponential Smoothing

The math behind exponential smoothing can be broken down into several steps:

  1. Initialization: The technique requires an initial value for the smoothed series, often referred to as the level. The level is usually set to the first observation in the time series.
  2. Smoothing: Exponential smoothing calculates a weighted average of past observations, where the weights decrease exponentially as the observations become older. The smoothed value at time t is calculated as follows:
  3. S_t = α*y_t + (1-α)*S_{t-1}
  4. where S_t is the smoothed value at time t, y_t is the actual value at time t, and S_{t-1} is the smoothed value at time t-1. The parameter α (0<α<1) is the smoothing factor or smoothing coefficient, which determines the weight given to the most recent observation.
  5. Updating the level: Exponential smoothing updates the level as follows:
  6. L_t = α*y_t + (1-α)*L_{t-1}
  7. where L_t is the updated level at time t, y_t is the actual value at time t, and L_{t-1} is the previous level.
  8. Forecasting: Exponential smoothing can be used to forecast future values of the time series by using the updated level and the smoothing factor. The forecast at time t+k is given by:
  9. F_{t+k} = L_t + k*(S_t)
  10. where F_{t+k} is the forecast at time t+k, L_t is the updated level at time t, S_t is the smoothed value at time t, and k is the number of periods ahead to forecast.

The effectiveness of exponential smoothing depends on the choice of the smoothing factor α. A high value of α gives more weight to recent observations, making the method more responsive to changes in the data but more prone to noise and overfitting. A low value of α gives more weight to past observations, making the method smoother but less responsive to changes in the data.

Example — Web Traffic Time Series Forecasting

The data we will be working with is from a Kaggle dataset of daily web page traffic for various Wikipedia pages from 2015 to 2017. We will be using the Holt’s linear method with additive trend and no seasonality to forecast the next 30 days of web traffic for a selected Wikipedia page.

Let’s get started!

Step 1: Importing Libraries First, we need to import the necessary libraries: Pandas for data manipulation, NumPy for numerical computing, Matplotlib for data visualization, and ExponentialSmoothing from statsmodels.tsa.api for applying exponential smoothing.pythonCopy cod

import pandas as pd 
import numpy as np
import matplotlib.pyplot as plt
fromstatsmodels.tsa.api import ExponentialSmoothing

Step 2: Loading Data Next, we load the web traffic data from the Kaggle dataset into a Pandas DataFrame using the read_csv function.pythonCopy cod

train = pd.read_csv('train_1.cs

Step 3: Reshaping Data The train DataFrame is in a wide format with a separate column for each Wikipedia page and a separate row for each day. To apply exponential smoothing, we need to reshape the DataFrame into a long format with a separate row for each day and page. We do this by using the melt function to unpivot the DataFrame, resulting in a tall and narrow DataFrame with three columns: 'Page', 'Date', and 'Visits'.

train = train.melt(id_vars=['Page'], var_name='Date', value_name='Visits')

Step 4: Converting Date to Datetime Object The ‘Date’ column is currently a string, so we convert it to a datetime object using the to_datetime function.

train['Date'] = pd.to_datetime(train['Date'], format='%Y-%m-%d')

Step 5: Setting Index We set the ‘Date’ column as the DataFrame’s index using the set_index method.

train.set_index('Date', inplace=True

Step 6: Grouping Data We group the data by page and take the median for each day using the groupby method.

df = train.groupby(['Page', pd.Grouper(freq='D')])['Visits'].median().unstack('Page')

Step 7: Selecting Subset of Data We select a single Wikipedia page to forecast and create a subset of the data for that page using the dropna method to remove any missing values.

page = '2NE1_zh.wikipedia.org_all-access_spider' df_subset = df[[page]].dropna()

Step 8: Applying Exponential Smoothing

We apply exponential smoothing to the data for the selected page using the Holt’s linear method with additive trend and no seasonality. We create an ExponentialSmoothing object named model with the parameters for the method, and then fit it to the data using the fit method and the specified smoothing parameters.

model = ExponentialSmoothing(df_subset[page], trend='add', seasonal=None) fit = model.fit(smoothing_level=0.2, smoothing_slope=0.1, optimized=False)

Step 9: Forecasting

We predict the next 30 days using the fitted model using the forecast method and specifying the number of periods to forecast.

forecast = fit.forecast(steps=30)

Step 10: Visualizing Results

Finally, we visualize the results by plotting the original web traffic data and the predicted values for the selected page using Matplotlib.

plt.figure(figsize=(15, 6))
plt.plot(df_subset.index, df_subset[page], label='Actual')
plt.plot(forecast.index, forecast, label='Forecast')
plt.title('Web Traffic Forecast for {} Wikipedia Page'.format(page))
plt.xlabel('Date')
plt.ylabel('Visits')
plt.legend()
plt.show()

Conclusion

In this article, we explored the concept of exponential smoothing and its application in time series analysis. We discussed the steps involved in implementing exponential smoothing in Python and demonstrated its usage on a Kaggle dataset.

We also looked at the advantages and disadvantages of exponential smoothing and its real-world applications, including sales forecasting, economic forecasting, supply chain management, finance, and energy forecasting.

Finally, we delved into the mathematical principles underlying exponential smoothing, including initialization, smoothing, updating the level, and forecasting, and discussed the importance of selecting the appropriate smoothing factor for accurate predictions.

Overall, exponential smoothing is a versatile and powerful tool in time series analysis that can help decision-makers make informed predictions based on historical data. Its simplicity and flexibility make it a popular choice for data analysts and researchers in various fields, and its potential for improving efficiency and reducing costs makes it an essential tool for businesses and organizations looking to stay ahead of the curve.

--

--

Cibaca Khandelwal
AI Skunks

Tech enthusiast at the nexus of Cloud ☁️, Software 💻, and Machine Learning 🤖, shaping innovation through code and algorithms.