a question about state space model with exogenous regressors

Discussion of State Space and Dynamic Stochastic General Equilibrium Models
fan
Posts: 215
Joined: Wed Jun 19, 2013 5:14 pm

a question about state space model with exogenous regressors

Unread post by fan »

Hello,
I am using state space techniques for the 1st time, I have a model but I am not sure how to represent it in state space form.

Here is my model: the measurement equation is Yt=B0X0,t+B1X1,t+B2X2,t+Ut and the transition equation is X2,t=Ct+X2,t-1+Dummyt+Et. X0 and X1 are the exogenous variables and X2 is the unobserved state.

Any help is deeply appreciated.
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: a question about state space model with exogenous regres

Unread post by TomDoan »

fan wrote:Hello,
I am using state space techniques for the 1st time, I have a model but I am not sure how to represent it in state space form.

Here is my model: the measurement equation is Yt=B0X0,t+B1X1,t+B2X2,t+Ut and the transition equation is X2,t=Ct+X2,t-1+Dummyt+Et. X0 and X1 are the exogenous variables and X2 is the unobserved state.

Any help is deeply appreciated.
Use the MU option to handle the B0 x X0 + B1 x X1, the Z option to handle the C+Dummy in the transition. As you have that written, you'll need the variance of E to be pegged to 1 to avoid a lack of identification between B2 and that variance. See harveyp393.rpf for an example of the use of MU.
fan
Posts: 215
Joined: Wed Jun 19, 2013 5:14 pm

Re: a question about state space model with exogenous regres

Unread post by fan »

TomDoan wrote:
fan wrote:Hello,
I am using state space techniques for the 1st time, I have a model but I am not sure how to represent it in state space form.

Here is my model: the measurement equation is Yt=B0X0,t+B1X1,t+B2X2,t+Ut and the transition equation is X2,t=Ct+X2,t-1+Dummyt+Et. X0 and X1 are the exogenous variables and X2 is the unobserved state.

Any help is deeply appreciated.
Use the MU option to handle the B0 x X0 + B1 x X1, the Z option to handle the C+Dummy in the transition. As you have that written, you'll need the variance of E to be pegged to 1 to avoid a lack of identification between B2 and that variance. See harveyp393.rpf for an example of the use of MU.

Thank you so much, Pro. Doan. It really helps. By the way, is there is any example I can refer to in Kim and Nelson's book? I do not have the harvy book. I am sorry for asking the additional question
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: a question about state space model with exogenous regres

Unread post by TomDoan »

No. Kim and Nelson really focus on MS models, so the state space examples are rather simple. The Harvey example isn't that hard to figure out just from the code.
fan
Posts: 215
Joined: Wed Jun 19, 2013 5:14 pm

Re: a question about state space model with exogenous regres

Unread post by fan »

TomDoan wrote:No. Kim and Nelson really focus on MS models, so the state space examples are rather simple. The Harvey example isn't that hard to figure out just from the code.
Dear Pro. Doan Thank you for the reply. I looked the code of the example. I think I get the basic idea about what the example tries to show. However, I still have questions about some details and I am seeking for your help. Correct me if I am making mistakes, please.

Basically, the estimation model for the example is that
Unemplt=trendt+Unemplt-1+output+output-1+output-2+Et
Trendt=trendt-1+trendratet+Ut
Trendratet=trendratet-1+Vt

frml(reg, vectpr=b) linereq
#empl{1} output{0 1}
Compute b=%zero(3,1)
These are codes ask Rats to run a simple linear regression for unemployment on its own lag and the output, output lagged once and output lagged twice and calculate the coefficients on three explanatory variables and store them in b.
I want to make sure whether b=%zero(3,1) means keep none-zero elements in a 3x1 vector.

@localdlm(type=trend, shocks=both, a=at,c=ct,f=ft)
This code is for the only state variable in the model (trend).

Nonlin sigspeps sigsqeta sigsqzeta b
This code is to specify the coefficients we are interested about. Sigspeps=var(Et), sigspeps=var(Ut) and sigsgzeta=var(Vt)

Compute sigsqeps=.5*1.e+4*%seesq, sigsqeta=.01*sigsqeps, sigsqzeta=sigsqeta
I am not too sure what this code for. Could you please explain it to me? The tip says that the variances have a naturally very small magnitude, so we force the estimated versions to be 4 orders of magnitude larger.

dlm(a=at,c=ct,f=ft,mu=lineareq,sv=1.e-4*sigsqeps,sw=%diag(||1.e-4*sigsqeta,1.e-4*sigsqzeta||),$
exact,y=empl,method=bfgs,vhat=vhat,svhat=svhat,type=smoothed) 2 * xstates
I know this is code for running state space model in Rats. However, I am not sure why sv=1.e-4*sigsqeps,sw=%diag(||1.e-4*sigsqeta,1.e-4*sigsqzeta||) and 2 * xstates. Could you please explain it to me?

set stdu 2 * = %scalar(vhat)/sqrt(%scalar(svhat))
This is code for generating stud2.

@STAMPdiags stdu
This is code for running state space model diagnostics on stud.

I am terribly sorry for asking so many questions to you at once. I am looking forward to hearing from you soon.

Best regards,
Fan
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: a question about state space model with exogenous regres

Unread post by TomDoan »

fan wrote:
TomDoan wrote:No. Kim and Nelson really focus on MS models, so the state space examples are rather simple. The Harvey example isn't that hard to figure out just from the code.
Dear Pro. Doan Thank you for the reply. I looked the code of the example. I think I get the basic idea about what the example tries to show. However, I still have questions about some details and I am seeking for your help. Correct me if I am making mistakes, please.

Basically, the estimation model for the example is that
Unemplt=trendt+Unemplt-1+output+output-1+output-2+Et
Trendt=trendt-1+trendratet+Ut
Trendratet=trendratet-1+Vt
Actually, no. The state-space model is

empl(t)=trend(t) + b1 x empl(t-1) + b2 x output(t) + b3 x output(t-1) + e(t)
Trendt=trendt-1+trendratet+Ut
Trendratet=trendratet-1+Vt

The dependent variable is employment not un... and the model with the extra lags is just for that first LINREG and isn't used later.
fan wrote: frml(reg, vectpr=b) linereq
#empl{1} output{0 1}
Compute b=%zero(3,1)
These are codes ask Rats to run a simple linear regression for unemployment on its own lag and the output, output lagged once and output lagged twice and calculate the coefficients on three explanatory variables and store them in b.
I want to make sure whether b=%zero(3,1) means keep none-zero elements in a 3x1 vector.

@localdlm(type=trend, shocks=both, a=at,c=ct,f=ft)
This code is for the only state variable in the model (trend).

Nonlin sigspeps sigsqeta sigsqzeta b
This code is to specify the coefficients we are interested about. Sigspeps=var(Et), sigspeps=var(Ut) and sigsgzeta=var(Vt)

Compute sigsqeps=.5*1.e+4*%seesq, sigsqeta=.01*sigsqeps, sigsqzeta=sigsqeta
I am not too sure what this code for. Could you please explain it to me? The tip says that the variances have a naturally very small magnitude, so we force the estimated versions to be 4 orders of magnitude larger.
It's a combination of that, and the 1.e-4*...' in the next line. The problem is that the variances (for this example, and probably many where the dependent variable is the log (and not 100*log) of a quantity) are naturally quite small, on the order of 10^-5 to 10^-8. For numerical reasons, it's helpful to re-parameterize those to have magnitudes closer to 1.
fan wrote: dlm(a=at,c=ct,f=ft,mu=lineareq,sv=1.e-4*sigsqeps,sw=%diag(||1.e-4*sigsqeta,1.e-4*sigsqzeta||),$
exact,y=empl,method=bfgs,vhat=vhat,svhat=svhat,type=smoothed) 2 * xstates
I know this is code for running state space model in Rats. However, I am not sure why sv=1.e-4*sigsqeps,sw=%diag(||1.e-4*sigsqeta,1.e-4*sigsqzeta||) and 2 * xstates. Could you please explain it to me?
The scaling on the variances is described above. The 2 * is the range that you can use. Since you need the observable output{1}, 2 is the earliest that this can be computed.
fan wrote: set stdu 2 * = %scalar(vhat)/sqrt(%scalar(svhat))
This is code for generating stud2.

@STAMPdiags stdu
This is code for running state space model diagnostics on stud.

I am terribly sorry for asking so many questions to you at once. I am looking forward to hearing from you soon.

Best regards,
Fan
fan
Posts: 215
Joined: Wed Jun 19, 2013 5:14 pm

Re: a question about state space model with exogenous regres

Unread post by fan »

TomDoan wrote:
fan wrote:Hello,
I am using state space techniques for the 1st time, I have a model but I am not sure how to represent it in state space form.

Here is my model: the measurement equation is Yt=B0X0,t+B1X1,t+B2X2,t+Ut and the transition equation is X2,t=Ct+X2,t-1+Dummyt+Et. X0 and X1 are the exogenous variables and X2 is the unobserved state.

Any help is deeply appreciated.
Dear Tom, These are codes in my mind for my model. I am sure there are many mistaks, Could you please kindly point out them for me?

Code: Select all

Set trend = X2
dec frml zf[vect] zf
frml(reg,vector=b) lineareq Y
#X0{0} X1{0}
compute b=%zero((2,1)
@localdlm(type=trend,shocks=trend,a=at,c=ct,f=ft)
nonlin sigsqeps sigsqeta b d
Frml zf = ||constant+d*dummy||
dlm(a=at,c=ct,f=ft,mu=lineareq,z=zf, sv=1.e-4*sigsqeps,sw=%diag(||1.e-4*sigsqeta,1.e-4*sigsqzeta||),$
  exact,y=Y,method=bfgs,vhat=vhat,svhat=svhat,type=smoothed) 2 * xstates
Furthermore, I am not sure how to peg the variance of E to 1, Could you please show me how to do that?

Best regards,
Fan
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: a question about state space model with exogenous regres

Unread post by TomDoan »

fan wrote: Dear Tom, These are codes in my mind for my model. I am sure there are many mistaks, Could you please kindly point out them for me?

Code: Select all

Set trend = X2
dec frml zf[vect] zf
frml(reg,vector=b) lineareq Y
#X0{0} X1{0}
compute b=%zero((2,1)
@localdlm(type=trend,shocks=trend,a=at,c=ct,f=ft)
nonlin sigsqeps sigsqeta b d
Frml zf = ||constant+d*dummy||
dlm(a=at,c=ct,f=ft,mu=lineareq,z=zf, sv=1.e-4*sigsqeps,sw=%diag(||1.e-4*sigsqeta,1.e-4*sigsqzeta||),$
  exact,y=Y,method=bfgs,vhat=vhat,svhat=svhat,type=smoothed) 2 * xstates
Furthermore, I am not sure how to peg the variance of E to 1, Could you please show me how to do that?
I'm not sure what the set trend = x2 is supposed to be doing. The "trend" is estimated as part of the calculation.

This

dec frml zf[vect] zf

should be just

dec frml[vect] zf

compute b=%zero((2,1)

has an extra (

What is ZF supposed to be doing? The Z is a shift in the state transition model, so it needs to be a 2-vector to match the number of states.

Other than the ZF, this looks correct. You don't need to peg any of the variances as (unlike the first =equation you posted), you don't have a constant multiplier on the state-space trend---you just have that adding on.
fan
Posts: 215
Joined: Wed Jun 19, 2013 5:14 pm

Re: a question about state space model with exogenous regres

Unread post by fan »

TomDoan wrote:
fan wrote: Dear Tom, These are codes in my mind for my model. I am sure there are many mistaks, Could you please kindly point out them for me?

Code: Select all

Set trend = X2
dec frml zf[vect] zf
frml(reg,vector=b) lineareq Y
#X0{0} X1{0}
compute b=%zero((2,1)
@localdlm(type=trend,shocks=trend,a=at,c=ct,f=ft)
nonlin sigsqeps sigsqeta b d
Frml zf = ||constant+d*dummy||
dlm(a=at,c=ct,f=ft,mu=lineareq,z=zf, sv=1.e-4*sigsqeps,sw=%diag(||1.e-4*sigsqeta,1.e-4*sigsqzeta||),$
  exact,y=Y,method=bfgs,vhat=vhat,svhat=svhat,type=smoothed) 2 * xstates
Furthermore, I am not sure how to peg the variance of E to 1, Could you please show me how to do that?
I'm not sure what the set trend = x2 is supposed to be doing. The "trend" is estimated as part of the calculation.

This

dec frml zf[vect] zf

should be just

dec frml[vect] zf

compute b=%zero((2,1)

has an extra (

What is ZF supposed to be doing? The Z is a shift in the state transition model, so it needs to be a 2-vector to match the number of states.

Other than the ZF, this looks correct. You don't need to peg any of the variances as (unlike the first =equation you posted), you don't have a constant multiplier on the state-space trend---you just have that adding on.


Dear Tom,

Thank you so much for your quick reply. I think I did not make it clear to you what my model is. So here is the model again
Yt+1=B0X0,t+B1X1,t+B2X2,t+Ut+1 (measurement equation)
X2,t=Ct+X2,t-1+Dummyt+Et (transition equation)

The latest codes in my mind are:
frml(reg,vector=b) lineareq Y
#X0{1} X1{1}
compute b=%zero((2,1)

@localdlm(type=trend,shocks=trend,a=at,c=ct,f=ft)
*since X2 is the only state variable in my model, I think I can borrow the code for the local trend model

dec frml[vect] zf
*since my transition equation includes a constant and a dummy variable, as you suggested earlier, I use Z option to handle the C+dummy. I guess I may incorrectly define it in the code. Could you please kindly correct my mistake?
Frml zf = IIconstant+d*dummyII

nonlin b d
*I am interested in knowing the coefficients for both the exogenous variables in the measurement equation and the dummy variable for my project.
dlm(a=at,c=ct,f=ft,mu=lineareq,z=zf, sv=1.e-4*sigsqeps,sw=1.e-4*sigsqeta,exact,y=Y,method=bfgs,vhat=vhat,svhat=svhat,type=smoothed) xstates
Do you think now I need to peg the variance of E to 1? If so, could you please show me how to do that?

Once again, I am terribly sorry for asking so many questions at once.

Best regards,
Fan
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: a question about state space model with exogenous regres

Unread post by TomDoan »

I'm confused about this:

X2,t=Ct+X2,t-1+Dummyt+Et (transition equation)

What are Ct and Dummyt supposed to be? Is X supposed to be a drifting random walk where the drift changes? If so, why do you have no parameters? Is the drift a rate of 1 for the non-dummied sample and 2 for the dummied part? That's how you wrote that. Is the local trend model something you copied from the Harvey example, since as you have this written, you don't want that?
fan
Posts: 215
Joined: Wed Jun 19, 2013 5:14 pm

Re: a question about state space model with exogenous regres

Unread post by fan »

TomDoan wrote:I'm confused about this:

X2,t=Ct+X2,t-1+Dummyt+Et (transition equation)

What are Ct and Dummyt supposed to be? Is X supposed to be a drifting random walk where the drift changes? If so, why do you have no parameters? Is the drift a rate of 1 for the non-dummied sample and 2 for the dummied part? That's how you wrote that. Is the local trend model something you copied from the Harvey example, since as you have this written, you don't want that?
Dear Tom,
Thank you so much for your quick reply. Actually, I made a mistake here. My X2,t=Const+A*X2,t-1+D*Dummyt+Et (transition equation). So X is supposed to be a AR(1) process with a drift and the drift changes with the time. The drift is a rate of Constant for the non-dummied sample and Constant +D for the dummied part. You are right that I copied the local trend model from the Harvey example. Now, I think I should include the Z option into it. Is it o.k. for me to do in this way
dec frml[vect] zf

frml zf = IIconstant+d*dummyII

@localdlm(type=trend,shocks=trend,a=at,c=ct,f=ft, z=zf)

nonlin b d

dlm(a=at,c=ct,f=ft,mu=lineareq,z=zf, sv=1.e-4*sigsqeps,sw=1.e-4*sigsqeta,exact,y=Y,method=bfgs,vhat=vhat,svhat=svhat,type=smoothed) xstates

Best regards,
Fan
fan
Posts: 215
Joined: Wed Jun 19, 2013 5:14 pm

Re: a question about state space model with exogenous regres

Unread post by fan »

TomDoan wrote:I'm confused about this:

X2,t=Ct+X2,t-1+Dummyt+Et (transition equation)

What are Ct and Dummyt supposed to be? Is X supposed to be a drifting random walk where the drift changes? If so, why do you have no parameters? Is the drift a rate of 1 for the non-dummied sample and 2 for the dummied part? That's how you wrote that. Is the local trend model something you copied from the Harvey example, since as you have this written, you don't want that?
Dear Tom
Sorry for keeping asking questions. I took another look at my model; I found I was worry to use the local trend model.
Given my model
Yt+1=B0X0,t+B1X1,t+B2X2,t+Ut+1
X2,t=Const+A*X2,t-1+D*Dummyt+Et,
I think my initial idea to use @localdlm(type=trend,shocks=trend,a=at,c=ct,f=ft, z=zf) is wrong. For the local trend model, a=at and c=ct are both supposed to be one. However, in my model, a=A, c=B2 (they are not fixed at one) and z=const+d*dummy. Could you please kindly advise me how to write the code for the transition equation in my model?
Best regards,
Fan
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: a question about state space model with exogenous regres

Unread post by TomDoan »

First of all, it would be helpful if you typed RATS commands into RATS itself, instead of into a word processor or whatever you're using. Things like the ||'s are being translated into non-standard characters.

Also, you can attach GIF files for formulas, as in:
formula.gif
formula.gif (1.06 KiB) Viewed 19216 times
The "constant" is a free parameter, isn't it? You didn't allow for that. The A is also a free parameter. With your model, the A "matrix" would be just A, the C matrix would be just 1.0. Z is C+D*DUMMY. You don't need F since you have just the one state. SW is a single parameter, not a matrix. So in total you have A, C, D, the regression parameters, and the two variances as your free parameters.
fan
Posts: 215
Joined: Wed Jun 19, 2013 5:14 pm

Re: a question about state space model with exogenous regres

Unread post by fan »

TomDoan wrote:First of all, it would be helpful if you typed RATS commands into RATS itself, instead of into a word processor or whatever you're using. Things like the ||'s are being translated into non-standard characters.

Also, you can attach GIF files for formulas, as in:
The attachment formula.gif is no longer available
The "constant" is a free parameter, isn't it? You didn't allow for that. The A is also a free parameter. With your model, the A "matrix" would be just A, the C matrix would be just 1.0. Z is C+D*DUMMY. You don't need F since you have just the one state. SW is a single parameter, not a matrix. So in total you have A, C, D, the regression parameters, and the two variances as your free parameters.
Dear Tom,

Thank you for your reply. Again my model is
my model.gif
my model.gif (1.8 KiB) Viewed 19084 times
I am interested in knowing all the coefficients in the model. Could you please kindly check my Rats commands for the model?

frml(reg,vector=b) lineareq
# dp{1} tsp{1} dsp{1}
computer b=%zero(3,1)


nonlin b ph1 beta4

frml a=||1.0||
frml z=||mu+ph1*democraticdummy||
frml c=||beta4||
frml sw=||sn^2||

compute f=||1.0||

dlm(presample=ergodic,a=a,z=z,sw=sw,c=c,f=f,y=return,method=bfgs,type=filter)/ states0

Thank you so much for your help.
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: a question about state space model with exogenous regres

Unread post by TomDoan »

You're still missing MU from your parameter set. You're also missing the SV for the measurement error. And, as I mentioned much earlier, if you use a non-zero multiplier on the "sentiment" variable in the measurement equation, you have to peg the variance of SW to 1. I would suggest that a simpler approach would be to make beta4=1 (so that C is just 1) and let SW be free as you have it written right now.
Post Reply