%matplotlib inline
from __future__ import print_function
import pandas as pd
import matplotlib.pyplot as plt
import statsmodels.api as sm
dta = sm.datasets.macrodata.load_pandas().data
index = pd.Index(sm.tsa.datetools.dates_from_range('1959Q1', '2009Q3'))
print(index)
dta.index = index
del dta['year']
del dta['quarter']
print(sm.datasets.macrodata.NOTE)
print(dta.head(10))
fig = plt.figure(figsize=(12,8))
ax = fig.add_subplot(111)
dta.realgdp.plot(ax=ax);
legend = ax.legend(loc = 'upper left');
legend.prop.set_size(20);
The Hodrick-Prescott filter separates a time-series yt into a trend τt and a cyclical component ζt
yt=τt+ζtThe components are determined by minimizing the following quadratic loss function
minτtT∑tζ2t+λT∑t=1[(τt−τt−1)−(τt−1−τt−2)]2gdp_cycle, gdp_trend = sm.tsa.filters.hpfilter(dta.realgdp)
gdp_decomp = dta[['realgdp']].copy()
gdp_decomp["cycle"] = gdp_cycle
gdp_decomp["trend"] = gdp_trend
fig = plt.figure(figsize=(12,8))
ax = fig.add_subplot(111)
gdp_decomp[["realgdp", "trend"]]["2000-03-31":].plot(ax=ax, fontsize=16);
legend = ax.get_legend()
legend.prop.set_size(20);
The Baxter-King filter is intended to explictly deal with the periodicty of the business cycle. By applying their band-pass filter to a series, they produce a new series that does not contain fluctuations at higher or lower than those of the business cycle. Specifically, the BK filter takes the form of a symmetric moving average
y∗t=k=K∑k=−Kakyt−kwhere a−k=ak and ∑Kk=−kak=0 to eliminate any trend in the series and render it stationary if the series is I(1) or I(2).
For completeness, the filter weights are determined as follows
aj=Bj+θ for j=0,±1,±2,…,±KB0=(ω2−ω1)πBj=1πj(sin(ω2j)−sin(ω1j)) for j=0,±1,±2,…,±Kwhere θ is a normalizing constant such that the weights sum to zero.
θ=−∑j=−KKbj2K+1ω1=2πPHω2=2πPLPL and PH are the periodicity of the low and high cut-off frequencies. Following Burns and Mitchell's work on US business cycles which suggests cycles last from 1.5 to 8 years, we use PL=6 and PH=32 by default.
bk_cycles = sm.tsa.filters.bkfilter(dta[["infl","unemp"]])
fig = plt.figure(figsize=(12,10))
ax = fig.add_subplot(111)
bk_cycles.plot(ax=ax, style=['r--', 'b-']);
The Christiano-Fitzgerald filter is a generalization of BK and can thus also be seen as weighted moving average. However, the CF filter is asymmetric about t as well as using the entire series. The implementation of their filter involves the calculations of the weights in
y∗t=B0yt+B1yt+1+⋯+BT−1−tyT−1+˜BT−tyT+B1yt−1+⋯+Bt−2y2+˜Bt−1y1for t=3,4,...,T−2, where
Bj=sin(jb)−sin(ja)πj,j≥1B0=b−aπ,a=2πPu,b=2πPL˜BT−t and ˜Bt−1 are linear functions of the Bj's, and the values for t=1,2,T−1, and T are also calculated in much the same way. PU and PL are as described above with the same interpretation.
The CF filter is appropriate for series that may follow a random walk.
print(sm.tsa.stattools.adfuller(dta['unemp'])[:3])
print(sm.tsa.stattools.adfuller(dta['infl'])[:3])
cf_cycles, cf_trend = sm.tsa.filters.cffilter(dta[["infl","unemp"]])
print(cf_cycles.head(10))
fig = plt.figure(figsize=(14,10))
ax = fig.add_subplot(111)
cf_cycles.plot(ax=ax, style=['r--','b-']);
Filtering assumes a priori that business cycles exist. Due to this assumption, many macroeconomic models seek to create models that match the shape of impulse response functions rather than replicating properties of filtered series. See VAR notebook.