Another Time-Varying Parameters Example
Another Time-Varying Parameters Example
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.
-
Henrique Andrade
- Posts: 19
- Joined: Thu Sep 09, 2010 8:33 pm
Re: Another Time-Varying Parameters Example
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?
I'm getting this message:
## MAT15. Subscripts Too Large or Non-Positive
Error was evaluating entry 1
Best regards,
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 *## MAT15. Subscripts Too Large or Non-Positive
Error was evaluating entry 1
Best regards,
- Attachments
-
- Dados.xls
- (82.5 KiB) Downloaded 923 times
Henrique C. de Andrade
Doutorando em Economia Aplicada
Universidade Federal do Rio Grande do Sul
http://www.ufrgs.br/ppge
Doutorando em Economia Aplicada
Universidade Federal do Rio Grande do Sul
http://www.ufrgs.br/ppge
Re: Another Time-Varying Parameters Example
Add the range to the SET instructions which pull information out of xstates:
set b0 2001:11 2010:05 = xstates(t)(1)
set b0 2001:11 2010:05 = xstates(t)(1)
-
Henrique Andrade
- Posts: 19
- Joined: Thu Sep 09, 2010 8:33 pm
Re: Another Time-Varying Parameters Example
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?
In your opinion, this sounds correct?
Best,
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
Best,
Henrique C. de Andrade
Doutorando em Economia Aplicada
Universidade Federal do Rio Grande do Sul
http://www.ufrgs.br/ppge
Doutorando em Economia Aplicada
Universidade Federal do Rio Grande do Sul
http://www.ufrgs.br/ppge
Re: Another Time-Varying Parameters Example
That looks correct.
-
Henrique Andrade
- Posts: 19
- Joined: Thu Sep 09, 2010 8:33 pm
Re: Another Time-Varying Parameters Example
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
Alternative Code
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,
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
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 vstatesALTcompute sigmavALT = (||0,0,0,0,8.22955e-04||)
Is this correct? Am I missing something important here?
Thanks in advance,
Henrique C. de Andrade
Doutorando em Economia Aplicada
Universidade Federal do Rio Grande do Sul
http://www.ufrgs.br/ppge
Doutorando em Economia Aplicada
Universidade Federal do Rio Grande do Sul
http://www.ufrgs.br/ppge
Re: Another Time-Varying Parameters Example
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.
-
Henrique Andrade
- Posts: 19
- Joined: Thu Sep 09, 2010 8:33 pm
Re: Another Time-Varying Parameters Example
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?
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?
Henrique C. de Andrade
Doutorando em Economia Aplicada
Universidade Federal do Rio Grande do Sul
http://www.ufrgs.br/ppge
Doutorando em Economia Aplicada
Universidade Federal do Rio Grande do Sul
http://www.ufrgs.br/ppge
Re: Another Time-Varying Parameters Example
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.
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.
-
Henrique Andrade
- Posts: 19
- Joined: Thu Sep 09, 2010 8:33 pm
Re: Another Time-Varying Parameters Example
Dear Tom,
I'm trying the following command:
But all the coefficients are invariant. Where is my error?
Best regards,
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
Best regards,
Henrique C. de Andrade
Doutorando em Economia Aplicada
Universidade Federal do Rio Grande do Sul
http://www.ufrgs.br/ppge
Doutorando em Economia Aplicada
Universidade Federal do Rio Grande do Sul
http://www.ufrgs.br/ppge
Re: Another Time-Varying Parameters Example
I had a typo in the START option on mine---it should have read:
I've also corrected that in the original post. With your code:
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.
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 vstatesALTCode: 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-
Henrique Andrade
- Posts: 19
- Joined: Thu Sep 09, 2010 8:33 pm
Re: Another Time-Varying Parameters Example
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:
But it didn't work and I'm not able to find out what's wrong. Could you please help me?
Best regards,
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 vstatesBest regards,
Henrique C. de Andrade
Doutorando em Economia Aplicada
Universidade Federal do Rio Grande do Sul
http://www.ufrgs.br/ppge
Doutorando em Economia Aplicada
Universidade Federal do Rio Grande do Sul
http://www.ufrgs.br/ppge
Re: Another Time-Varying Parameters Example
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:
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.
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 vstatesALTThe 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
Can you provide the codes to get the forecast error series and the conditional variance of forecast error in your program, kimnp044.rpf?
Thanks.
Thanks.
Re: Another Time-Varying Parameters Example
Add the VHAT and SVHAT options to the DLM. The two lines after that extract the series of residuals and compute the standardized residuals.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.
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))