Fundamentals 10 min read

Master Exponential Smoothing: Theory, Python Code, and Real‑World Forecasting

This article explains the principles of single and double exponential smoothing, shows how to choose the smoothing coefficient, demonstrates forecasting with Python code on product price and steel production data, and compares prediction errors to select the optimal model.

Model Perspective
Model Perspective
Model Perspective
Master Exponential Smoothing: Theory, Python Code, and Real‑World Forecasting

Exponential Smoothing

Moving‑average methods treat recent observations equally, but exponential smoothing assigns decreasing weights to older data, matching the intuition that influence fades over time. It provides a simple recursive formula and works well for short‑ to medium‑term forecasts.

Single Exponential Smoothing

Prediction Model

For a time series \(y_t\), the forecast \(\hat y_{t}\) is computed as \(\hat y_{t}=\alpha y_{t}+ (1-\alpha)\hat y_{t-1}\), where \(\alpha\) (0 < \alpha < 1) is the smoothing coefficient. The initial value can be the first observation or an average of early points.

Choosing the Smoothing Coefficient

A larger \(\alpha\) gives more weight to the newest observation, making the forecast more responsive but less stable; a smaller \(\alpha\) yields smoother forecasts that incorporate longer history. Typical choices depend on data volatility: stable series use \(\alpha\) around 0.1–0.5, while rapidly changing series may require \(\alpha\) near 0.8.

Initial Value Determination

If the series has many observations (e.g., >20), the impact of the initial value diminishes, so the first observation is often used. For shorter series, the average of the first few actual values is a common choice.

Case Study: Product Price Forecast

A product’s price for 11 periods is given. Forecasts are generated with \(\alpha=0.2, 0.5, 0.8\). Standard errors are computed for each \(\alpha\): 0.4148 (\(\alpha=0.2\)), 0.3216 (\(\alpha=0.5\)), and 0.2588 (\(\alpha=0.8\)). The smallest error occurs at \(\alpha=0.8\), so the predicted price for period 12 is 5.817.

“某产品的11期价格如下表所示。试预测该产品第12期的价格。”
import numpy as np
import pandas as pd
y = np.array([4.81,4.8,4.73,4.7,4.7,4.73,4.75,4.75,5.43,5.78,5.85])

def ExpMove(y, a):
    n = len(y)
    M = np.zeros(n)
    M[0] = (y[0] + y[1]) / 2
    for i in range(1, n):
        M[i] = a * y[i-1] + (1 - a) * M[i-1]
    return M

yt1 = ExpMove(y, 0.2)
yt2 = ExpMove(y, 0.5)
yt3 = ExpMove(y, 0.8)

s1 = np.sqrt(((y-yt1)**2).mean())
s2 = np.sqrt(((y-yt2)**2).mean())
s3 = np.sqrt(((y-yt3)**2).mean())

print("Standard errors:", s1, s2, s3)

# Forecast next period using the best alpha (0.8)
next_pred = 0.8 * y[-1] + 0.2 * yt3[-1]
print("Next period forecast:", next_pred)

Double Exponential Smoothing

When a series exhibits a linear trend, single exponential smoothing lags behind. Double exponential smoothing adds a second smoothing level to capture the trend. The first‑level smoothed value \(s_t\) and second‑level \(s'_t\) are computed recursively, and the forecast is \(\hat y_{t+m}=2s_t - s'_t + \frac{\alpha}{1-\alpha}(s_t - s'_t)m\).

Case Study: Steel Production

Using steel production data for 10 periods, single and double smoothed values are calculated, and forecasts for periods 11 and 12 are produced. The method demonstrates how the trend component improves forecast accuracy compared with single smoothing.

import numpy as np
import pandas as pd

y = np.loadtxt('Pdata18_3.txt')
alpha = 0.3
n = len(y)
s1 = np.zeros(n)
s2 = np.zeros(n)
# Initialize
s1[0] = y[0]
s2[0] = y[0]
for i in range(1, n):
    s1[i] = alpha * y[i] + (1 - alpha) * s1[i-1]
    s2[i] = alpha * s1[i] + (1 - alpha) * s2[i-1]
    # Double exponential forecast
    at = 2 * s1[i-1] - s2[i-1]
    bt = (alpha / (1 - alpha)) * (s1[i-1] - s2[i-1])
    # Example forecast for next two periods
    forecast = at + bt * np.array([1, 2])
    print('Forecast for periods', i, 'and', i+1, ':', forecast)

# Save results
df = pd.DataFrame(np.c_[s1, s2])
with pd.ExcelWriter('pre2002.xlsx') as writer:
    df.to_excel(writer)
Pythontime series forecastingexponential smoothingdouble smoothingprediction error
Model Perspective
Written by

Model Perspective

Insights, knowledge, and enjoyment from a mathematical modeling researcher and educator. Hosted by Haihua Wang, a modeling instructor and author of "Clever Use of Chat for Mathematical Modeling", "Modeling: The Mathematics of Thinking", "Mathematical Modeling Practice: A Hands‑On Guide to Competitions", and co‑author of "Mathematical Modeling: Teaching Design and Cases".

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.