| """ | |
| 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() | |