mFilter-package {mFilter}R Documentation

Getting started with the mFilter package

Description

Getting started with the mFilter package

Details

This package provides some tools for decomposing time series into trend (smooth) and cyclical (irregular) components. The package implements come commonly used filters such as the Hodrick-Prescott, Baxter-King and Christiano-Fitzgerald filter.

For loading the package, type:

library(mFilter)

A good place to start learning the package usage is to examine examples for the mFilter function. At the R prompt, write:

example("mFilter")

For a full list of functions exported by the package, type:

ls("package:mFilter")

Each exported function has a corresponding man page (some man pages are common to more functions). Display it by typing

help(functionName).

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

Consider the following decomposition of the time series

xt=yt+xˉtx_t = y_t + \bar{x}_t

The component yty_t is assumed to have power only in the frequencies in the interval {(a,b)(a,b)}(π,π)\{(a,b) \cup (-a,-b)\} \in (-\pi, \pi). aa and bb are related to plp_l and pup_u by

a=2πpu     b=2πpla=\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

yt=B(L)xty_t = B(L)x_t

where the filter, B(L)B(L), is given in terms of the lag operator LL and defined as

B(L)=j=BjLj,   Lkxt=xtkB(L) = \sum^\infty_{j=-\infty} B_j L^j, \ \ \ L^k x_t = x_{t-k}

The ideal bandpass filter weights are given by

Bj=sin(jb)sin(ja)πjB_j = \frac{\sin(jb)-\sin(ja)}{\pi j}

B0=baπB_0=\frac{b-a}{\pi}

The finite sample approximation to the ideal bandpass filter uses the alternative filter

yt=B^(L)xt=j=n1n2B^t,jxt+jy_t = \hat{B}(L)x_t=\sum^{n_2}_{j=-n_1}\hat{B}_{t,j} x_{t+j}

Here the weights, B^t,j\hat{B}_{t,j}, of the approximation is a solution to

B^t,j=argminE{(yty^t)2}\hat{B}_{t,j}= \arg \min E \{ (y_t-\hat{y}_t)^2 \}

The Christiano-Fitzgerald filter is a finite data approximation to the ideal bandpass filter and minimizes the mean squared error defined in the above equation.

Several band-pass approximation strategies can be selected in the function cffilter. The default setting of cffilter returns the filtered data yt^\hat{y_t} associated with the unrestricted optimal filter assuming no unit root, no drift and an iid filter.

If theta is not equal to 1 the series is assumed to follow a moving average process. The moving average weights are given by theta. The default is theta=1 (iid series). If theta=(θ1,θ2,)=(\theta_1, \theta_2, \dots) then the series is assumed to be

xt=μ+1rootxt1+θ1et+θ2et1+x_t = \mu + 1_{root} x_{t-1} + \theta_1 e_t + \theta_2 e_{t-1} + \dots

where 1root=11_{root}=1 if the option root=1 and 1root=01_{root}=0 if the option root=0, and ete_t is a white noise.

The Baxter-King filter is a finite data approximation to the ideal bandpass filter with following moving average weights

yt=B^(L)xt=j=nnB^jxt+j=B^0xt+j=1nB^j(xtj+xt+j)y_t = \hat{B}(L)x_t=\sum^{n}_{j=-n}\hat{B}_{j} x_{t+j}=\hat{B}_0 x_t + \sum^{n}_{j=1} \hat{B}_j (x_{t-j}+x_{t+j})

where

B^j=Bj12n+1j=nnBj\hat{B}_j=B_j-\frac{1}{2n+1}\sum^{n}_{j=-n} B_{j}

The Hodrick-Prescott filter obtains the filter weights B^j\hat{B}_j as a solution to

B^j=argminE{(yty^t)2}=argmin{t=1T(yty^t)2+λt=2T1(y^t+12y^t+y^t1)2}\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

B^j=12πππ4λ(1cos(ω))21+4λ(1cos(ω))2eiωjdω\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

The digital version of the Butterworth highpass filter is described by the rational polynomial expression (the filter's z-transform)

λ(1z)n(1z1)n(1+z)n(1+z1)n+λ(1z)n(1z1)n\frac{\lambda(1-z)^n(1-z^{-1})^n}{(1+z)^n(1+z^{-1})^n+\lambda(1-z)^n(1-z^{-1})^n}

The time domain version can be obtained by substituting zz for the lag operator LL.

Pollock (2000) derives a specialized finite-sample version of the Butterworth filter on the basis of signal extraction theory. Let sts_t be the trend and ctc_t cyclical component of yty_t, then these components are extracted as

yt=st+ct=(1+L)n(1L)dνt+(1L)ndεty_t=s_t+c_t=\frac{(1+L)^n}{(1-L)^d}\nu_t+(1-L)^{n-d}\varepsilon_t

where νtN(0,σν2)\nu_t \sim N(0,\sigma_\nu^2) and εtN(0,σε2)\varepsilon_t \sim N(0,\sigma_\varepsilon^2).

Let TT be even and define n1=T/pun_1=T/p_u and n2=T/pln_2=T/p_l. The trigonometric regression filter is based on the following relation

yt=j=n2n1{ajcos(ωjt)+bjsin(ωjt)}{y}_t=\sum^{n_1}_{j=n_2}\left\{ a_j \cos(\omega_j t) + b_j \sin(\omega_j t) \right\}

where aja_j and bjb_j are the coefficients obtained by regressing xtx_t on the indicated sine and cosine functions. Specifically,

aj=T2t=1Tcos(ωjt)xt,   a_j=\frac{T}{2}\sum^{T}_{t=1}\cos(\omega_j t) x_t,\ \ \ for j=1,,T/21j=1,\dots,T/2-1

aj=T2t=1Tcos(πt)xt,   a_j=\frac{T}{2}\sum^{T}_{t=1}\cos(\pi t) x_t,\ \ \ for j=T/2j=T/2

and

bj=T2t=1Tsin(ωjt)xt,   b_j=\frac{T}{2}\sum^{T}_{t=1}\sin(\omega_j t) x_t,\ \ \ for j=1,,T/21j=1,\dots,T/2-1

bj=T2t=1Tsin(πt)xt,   b_j=\frac{T}{2}\sum^{T}_{t=1}\sin(\pi t) x_t,\ \ \ for j=T/2j=T/2

Let B^(L)xt\hat{B}(L) x_t be the trigonometric regression filter. It can be showed that B^(1)=0\hat{B}(1)=0, so that B^(L)\hat{B}(L) has a unit root for t=1,2,,Tt=1,2,\dots,T. Also, when B^(L)\hat{B}(L) is symmetric, it has a second unit root in the middle of the data for tt. Therefore it is important to drift adjust data before it is filtered with a trigonometric regression filter.

If drift=TRUE the drift adjusted series is obtained as

x~t=xtt(xTx1T1),  t=0,1,,T1\tilde{x}_{t}=x_t-t\left(\frac{x_{T}-x_{1}}{T-1}\right), \ \ t=0,1,\dots,T-1

where x~t\tilde{x}_{t} is the undrifted series.

Author(s)

Mehmet Balcilar, mehmet@mbalcilar.net

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.

See Also

mFilter-methods for listing all currently available mFilter methods. For help on common interface function "mFilter", mFilter. For individual filter function usage, bwfilter, bkfilter, cffilter, hpfilter, trfilter.


[Package mFilter version 0.1-5 Index]