*
* Example of use of the ksmooth (Kalman smoothing) procedure
* This does a Hodrick-Prescott filter. Kalman smoothing is an approach
* related to, but different from, that used in the HPFILTER procedure.
* The results from using either process should be nearly identical:
* the only difference is in the starting conditions.
*
source ksmooth.src
*
cal 55 1 4
all 85:4
open data citismpl.rat
data(format=rats) / gnp82
log gnp82 / lgnp
*
* The model:
*
* G(t)=2G(t-1)-G(t-2)+w(t)
* y(t)=G(t)+u(t)
*
* The ratio of variances between u and w is the "lambda" in the
* HP filter criterion function.
*
* The state vector is (G(t),G(t-1))
*
compute nper = 85:4-55:1+1
declare VECTOR Y(nper)
declare VECT[VECTOR] c(nper)
compute lambda = 1600.0
compute [RECT] A=||2.0,-1.0|$
1.0, 0.0||
*
* Set all elements of c to (1.0,0.0)
* Set the elements of y
*
ewise c(i)=||1.0,0.0||
ewise y(i)=lgnp(54:4+i)
*
* Normalize variances on var(w)=1.0.
* (Ratios of variances are all that matter)
*
compute [SYMM] SW=|| 1.0|$
0.0 ,0.0||
*
* Initialize both elements of x0 to the initial value with a
* high variance
*
compute [VECT] x0=||lgnp(55:1),lgnp(55:1)||
compute [SYMM] v0=||4.0*lambda|$
0.0,4.0*lambda||
*
@ksmooth nper A C Y SW lambda x0 v0 xkk
set growth 55:1 85:4 = xkk(t)(1)
print / growth lgnp