An implementation in R using the dlm package is shown below. It's a bit hackish, but it does the job nonetheless. You may be able to take it and adjust it to your need. I may come back and tidy this up and put the corresponding equations to help.
library(dlm)
filterHP <- function(series,lambda=1600){
# Function description: HP filter using DLM package.
if(!"ts" %in% class(series)) stop("series is not a \"ts\" object.")
# Set state priors
level <- series[1]
slope <- mean(diff(series),na.rm=TRUE)
# Set up HP filter in a DLM model
model <- function(param){
trend <- dlmModPoly(dV = 1,
dW = c(0,1/lambda),
m0 = c(level,slope),
C0 = 2 * diag(2))
# AR(2) model
cycle <- dlmModARMA(ar = ARtransPars(c(0,0)),
sigma2 = 1e-07)
hp_filter_dlm <- trend + cycle
return(hp_filter_dlm)
}
# MLE Estimation
MLE <- dlmMLE(series,c(0.5,0.4),model)
# Estimated parameters
EstParams <- model(MLE$par)
# Smoothed series
Smooth_Estimates <- dlmSmooth(series,EstParams)
# Trend and Cycle
trend <- Smooth_Estimates$s[,1]
cycle <- series - trend
# Plot the data ---
par(mfrow = c(2,1),
oma = c(1,3,0,0) + 0.1,
mar = c(1.5,1,1,1) + 0.1)
plot(series,las=1,col="black")
lines(trend,col="blue")
legend("topleft",legend=c("Observed","Trend"),border=FALSE,bty="n",col=c("black","blue"),lwd=1)
title(main="HP Filter - Trend")
par(mar = c(1,1,1.5,1) + 0.1)
plot(cycle,las=1,col="red")
title(main="HP Filter - Cycle")
abline(h=0)
par(mfrow = c(1,1),
oma = c(0,0,0,0),
mar = c(5.1,4.1,4.1,2.1))
# Return the data
data <- ts.union(series,trend,cycle)
return(data)
}
# Load some data
data(USecon)
# Compare two implementations
filterHP(USecon[,"M1"],lambda=1600)
library(mFilter)
plot(hpfilter(USecon[,"M1"],freq=1600))
# The data used is annual, so arguably lambda = 1600 should really be 6.25.
Best Answer
A) Actually, your reference says "a stationary residual or cyclical component, $c_t$", while this calls $c_t$ a 'cyclical component'.
Maravall and del Rio (2001) say "a residual, $c_t$, to be called “cycle”."
B) This formulation follows from the form of $A$; the second difference of $\tau$ is the error term.
C) some other references you may find helpful:
http://www.reservebank.govt.nz/research/discusspapers/dp03_02.pdf
http://www.stat.itam.mx/seminarios/Resumens/p136.pdf (see p6 which is relevant to your B, but they appear to have a sign in the A-matrix wrong - c.f. your link, p13)
http://www3.istat.it/dati/pubbsci/contributi/Contributi/contr_2005/2005_07.pdf
http://www.terrapub.co.jp/journals/jjss/pdf/3801/38010041.pdf