Hodrick-Prescott filter
Parameters: | X : array-like
lamb : float
|
---|---|
Returns: | cycle : array
trend : array
|
See also
statsmodels.tsa.filters.bk_filter.bkfilter, statsmodels.tsa.filters.cf_filter.cffilter, statsmodels.tsa.seasonal.seasonal_decompose
Notes
The HP filter removes a smooth trend, T, from the data X. by solving
Here we implemented the HP filter as a ridge-regression rule using scipy.sparse. In this sense, the solution can be written as
T = inv(I - lamb*K’K)X
where I is a nobs x nobs identity matrix, and K is a (nobs-2) x nobs matrix such that
K[i,j] = 1 if i == j or i == j + 2 K[i,j] = -2 if i == j + 1 K[i,j] = 0 otherwise
References
Examples
>>> import statsmodels.api as sm
>>> import pandas as pd
>>> dta = sm.datasets.macrodata.load_pandas().data
>>> dates = sm.tsa.datetools.dates_from_range('1959Q1', '2009Q3')
>>> index = pd.DatetimeIndex(dates)
>>> dta.set_index(index, inplace=True)
>>> cycle, trend = sm.tsa.filters.hpfilter(dta.realgdp, 1600)
>>> gdp_decomp = dta[['realgdp']]
>>> gdp_decomp["cycle"] = cycle
>>> gdp_decomp["trend"] = trend
>>> import matplotlib.pyplot as plt
>>> fig, ax = plt.subplots()
>>> gdp_decomp[["realgdp", "trend"]]["2000-03-31":].plot(ax=ax,
... fontsize=16);
>>> plt.show()
(Source code, png, hires.png, pdf)