Skip to content
Prev 9613 / 15274 Next

Causal version of HP filter and Kernel Smoothing in R?

Being interpellated I provide cursory feedback on the topic. R-code illustrating the HP filter (symmetric or real-time) is proposed below.



Modern (traditional) filter design goes back to early work by Wiener and Kolmogorov (WK). State-space models (Kalman-filter/smoothing) is an alternative formulation - parameterization - of the problem which is found more convenient/appealing by proponents: Harvey has highlighted some of the natural appeal in economic applications. In general, both approaches could replicate each other by suitable reformulation (parameterization) of models. So ultimately it is a matter of taste which is to be preferred. I frequently use state-space for didactical purposes (not that the filter is `simpler'; but `outputs' are appealing to non-experts).



HP-filter goes back to ideas proposed by Whittaker: it is ultimately a tradeoff between `fit' and `smoothness'. One can derive an ARIMA-model which replicates HP perfectly (it is a model with a double unit-root in frequency zero).



All these approaches are pure mean-square incarnations: real-time filter minimize the mean-square error between concurrent and final (symmetric) filters assuming that the (implicit or explicit) data-model is `true'.



My filter-design emphasizes a more general `customized' perspective: one can replicate ordinary mean-square designs (WK, Kalman, HP). But one can also emphasize alternative research priorities such as `timeliness', `noise suppression', `accuracy'. Depending on your priorities these aspects might be more relevant - to you -.  customization means that the user can tweak the optimization criterion in order to match his individual priorities. My experience is that practitioners frequently assign priorities (in forecasting) differently than assumed by the `mean-square' paradigm.



HP-filter can be implemented in R as follows:



library(mFilter)

# articial data: white noise (not very clever because HP assumes a double unit-root)
set.seed(1)
len<-201
eps<-rnorm(len)

# lambda=1600 is a typical setting for working with quarterly GDP

lambda_hp<-1600
eps.hp <- hpfilter(eps,type="lambda", freq=lambda_hp)
# plot data (here noise) and HP-trend

plot(eps.hp$x)
lines(eps.hp$trend,col=2)



# Here is the coefficient matrix: it is a full revision sequence.

parm<-eps.hp$fmatrix-diag(rep(1,len)
parm<--parm

# And a plot of the HP-filter coefficients: symmetric and real-time (concurrent) trend filters

ts.plot(cbind(parm[,c(length(parm[1,])/2,length(parm[1,]))]),lty=1:2)



Marc