Page 2 of 3
Re: TVTP Markov Regime switching model
Posted: Thu Apr 05, 2012 1:36 pm
by TomDoan
First, Filardo didn't write the RATS program---we did. I don't believe he ever made his (I believe Gauss) program available.
This is definitely wrong:
Code: Select all
compute p(1,1)=%(z=log(%dot(%eqnxvector(p1eq,time-1),v1)),%cdf(z)
compute p(1,2)=%(z=log(%dot(%eqnxvector(p2eq,time-1),v2)),%cdf(-z)
You shouldn't have the log(...) there. It's just z=linear function,%cdf(z) for the first and %cdf(-z) for the second.
This is the one part in the EM algorithm that's specific to the logistic probability function. All the other parts of EM are the same since they take the transitions as given. This is simpler with the logistic than it would be with the probit type function. The logistic is simpler for EM, harder for Gibbs sampling and they are a toss-up for straight ML. But they should give
very similar results. Since you're not doing Gibbs sampling, why are you using the less used and more complicated of two almost identical ways to handle this.
Code: Select all
compute w11(time)=p(1,1)*(1-p(1,1))*(thisEntry(1,1)+thisEntry(2,1))
compute w22(time)=p(1,2)*(1-p(1,2))*(thisEntry(2,2)+thisEntry(1,2))
compute l11(time)=(thisEntry(1,1)- p(1,1)*(thisEntry(1,1)+thisEntry(2,1)))/w11(time)
compute l22(time)=(thisEntry(2,2)-(1-p(1,2))*(thisEntry(2,2)+thisEntry(1,2)))/w22(time)
Re: TVTP Markov Regime switching model
Posted: Tue Apr 10, 2012 4:13 pm
by superper2008
Hello Tom:
Appreciate you very much for your help. Now I am writing the code. But when I try to run the markov switching linear model firstly with fixed probability with my program, the mistakes appears as follows.
cal(d) 2006:03:24
open data "D:\Documents and Settings\f_huan\My Documents\mutual fund thesis\one_sample.xls"
data(format=xls,org=cols) 2006:03:24 2012:03:05 apexcess aprm aprmsq
@msregression(states=2,switch=ch) apexcess
# constant aprm aprmsq
nonlin(parmset=regparms) betas sigsqv
nonlin(parmset=msparms) p
compute gstart=2006:03:24, gend=2012:03:05
@MSRegInitial gstart gend
@MSRegEMGeneralSetup
do emits=1,50
@MSRegEMStep gstart gend
disp "Iteration" emits "Log likelihood" %logl
end do emits
* Polish estimates with 10 iterations of BHHH
*
compute p=%xsubmat(p,1,nstates-1,1,nstates)
nonlin(parmset=regparms) betas sigsqv
nonlin(parmset=msparms) p
frml logl = f=%MSRegFVec(t),fpt=%MSProb(t,f),log(fpt)
@MSFilterInit
maximize(start=%(%MSRegInitVariances(),pstar=%msinit()),$
parmset=regparms+msparms,$
method=bhhh,iters=10) logl gstart gend
set p1smooth = psmooth(t)(1)
graph(footer="Smoothed Probability of Regime 1",max=1.0,min=0.0)
# p1smooth
## SR10. Missing Values And/Or SMPL Options Leave No Usable Data Points
The Error Occurred At Location 274, Line 21 of MSREGMSTEP
Called From Location 170, Line 9 of MSREGEMSTEP
Called From Location 68, Line 2 of loop/block
Could you please help me figure it out?
Appreciate you very much for your help. P.S. I am using WINRATS 8.0 version and waiting for my university's support for 8.1 version.
Sincerely,
Feiyu
Re: TVTP Markov Regime switching model
Posted: Wed Apr 11, 2012 4:46 pm
by superper2008
Dear Tom
Could you help me figure it out what is wrong with my code in the Markov switching linear with fixed probabilities in the previous reply?
Look forward to your reply at your earliest convenience.
Sincerely,
Feiyu
Re: TVTP Markov Regime switching model
Posted: Wed Apr 11, 2012 9:57 pm
by TomDoan
You have embedded missing data. Because of the recursive nature of the likelihood, you can't have that. You need to squeeze out the missing data points. See the description of the SAMPLE instruction.
Re: TVTP Markov Regime switching model
Posted: Thu Apr 12, 2012 10:45 am
by superper2008
Hello Tom:
Appreciate you very much for your help. I understood what you told me. However, I am using daily data in Canadian market so that there is no data at weekends or Canadian holidays. I have looked up Sample instruction, but I still have no idea how to squeeze out my missing data. What should I do with my daily data to make our daily consecutive?
Appreciate you again for your patience and help.
Sincerely,
Feiyu
Re: TVTP Markov Regime switching model
Posted: Thu Apr 12, 2012 11:28 am
by superper2008
Hello Tom:
cal(d) 2006:03:24
open data "D:\Documents and Settings\f_huan\My Documents\mutual fund thesisone_sample.xls"
data(format=xls,org=cols) 2006:03:24 2012:03:05 apexcess aprm aprmsq
sample (smpl=%valid(apexcess)) apexcess / apexcess_nomissing
linreg(define=fulleqn) apexcess
# constant aprm aprmsq
Just like what I wrote above, I used Sample instruction to create "compressed" version of data without any missing value to make my data consecutive entries. However, the mistakes appeared as follows.
## SX22. Expected Type SERIES[REAL], Got REAL Instead
>>>>=%valid(apexcess)) <<<<
could you please help me figure that out?
Appreciate you very much for your help.
Feiyu
Re: TVTP Markov Regime switching model
Posted: Thu Apr 12, 2012 4:28 pm
by superper2008
Hello Tom:
I have solved the problem about missing value. Thanks for your help. However, when I put the code together and run them step by step. The mistakes appeared in the middle about EM 50 iteration, but when I continue to run the code in the following, the results could be shown as follows. Could you help me explain about that? Appreciate very much for your kindly help. A millions thanks.
cal(d) 2006:03:24
open data "D:\Documents and Settings\f_huan\My Documents\mutual fund thesisone_sample.xls"
data(format=xls,org=cols) 2006:03:24 2012:03:05 apexcess aprm aprmsq
sample(smpl=%valid(apexcess)) apexcess / apexcess_nomissing
sample(smpl=%valid(aprm)) aprm / aprm_nomissing
sample(smpl=%valid(aprmsq)) aprmsq / aprmsq_nomissing
@msregression(states=2,switch=ch) apexcess_nomissing
# constant aprm_nomissing aprmsq_nomissing
compute gstart=2006:03:24, gend=2012:03:05
@MSRegInitial gstart gend
@MSRegEMGeneralSetup
do emits=1,25
@MSRegEMStep gstart gend
disp "Iteration" emits "Log likelihood" %logl
end do emits
## SR10. Missing Values And/Or SMPL Options Leave No Usable Data Points
The Error Occurred At Location 274, Line 21 of MSREGMSTEP
Called From Location 170, Line 9 of MSREGEMSTEP
Called From Location 68, Line 2 of loop/block
* Polish estimates with 10 iterations of BHHH
*
compute p=%xsubmat(p,1,nstates-1,1,nstates)
nonlin(parmset=regparms) betas sigsqv
nonlin(parmset=msparms) p
frml logl = f=%MSRegFVec(t),fpt=%MSProb(t,f),log(fpt)
@MSFilterInit
maximize(start=%(%MSRegInitVariances(),pstar=%msinit()),$
parmset=regparms+msparms,$
method=bhhh,iters=10) logl gstart gend
MAXIMIZE - Estimation by BHHH
NO CONVERGENCE IN 10 ITERATIONS
LAST CRITERION WAS 0.1477305
Daily(5) Data From 2006:03:24 To 2012:03:05
Usable Observations 1493
Skipped/Missing (from 1552) 59
Function Value 2987.6828
Variable Coeff Std Error T-Stat Signif
************************************************************************************
1. BETAS(1)(1) 0.000064403 0.000452654 0.14228 0.88686041
2. BETAS(1)(2) 0.604359705 0.023436638 25.78696 0.00000000
3. BETAS(1)(3) 1.802335844 0.331478272 5.43727 0.00000005
4. BETAS(2)(1) -0.004748251 0.005501272 -0.86312 0.38807221
5. BETAS(2)(2) 0.868780030 0.175884328 4.93950 0.00000078
6. BETAS(2)(3) 3.143974135 3.127256580 1.00535 0.31473039
7. SIGSQV(1) 0.000180526 0.000007037 25.65336 0.00000000
8. SIGSQV(2) 0.012834751 0.000341708 37.56058 0.00000000
9. P(1,1) 0.962684175 0.007069809 136.16835 0.00000000
10. P(1,2) 0.069079291 0.010034231 6.88436 0.00000000
Re: TVTP Markov Regime switching model
Posted: Thu Apr 12, 2012 10:00 pm
by TomDoan
superper2008 wrote:Hello Tom:
I have solved the problem about missing value. Thanks for your help. However, when I put the code together and run them step by step. The mistakes appeared in the middle about EM 50 iteration, but when I continue to run the code in the following, the results could be shown as follows. Could you help me explain about that? Appreciate very much for your kindly help. A millions thanks.
cal(d) 2006:03:24
open data "D:\Documents and Settings\f_huan\My Documents\mutual fund thesisone_sample.xls"
data(format=xls,org=cols) 2006:03:24 2012:03:05 apexcess aprm aprmsq
sample(smpl=%valid(apexcess)) apexcess / apexcess_nomissing
sample(smpl=%valid(aprm)) aprm / aprm_nomissing
sample(smpl=%valid(aprmsq)) aprmsq / aprmsq_nomissing
calendar(irregular)
compute gstart=1,gend=%nobs
@msregression(states=2,switch=ch) apexcess_nomissing
# constant aprm_nomissing aprmsq_nomissing
When you do the SAMPLE, your data is no longer daily, but irregular with fewer observations. Add the two lines in red and deleting the existing line for setting gstart and gend should fix that.
Note, BTW, that the failure to converge on the last MAXIMIZE is intentional. That's just there to get the BHHH covariance matrix, which EM can't estimate itself.
Re: TVTP Markov Regime switching model
Posted: Fri Apr 13, 2012 2:52 pm
by superper2008
Dear Tom:
Appreciate you very much for your help. I have put two lines in red as you suggested in my model. However, there were still mistakes as follows.The Error Occurred At Location 58, Line 7 of MSREGINITIAL Could you please help me figure it out ? Look forward to hearing from you. Thanks again.
cal(d) 2006:03:24
open data "D:\Documents and Settings\f_huan\My Documents\mutual fund thesisone_sample.xls"
data(format=xls,org=cols) 2006:03:24 2012:03:05 apexcess aprm aprmsq
sample(smpl=%valid(apexcess)) apexcess / apexcess_nomissing
sample(smpl=%valid(aprm)) aprm / aprm_nomissing
sample(smpl=%valid(aprmsq)) aprmsq / aprmsq_nomissing
calendar(irregular)
compute gstart=1,gend=%nobs
@msregression(states=2,switch=ch) apexcess_nomissing
# constant aprm_nomissing aprmsq_nomissing
compute gstart=2006:03:24, gend=2012:03:05
@MSRegInitial gstart gend
@MSRegEMGeneralSetup
do emits=1,50
@MSRegEMStep gstart gend
disp "Iteration" emits "Log likelihood" %logl
end do emits
## SR10. Missing Values And/Or SMPL Options Leave No Usable Data Points
The Error Occurred At Location 58, Line 7 of MSREGINITIAL
*
compute p=%xsubmat(p,1,nstates-1,1,nstates)
nonlin(parmset=regparms) betas sigsqv
nonlin(parmset=msparms) p
frml logl = f=%MSRegFVec(t),fpt=%MSProb(t,f),log(fpt)
@MSFilterInit
maximize(start=%(%MSRegInitVariances(),pstar=%msinit()),$
parmset=regparms+msparms,$
method=bhhh,iters=10) logl gstart gend
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.
## NL6. NONLIN Parameter P(1,1) Has Not Been Initialized. Trying 0
## NL6. NONLIN Parameter P(1,2) Has Not Been Initialized. Trying 0
## MAT15. Subscripts Too Large or Non-Positive
The Error Occurred At Location 196, Line 19 of %MSPROB
Re: TVTP Markov Regime switching model
Posted: Fri Apr 13, 2012 3:17 pm
by superper2008
Dear Tom:
I made one mistake about that previously. After I deleted the line for setting gstart and gend, everything is all right and perfect. The results are shown below. Thanks you very much for your kindly help and suggestions.
Variable Coeff Std Error T-Stat Signif
************************************************************************************
1. BETAS(1)(1) 0.000345991 0.000490941 0.70475 0.48096573
2. BETAS(1)(2) 0.604890635 0.026696461 22.65808 0.00000000
3. BETAS(1)(3) 0.187181525 0.686965627 0.27248 0.78525618
4. BETAS(2)(1) -0.006383747 0.006674866 -0.95639 0.33887737
5. BETAS(2)(2) 0.983682912 0.213718251 4.60271 0.00000417
6. BETAS(2)(3) 2.282115414 3.799917039 0.60057 0.54812656
7. SIGSQV(1) 0.000193687 0.000007508 25.79592 0.00000000
8. SIGSQV(2) 0.014987340 0.000506806 29.57217 0.00000000
9. P(1,1) 0.963136585 0.006720898 143.30475 0.00000000
10. P(1,2) 0.075835763 0.010634384 7.13119 0.00000000
Appreciate you very much for your help. Now I am begining with model with time varying probability by means of Filardo's logistic function as you suggested. However, my obersvation model is one multivariate linear regression without any lag so that I have to use code
@msregression(states=2,switch=ch) apexcess_nomissing
# constant aprm_nomissing aprmsq_nomissing
nonlin(parmset=common) betas sigsqv[/color
Compared with Filardo's code using @MSVARSetup(lags=nlags)
nonlin(parmset=common) mu phi sigma, compute thisEntry(MSVARLagState(k,1),MSVARLagState(k,2))+=MSVARPTSM(time)(k) & @msvartestvector(domu,dovar) baseparms
, these code is applied into markov swithcing VAR model. Therefore, I am not sure how to modify Filardo's code to fit into my multivariate linear regression with time varying probability. Could you please give me more suggestions about that?
Appreciate you very much for your constant instruction.
Feiyu
Re: TVTP Markov Regime switching model
Posted: Mon Apr 16, 2012 1:43 pm
by superper2008
Hello Tom:
Now I am writing code for time-varying multivariate linear regression without no lag coefficients. The code is post as follows, but the mistake occurred in the bottom line. Just what I mentioned in the previous reply, I still didn't understand how to apply @msversteup (nonlin(parmset=common) mu phi sigma)
)into @msregression (nonlin(parmset=common) betas sigsqv)
)
I am trying my best to combine them, but the results can't be showns.I am not sure if I need the code such as @msvartestvector(domu,dovar) baseparms, do k=1,MSVARFilterSize
compute thisEntry(MSVARLagState(k,1),MSVARLagState(k,2))+=MSVARPTSM(time)(k), because in my tested model, there are no lag terms and state-dependent means. Moreover, without the codes above, is it possible to run one result?
Could you please give me more suggestions about that to modify the Filado's code?
Appreciate you very much for your help. Look forward to hearing from you .
cal(d) 2006:03:24
open data "D:\Documents and Settings\f_huan\My Documents\mutual fund thesisone_sample.xls"
data(format=xls,org=cols) 2006:03:24 2012:03:05 apexcess aprm aprmsq logvix
sample(smpl=%valid(apexcess)) apexcess / apexcess_nomissing
sample(smpl=%valid(aprm)) aprm / aprm_nomissing
sample(smpl=%valid(aprmsq)) aprmsq / aprmsq_nomissing
sample(smpl=%valid(logvix)) logvix / logvix_nomissing
calendar(irregular)
compute gstart=1,gend=%nobs
@msregression(states=2,switch=ch) apexcess_nomissing
# constant aprm_nomissing aprmsq_nomissing
source msvarsetup.src
nonlin(parmset=common) betas sigsqv
* Define the logistic index for the transitions
*
equation p1eq *
# constant logvix{1}
equation p2eq *
# constant logvix{1}
*
dec equation p1eq p2eq
dec vector v1 v2
nonlin(parmset=tv) v1 v2
function %MSVARPmat time
type rect %MSVARPmat
type int time
*
local integer i j
local rect pexpand
local real z
*
compute p(1,1)=%(z=exp(%dot(%eqnxvector(p1eq,time),v1)),z/(1+z))
compute p(1,2)=%(z=exp(%dot(%eqnxvector(p2eq,time),v2)),1/(1+z))
compute %MSVARInitTransition()
if nexpand==nstates {
dim pexpand(nstates,nstates-1)
ewise pexpand(i,j)=%if(i==nstates,-1.0,i==j)
compute %MSVARPmat=pexpand*p
ewise %MSVARPmat(i,j)=%if(i==nstates,1.0+%MSVARPmat(i,j),%MSVARPmat(i,j))
}
else {
dim %MSVARPmat(nexpand,nexpand)
ewise %MSVARPmat(i,j)=MSVARTransProbs(MSVARTransLookup(i,j))
}
end
Initialize the probabilities using only the intercepts
*
function TVPInit
type vector TVPInit
*
local rect a
local integer i j
*
compute p=||%logistic(v1(1),1.0),1-%logistic(v2(1),1.0)||
compute TVPInit=%MSVARInit()
end
procedure MSVARTVPEMEstimate start end
type integer start end
option integer iters 100
option real cvcrit 1.e-6
local integer emits time k
local series l11 l22 w11 w22
local vector baseparms testparms
local rect thisEntry
*
@msvartestvector(domu,dovar) baseparmscompute baseparms=baseparms~~v1~~v2
do emits=1,iters
compute %eqnsetcoeffs(p1eq,v1)
compute %eqnsetcoeffs(p2eq,v2)
@msvarestep start end
compute %emlogl=%logl
@msvarmstep(nodop) start end
set l11 start end = 0.0
set l22 start end = 0.0
set w11 start end = 0.0
set w22 start end = 0.0
do time=start,end
compute %MSVARPMat(time)
compute thisEntry=%zeros(nstates,nstates)
do k=1,MSVARFilterSize
compute thisEntry(MSVARLagState(k,1),MSVARLagState(k,2))+=MSVARPTSM(time)(k)
end do k compute w11(time)=p(1,1)*(1-p(1,1))*(thisEntry(1,1)+thisEntry(2,1))
compute w22(time)=p(1,2)*(1-p(1,2))*(thisEntry(2,2)+thisEntry(1,2))
compute l11(time)=(thisEntry(1,1)- p(1,1)*(thisEntry(1,1)+thisEntry(2,1)))/w11(time)
compute l22(time)=(thisEntry(2,2)-(1-p(1,2))*(thisEntry(2,2)+thisEntry(1,2)))/w22(time)
end do time
* Update the coefficients
*
linreg(noprint,weight=w11,equation=p1eq) l11 start end
compute v1=v1+%beta
linreg(noprint,weight=w22,equation=p2eq) l22 start end
compute v2=v2+%beta
* Check for convergence
*
@msvartestvector(domu,dovar) testparms compute testparms=testparms~~v1~~v2
compute %cvcrit=%testdiff(baseparms,testparms)
if %cvcrit<cvcrit
break
compute baseparms=testparms
disp "Iteration" emits "Log Likelihood" %emlogl %cvcrit
end do emits
end MSVARTVPEMEstimate
frml logltvtp = %MSVARPMat(t),log(%MSVarProb(t))
compute v1=log(.7/.3)~~0.0
compute v2=log(.95/.05)~~0.0
@MSVARTVPEMEstimate(cvcrit=.0001) gstart gend
*
maximize(parmset=tv+common,start=(pstar=TVPInit()),$
method=bhhh,iters=20) logltvtp gstart gend
## MAT15. Subscripts Too Large or Non-Positive
The Error Occurred At Location 213, Line 16 of MSVARTESTVECTOR
Called From Location 59, Line 10 of MSVARTVPEMESTIMA
A millions thanks for your kindly help.
Sincerely,
Feiyu
Re: TVTP Markov Regime switching model
Posted: Tue Apr 17, 2012 2:39 pm
by superper2008
Hello Tom:
Could you please help me figure it out in the previous reply?
Appreciate you very much for your help and patience and understanding. Look forward to your reply soon.Thanks again.
Sincerely,
Feiyu
Re: TVTP Markov Regime switching model
Posted: Thu Apr 19, 2012 1:16 pm
by superper2008
Dear Tom:
I am not sure if you have seen my posts. For these days, I have been trying my best to solve the problems concerning markov switching linear model with time vary transition probability. Under your help, I have firstly addressed the issue about fixed transition probability. However, to meet the requirement of thesis at Master level, I have to move further into time varying transition.
Just as what I post in the previous post, I combined @msregression with @msvarsetup and run every procedure step by step. There seems to be all right except for the last part with maximize likelihood (common+tv) followed by
## MAT15. Subscripts Too Large or Non-Positive
The Error Occurred At Location 213, Line 16 of MSVARTESTVECTOR
Called From Location 59, Line 10 of MSVARTVPEMESTIMA
I am not sure if the error is related to irregular dataset, or should I set initial value for betas including constant (intercepts) and coefficients (betas and gammas) in the linear regression because I deleted the code line to initial value of mu in Filardo's code which is not considered in my model.
Could you please give me some advice about that? I wish that the problem can be solved under your further help.
Sincerely,
Feiyu
Re: TVTP Markov Regime switching model
Posted: Thu Apr 19, 2012 4:06 pm
by moderator
I'd suggest that you email the code and data set to us at
support@estima.com
That will be easier than trying to cut and paste code out of the forum post.
Please include your full name and RATS serial number in the email.
Regards,
Tom Maycock
Estima
Re: TVTP Markov Regime switching model
Posted: Wed May 02, 2012 10:56 am
by TomDoan
This will estimate the model using logistic index transitions using EM with a BHHH polishing.
Code: Select all
cal(d) 2006:03:24
open data "one_sample_ap.xls"
data(format=xls,org=cols) 2006:03:24 2012:03:05 apexcess aprm aprmsq logvix
sample(smpl=%valid(apexcess)) apexcess / apexcess_nomissing
sample(smpl=%valid(aprm)) aprm / aprm_nomissing
sample(smpl=%valid(aprmsq)) aprmsq / aprmsq_nomissing
sample(smpl=%valid(logvix)) logvix / logvix_nomissing
calendar(irregular)
compute gstart=1,gend=%nobs
@msregression(states=2,switch=ch) apexcess_nomissing
# constant aprm_nomissing aprmsq_nomissing
@msreginitial gstart gend
@MSFilterSetup(em)
*
nonlin(parmset=common) betas sigsqv
dec equation p1eq p2eq
dec vector v1 v2
dec vector p1mean p2mean
nonlin(parmset=tv) v1 v2
*
* Define the logistic index for the transitions
*
equation p1eq *
# constant
equation p2eq *
# constant
dim p1mean(1) p2mean(1)
compute p1mean(1)=1.0,p2mean(1)=1.0
**************************************************************************
*
* Function to compute the transition probability matrix
*
function %MSPmat time
type rect %MSPmat
type int time
local integer i j
local rect pexpand
local real z
local rect p
dim p(1,2)
compute p(1,1)=%(z=exp(%dot(%eqnxvector(p1eq,time),v1)),z/(1+z))
compute p(1,2)=%(z=exp(%dot(%eqnxvector(p2eq,time),v2)),1/(1+z))
compute %MSInitTransition()
if nexpand==nstates {
dim pexpand(nstates,nstates-1)
ewise pexpand(i,j)=%if(i==nstates,-1.0,i==j)
compute %MSPmat=pexpand*p
ewise %MSPmat(i,j)=%if(i==nstates,1.0+%MSPmat(i,j),%MSPmat(i,j))
}
else {
dim %MSPmat(nexpand,nexpand)
ewise %MSPmat(i,j)=MSTransProbs(MSTransLookup(i,j))
}
end
**************************************************************************
*
* Initialize the probabilities using only the means
*
function TVPInit
type vector TVPInit
*
local rect a
local integer i j
*
dim p(1,2)
compute p(1,1)=%(z=exp(%dot(p1mean,v1)),z/(1+z))
compute p(1,2)=%(z=exp(%dot(p2mean,v2)),1/(1+z))
compute %MSInitTransition()
compute TVPInit=%MSInit()
compute p=%MSPExpand(p)
end
**************************************************************************
procedure MSTVPEMEstimate start end
type integer start end
option integer iters 100
option real cvcrit 1.e-6
local integer emits time k
local series l11 l22 w11 w22
local vector baseparms testparms
local rect thisEntry
*
dim thisEntry(nstates,nstates)
*
compute baseparms=v1~~v2
do emits=1,iters
compute %eqnsetcoeffs(p1eq,v1)
compute %eqnsetcoeffs(p2eq,v2)
compute TVPInit()
@MSRegestep start end
compute %emlogl=%logl
@MSRegmstep(nodop,nodobeta,dosigma) start end
set l11 start end = 0.0
set l22 start end = 0.0
set w11 start end = 0.0
set w22 start end = 0.0
compute tvemlogl=0.0
do time=start,end
compute p=%MSPMat(time)
compute thisEntry=%zeros(nstates,nstates)
do k=1,MSEMSize
compute thisEntry(MSEMLagRegime(k,1),MSEMLagRegime(k,2))+=MSEMpt_sm(time)(k)
compute tvemlogl+=log(p(MSEMLagRegime(k,1),MSEMLagRegime(k,2)))*MSEMpt_sm(time)(k)
end do k
compute w11(time)=p(1,1)*(1-p(1,1))*(thisEntry(1,1)+thisEntry(2,1))
compute w22(time)=p(1,2)*(1-p(1,2))*(thisEntry(2,2)+thisEntry(1,2))
compute l11(time)=(thisEntry(1,1)- p(1,1)*(thisEntry(1,1)+thisEntry(2,1)))/w11(time)
compute l22(time)=(thisEntry(2,2)-(1-p(1,2))*(thisEntry(2,2)+thisEntry(1,2)))/w22(time)
end do time
*
* Update the coefficients
*
linreg(noprint,weight=w11,equation=p1eq) l11 start end
compute v1=v1+%beta
linreg(noprint,weight=w22,equation=p2eq) l22 start end
compute v2=v2+%beta
compute tvemlogl=0.0
do time=start,end
compute p=%MSPMat(time)
compute thisEntry=%zeros(nstates,nstates)
do k=1,MSEMSize
compute thisEntry(MSEMLagRegime(k,1),MSEMLagRegime(k,2))+=MSEMpt_sm(time)(k)
compute tvemlogl+=log(p(MSEMLagRegime(k,1),MSEMLagRegime(k,2)))*MSEMpt_sm(time)(k)
end do k
end do time
*
* Check for convergence
*
compute testparms=v1~~v2
compute %cvcrit=%testdiff(baseparms,testparms)
if %cvcrit<cvcrit
break
compute baseparms=testparms
disp "Iteration" emits "Log Likelihood" %emlogl %cvcrit
end do emits
end MSTVPEMEstimate
compute v1=log(.7/.3)
compute v2=log(.95/.05)
@MSTVPEMEstimate(cvcrit=.0001) gstart gend
equation p1eq *
# constant logvix{1}
equation p2eq *
# constant logvix{1}
dim p1mean(%eqnsize(p1eq)) p2mean(%eqnsize(p2eq))
do i=1,%eqnsize(p1eq)
sstats(mean) gstart gend %eqnxvector(p1eq,t)(i)>>p1mean(i)
end do i
do i=1,%eqnsize(p2eq)
sstats(mean) gstart gend %eqnxvector(p2eq,t)(i)>>p2mean(i)
end do i
compute v1=v1~~0.0
compute v2=v2~~0.0
@MSTVPEMEstimate(cvcrit=.0001,iters=50) gstart gend
*
* Polish estimates using BHHH
*
frml logltvtp = p=%MSPMat(t),log(%MSRegProb(t))
*
@MSFilterInit
maximize(parmset=tv+common,start=(pstar=TVPInit()),method=bhhh,iters=10) logltvtp gstart gend