Content

Exponential Smoothing is a widely used time series modeling technique that uses a weighted average of past observations to make future predictions. It is a powerful technique for forecasting trends in time series data and is commonly used in industries such as finance, economics, and marketing. Here’s a detailed cheat sheet for exponential smoothing in Python.

Install statsmodels in Python

Installing the statsmodels library directly may cause some issues as it will install the latest version of the library. You can install the one used in this cheat sheet by specifying the package version. In this way, the syntax used here will match the one in your code.

Python
!pip install statsmodels

# Install specific version of the package
!pip install statsmodels==0.13.5

Train your model

Simple Exponential Smoothing

This model is used to model the level of the data. Suitable for forecasting data that lacks any systematic structure, such as a clear trend or seasonality.

Python
# Import library
from statsmodels.tsa.api import SimpleExpSmoothing
  
# Train the model
model = SimpleExpSmoothing(
    df,
    initialization_method = 'estimated',
).fit(
    smoothing_level = 0.2
)

The main parameters to define are:

  • The initialization_method determines how the initial level value is estimated before smoothing begins. There are two main options:
    • 'estimated': the default option, and it estimates the initial level value using the first ‘initial_seasonal_components’ periods of the time series. Useful when there is no prior knowledge of the underlying level of the time series.
    • 'heuristic': This option uses a heuristic formula to estimate the initial level value based on the first two periods of the time series. Useful when the time series has a clear upward or downward trend.
  • The smoothing_level (the $\alpha$ smoothing parameter for the trend) can be input as an argument to the fit method. If it is not defined the optimal one will be estimated.

Further details and parameters can be found on the official documentation: statsmodels.tsa.holtwinters.SimpleExpSmoothing

Double Exponential Smoothing

It is an extension of the previous type that adds support for modelling the trend. Therefore, it is suitable for data with a trend but no seasonality. There can be two kinds of models, depending on the type of trend additive trend and multiplicative or exponential trend.

Python
# Import library
from statsmodels.tsa.api import Holt

# Train the model
model = Holt(
    df,
    exponential = False,
    initialization_method = "estimated"
).fit(
    smoothing_level = 0.2,
    smoothing_trend = 0.1,
    damping_trend = 0.2
)
    
# Forecast for next 30 days    
forecast = model.forecast(steps=30) 

The main parameters to define are:

  • exponential: False if the trend is additive or True if multiplicative.
  • initialization_method: determines how the initial level and trend values are estimated before smoothing begins. Check the simple exponential smoothing for more details.
  • smoothing_level: the $\alpha$ smoothing parameter for the level, can be input as an argument to the fit method. If it is not defined the optimal one will be estimated.
  • smoothing_trend: the $\beta$ smoothing parameter for the trend, it can be input as an argument to the fit method. If it is not defined the optimal one will be estimated.
  • damping_trend: the $\phi$ parameter, it can be input as an argument to the fit method to add damping to the trend.

Further details and parameters can be found on the official documentation: statsmodels.tsa.holtwinters.Holt

Triple or Holt-Winters Exponential Smoothing

This type extends the previous model by adding support for modelling the seasonal component. Therefore, it is suitable for data with a trend and seasonality. It is also called Holt-Winters Exponential Smoothing. As with the Double Exponential Smoothing model, we need to distinguish two kinds of models, depending on the type of seasonal component, in addition to the two variations depending on the type of trend: additive and multiplicative or exponential.

Python
# Import library
from statsmodels.tsa.api import ExponentialSmoothing

# Train the model
model = ExponentialSmoothing(
    df,
    seasonal_periods = 4,
    trend = "add",
    seasonal = "add",
    initialization_method = "estimated",
).fit(
    smoothing_level = None,
    smoothing_trend = None,
    smoothing_seasonal = None,
    damping_trend = None
)

# Forecast for next 30 days
forecast = model.forecast(steps=30) 

The main parameters to define are:

  • seasonal_periods: the number of periods in a seasonal cycle for the time series
  • trend: ‘add’ for additive trend, ‘mul’ for multiplicative or exponential trend, None for no trend modelling.
  • seasonal: ‘add’ for additive seasonality, ‘mul’ for multiplicative or exponential seasonality, None for no seasonality modelling.
  • initialization_method: determines how the initial level, trend and seasonality values are estimated before smoothing begins. Check the simple exponential smoothing for more details.
  • smoothing_level: the $\alpha$ smoothing parameter for the level, can be input as an argument to the fit method. If it is not defined the optimal one will be estimated.
  • smoothing_trend: the $\beta$ smoothing parameter for the trend, it can be input as an argument to the fit method. If it is not defined the optimal one will be estimated.
  • smoothing_seasonal: the $\gamma$ smoothing parameter for the seasonality, it can be input as an argument to the fit method. If it is not defined the optimal one will be estimated.
  • damping_trend: the $\phi$ parameter, it can be input as an argument to the fit method to add some damping to the trend.

This model can also be used for the Simple Exponential Smoothing by setting trend and seasonal to None; and for the Double Exponential Smoothing by setting seasonal to None.

Further details and parameters can be found on the official documentation: statsmodels.tsa.holtwinters.ExponentialSmoothing

Forecast

Python
# Forecast for next 30 periods
forecast = model.forecast(steps=30) 

You can define the number of periods to forecast by defining steps.

You can also use a rolling forecast strategy to take into consideration all the available data.

Evaluating Model Performance

Python
# Import libraries
import numpy as np
from sklearn.metrics import mean_squared_error

# Evaluate performance
mse = mean_squared_error(actual, forecast)
rmse = np.sqrt(mse)

You can use the Mean Squared Error (MSE) or the Root Mean Square Error (RMSE) to assess the performance of your model and use compare between models.

Visualizing Results

Python
# Import libraries
import matplotlib.pyplot as plt

# Visualize results
plt.plot(df, label='Actual')
plt.plot(forecast, label='Forecast')
plt.legend()
plt.show()


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *