MS-ARIMA
Re: MS-ARIMA
That would be a special case of a MS State-Space model. It has the path-dependence problem that requires some form of approximation such as the Kim filter.
Re: MS-ARIMA
Hi Tom. I am trying to replicate the example of KIMNP111.RPFTomDoan wrote:That would be a special case of a MS State-Space model. It has the path-dependence problem that requires some form of approximation such as the Kim filter.
5.4.2 from pp 111-115 of Kim and Nelson, "State-space Models with Regime Switching". Lam's model of GNP, estimation with Kim's filter.
I am having following error messages. Could you please help me understand why I am having the message?
Code: Select all
The Error Occurred At Location 170, Line 13 of %MSINIT
Called From Location 127, Line 13 of MSFILTERINIT
## MAT14. Non-invertible Matrix. Using Generalized Inverse for SYMMETRIC.
The Error Occurred At Location 364, Line 26 of KIMFILTER
## MAT14. Non-invertible Matrix. Using Generalized Inverse for SYMMETRIC.
The Error Occurred At Location 364, Line 26 of KIMFILTER
## MAT14. Non-invertible Matrix. Using Generalized Inverse for SYMMETRIC.
The Error Occurred At Location 364, Line 26 of KIMFILTER
## MAT14. Non-invertible Matrix. Using Generalized Inverse for SYMMETRIC.
The Error Occurred At Location 364, Line 26 of KIMFILTER
## MAT14. Non-invertible Matrix. Using Generalized Inverse for SYMMETRIC.
The Error Occurred At Location 364, Line 26 of KIMFILTER
## MAT14. Non-invertible Matrix. Using Generalized Inverse for SYMMETRIC.
The Error Occurred At Location 364, Line 26 of KIMFILTER
## MAT14. Non-invertible Matrix. Using Generalized Inverse for SYMMETRIC.
The Error Occurred At Location 364, Line 26 of KIMFILTER
## MAT14. Non-invertible Matrix. Using Generalized Inverse for SYMMETRIC.
The Error Occurred At Location 364, Line 26 of KIMFILTER
## MAT14. Non-invertible Matrix. Using Generalized Inverse for SYMMETRIC.
In addition, could you please let me know how I can incorporate time-varying transition probability in example?
Thank you
- Attachments
-
- gdp4795.prn
- (2.44 KiB) Downloaded 1147 times
-
- kimnp111.rpf
- (5.18 KiB) Downloaded 1159 times
Re: MS-ARIMA
Hi Tom, I am trying to estimate a ARMA(2,4) model with mean-switching. I came up my code but there are error messages which i can not figure out what the mistakes are in my code. Could you kindly take a look my code and let me know what mistakes I made?TomDoan wrote:That would be a special case of a MS State-Space model. It has the path-dependence problem that requires some form of approximation such as the Kim filter.
error message
Code: Select all
The Error Occurred At Location 170, Line 13 of %MSINIT
Called From Location 127, Line 13 of MSFILTERINIT
## MAT14. Non-invertible Matrix. Using Generalized Inverse for SYMMETRIC.
## MAT2. Matrices with Dimensions 5 x 5 and 2 x 1 Involved in * Operation
The Error Occurred At Location 175, Line 17 of KIMFILTER
Code: Select all
* This is common to any MS state space model analyzed using the
* Kim filter.
*
@MSSetup(states=2)
*
* These are the collapsed SSM mean and variance from the previous time
* period.
*
dec vect[vect] xstar(nstates)
dec vect[symm] sstar(nstates)
*
* This is the filtered best estimate for the state vector
*
dec series[vect] xstates
*
*********************************************************************
*
*
* Fill in the fixed coefficients in the A, C and F matrices
*
compute ndlm=5
dec rect a(ndlm,ndlm)
input a
. . 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
dec vect phi(2)
dec rect c(1,ndlm)
input c
1 . . . .
dec vect tau(4)
dec rect f(ndlm,1)
compute f=%unitv(ndlm,1)
dec symm sw(ndlm,ndlm)
*
dec vect mu(nstates)
compute sv=0.0
*
dec vect x0(ndlm)
compute x0=%zeros(ndlm,1)
*
nonlin(parmset=initparms) x0
nonlin(parmset=dlmparms) mu sigsq phi tau
nonlin(parmset=msparms) theta
compute theta=%const(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(nstates,nstates)
local rect fwork(nstates,nstates)
local rect[vect] xwork(nstates,nstates)
local rect[symm] swork(nstates,nstates)
*
do i=1,nstates
do j=1,nstates
*
* Do the SSM predictive step
*
compute xwork(i,j)=a*xstar(j)
compute swork(i,j)=a*sstar(j)*tr(a)+sw
*
* Do the prediction error for y under state i, and compute the
* density function for the prediction error.
*
compute yerr=indgrowth(time)-(%dot(c,xwork(i,j))+mu(i))
compute vhat=c*swork(i,j)*tr(c)+sv
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
*
* Compute the Hamilton filter likelihood
*
compute likely=0.0
do i=1,nstates
do j=1,nstates
compute phat(i,j)=p(i,j)*pstar(j)*fwork(i,j)
compute likely=likely+phat(i,j)
end do j
end do i
*
* Compute the updated probabilities of the state combinations
*
compute phat=phat/likely
compute pstar=%sumr(phat)
compute pt_t(time)=pstar
*
* Collapse the SSM matrices down to one per state
*
compute xstates(time)=%zeros(ndlm,1)
do i=1,nstates
compute xstar(i)=%zeros(ndlm,1)
do j=1,nstates
compute xstar(i)=xstar(i)+xwork(i,j)*phat(i,j)/pstar(i)
end do j
compute sstar(i)=%zeros(ndlm,ndlm)
do j=1,nstates
compute sstar(i)=sstar(i)+phat(i,j)/pstar(i)*$
(swork(i,j)+%outerxx(xstar(i)-xwork(i,j)))
end do j
*
* This is the overall best estimate of the filtered state.
*
compute xstates(time)=xstates(time)+xstar(i)*pstar(i)
end do i
compute KimFilter=likely
end
*********************************************************************
*
* This is the start up code for each function evaluation
*
function DLMStart
*
local integer i
*
* Fill in the top row in the A matrix
*
compute %psubmat(a,1,1,tr(phi))
*
* Fill in the C matrix
*
compute %psubmat(c,1,2,tr(tau))
*
* Compute the full matrix for the transition variance
*
compute sw=f*tr(f)*sigsq
*
* Transform the theta to transition probabilities.
*
compute p=%MSLogisticP(theta)
compute pstar=%mcergodic(p)
compute p=%mspexpand(p)
*
* Initialize the KF state and variance. This is set up for estimating
* the pre-sample states.
*
ewise xstar(i)=x0
ewise sstar(i)=%zeros(ndlm,ndlm)
end
*********************************************************************
*
* Get guess values for the means from running an AR(2) on the growth
* rate. However, the guess values for the AR coefficients need to be
* input.
*
boxjenk(ar=2,constant) indgrowth
compute mu(1)=%beta(1)+1.0,mu(2)=%beta(1)-1.0
compute phi=||1.2,-.3||
compute sigsq=%seesq
compute tau=||0.1,0.1,0.1,-0.4||
*
frml kimf = kf=KimFilter(t),log(kf)
compute x0=||5.224,2.699||
*********************************************************************
gset xstates = %zeros(ndlm,1)
@MSFilterInit
maximize(parmset=msparms+dlmparms,start=DLMStart(),method=bfgs,trace) kimf 1945:04 *
maximize(parmset=msparms+dlmparms+initparms,start=DLMStart(),method=bfgs,trace) kimf 1945:04 *
- Attachments
-
- ARMA model in state-space.docx
- (15.11 KiB) Downloaded 934 times
Re: MS-ARIMA
You need to add the second line after the initialization of THETA:fan wrote:Hi Tom. I am trying to replicate the example of KIMNP111.RPFTomDoan wrote:That would be a special case of a MS State-Space model. It has the path-dependence problem that requires some form of approximation such as the Kim filter.
5.4.2 from pp 111-115 of Kim and Nelson, "State-space Models with Regime Switching". Lam's model of GNP, estimation with Kim's filter.
I am having following error messages. Could you please help me understand why I am having the message?
Code: Select all
The Error Occurred At Location 170, Line 13 of %MSINIT Called From Location 127, Line 13 of MSFILTERINIT ## MAT14. Non-invertible Matrix. Using Generalized Inverse for SYMMETRIC. The Error Occurred At Location 364, Line 26 of KIMFILTER ## MAT14. Non-invertible Matrix. Using Generalized Inverse for SYMMETRIC. The Error Occurred At Location 364, Line 26 of KIMFILTER ## MAT14. Non-invertible Matrix. Using Generalized Inverse for SYMMETRIC. The Error Occurred At Location 364, Line 26 of KIMFILTER ## MAT14. Non-invertible Matrix. Using Generalized Inverse for SYMMETRIC. The Error Occurred At Location 364, Line 26 of KIMFILTER ## MAT14. Non-invertible Matrix. Using Generalized Inverse for SYMMETRIC. The Error Occurred At Location 364, Line 26 of KIMFILTER ## MAT14. Non-invertible Matrix. Using Generalized Inverse for SYMMETRIC. The Error Occurred At Location 364, Line 26 of KIMFILTER ## MAT14. Non-invertible Matrix. Using Generalized Inverse for SYMMETRIC. The Error Occurred At Location 364, Line 26 of KIMFILTER ## MAT14. Non-invertible Matrix. Using Generalized Inverse for SYMMETRIC. The Error Occurred At Location 364, Line 26 of KIMFILTER ## MAT14. Non-invertible Matrix. Using Generalized Inverse for SYMMETRIC.
In addition, could you please let me know how I can incorporate time-varying transition probability in example?
Thank you
compute theta=%const(0.0)
compute p=%MSLogisticP(theta)
You need to recompute P inside the KimFilter function to be dependent upon the entry. You also need to figure out how to handle the pre-sample value of the regime probabilities. Have you looked at the Filardo example?
Re: MS-ARIMA
Please post that in PDF. DOCX with formulas isn't portable---it depends upon the software that you have on your computer.fan wrote: In addition, if I would like to examine the transition probabilities in different periods, such as initial 2 years and last 2 years of a presidency, how i should modify the code here. Many Thanks.
Why are you using such a complicated ARMA model?
Re: MS-ARIMA
hi tom, thank you for the quick reply. Here is my model. The reason why I am using ARMA model is because the conventional AR model can not well describe the data. Could you please kindly take a look my code and point out my mistakes? Thank youTomDoan wrote:Please post that in PDF. DOCX with formulas isn't portable---it depends upon the software that you have on your computer.fan wrote: In addition, if I would like to examine the transition probabilities in different periods, such as initial 2 years and last 2 years of a presidency, how i should modify the code here. Many Thanks.
Why are you using such a complicated ARMA model?
- Attachments
-
- ARMA model in state-space.pdf
- (193.94 KiB) Downloaded 999 times
Re: MS-ARIMA
That's not a correct representation for an ARMA(2,4)---I can't even make sense out of what you're trying to do there. The @ARMADLM procedure can create the DLM system matrices for an ARMA model.
The fact that you find that you need an ARMA(2,4) to adequately fit the data with a non-switching model isn't relevant to the choice of the model for a MS version---the MS adds complex dynamic of its own.
The fact that you find that you need an ARMA(2,4) to adequately fit the data with a non-switching model isn't relevant to the choice of the model for a MS version---the MS adds complex dynamic of its own.
Re: MS-ARIMA
TomDoan wrote:That's not a correct representation for an ARMA(2,4)---I can't even make sense out of what you're trying to do there. The @ARMADLM procedure can create the DLM system matrices for an ARMA model.
The fact that you find that you need an ARMA(2,4) to adequately fit the data with a non-switching model isn't relevant to the choice of the model for a MS version---the MS adds complex dynamic of its own.
Sorry for the poor explanation of the model. Here is the model I am trying estimate. The final goal is to estimate the probabilities of the economy will be in recession and expansion during election and non-election years, and democratic and republican administrations, respectively.
If non-switching model isn't relevant to the choice of the model for a MS version, how I should select the model for a MS version? I tried the AR(4) with mean-switching model ( the Hamilton model), the estimated state probability misses most of the economic recessions(NBER) for the period before WW2. This is why I am trying to use a different model. Thank you for your great patience
- Attachments
-
- My data.xlsx
- (20.89 KiB) Downloaded 932 times
-
- My model.pdf
- (200.73 KiB) Downloaded 949 times
Re: MS-ARIMA
It's fairly well known that the Hamilton model doesn't work very well when extended even another 10 years in the postwar period. (That's covered in the Kim-Nelson book). I can't imagine that it would be possible to get the switching model to work in the pre-war era. You probably need a bunch of dummies in transition probabilities, mean shifts and variance shifts to handle the changing dynamics. (Pre-war had much wider variation; post war through early 1980's had milder but rather frequent cycles; after 1980's the expansions are much longer). Whether you end up with much of a "model" if everything has to be dummied out separately is hard to say. At any rate, it's probably not the complexity of the ARMA model that's the problem.
Re: MS-ARIMA
Hi Tom, Thank you so much for your detailed explanation. Just for the purpose of the exercise, could you please kindly take a look the attached model, code and error message in the last post and let me know what the mistake(s) is (are) in the code? Thank you againTomDoan wrote:It's fairly well known that the Hamilton model doesn't work very well when extended even another 10 years in the postwar period. (That's covered in the Kim-Nelson book). I can't imagine that it would be possible to get the switching model to work in the pre-war era. You probably need a bunch of dummies in transition probabilities, mean shifts and variance shifts to handle the changing dynamics. (Pre-war had much wider variation; post war through early 1980's had milder but rather frequent cycles; after 1980's the expansions are much longer). Whether you end up with much of a "model" if everything has to be dummied out separately is hard to say. At any rate, it's probably not the complexity of the ARMA model that's the problem.
Re: MS-ARIMA
Thank you for the quick reply. Here are the attachmentsTomDoan wrote:you forgot the attachments.
My code
Code: Select all
* This is common to any MS state space model analyzed using the
* Kim filter.
*
@MSSetup(states=2)
*
* These are the collapsed SSM mean and variance from the previous time
* period.
*
dec vect[vect] xstar(nstates)
dec vect[symm] sstar(nstates)
*
* This is the filtered best estimate for the state vector
*
dec series[vect] xstates
*
*********************************************************************
*
*
* Fill in the fixed coefficients in the A, C and F matrices
*
compute ndlm=5
dec rect a(ndlm,ndlm)
input a
. . 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
dec vect phi(2)
dec rect c(1,ndlm)
input c
1 . . . .
dec vect tau(4)
dec rect f(ndlm,1)
compute f=%unitv(ndlm,1)
dec symm sw(ndlm,ndlm)
*
dec vect mu(nstates)
compute sv=0.0
*
dec vect x0(ndlm)
compute x0=%zeros(ndlm,1)
*
nonlin(parmset=initparms) x0
nonlin(parmset=dlmparms) mu sigsq phi tau
nonlin(parmset=msparms) theta
compute theta=%const(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(nstates,nstates)
local rect fwork(nstates,nstates)
local rect[vect] xwork(nstates,nstates)
local rect[symm] swork(nstates,nstates)
*
do i=1,nstates
do j=1,nstates
*
* Do the SSM predictive step
*
compute xwork(i,j)=a*xstar(j)
compute swork(i,j)=a*sstar(j)*tr(a)+sw
*
* Do the prediction error for y under state i, and compute the
* density function for the prediction error.
*
compute yerr=indgrowth(time)-(%dot(c,xwork(i,j))+mu(i))
compute vhat=c*swork(i,j)*tr(c)+sv
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
*
* Compute the Hamilton filter likelihood
*
compute likely=0.0
do i=1,nstates
do j=1,nstates
compute phat(i,j)=p(i,j)*pstar(j)*fwork(i,j)
compute likely=likely+phat(i,j)
end do j
end do i
*
* Compute the updated probabilities of the state combinations
*
compute phat=phat/likely
compute pstar=%sumr(phat)
compute pt_t(time)=pstar
*
* Collapse the SSM matrices down to one per state
*
compute xstates(time)=%zeros(ndlm,1)
do i=1,nstates
compute xstar(i)=%zeros(ndlm,1)
do j=1,nstates
compute xstar(i)=xstar(i)+xwork(i,j)*phat(i,j)/pstar(i)
end do j
compute sstar(i)=%zeros(ndlm,ndlm)
do j=1,nstates
compute sstar(i)=sstar(i)+phat(i,j)/pstar(i)*$
(swork(i,j)+%outerxx(xstar(i)-xwork(i,j)))
end do j
*
* This is the overall best estimate of the filtered state.
*
compute xstates(time)=xstates(time)+xstar(i)*pstar(i)
end do i
compute KimFilter=likely
end
*********************************************************************
*
* This is the start up code for each function evaluation
*
function DLMStart
*
local integer i
*
* Fill in the top row in the A matrix
*
compute %psubmat(a,1,1,tr(phi))
*
* Fill in the C matrix
*
compute %psubmat(c,1,2,tr(tau))
*
* Compute the full matrix for the transition variance
*
compute sw=f*tr(f)*sigsq
*
* Transform the theta to transition probabilities.
*
compute p=%MSLogisticP(theta)
compute pstar=%mcergodic(p)
compute p=%mspexpand(p)
*
* Initialize the KF state and variance. This is set up for estimating
* the pre-sample states.
*
ewise xstar(i)=x0
ewise sstar(i)=%zeros(ndlm,ndlm)
end
*********************************************************************
*
* Get guess values for the means from running an AR(2) on the growth
* rate. However, the guess values for the AR coefficients need to be
* input.
*
boxjenk(ar=2,constant) indgrowth
compute mu(1)=%beta(1)+1.0,mu(2)=%beta(1)-1.0
compute phi=||1.2,-.3||
compute sigsq=%seesq
compute tau=||0.1,0.1,0.1,-0.4||
*
frml kimf = kf=KimFilter(t),log(kf)
compute x0=||5.224,2.699||
*********************************************************************
gset xstates = %zeros(ndlm,1)
@MSFilterInit
maximize(parmset=msparms+dlmparms,start=DLMStart(),method=bfgs,trace) kimf 1945:04 *
maximize(parmset=msparms+dlmparms+initparms,start=DLMStart(),method=bfgs,trace) kimf 1945:04 *
Code: Select all
The Error Occurred At Location 170, Line 13 of %MSINIT
Called From Location 127, Line 13 of MSFILTERINIT
## MAT14. Non-invertible Matrix. Using Generalized Inverse for SYMMETRIC.
## MAT2. Matrices with Dimensions 5 x 5 and 2 x 1 Involved in * Operation
The Error Occurred At Location 175, Line 17 of KIMFILTER
- Attachments
-
- My model.pdf
- (200.73 KiB) Downloaded 970 times
Re: MS-ARIMA
I'm not seeing where you initialize the transition probabilities.
It doesn't help for you to give me 100 lines out of a program to diagnose a running program error (rather than a syntax error). I need the whole program and data.
It doesn't help for you to give me 100 lines out of a program to diagnose a running program error (rather than a syntax error). I need the whole program and data.
Re: MS-ARIMA
Hi Tom. I am sorry to bother you again. I attached my whole program and data sample here. Could you please kindly take a look at my code and help me find out the mistakes? Thank you!!TomDoan wrote:I'm not seeing where you initialize the transition probabilities.
It doesn't help for you to give me 100 lines out of a program to diagnose a running program error (rather than a syntax error). I need the whole program and data.
- Attachments
-
- quarterlydata.xlsx
- (124.44 KiB) Downloaded 928 times
-
- arma24.RPF
- (4.71 KiB) Downloaded 1165 times