File size: 997 Bytes
50bacee |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
"""
smoothing.py
Simple exponential smoothing and plotting for time series data.
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from statsmodels.tsa.holtwinters import SimpleExpSmoothing
def simple_exponential_smoothing(series, smoothing_level=None, optimized=True):
"""Apply Simple Exponential Smoothing to a pandas Series."""
model = SimpleExpSmoothing(series.dropna())
fit = model.fit(smoothing_level=smoothing_level, optimized=optimized)
return fit.fittedvalues, fit
def plot_ses(series, smoothing_level=None, optimized=True, title='Simple Exponential Smoothing'):
"""Plot original series and SES smoothed series."""
fitted_values, _ = simple_exponential_smoothing(series, smoothing_level, optimized)
plt.figure(figsize=(10,6))
plt.plot(series, label='Original', color='blue')
plt.plot(fitted_values, label='SES Smoothed', color='green')
plt.title(title)
plt.legend(loc='best')
plt.show()
|