0

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?

4
  • 1
    If you ignore the 1st value of Statsmodels list, both lists gives the same values it seems. Commented Dec 7, 2024 at 20:43
  • Of course it is. But i cant solve this task) It has to be possible to find parameters of inner statmodels functions. Commented Dec 7, 2024 at 20:52
  • 1
    I would use this formula: smoothed_value = alpha * series[t - 1] + (1 - alpha) * smoothed[t - 1] Commented Dec 7, 2024 at 21:23
  • But this formula is not correct according to the sources. But thanks. It will work. Commented Dec 7, 2024 at 22:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.