I have written a code to test for starting values of different probabilities. My idea was to loop the estimation over several starting probabilities and then return the likelihoods. Then I would choose the one which generates the highest likelihood. My general setup is the following:
Code: Select all
****************************************************************************************
************************* Reading in and Transforming the Data *************************
****************************************************************************************
cal(m) 1948
open data filardo.dat
data(format=prn,org=cols) 1948:1 1992:8 year month ip cli dfi xli sp fed tspread
set ipgr = 100.0*log(ip/ip{1})
stats(noprint) ipgr * 1959:12
compute stdearly=sqrt(%variance)
stats(noprint) ipgr 1960:1 *
compute stdlate=sqrt(%variance)
set ipgradjust = %if(t<=1959:12,ipgr*stdlate/stdearly,ipgr)
set g = ipgradjust
set cligr = 100.0*log(cli/cli{1})
set spgr = 100.0*log(sp/sp{1})
set xlidiff = xli-xli{1}
set ffdiff = fed-fed{1}
set tspdiff = tspread-tspread{1}
set dfismooth = dfi+2*dfi{1}+2*dfi{2}+dfi{3}
* Center variabes to zero means
dofor s = cligr spgr xlidiff ffdiff tspdiff dfismooth
diff(center) s
end dofor s
*****************************************************************************************
**************************** Setting up the Markov Switching ****************************
*****************************************************************************************
source msvarsetup.src
compute nlags=1
compute gstart=1948:6,gend=1992:8
@MSVARSetup(lags=nlags, Switch=M)
# g
nonlin(parmset=common) mu phi sigma
nonlin(parmset=fixed) p
frml loglftp = log(%MSVarProb(t))
@MSVARInitial gstart gend
maximize(parmset=fixed+common,start=(pstar=%MSVARInit()), reject=%MSVARInitTransition()==0.0,$
method=bfgs) loglftp gstart gend
**********************************************************************
******************** Time varying probabilities **********************
**********************************************************************
dec equation p1eq p2eq
dec vector v1 v2
nonlin(parmset=tv) v1 v2
**********************************************************************
********** Overwriting the standard fixed transition matrix **********
**********************************************************************
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
**********************************************************************
********************* Calculating the Likelihood *********************
**********************************************************************
frml logltvtp = %MSVARPMat(t),log(%MSVarProb(t))
**********************************************************************
****************** Initialize the other Parameters *******************
**********************************************************************
*** Define the logistic index for the transitions
equation p1eq *
# constant cligr{1}
equation p2eq *
# constant cligr{1}
@MSVARInitial gstart gend
***Starting Values Except Sigma (initialized in @MSVARInitial)
compute v1=log(.7/.3)~~0.0
compute v2=log(.95/.05)~~0.0
compute mu(1)=-1.7,mu(2)=.3
*********************************************************************
**************** Maximization of the Log-Likelihood *****************
*********************************************************************
maximize(parmset=tv+common,start=(pstar=TVPInit()),$
method=bfgs) logltvtp gstart gend
Code: Select all
**********************************************************************
********************** Looping Stating Values ************************
**********************************************************************
declare real test2
report(action=define, hlabels=||"Prob", "Likelihood"||)
dofor test2 = %seqa(0.9,0.01,10)
@MSVARInitial gstart gend
*** Coming from State 1 Going to State 1 ***
compute v1=log(test2/(1-test2))~~0.0
*** Coming from State 2 Going to State 2 ***
compute v2=log(test2/(1-test2))~~0.0
maximize(noprint,parmset=tv+common,start=(pstar=TVPInit()),$
method=bfgs, iterations=600,subiterations=300, pmethod=Simplex, piters=10) logltvtp gstart gend
report(row=new,atcol=1) test2 %FUNCVAL
end do
report(action=format, tag=maximum, special=onestar, atcol=2, tocol=2)
report(action=show)
The Error Occurred At Location 196, Line 19 of %MSVARINIT" I dont really care about it, but whenever this error occurs the loop stops.
Is there any way to force the loop to continue and writing an NA into the table when the error occurs?
Best
Jules