Exponential smoothing

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:

Exponential smoothing

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.

How to do it...

  1. The imports are as follows:
    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
  2. Define the following function to help visualize the result of double exponential smoothing:
    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)
  3. Load the wind speed data and calculate annual means and rolling deviations:
    wind = dl.data.Weather.load()['WIND_SPEED'].dropna()
    wind = dl.ts.groupby_year(wind).mean()
    devs = dl.ts.rolling_deviations(wind, 12).dropna()
  4. Plot the annual means of the wind speed data:
    sp = dl.plotting.Subplotter(2, 2, context)
    sp.label(ylabel_params=dl.data.Weather.get_header('WIND_SPEED'))
    sp.ax.plot(wind.index, wind)
  5. Plot the rolling deviations with an α of 0.7:
    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()
  6. Plot the MSE for varying smoothing factors:
    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)
  7. Plot the MSE for a grid of α and β values:
    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:

How to do it...

The code is in the exp_smoothing.ipynb file in this book's code bundle.

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset