Quite a few of these examples (switching space-space models and some of the simpler examples) are explained in much greater detail in the "Structural Breaks and Switching Models" e-course.
Kim and Nelson, State-space Models with Regime Switching
Kim and Nelson, State-space Models with Regime Switching
The attached zip has the examples and data files for Kim and Nelson, State-space Models with Regime Switching, 1999, MIT Press. The book demonstrates estimation of state-space models with Markov switching using both approximate maximum likelihood using the "Kim" filter and MCMC methods. In both cases, it starts with simpler models and works up, so the examples through kimnp126.rpf are for maximum likelihood and those starting with kimnp180.rpf are for MCMC.
Quite a few of these examples (switching space-space models and some of the simpler examples) are explained in much greater detail in the "Structural Breaks and Switching Models" e-course.
Quite a few of these examples (switching space-space models and some of the simpler examples) are explained in much greater detail in the "Structural Breaks and Switching Models" e-course.
Re: Kim and Nelson, State-space Models with Regime Switching
Hi Tom, thanks for posting these examples. I have a query on the setting of initial values in KIMNP111.RPF. Specifically the phi and x0 vectors
compute phi=||1.2,-.3||
compute sigsq=%seesq
*
frml kimf = kf=KimFilter(t),log(kf)
compute x0=||5.224,2.699||
Am I right to say the phi's are AR coefficients for loggdp? I'm not sure where the x0 values are derived from.
compute phi=||1.2,-.3||
compute sigsq=%seesq
*
frml kimf = kf=KimFilter(t),log(kf)
compute x0=||5.224,2.699||
Am I right to say the phi's are AR coefficients for loggdp? I'm not sure where the x0 values are derived from.
Re: Kim and Nelson, State-space Models with Regime Switching
Yes. The phi's are the AR coefficients. The x0's are taken from the book. Since the pre-sample values are estimated (that is, x0 is considered to be free parameters), those really just need to be in the right ballpark. The published results in the book actually aren't quite right---Kim cleaned up the code after the book went to press.mob7 wrote:Hi Tom, thanks for posting these examples. I have a query on the setting of initial values in KIMNP111.RPF. Specifically the phi and x0 vectors
compute phi=||1.2,-.3||
compute sigsq=%seesq
*
frml kimf = kf=KimFilter(t),log(kf)
compute x0=||5.224,2.699||
Am I right to say the phi's are AR coefficients for loggdp? I'm not sure where the x0 values are derived from.
Re: Kim and Nelson, State-space Models with Regime Switching
Hello Mr. Doan,
I try to execute kimnp115 procudure with my own data set. However some standart errors have negative sign.Also standart errors transition probabilities are 0 all the time. What should I do for this situation?
I try to execute kimnp115 procudure with my own data set. However some standart errors have negative sign.Also standart errors transition probabilities are 0 all the time. What should I do for this situation?
Re: Kim and Nelson, State-space Models with Regime Switching
The negative "standard deviations" aren't a problem as they get squared to produce the variance, so the sign is arbitrary.
Note that if there is no actual switching much of the model is unidentified, and you get results that make little sense. Have you done the simpler models to see whether they seem to be adequate?
Note that if there is no actual switching much of the model is unidentified, and you get results that make little sense. Have you done the simpler models to see whether they seem to be adequate?
Re: Kim and Nelson, State-space Models with Regime Switching
Thanks for your fast response.
Yes I did. When I executed the same code to the AR(1) model, the standard errors of the transition probabilities were zero.
Yes I did. When I executed the same code to the AR(1) model, the standard errors of the transition probabilities were zero.
Re: Kim and Nelson, State-space Models with Regime Switching
How did you do the "Hamilton" model (simpler switching AR?) The K&N text has two examples---one with the 1984 data (which works fine), the other with 1995 data (which doesn't, and requires a great deal of finesse to even get anywhere).
Re: Kim and Nelson, State-space Models with Regime Switching
Hello Mr. Doan,
I try to execute kimnp115 procudure with my own data set and I am trying to learn write this procedure on RATS. In the code, the number of time-varying parameters is set as the number of regressors. I want to determine the number of time-varying parameters myself. What should I do? I switch this part of code;
dec vect sigmav(ndlm)
dec symm sw(ndlm,ndlm)
as
dec vect sigmav(2)
dec symm sw(2,2)
But it didnt work. Please could you help me?
Thank you so much in advance.
I try to execute kimnp115 procudure with my own data set and I am trying to learn write this procedure on RATS. In the code, the number of time-varying parameters is set as the number of regressors. I want to determine the number of time-varying parameters myself. What should I do? I switch this part of code;
dec vect sigmav(ndlm)
dec symm sw(ndlm,ndlm)
as
dec vect sigmav(2)
dec symm sw(2,2)
But it didnt work. Please could you help me?
Thank you so much in advance.
Re: Kim and Nelson, State-space Models with Regime Switching
What are you trying to do that's different? The model allows for drift in all the coefficients, with Markov Switching in the variance of the measurement equation. NDLM is set automatically to match the number of coefficients in the regression.
Re: Kim and Nelson, State-space Models with Regime Switching
sigmav of some variables are statistically insignificant. Therefore, I would like to try the model that the coefficients of these variables remain unchanged over time.
Thank you in advance.
Thank you in advance.
Re: Kim and Nelson, State-space Models with Regime Switching
It's important to note that tests for zero variance in TVC models have non-standard distributions (which includes both likelihood ratio tests and the standard t-tests in regression output). This is because the zero restriction is at the edge of the feasible range. And doing a formal test is fairly complicated---see viewtopic.php?p=19454#p19454 (which looks at a simpler version of a TVC model).
That being said, the parameters which control the time-variation of the individual coefficients are the SIGMAV. The final estimation in the example pegs those at all zeros and drops them from the parameter set:
nonlin(parmset=dlmparmsfix) sigmae
compute sigmav=%zeros(ndlm,1)
maximize(parmset=dlmparmsfix+msparms,start=DLMStart(),method=bfgs) kimf 1959:3 1989:2
If you want to just peg a few to zero, you would instead do something like:
nonlin(parmset=dlmparmsfix) sigmae sigmav(3)=0 sigmav(4)=0
maximize(parmset=dlmparmsfix+msparms,start=DLMStart(),method=bfgs) kimf 1959:3 1989:2
which would freely estimate the sigmav on all the parameters except 3 and 4 and would make those two equal to zero (thus no time variation).
That being said, the parameters which control the time-variation of the individual coefficients are the SIGMAV. The final estimation in the example pegs those at all zeros and drops them from the parameter set:
nonlin(parmset=dlmparmsfix) sigmae
compute sigmav=%zeros(ndlm,1)
maximize(parmset=dlmparmsfix+msparms,start=DLMStart(),method=bfgs) kimf 1959:3 1989:2
If you want to just peg a few to zero, you would instead do something like:
nonlin(parmset=dlmparmsfix) sigmae sigmav(3)=0 sigmav(4)=0
maximize(parmset=dlmparmsfix+msparms,start=DLMStart(),method=bfgs) kimf 1959:3 1989:2
which would freely estimate the sigmav on all the parameters except 3 and 4 and would make those two equal to zero (thus no time variation).
Re: Kim and Nelson, State-space Models with Regime Switching
Its working thank you Mr. Doan.
These days, I'm trying to integrate KIMNP115.RPF's following part
to KIMNP044.RPF procedure by changing the code as follows (I just deleted the Markov Switching parts)
But I cant have any result. Please could you tell me where is my mistake? Full code and my own data are following:
Thank you so much in advance.
These days, I'm trying to integrate KIMNP115.RPF's following part
Code: Select all
set f1hat = 0.0
set f2hat = 0.0
*********************************************************************
*
* This does a single step of the Kim (approximate) filter
*
function KimFilter time
type integer time
*
local integer i j
local real yerr likely
local symm vhat
local rect gain
local rect phat(nregimes,nregimes)
local rect fwork(nregimes,nregimes)
*
local rect c
*
* Pull out the C matrix, which is time-varying (regressors at *time*)
*
compute c=tr(%eqnxvector(mdeq,time))
*
compute f1hat(time)=0.0,f2hat(time)=0.0
do i=1,nregimes
do j=1,nregimes
*
* Do the SSM predictive step. In this application A is the
* identity, so the calculations simplify quite a bit.
*
compute xwork(i,j)=xstar(j)
compute swork(i,j)=sstar(j)+sw
*
* Do the prediction error and variance for y under state i. The
* predictive variance is the only part of this that depends upon
* the regime. Compute the density function for the prediction
* error.
*
compute yerr=m1gr(time)-%dot(c,xwork(i,j))
compute vhat=c*swork(i,j)*tr(c)+sigmae(i)^2
*
* Do the decomposition of vhat into its components and add
* probability-weighted values to the sums across (i,j)
*
compute f1hat(time)=f1hat(time)+%scalar(c*swork(i,j)*tr(c))*p(i,j)*pstar(j)
compute f2hat(time)=f2hat(time)+sigmae(i)^2*p(i,j)*pstar(j)
compute gain=swork(i,j)*tr(c)*inv(vhat)
compute fwork(i,j)=exp(%logdensity(vhat,yerr))
*
* Do the SSM update step
*
compute xwork(i,j)=xwork(i,j)+gain*yerr
compute swork(i,j)=swork(i,j)-gain*c*swork(i,j)
end do j
end do i
Code: Select all
compute nregimes = 1
dec vect[vect] xstar(nregimes)
dec vect[symm] sstar(nregimes)
*
* These are the work SSM mean and variance for the current time period
*
dec rect[vect] xwork(nregimes,nregimes)
dec rect[symm] swork(nregimes,nregimes)
compute ndlm=%nreg
dec symm sw(ndlm,ndlm)
compute sw=%diag(sigmav.^2)
compute sigmae=.5*sqrt(%seesq)
***
set f1hat = 0.0
set f2hat = 0.0
*********************************************************************
*
* This does a single step of the Kim (approximate) filter
*
function KimFilter time
type integer time
*
local integer i j
local real yerr likely
local symm vhat
local rect gain
local rect phat(nregimes,nregimes)
local rect fwork(nregimes,nregimes)
*
local rect c
*
* Pull out the C matrix, which is time-varying (regressors at *time*)
*
compute c=tr(%eqnxvector(mdeq,time))
*
compute f1hat(time)=0.0,f2hat(time)=0.0
do i=1,nregimes
do j=1,nregimes
*
* Do the SSM predictive step. In this application A is the
* identity, so the calculations simplify quite a bit.
*
compute xwork(i,j)=xstar(j)
compute swork(i,j)=sstar(j)+sw
*
* Do the prediction error and variance for y under state i. The
* predictive variance is the only part of this that depends upon
* the regime. Compute the density function for the prediction
* error.
*
compute yerr=enf(time)-%dot(c,xwork(i,j))
compute vhat=c*swork(i,j)*tr(c)+(sigmaee^2)
*
* Do the decomposition of vhat into its components and add
* probability-weighted values to the sums across (i,j)
*
compute f1hat(time)=f1hat(time)+%scalar(c*swork(i,j)*tr(c))
compute f2hat(time)=f2hat(time)+(sigmaee^2)
compute gain=swork(i,j)*tr(c)*inv(vhat)
compute fwork(i,j)=exp(%logdensity(vhat,yerr))
*
* Do the SSM update step
*
compute xwork(i,j)=xwork(i,j)+gain*yerr
compute swork(i,j)=swork(i,j)-gain*c*swork(i,j)
end do j
end do i
Code: Select all
cal(m) 2003:3
all 2024:12
open data C:\fark1.xlsx
data(format=xlsx,org=cols) / enf lenf ldrdk
set trend = t
compute n = 262
*
linreg enf 2004:1 *
# constant lenf ldrdk
*
equation(lastreg) mdeq
dec vect sigmav(%nreg)
dec real sigmaee
*
* Get guess values based upon the results of a linear regression. All
* standard deviations are scales of the corresponding variances in the
* least squares regression. (The drift ones a much smaller multiple).
*
compute sigmaee=.5*sqrt(%seesq)
compute sigmav=.01*%stderrs
*
nonlin sigmaee sigmav
compute nregimes = 1
dec vect[vect] xstar(nregimes)
dec vect[symm] sstar(nregimes)
*
* These are the work SSM mean and variance for the current time period
*
dec rect[vect] xwork(nregimes,nregimes)
dec rect[symm] swork(nregimes,nregimes)
compute ndlm=%nreg
dec symm sw(ndlm,ndlm)
compute sw=%diag(sigmav.^2)
compute sigmae=.5*sqrt(%seesq)
***
set f1hat = 0.0
set f2hat = 0.0
*********************************************************************
*
* This does a single step of the Kim (approximate) filter
*
function KimFilter time
type integer time
*
local integer i j
local real yerr likely
local symm vhat
local rect gain
local rect phat(nregimes,nregimes)
local rect fwork(nregimes,nregimes)
*
local rect c
*
* Pull out the C matrix, which is time-varying (regressors at *time*)
*
compute c=tr(%eqnxvector(mdeq,time))
*
compute f1hat(time)=0.0,f2hat(time)=0.0
do i=1,nregimes
do j=1,nregimes
*
* Do the SSM predictive step. In this application A is the
* identity, so the calculations simplify quite a bit.
*
compute xwork(i,j)=xstar(j)
compute swork(i,j)=sstar(j)+sw
*
* Do the prediction error and variance for y under state i. The
* predictive variance is the only part of this that depends upon
* the regime. Compute the density function for the prediction
* error.
*
compute yerr=enf(time)-%dot(c,xwork(i,j))
compute vhat=c*swork(i,j)*tr(c)+(sigmaee^2)
*
* Do the decomposition of vhat into its components and add
* probability-weighted values to the sums across (i,j)
*
compute f1hat(time)=f1hat(time)+%scalar(c*swork(i,j)*tr(c))
compute f2hat(time)=f2hat(time)+(sigmaee^2)
compute gain=swork(i,j)*tr(c)*inv(vhat)
compute fwork(i,j)=exp(%logdensity(vhat,yerr))
*
* Do the SSM update step
*
compute xwork(i,j)=xwork(i,j)+gain*yerr
compute swork(i,j)=swork(i,j)-gain*c*swork(i,j)
end do j
end do i
dlm(y=enf,c=%eqnxvector(mdeq,t),sw=%diag(sigmav.^2),sv=sigmaee^2,$
sx0=30.0*%identity(3),method=bfgs,condition=10) 2003:3 2024:12 xstates vstates
dlm(y=enf,c=%eqnxvector(mdeq,t),sw=%diag(sigmav.^2),sv=sigmaee^2,$
presample=diffuse,method=bfgs,condition=10) 2003:3 2024:12 xstates vstates
Re: Kim and Nelson, State-space Models with Regime Switching
You are missing the END for the KIMFILTER procedure. Add a separate END instruction after the END DO I and before the DLM.
Code: Select all
end do j
end do i
end KimFilter
dlm(y=enf,c=%eqnxvector(mdeq,t),sw=%diag(sigmav.^2),sv=sigmaee^2,$
sx0=30.0*%identity(3),method=bfgs,condition=10) 2003:3 2024:12 xstates vstates
dlm(y=enf,c=%eqnxvector(mdeq,t),sw=%diag(sigmav.^2),sv=sigmaee^2,$
presample=diffuse,method=bfgs,condition=10) 2003:3 2024:12 xstates vstates
Re: Kim and Nelson, State-space Models with Regime Switching
Thank you Mr. Doan but when I check f1hat with "display f1hat" all are 0. Is there a wrong on loop?
Re: Kim and Nelson, State-space Models with Regime Switching
If you aren't doing Markov Switching, you wouldn't be using the Kim filter (and you aren't---there is no reference to it in your DLM calculations). So something that is calculated as part of the Kim filter wouldn't be set in your application.