import pandas import matplotlib.pyplot as plt import statsmodels.api as sm dta = sm.datasets.macrodata.load_pandas().data index = pandas.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); gdp_cycle, gdp_trend = sm.tsa.filters.hpfilter(dta.realgdp) gdp_decomp = dta[['realgdp']] 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); bk_cycles = sm.tsa.filters.bkfilter(dta[["infl","unemp"]]) * We lose K observations on both ends. It is suggested to use K=12 for quarterly data. fig = plt.figure(figsize=(14,10)) ax = fig.add_subplot(111) bk_cycles.plot(ax=ax, style=['r--', 'b-']); 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-']);