Untitled

 avatar
unknown
plain_text
22 days ago
1.3 kB
2
Indexable
import numpy as np
import pandas as pd
from statsmodels.tsa.holtwinters import SimpleExpSmoothing

# Example time series (growth rates or multiplicative data)
data = [1.02, 1.03, 0.99, 1.05, 1.01]  # Example growth rates

# Initialize SES parameter
alpha = 0.5

# ---- 1. Casual Exponential Smoothing ----
# Apply Simple Exponential Smoothing directly to the original data
model_casual = SimpleExpSmoothing(data, initialization_method="legacy-heuristic")
fit_casual = model_casual.fit(smoothing_level=alpha, optimized=False)  # Use fixed alpha
smoothed_casual = fit_casual.fittedvalues

# ---- 2. Log-Transformed Exponential Smoothing ----
# Log transform the data
log_data = np.log(data)

# Apply Simple Exponential Smoothing to the log-transformed data
model_log = SimpleExpSmoothing(log_data, initialization_method="legacy-heuristic")
fit_log = model_log.fit(smoothing_level=alpha, optimized=False)  # Use fixed alpha
smoothed_log = fit_log.fittedvalues

# Exponentiate back to the original scale
smoothed_log_transformed = np.exp(smoothed_log)

# ---- Combine Results into a DataFrame ----
result = pd.DataFrame({
    'Original': data,
    'Smoothed Casual': smoothed_casual,
    'Smoothed Log-Transformed': smoothed_log_transformed
})

print(result)
Leave a Comment