*
*  hpfilter.prg
*  Example 12.3
*
cal 1959 1 4
allocate 2000:4
open data haversmp.rat
data(format=rats) / gdph
log gdph / lgdp
*
*  The model:
*
*    g(t)=2g(t-1)-g(t-2)+e(t)
*    y(t)=g(t)+v(t)
*
*  The ratio of variances between v and e is the "lambda" in the
*  HP filter criterion function.
*
*  The state vector is (g(t),g(t-1))
*
compute lambda = 1600.0
*
*  Since lambda is being treated as fixed, the only component which needs
*  to be a frml is y. As relative variances are all that matter, sw is
*  pegged at 1 with sv being lambda. The initial state is set to the first
*  data value for both lags of g, with a high variance and perfect correlation.
*
dec rect a
dec rect c
dec frml[vect] y
dec symm sw
dec vect x0
dec symm v0
dec vect sv
*
compute a  = ||2.0,-1.0|1.0,0.0||
compute c  = ||1.0|0.0||
frml    y  = ||lgdp||
compute sw = ||1.0|0.0,0.0||
compute sv = ||lambda||
compute x0 = ||lgdp(1959:1),lgdp(1959:1)||
compute v0 = ||4.0*lambda|4.0*lambda,4.0*lambda||
*
dlm(a=a,c=c,y=y,sv=sv,sw=sw,x0=x0,sx0=v0,type=smooth) 1959:1 2000:4 states
set hpsmooth = states(t)(1)
set cycle    = lgdp-hpsmooth
*
graph(window='HP filter') 2
# lgdp
# hpsmooth

