TomDoan wrote:You never dimension %MSVARFVEC inside the function.
Thank you so much for pointing out the missing instruction for me. The code now works but the estimates on transition indices (V1(1), V2(1)) are surprisingly large. I guess this may be due to the initial values used for the estimation. I tried several initial values but none can fix the problem. Could you please kind take a look at my data (attached) and let me know how you will pick up the initial values? My data cover the period from 1875:Q1 to 2010:Q4, and based on the recessions and expansions defined by NBER, the average growth rate for recession period is -0.82 and is 2.1 for expansion,before WW2, up to 1949:Q4. while, the average growth rate for recession period is -0.54 and is 1.0 for expansion for the after-war period. It seems the gap between expansion and recession is getting smaller in the later period. Is it necessary to account this change into the estimation? Thank you in advance
MAXIMIZE - Estimation by BHHH
Convergence in 18 Iterations. Final criterion was 0.0000064 <= 0.0000100
Quarterly Data From 1876:02 To 2010:04
Usable Observations 53
Function Value -1518.0084
Variable Coeff Std Error T-Stat Signif
************************************************************************************
1. V1(1) -442.4565934 0.0000000 0.00000 0.00000000
2. V1(2) -0.9840729 1.2178426 -0.80805 0.41906408
3. V2(1) 31.5697815 0.0000000 0.00000 0.00000000
4. V2(2) 3.6862505 0.5051533 7.29729 0.00000000
5. MU(1)(1) -6.4422171 0.9493275 -6.78609 0.00000000
6. MU(2)(1) 0.8341408 0.0858809 9.71277 0.00000000
7. PHI(1)(1,1) 0.4303842 0.0402075 10.70407 0.00000000
8. PHI(2)(1,1) 0.0430398 0.0431374 0.99774 0.31840560
9. PHI(3)(1,1) 0.0283109 0.0424446 0.66701 0.50476710
10. PHI(4)(1,1) -0.1287053 0.0386235 -3.33230 0.00086130
11. SIGMA(1,1) 0.7865617 0.0565250 13.91529 0.00000000
12. ALFA 6.3003351 0.8173465 7.70828 0.00000000
Code: Select all
OPEN DATA "C:\Users\Sam\Desktop\historyofrealgdp.xlsx"
CALENDAR(Q) 1875:1
DATA(FORMAT=XLSX,ORG=COLUMNS) 1875:01 2010:04 quarter recession rgdp rep yr34
set demo = 1-rep
set yr12 = 1-yr34
set grgdp = 100.0*log(rgdp/rgdp{1})
graph(footer="Figure 1. Log Growth Rate of Quarter Real GDP")
# grgdp
states(smpl=recession) grgdp 1950:01 *
states(smpl=.not.recession) grgdp 1950:01 *
@nbercycles(up=ups,down=downs)
states(smpl=rep) ups 1950:01 *
set g = grgdp
sstats(mean,smpl=ups{1}) 1950:01 * ups>>pups
display pups
sstats(mean,smpl=downs{1}) 1950:01 * downs>>pdowns
display pdowns
stats(smpl=downs) g /
*******************************************************
*
* Common setup
*
compute nlags=4
*
source msvarsetup.src
compute gstart=1876:02,gend=2010:04
@MSVARSetup(lags=nlags,switch=m)
# g
*********************************************************************
nonlin(parmset=common) mu phi sigma alfa mustar
@MSVARinitial gstart gend
************************************************************************
*
* Time varying probabilities
*
dec equation p1eq p2eq
dec vector v1 v2
nonlin(parmset=tv) v1 v2
*********************************************************************
*
* This overrides the standard fixed definition
*
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
*********************************************************************
set dummy = t<=1949:04
dec real alfa
dec vect mustar(2)
*
* Override the %MSVARFVec function. This returns the expanded set of
* likelihood functions across all state combinations.
*
function %MSVARFVec time
type vector %MSVARFVec
type integer time
*
local integer i j state
local vect u
local real iconst
*
dim %MSVARFVec(nexpand)
*
compute iconst=(2*%pi)^(-.5*nvar)
do i=1,nexpand
compute state=MSVARLagState(i,1)
compute u=%xt(y,time)-mu(state)(1)-mustar(state)*dummy(t)
do j=1,nlags
compute statej=MSVARLagState(i,j+1),$
u=u-phi(j)*(%xt(y,time-j)-mu(statej)(1)-mustar(statej)*dummy(time-j))
end do j
compute state=MSVARLagState(i,1)
compute %MSVARFVec(i)=iconst*exp(-.5*(MSVARLogDetV(state)+Log(1+alfa*dummy(t)))-.5*%qform(MSVARSigmaInv(state)/(1+alfa*dummy(t)),u))
end do i
*
end
********************************************************************************
frml msvarf = %MSVARPMat(t),log(%MSVarProb(t))
*********************************************************************
*
* Define the logistic index for the transitions
*
equation p1eq *
# yr34 yr12
equation p2eq *
# yr34 yr12
*
sstats(mean,smpl=yr12.and.downs{1}) / downs>>pdowns
display pdowns
sstats(mean,smpl=yr12.and.ups{1}) / ups>>pups
display pups
* Standard guess values aiming to make state 1 the low growth state.
*
compute v1=log(.8/.2)~~log(.85/.15)
compute v2=log(.94/.06)~~log(.9/.1)
compute mu(1)=-0.8, mustar(1)=0.0
compute mu(2)=1.5, mustar(2)=0.0
*
maximize(parmset=tv+common,start=(pstar=TVPInit()),$
method=bhhh,iters=100,pmethod=simplex,piters=5) msvarf gstart gend
* Do graph of NBER recessions vs the probability of slow growth state
*
@NBERCycles(down=recession)
@MSVARsmoothed gstart gend psmooth
set pdown gstart gend = psmooth(t)(1)
graph(shade=recession)
# pdown