Page 1 of 1

Time varying coefficient models with kalman filter

Posted: Tue May 26, 2009 10:23 am
by Aktar
Hi all

I want build a time varying coefficent model with a kalman filter.

I have take the programme tvarying which come from example in user guide chap 10.13.2....

but i must make some modification because my regressions is not a VAR and my process is a return to normaility process. so i have program this

Code: Select all

calendar 2000 1 1
allocate 2008:5
open data Sspace.xls
data(format=xls, org=obs) / *
table
compute kstart=2001:1,kend=2008:5

equation kalmeq returns
# constant vol_g dif_inf dif_tx var_re libor abcp swap stan_poo_vol gdpre_rate ratio_cap_gdp
declare vector b(11) bmean(11)
ewise bmean(i)=(i==2)
compute bmean=b
associate(perm) kalmeq b

linreg returns
# constant vol_g dif_inf dif_tx var_re libor abcp swap stan_poo_vol gdpre_rate ratio_cap_gdp
compute rseesq=.9*%seesq

system kalmeq
kfset(constant,noscale,likelihood=likely) xxx
#rseesq
tvarying tvx
end(system)

compute pi5 = .20**2
compute pi2 = .5**2
compute pi3 = 10000.
compute pi7 = 0.00000001
compute pi8 = 1.00

dim xxx(11,11) tvx(11,11)
*
compute likely=%const(0.0)
compute b=bmean
compute xxx=%const(0.0)
compute xxx(1,1)=pi5*pi3*rseesq
do i=1,4
  compute decayfac=i**(2*.5)
  compute xxx(i+1,i+1)=pi5*decayfac
  compute xxx(i+5,i+5)=pi5*pi2*decayfac*rseesq
end do i
compute tvx=pi7*xxx
*
do time=kstart,kend
  if time==kstart
     kalman(start=time)
  else {
     compute b=pi8*b+(1-pi8)*bmean
     compute xxx=(pi8**2)*xxx
     kalman
     }
end do time
it don't work when i run the loop, i have nothing in the output

Re: Time varying coefficient models with kalman filter

Posted: Tue May 26, 2009 5:30 pm
by TomDoan
You might want to look at tsayp510.prg and tsayp533.prg (Tsay's textbook examples). Those estimate models with time-varying betas. It might be closer to what you want than you'll be getting from the "VAR" code that you're trying to modify.

Re: Time varying coefficient models with kalman filter

Posted: Wed May 27, 2009 5:16 pm
by Aktar
ok thanks,

but in tvarying prog which is the correspondence of hyperparameters Pi5 pi2 pi3 pi7 pi8 and how we must determinate it

Re: Time varying coefficient models with kalman filter

Posted: Thu May 28, 2009 9:13 am
by TomDoan
KALMAN has a COHISTORY option. There's an example of that on page 262 in the RATS 7 Reference Manual.

You're trying to adapt an example that is set up for a VAR to a completely different setting. For instance,

Code: Select all

ewise bmean(i)=(i==2)
is making the prior mean 1 for the 2nd variable, and zero for the others which is probably not appropriate in your case. Similarly, the prior standard deviations are being generated in a pattern designed for a VAR. There generally won't be as many hyperparameters governing time-varying betas. (That's what you're doing, isn't it?)

Re: Time varying coefficient models with kalman filter

Posted: Thu May 28, 2009 11:27 am
by Aktar
the problem it is that my state equation is an return to normality process (mean reverting) and i don't know how i can make this with dlm instruction. it is possible ?

Re: Time varying coefficient models with kalman filter

Posted: Thu May 28, 2009 1:31 pm
by Aktar
ok

i have try with my data and i have a graph with evolution of beta !! but i don't have made modification on different option because i don't know what are signification

it is possible to have some explanation on the command ? because i don't understand what it is write on user guide on manual ?

it is very important for me (thesis of master degree and i want make a good thing for have allocation) it is possible to have your msn tomdoan and see in "directe live" my different question ?

Code: Select all

dlm(y=returns,c=||1.0,stan_poo_vol||,sv=1.0,var=concentrate,exact,free=2,type=filter) / xstates vstates
what is it c and sv and xstates and vstates

i dont understanr what are

Code: Select all

DLM Setup Options 

A=RECTANGULAR or FRML[RECT] [identity matrix]
This gives the A matrices. The expression should evaluate to an NxN matrix.

C=RECTANGULAR or FRML[RECT] [zero matrix]
This gives the C matrices—it should evaluate to an NxM matrix.

Y=VECTOR or FRML[VECT]  [no measurement equation]
This gives the Y vectors—it should evaluate to an M vector

Y=VECTOR or FRML[VECT]  [no measurement equation]
This gives the Y vectors—it should evaluate to an M vector

SV=SYMMETRIC or FRML[SYMMETRIC] [zero]
This gives the covariance matrices of the v’s—it should evaluate to an MxM SYMMETRIC.

SW=SYMMETRIC or FRML[SYMMETRIC] [zero]
This gives the covariance matrices of the w’s—it should evaluate to an NxN SYMMETRIC array.

SX0=SYMMETRIC or FRML[SYMMETRIC] [zero]
This gives the covariance of X(0)—it should evaluate to an NxN SYMMETRIC array.

X0=VECTOR or FRML[VECTOR] [zero vector]
This gives the initial X vector—it should evaluate to an N vector.

Z=VECTOR or FRML[VECTOR] [zero]
This allows the inclusion of exogenous shifts in the state equation—it should evaluate to an N VECTOR.

B=RECTANGULAR or FRML[RECT] [NxN identity matrix]
This gives the loadings from the w's to the X's. It should evaluate to an NxL matrix. 

F=RECTANGULAR or FRML[RECT] [zero matrix]
This gives the B matrices. The expression should evaluate to an NxP matrix.

Q=SYMMETRIC or FRML[SYMMETRIC] [zero]
This gives the Q matrices—it should evaluate to an NxN SYMMETRIC.

R=SYMMETRIC or FRML[SYMMETRIC] [zero]
This gives the R matrices—it should evaluate to a PxP SYMMETRIC.

i don't understand what are this different command

if i want make a filter i just replace smoothed by filterde ?

and if i want modify initial valu how i can do ?

thanks thanks thanks M tomdoan :P

Re: Time varying coefficient models with kalman filter

Posted: Fri May 29, 2009 9:12 am
by Aktar
it is ok i have found on the user's guide for dlm instruction

Re: Time varying coefficient models with kalman filter

Posted: Mon Jun 01, 2009 11:27 am
by Aktar

Code: Select all

## MAT13. Store into Out-of-Range Matrix or Series Element
The Error Occurred At Location 0113 of loop/block
Line 4 of loop/block
when i run the prog Tvarying i've got this message ... What is the sens please ?

Re: Time varying coefficient models with kalman filter

Posted: Mon Jun 01, 2009 1:06 pm
by TomDoan
Aktar wrote:

Code: Select all

## MAT13. Store into Out-of-Range Matrix or Series Element
The Error Occurred At Location 0113 of loop/block
Line 4 of loop/block
when i run the prog Tvarying i've got this message ... What is the sens please ?
You have to initialize series prior to a loop if you are going to fill them up an entry at a time. We can't really help with this without seeing the whole program. Email that to support@estima.com along with your data set.