statsmodels.tsa.filters.hp_filter.hpfilter(X, lamb=1600)
[source]
Hodrick-Prescott filter
Parameters: |
|
---|---|
Returns: |
|
>>> import statsmodels.api as sm >>> import pandas as pd >>> dta = sm.datasets.macrodata.load_pandas().data >>> index = pd.DatetimeIndex(start='1959Q1', end='2009Q4', freq='Q') >>> 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)
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
See also
statsmodels.tsa.filters.bk_filter.bkfilter
, statsmodels.tsa.filters.cf_filter.cffilter
, statsmodels.tsa.seasonal.seasonal_decompose
Carnegie Mellon University discussion paper no. 451
.The Review of Economics and Statistics
, 84(2), 371-80.
© 2009–2012 Statsmodels Developers
© 2006–2008 Scipy Developers
© 2006 Jonathan E. Taylor
Licensed under the 3-clause BSD License.
http://www.statsmodels.org/stable/generated/statsmodels.tsa.filters.hp_filter.hpfilter.html