Kalman Filter

Use this forum to post questions about syntax problems or general programming issues. Questions on implementing a particular aspect of econometrics should go in "Econometrics Issues" below.
JohnV

Kalman Filter

Unread post by JohnV »

Hi,
I am trying to estimate the Kalman filter. I am using notation from Hamilton(1994), the chapter on the Kalman filter.
EE(state equation) and PP are the forecasting equations. E and P are array series used for storing. E1 and P1 are P and E lagged.
I don't think I am using E and P correctly in setting up the recursion. That is where I am getting the error.

Can you take a look and see where I am going wrong? The program is below.
Thanks in advance.


dec frml[vec] y
frml y = ||IP|EM|PDI||

dec vec[series] E(4)
dec symmetric[series] P(4,4)

dec rec F(4,4)
dec vec A(3)

dec rec H(3,4)
dec rec R(3,3)

dec frml[vec] EE
dec frml[symmetric] PP

dec frml[vec] error
dec frml[rec] covar

dec vec E1(4)

vcv(mat=P1,noprint)
# IP EM PDI
com E1 = ||0,0,0,0||

com q1 = 0.0000075, q2 = 0.0000023, q3 = 0.0000069, q4 = 0.0000089
com Q = ||q1,0,0,0|0,q2,0,0|0,0,q3,0|0,0,0,q4||

com r1 = 0.0000056 , r2 = 0.0000042 , r3 = 0.0000065
com R = ||r1,0,0|0,r2,0|0,0,r3||

com f1 = 0.25, f2 = .75, f3 = 0.89, f4=0.5
com F = ||f1,0,0,0|0,f2,0,0|0,0,0,f3|0,0,0,f4||

com h1 = 0.5, h2 = 0.9, h3 = 0.23

com H = ||h1,1,0,0|h2,0,1,0|h3,0,0,1||
com A = ||0.0002,0.0006,0.0009||

nonlin(parmset=QQ) q1 q2 q3 q4
nonlin(parmset=RR) r1 r2 r3
nonlin(parmset=FF) f1 f2 f3
nonlin(parmset=HH) h1 h2 h3
nonlin(parmset=AA) A

frml error = y-tr(H)*E1
frml covar = H*P1*tr(H) + R

frml PP = F*(P1-P1*H*inv(H*P1*tr(H) + R)*tr(H)*P1)*tr(F) + Q
frml EE = F*E1 + F*P1*H*inv(H*P1*tr(H) + R)*(y-tr(H)*E1)

frml Likely = %logdensity(covar,error)
frml Logly = (E1 = E(t)), P1=P(t), (EE=E1(t)),(PP=P1(t)),(%pt(E,t,EE)),(%pt(P,t,PP)),log(Likely)
## SX20. Expected , Here
>>>>E1 = E(t)), P1=P(t)<<<<


maximize(parset=QQ+RR+FF+HH+AA,method=simplex,iter=20,cvcrit=0.000001) logly 15 642
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Kalman Filter

Unread post by TomDoan »

First off, why aren't you using DLM? It would be much easier.

P is a SYMMETRIC[SERIES]. To get the ensemble at time T, you need %XT(P,T). If it were a SERIES[SYMMETRIC] (organized the other way around) you would be able to just reference P.
JohnV

Re: Kalman Filter

Unread post by JohnV »

Hi Tom,
Sorry I didn't know about DLM. I would still like to finish the program, just for my understanding.
I set up E and P as series of arrays. I don't know how to assign values to a series of arrays. I tried using set but that gave an error.
Also, I am getting an error when I try to assign E and P to EE and PP, in the last line when building up the likelyhood.
Part of the program is below, with errors I am getting.

dec series[vec] E
dec series[sym] P

vcv(mat=P1,noprint)
# IP EM PDI

set P = P1
## SX22. Expected Type SERIES, Got SERIES(SYMMETRIC) Instead
>>>>set P <<<<
set E = ||0,0,0,0||


frml error = y-tr(H)*E{1}
frml covar = H*P{1}*tr(H) + R

frml PP = F*(P{1}-P{1}*H*inv(H*P{1}*tr(H) + R)*tr(H)*P{1})*tr(F) + Q
frml EE = F*E{1} + F*P{1}*H*inv(H*P{1}*tr(H) + R)*(y-tr(H)*E{1})

frml Likely = %logdensity(covar,error)
frml Logly = (EE = E(t)), (PP=P(t)),log(Likely)
## SX15. Trying to Store into Constant or Expression. Called Parameter by Value?
>>>>frml Logly = (EE = <<<<


maximize(parset=QQ+RR+FF+HH+AA,method=simplex,iter=20,cvcrit=0.000001) logly 15 642
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Kalman Filter

Unread post by TomDoan »

Use GSET, not SET to create a SERIES of any type of matrix.

On the FRML, you have the E and EE's reversed---you need the FRML on the right of =, not the left.
Post Reply