I'm trying to get the same results from my Exponential Smoothing function and the built-in one. Here is my implementation.
def exponential_smoothing(series, alpha):
smoothed = [series[0]]
for t in range(1, len(series)):
smoothed_value = alpha * series[t] + (1 - alpha) * smoothed[t - 1]
smoothed.append(smoothed_value)
return smoothed
This implementation strictly follows the formula. But when I try to compare it with the built-in one. The result is shifted by 1 step.
# Sample data
data = [10, 12, 13, 15, 18, 21, 25]
alpha = 0.5
# Manual function call
smoothed_manual = exponential_smoothing(data, alpha)
# statsmodels
import pandas as pd
from statsmodels.tsa.holtwinters import SimpleExpSmoothing
index = pd.date_range(start="2023-01-01", periods=len(data), freq="D")
series = pd.Series(data, index=index)
model = SimpleExpSmoothing(series).fit(smoothing_level=alpha, optimized=False)
smoothed_statsmodels = model.fittedvalues.tolist()
print("Manual:", smoothed_manual)
print("Statsmodels:", smoothed_statsmodels)
Result:
Manual [10, 11.0, 12.0, 13.5, 15.75, 18.375, 21.6875]
Statsmodels: [10.0, 10.0, 11.0, 12.0, 13.5, 15.75, 18.375]
What parameters of the built-in function should I set so that everything fits together?