Filardo (1994) using Maximum Likelihood, Guess Values

Discussion of models with structural breaks or endogenous switching.
Jules89
Posts: 140
Joined: Thu Jul 14, 2016 5:32 am

Re: Filardo (1994) using Maximum Likelihood, Guess Values

Unread post by Jules89 »

Hey Tom,
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
I have changed the number of lags to 1 such that everything is a bit faster. The code for the loop is the following:

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)

For some starting values I get the error "## MAT14. Non-invertible Matrix. Using Generalized Inverse for SYMMETRIC.
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
Jules89
Posts: 140
Joined: Thu Jul 14, 2016 5:32 am

Re: Filardo (1994) using Maximum Likelihood, Guess Values

Unread post by Jules89 »

Already tryed, but then the loop nevertheless stops but produces outpu
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Filardo (1994) using Maximum Likelihood, Guess Values

Unread post by TomDoan »

ENV TRAPERRORS seems to work fine for me. It works through .96 and then fails on the rest.
  • Why are you using ML rather than EM? ML is used by many people because it's easy to set up. (You just need off-the-shelf variational optimization software since the Hamilton filter is easy to write). The hard part is that it's an ill-behaved likelihood, so it's easy to write and hard to use. EM takes care of most of the hard work with exact calculations. We wrote that so you don't have to.
  • If you want to search over initial conditions, you're changing the wrong value. If there are multiple modes (not likely with just one parameter switching), they would be determined by the parameter that switches. The probabilities are driven by that.
Jules89
Posts: 140
Joined: Thu Jul 14, 2016 5:32 am

Re: Filardo (1994) using Maximum Likelihood, Guess Values

Unread post by Jules89 »

I wanted to have a Command which forces the loop to begin with the next Iteration after an error occurs. Env traperrors does not do this. The loop stops and then the output is produced.

I used ml for the reasons you named. Its easy to set up. But when I use the EM code in the paper replication an error occurs...
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Filardo (1994) using Maximum Likelihood, Guess Values

Unread post by TomDoan »

I have no idea how an incorrect version of the Filardo program got onto the program distribution, but the one with the correct EM code is at https://estima.com/forum/viewtopic.php?f=8&t=513.
Jules89
Posts: 140
Joined: Thu Jul 14, 2016 5:32 am

Re: Filardo (1994) using Maximum Likelihood, Guess Values

Unread post by Jules89 »

Thank you very much Tom, the new code works :-)

Great software with great support!

Best

Jules
Post Reply