Page 1 of 1
Another Time-Varying Parameters Example
Posted: Sat Jan 22, 2011 3:17 pm
by TomDoan
This is a somewhat cleaner example of estimation of a time-varying coefficients model, taken from Kim and Nelson,
State space models with regime-switching.
- tvp.xls
- (11.72 KiB) Downloaded 1414 times
Re: Another Time-Varying Parameters Example
Posted: Wed Feb 02, 2011 7:20 pm
by Henrique Andrade
Dear Tom,
I'm trying to use your Kim-Nelson code with my data but it isn't working. Could you please take a look at the following code?
Code: Select all
open data "Users/henrique/Desktop/Dados.xls"
cal(m) 1980:01
data(format=xls,org=columns) 1980:01 2010:05 prod ipca ipca_l ipca12m_e com_pib fin1
set dummy1 = t==2002:10.or.t==2002:11.or.t==2007:12.or.t==2008:5.or.t==2009:2
set inflacao = ((ipca/ipca{1})-1)*100
set livres = ((ipca_l/ipca_l{1})-1)*100
filter(type=hp,tuning=14400) prod / prod_hp
set hiato = ((prod/prod_hp)-1)*100
linreg livres 2001:11 2010:05
# inflacao{1} ipca12m_e hiato{1} com_pib{1} dummy1
equation(lastreg) mdeq
dec vect sigmav(%nreg)
dec real sigmae
compute sigmae=.5*sqrt(%seesq)
compute sigmav=.01*%stderrs
nonlin sigmae sigmav
dlm(y=livres,c=%eqnxvector(mdeq,t),sw=%diag(sigmav.^2),sv=sigmae^2,$
presample=diffuse,method=bfgs,condition=10) 2001:11 2010:05 xstates vstates
set b0 = xstates(t)(1)
graph(footer="Figure 3.9 Time-varying regression coefficient b0")
# b0 2001:11 *
set b1 = xstates(t)(2)
graph(footer="Figure 3.10 Time-varying regression coefficient b1")
# b1 2001:11 *
set b2 = xstates(t)(3)
graph(footer="Figure 3.11 Time-varying regression coefficient b2")
# b2 2001:11 *
set b3 = xstates(t)(4)
graph(footer="Figure 3.12 Time-varying regression coefficient b3")
# b3 2001:11 *
set b4 = xstates(t)(5)
graph(footer="Figure 3.13 Time-varying regression coefficient b4")
# b4 2001:11 *
I'm getting this message:
## MAT15. Subscripts Too Large or Non-Positive
Error was evaluating entry 1
Best regards,
Re: Another Time-Varying Parameters Example
Posted: Wed Feb 02, 2011 8:39 pm
by TomDoan
Add the range to the SET instructions which pull information out of xstates:
set b0 2001:11 2010:05 = xstates(t)(1)
Re: Another Time-Varying Parameters Example
Posted: Thu Feb 03, 2011 8:01 pm
by Henrique Andrade
Dear Tom,
At first I would like to thank you for your quick response! Now I want to build a confidence interval for the parameters. Could you please take a look at my code?
Code: Select all
dec vect[series] b(5) lower(5) upper(5)
set b0 2001:11 2010:05 = xstates(t)(1)
set lower_b0 2001:11 2010:05 = b0-1.5*sqrt(vstates(t)(1,1))
set upper_b0 2001:11 2010:05 = b0+1.5*sqrt(vstates(t)(1,1))
graph(footer="Time-varying regression coefficient b0") 3
# b0
# lower_b0
# upper_b0
In your opinion, this sounds correct?
Best,
Re: Another Time-Varying Parameters Example
Posted: Thu Feb 03, 2011 10:00 pm
by TomDoan
That looks correct.
Re: Another Time-Varying Parameters Example
Posted: Sat Feb 05, 2011 11:53 pm
by Henrique Andrade
Dear Tom,
Now I would like to fix some parameters (i.e. allow only one time varying parameter). Could please take a look at the following code (based on Kim-Nelson's code):
Kim-Nelson's Code
Code: Select all
open data tvp.xls
cal(q) 1959:3
data(format=xls,org=columns) 1959:03 1985:04 m1gr dintlag inflag surplag m1lag
linreg m1gr 1961:1 *
# constant dintlag inflag surplag m1lag
declare vect[series] b(5) lower(5) upper(5)
equation(lastreg) mdeq
dec vect sigmav(%nreg)
dec real sigmae
compute sigmae=.5*sqrt(%seesq)
compute sigmav=.01*%stderrs
nonlin sigmae sigmav
dlm(y=m1gr,c=%eqnxvector(mdeq,t),sw=%diag(sigmav.^2),sv=sigmae^2,$
presample=diffuse,method=bfgs,condition=10) 1959:3 1985:4 xstates vstates
Alternative Code
Code: Select all
display "sigmae = " sigmae
* sigmae = 0.31310
display "sigmav = " sigmav
* sigmav = 0.00157 6.63841e-04 7.26744e-04 0.00203 8.22955e-04
declare vector sigmavALT ;* WHERE "ALT" STANDS FOR "ALTERNATIVE"
compute sigmavALT = (||0.00157,6.63841e-04,7.26744e-04,0.00203,8.22955e-04||)
compute sigmaeALT = 0.31310
nonlin sigmavALT sigmaeALT
dlm(y=m1gr,c=%eqnxvector(mdeq,t),sw=%diag(sigmavALT.^2),sv=sigmaeALT^2,presample=diffuse,$
method=bfgs,condition=10) 1959:3 1985:4 xstatesALT vstatesALT
Using
sigmaALT I can choose which variable will be time varying. For instance, if I want to fix
b0,
b1,
b2, and
b3 I need to modify
sigmavALT in that way:
compute sigmavALT = (||0,0,0,0,8.22955e-04||)
Is this correct? Am I missing something important here?
Thanks in advance,
Re: Another Time-Varying Parameters Example
Posted: Sun Feb 06, 2011 7:21 am
by TomDoan
That's the correct setup. However, if you are looking at filtered estimates of the coefficients, then your "fixed" coefficients will change because they're being estimated with different amounts of data. The smoothed estimates of those four will be constant across the data set, while the two that are allowed to vary won't be.
Re: Another Time-Varying Parameters Example
Posted: Wed Feb 09, 2011 7:22 pm
by Henrique Andrade
Tom,
I'd altered my model to make only b4 to vary:
compute sigmavALT = (||0,0,0,0,8.22955e-04||)
dlm(y=m1gr,c=%eqnxvector(mdeq,t),sw=%diag(sigmavALT.^2),sv=sigmaeALT^2,presample=diffuse,$
method=bfgs,condition=10,type=smooth) 1959:3 1985:4 xstatesALT vstatesALT
But this didn't produce fixed (time invariant) parameters for b0...b3. Where is the error in my code?
Re: Another Time-Varying Parameters Example
Posted: Thu Feb 10, 2011 7:11 am
by TomDoan
You're using METHOD=BFGS with the SIGMAVALT vector (the whole one) as part of your parameter set. So it's estimating non-zero variances for those, hence the moving coefficients.
The simplest adjustment to get what you want is to add a START option to your DLM:
dlm(start=(sigmavalt(5)=sigmav5),y=m1gr,c=%eqnxvector(mdeq,t),sw=%diag(sigmavALT.^2),sv=sigmaeALT^2,presample=diffuse,$
method=bfgs,condition=10,type=smooth) 1959:3 1985:4 xstatesALT vstatesALT
and use only sigmav5 in the parameter set.
Re: Another Time-Varying Parameters Example
Posted: Thu Feb 17, 2011 9:47 pm
by Henrique Andrade
Dear Tom,
I'm trying the following command:
Code: Select all
compute sigmae=.5*sqrt(%seesq)
compute sigmav = (||0,0,0,0,8.22955e-04||)
nonlin sigmae sigmav
dlm(start=sigmav(5),y=m1gr,c=%eqnxvector(mdeq,t),sw=%diag(sigmav.^2),sv=sigmae^2,$
presample=diffuse,method=bfgs,condition=10,type=smooth) 1959:3 1985:4 xstates vstates
But all the coefficients are invariant. Where is my error?
Best regards,
Re: Another Time-Varying Parameters Example
Posted: Fri Feb 18, 2011 5:29 am
by TomDoan
I had a typo in the START option on mine---it should have read:
Code: Select all
dlm(start=(sigmavalt(5)=sigmav5),y=m1gr,c=%eqnxvector(mdeq,t),sw=%diag(sigmavALT.^2),sv=sigmaeALT^2,presample=diffuse,$
method=bfgs,condition=10,type=smooth) 1959:3 1985:4 xstatesALT vstatesALT
I've also corrected that in the original post. With your code:
Code: Select all
compute sigmae=.5*sqrt(%seesq)
compute sigmav = (||0,0,0,0,8.22955e-04||)
nonlin sigmae sigmav
dlm(start=sigmav(5),y=m1gr,c=%eqnxvector(mdeq,t),sw=%diag(sigmav.^2),sv=sigmae^2,$
presample=diffuse,method=bfgs,condition=10,type=smooth) 1959:3 1985:4 xstates vstates
it should have all the parameters
varying, not invariant. You're still including all the variances in the parameter set when you only want the fifth component.
Re: Another Time-Varying Parameters Example
Posted: Sun Feb 20, 2011 8:00 pm
by Henrique Andrade
Dear Tom,
I'm a little bit confused. My problem (a Phillips curve for Brazilian economy) can be represented in that way:
inf_f = beta1*inf(-1) + beta2*exp12m + beta3*gap(-1) + beta4(t)*trade(-1) + v(t)
b4(t) = b4(t-1) + w4(t)
Where:
inf_f - non monitored prices
inf - total inflation
exp12m - 12 month expected inflation
gap - output gap
trade - trade openness
According to equations (1) and (2) from RATS Reference Manual (p. RM-114) we have:
* Transition equation:
X(t) = A(t)*X(t-1) + Z(t) + F(t)*W(t)
* Measurement equation:
Y(t) = mu(t) + C'(t)X(t) + V(t)
So to set up my problem I need to define:
Y = inf_f
mu = ||inf{1}, exp12m,gap{1}||
C = trade
X = beta4(t)
A = 1
Z = 0
F = 1
My DLM command takes the form:
Code: Select all
dlm(y=inf_f,c=c,mu=mu,sw=%diag(sigmav.^2),sv=sigmae^2,presample=diffuse,method=bfgs,$
condition=10,yhat=yhat,vhat=vhat,svhat=svhat) 2001:11 2010:12 xstates vstates
But it didn't work and I'm not able to find out what's wrong. Could you please help me?
Best regards,
Re: Another Time-Varying Parameters Example
Posted: Wed Feb 23, 2011 9:28 am
by TomDoan
There are two ways to handle the partially fixed-partially varying TVP model. The first is what you started doing, which has all the regression coefficients as states, but gives the fixed coefficients zero variances in the state disturbances. The mistake that you made there was that you started with the zeros where you wanted them, but then estimated the whole vector of variances. The way I had that written:
Code: Select all
dlm(start=(sigmavalt(5)=sigmav5),y=m1gr,c=%eqnxvector(mdeq,t),$
sw=%diag(sigmavALT.^2),sv=sigmaeALT^2,presample=diffuse,$
method=bfgs,condition=10,type=smooth) 1959:3 1985:4 xstatesALT vstatesALT
with sigmav5 only (and not the whole sigmavalt vector) in the parameter set, will do that.
The other way is to pull the fixed regressors into a MU component. However, the MU option has to give the shifting scalar value, not a set of regressors. Your MU option should be
mu=beta1*inf{1}+beta2*exp12m+beta3*gap{1}
where you need to add beta1, beta2 and beta3 to the parameter set.
Re: Another Time-Varying Parameters Example
Posted: Fri Dec 14, 2012 4:15 pm
by ying2728
Can you provide the codes to get the forecast error series and the conditional variance of forecast error in your program, kimnp044.rpf?
Thanks.
Re: Another Time-Varying Parameters Example
Posted: Sat Dec 15, 2012 10:55 am
by TomDoan
ying2728 wrote:Can you provide the codes to get the forecast error series and the conditional variance of forecast error in your program, kimnp044.rpf?
Thanks.
Add the VHAT and SVHAT options to the DLM. The two lines after that extract the series of residuals and compute the standardized residuals.
dlm(y=m1gr,c=%eqnxvector(mdeq,t),sw=%diag(sigmav.^2),sv=sigmae^2,$
presample=diffuse,method=bfgs,condition=10,vhat=vhat,svhat=svhat) 1959:3 1985:4 xstates vstates
set uhat = vhat(t)(1)
set stdu = uhat/sqrt(svhat(t)(1,1))