mFilter (version 0.1-5)

# hpfilter: Hodrick-Prescott filter of a time series

## Description

This function implements the Hodrick-Prescott for estimating cyclical and trend component of a time series. The function computes cyclical and trend components of the time series using a frequency cut-off or smoothness parameter.

## Usage

hpfilter(x,freq=NULL,type=c("lambda","frequency"),drift=FALSE)

## Arguments

x

a regular time series.

type

character, indicating the filter type, "lambda", for the filter that uses smoothness penalty parameter of the Hodrick-Prescott filter (default), "frequency", for the filter that uses a frequency cut-off type Hodrick-Prescott filter. These are related by $$lambda = (2*sin(pi/frequency))^{-4}$$.

freq

integer, if type="lambda" then freq is the smoothing parameter (lambda) of the Hodrick-Prescott filter, if type="frequency" then freq is the cut-off frequency of the Hodrick-Prescott filter.

drift

logical, FALSE if no drift in time series (default), TRUE if drift in time series.

## Value

A "mFilter" object (see mFilter).

## Details

Almost all filters in this package can be put into the following framework. Given a time series $$\{x_t\}^T_{t=1}$$ we are interested in isolating component of $$x_t$$, denoted $$y_t$$ with period of oscillations between $$p_l$$ and $$p_u$$, where $$2 \le p_l < p_u < \infty$$.

Consider the following decomposition of the time series $$x_t = y_t + \bar{x}_t$$ The component $$y_t$$ is assumed to have power only in the frequencies in the interval $$\{(a,b) \cup (-a,-b)\} \in (-\pi, \pi)$$. $$a$$ and $$b$$ are related to $$p_l$$ and $$p_u$$ by $$a=\frac{2 \pi}{p_u}\ \ \ \ \ {b=\frac{2 \pi}{p_l}}$$

If infinite amount of data is available, then we can use the ideal bandpass filter $$y_t = B(L)x_t$$ where the filter, $$B(L)$$, is given in terms of the lag operator $$L$$ and defined as $$B(L) = \sum^\infty_{j=-\infty} B_j L^j, \ \ \ L^k x_t = x_{t-k}$$ The ideal bandpass filter weights are given by $$B_j = \frac{\sin(jb)-\sin(ja)}{\pi j}$$ $$B_0=\frac{b-a}{\pi}$$

The Hodrick-Prescott filter obtains the filter weights $$\hat{B}_j$$ as a solution to $$\hat{B}_{j}= \arg \min E \{ (y_t-\hat{y}_t)^2 \} = \arg \min \left\{ \sum^{T}_{t=1}(y_t-\hat{y}_{t})^2 + \lambda\sum^{T-1}_{t=2}(\hat{y}_{t+1}-2\hat{y}_{t}+\hat{y}_{t-1})^2 \right\}$$

The Hodrick-Prescott filter is a finite data approximation with following moving average weights $$\hat{B}_j=\frac{1}{2\pi}\int^{\pi}_{-\pi} \frac{4\lambda(1-\cos(\omega))^2}{1+4\lambda(1-\cos(\omega))^2}e^{i \omega j} d \omega$$

If drift=TRUE the drift adjusted series is obtained as $$\tilde{x}_{t}=x_t-t\left(\frac{x_{T}-x_{1}}{T-1}\right), \ \ t=0,1,\dots,T-1$$ where $$\tilde{x}_{t}$$ is the undrifted series.

## References

M. Baxter and R.G. King. Measuring business cycles: Approximate bandpass filters. The Review of Economics and Statistics, 81(4):575-93, 1999.

L. Christiano and T.J. Fitzgerald. The bandpass filter. International Economic Review, 44(2):435-65, 2003.

J. D. Hamilton. Time series analysis. Princeton, 1994.

R.J. Hodrick and E.C. Prescott. Postwar US business cycles: an empirical investigation. Journal of Money, Credit, and Banking, 29(1):1-16, 1997.

R.G. King and S.T. Rebelo. Low frequency filtering and real business cycles. Journal of Economic Dynamics and Control, 17(1-2):207-31, 1993.

D.S.G. Pollock. Trend estimation and de-trending via rational square-wave filters. Journal of Econometrics, 99:317-334, 2000.

mFilter, bwfilter, cffilter, bkfilter, trfilter

## Examples

Run this code
# NOT RUN {
## library(mFilter)

data(unemp)

unemp.hp <- hpfilter(unemp)
plot(unemp.hp)
unemp.hp1 <- hpfilter(unemp, drift=TRUE)
unemp.hp2 <- hpfilter(unemp, freq=800, drift=TRUE)
unemp.hp3 <- hpfilter(unemp, freq=12,type="frequency",drift=TRUE)
unemp.hp4 <- hpfilter(unemp, freq=52,type="frequency",drift=TRUE)

par(mfrow=c(2,1),mar=c(3,3,2,1),cex=.8)
plot(unemp.hp1$x, ylim=c(2,13), main="Hodrick-Prescott filter of unemployment: Trend, drift=TRUE", col=1, ylab="") lines(unemp.hp1$trend,col=2)
lines(unemp.hp2$trend,col=3) lines(unemp.hp3$trend,col=4)
lines(unemp.hp4$trend,col=5) legend("topleft",legend=c("series", "lambda=1600", "lambda=800", "freq=12", "freq=52"), col=1:5, lty=rep(1,5), ncol=1) plot(unemp.hp1$cycle,
main="Hodrick-Prescott filter of unemployment: Cycle,drift=TRUE",
col=2, ylab="", ylim=range(unemp.hp4$cycle,na.rm=TRUE)) lines(unemp.hp2$cycle,col=3)
lines(unemp.hp3$cycle,col=4) lines(unemp.hp4$cycle,col=5)
## legend("topleft",legend=c("lambda=1600", "lambda=800",
## "freq=12", "freq=52"), col=1:5, lty=rep(1,5), ncol=1)

par(opar)
# }


Run the code above in your browser using DataLab