Filters

Learn two techniques to decompose time series, the Hodrick-Prescott filter and the STL decomposition.

Calculating moving averages or using statsmodels’ seasonal_decompose function should be our first approach when exploring the components of our time series. However, these methods often fall short of fully decomposing the data. In this lesson, we’ll learn about two filters that help us better separate the elements of our series: the Hodrick-Prescott Filter and the Seasonal-Trend decomposition using LOESS (STL), where LOESS is the acronym for locally estimated scatterplot smoothing.

The Hodrick-Prescott filter

The Hodrick-Prescott (HP) filter is a filtering method used to separate the trend and the cyclical components of a series (gtg_t and ctc_t, respectively). The filter is not designed to yield a seasonal component. Any seasonality in the data will leak to the trend component. Consequently, the filter is more suitable for nonseasonal series. This is not to say that the series can’t have periodical ups and downs, but that these should be of an irregular duration. This is the case for financial and economic data. Stock prices and countries’ GPD growth rates usually have a clearly defined trend, but this is subject to largely unpredictable fluctuations. This is what we call a business cycle.

Daily Brent crude oil prices in USD, 2021–mid 2023. Prices of commodities are subject to irregular fluctuations.
Daily Brent crude oil prices in USD, 2021–mid 2023. Prices of commodities are subject to irregular fluctuations.

The idea behind the HP filter is to draw a smooth line through the series, the trend. Then, the difference between the estimated smooth line and the actual data will give us the cyclical component. The HP filter is derived from the following minimization problem:

We will not go into the details of the minimization problem, but let’s focus on the intuition for a moment. The goal of the problem is to minimize the difference between the actual series, yty_t, and the trend, gtg_t. This is what the first part of the formula, (ytgt)2(y_t - g_t)^2, achieves. The second part, λ[(gt+1gt)(gtgt1)]2\lambda[(g_{t+1} - g_t)-(g_t-g_{t-1})]^2 ...