Skip to main content

Simple Exponential Smoothing in Python

Exponential Smoothing

Exponential smoothing is a technique used to smooth time series data using the exponential window function. Unlike the simple moving average, exponential functions assign exponentially decreasing weights to past observations over time. It is a simple and widely used method for making determinations based on user assumptions, such as seasonality. Exponential smoothing is commonly used to analyze time-series data.

Exponential smoothing is one of many window functions used to smooth data in signal processing. It acts as a low-pass filter, removing high-frequency noise. Recursive exponential window functions were used by Poisson in convolutions in the 19th century, and recursive moving averages were used by Kolmogorov and Zurbenko in their studies of turbulence in the 1940s.

The raw data sequence is represented by xt at time t=0, and the output of the exponential smoothing algorithm is written as st, which is considered the best estimate of the next value of x. For a sequence that starts at t=0, the simplest form of exponential smoothing is given by the formulas:

s0 = x0
st = a*xt + (1-a)*st-1, t>0

where a is the smoothing factor, and 0 < a < 1.

This simple form of exponential smoothing is also called an exponentially weighted moving average (EWMA). Technically, it can be classified as an autoregressive integrated moving average (ARIMA) model with no constant term.

The code

To implement this method, we need to import the necessary libraries first. We only need numpy for random data functions and matplotlib to visualize the results.

import matplotlib.pyplot as plt
import numpy as np
plt.style.use("fivethirtyeight")
plt.rcParams["figure.figsize"] = (20,10)

Next, we create our datasets. We test this method on two datasets: random noise and a random walk.

time_steps = 1000
random_data = np.random.normal(0,1,time_steps)
start = 0
rw = [start]
for i in range(time_steps):
    mv = 1 if np.random.normal(0,1) > 0 else -1
    rw.append(rw[-1] + mv)

Then we calculate the exponential smoothing for both datasets. We choose the smoothing factor a to be 0.2.

a = 0.2

exp_smooth1 = [random_data[0]]
exp_smooth2 = [rw[0]]

for i in range(1,time_steps):
    exp_smooth1.append(random_data[i]*a + (1-a)*exp_smooth1[-1])
    exp_smooth2.append(rw[i]*a + (1-a)*exp_smooth2[-1])

Finally, we visualize the random dataset and the smoothed data.

plt.plot(random_data,label='Random Data')
plt.plot(exp_smooth1,label='Exponential Smoothing a = {}'.format(a))
plt.legend()

Simple Exponential Smoothing For Random Data

Then, we visualize the random walk with the smoothed data.

plt.plot(rw,label='Random walk')
plt.plot(exp_smooth2, label='Exponential Smoothing a = {}'.format(a))
plt.legend()

Simple Exponential Smoothing For Random Walk

The full code for this article can be found on GitHub