Exponential smoothing is a low-pass filter that aims to remove noise. In this recipe, we will apply single and double exponential smoothing, as shown by the following equations:
Single exponential smoothing (6.3) requires the smoothing factor α, where 0 < α < 1. Double exponential smoothing (6.4 and 6.5) attempts to handle trends in data via the trend smoothing factor β, where 0 < β < 1.
We will also take a look at rolling deviations of wind speed, which are similar to z-scores, but they are applied to a rolling window. Smoothing is associated with regression, although the goal of smoothing is to get rid of noise. Nevertheless, metrics related to regression, such as the Mean Squared Error (MSE), are also appropriate for smoothing.
import dautil as dl import pandas as pd import matplotlib.pyplot as plt import numpy as np import seaborn as sns from IPython.display import HTML
def grid_mse(i, j, devs): alpha = 0.1 * i beta = 0.1 * j cell = dl.ts.double_exp_smoothing(devs.values, alpha, beta) return dl.stats.mse(devs, cell)
wind = dl.data.Weather.load()['WIND_SPEED'].dropna() wind = dl.ts.groupby_year(wind).mean() devs = dl.ts.rolling_deviations(wind, 12).dropna()
sp = dl.plotting.Subplotter(2, 2, context) sp.label(ylabel_params=dl.data.Weather.get_header('WIND_SPEED')) sp.ax.plot(wind.index, wind)
cp = dl.plotting.CyclePlotter(sp.next_ax()) cp.plot(devs.index, devs, label='Rolling Deviations') cp.plot(devs.index, dl.ts.exp_smoothing(devs.values, 0.7), label='Smoothing') sp.label()
alphas = 0.01 * np.arange(1, 100) errors = [dl.stats.mse(devs, dl.ts.exp_smoothing(devs.values, alpha) for alpha in alphas] sp.label(advance=True) sp.ax.plot(alphas, errors)
sp.label(advance=True) rng = range(1, 10) df = dl.report.map_grid(rng, rng, ["alpha", "beta", "mse"], grid_mse, devs) sns.heatmap(df, cmap='Blues', square=True, annot=True, fmt='.1f', ax=sp.ax) HTML(sp.exit())
Refer to the following screenshot for the end result:
The code is in the exp_smoothing.ipynb
file in this book's code bundle.