' This code replicate Tables 4 and 6 in Chen (2025) "Inflation Expectations and Inflation Persistence"
' Table 4: Matrix "oos" columns 1 to 10
' Table 6: Matrix "oos" columns 11 to 12


%path = @runpath
cd %path
mode quiet

wfcreate(wf=sticky) m 1947:1 2022:11
read(b14,s=Data) CPIdata.xls cpi corecpi MICH pcepi NY Cleveland
read(b16,s=Data) Controls.xls UNRATE WTI M2 TB3MS GS10 FEDFUNDS


'=========================

!PR = 1
' 1 if 'P/R=4  1986:12
' 2 if P/R=3 1988:12
' 3 if P/R=2 1992:12
' 4 if P/R=1 1999:12
' 5 if P/R=0.5 2007:12



!scheme = 1 ' 1 if rolling, 2 if recursive

!table = 4  '4 if other controls, 6 if Cleveland
'=========================




svector renddate = @wsplit("1986:12 1988:12 1992:12 1999:12 2007:12")  
 
!rend = @dateval(renddate(!PR))
!pstart = @dateadd(!rend, 1, "mm")
!pend = @dateval("2022:11")
scalar Psample = @datediff(!pend,!pstart,"mm")
scalar Rsample = @datediff(!rend,@dateval("1978:01"),"mm")
scalar PRratio = Psample/Rsample
sample spred !pstart !pend


'read(b2,s=Data) stickyprice.xls _
'flexcpi flexcpi1 flexcpi3 flexcpi12 coreflexcpi coreflexcpi1 coreflexcpi3 coreflexcpi12 stickycpi stickycpi1 stickycpi3 stickycpi12 corestickycpi corestickycpi1 corestickycpi3 corestickycpi12 stickycpi_ex_shelter stickycpi_ex_shelter1 stickycpi_ex_shelter3 stickycpi_ex_shelter12 corestickycpi_ex_shelter corestickycpi_ex_shelter1 corestickycpi_ex_shelter3 corestickycpi_ex_shelter12 

smpl 1947:1 2022:11

genr p = cpi
genr dcpi12 = @pcy(p)
'  monthly change of the price level at an annualized rate
'genr dcpi = 100*(p-p(-12))/p(-12)
genr dcpi = 1200*(p-p(-1))/p(-1)
'genr dsticky = 100*corestickycpi_ex_shelter






genr M2G = 100*(M2-M2(-1))/m2(-1)
genr FF = FEDFUNDS
genr INFL = dcpi



'equation linear.ls dcpi c dcpi(-1)
'freeze(waldtable1) linear.wald C(1)/(1-C(2))=0

equation MS.switchreg(type=markov,  heterr, seed=1399200139) dcpi c dcpi(-1)
'freeze(waldtable2) ms.wald C(1)/(1-C(2))=0
'freeze(waldtable3) ms.wald C(4)/(1-C(5))=0
'freeze(waldtable4) ms.wald exp(c(3))=0
'freeze(waldtable5) ms.wald exp(c(6))=0
'freeze(waldtable6) ms.wald C(2)=C(5)

MS.makergmprobs(type=smooth) prob1 prob2


genr persistence = prob1*C(2) + prob2*C(5)

matrix(4,12) oos
svector ind = @wsplit("1 3 6 12")
svector Zname = @wsplit("UNRATE WTI M2G FF INFL Cleveland")



for !z = 1 to 6

' 1 if UNRATE, 
' 2 if WTI, 
' 3 if M2 growth, 
' 4 if FF, 
' 5 if Inflation
'6 if Cleveland


%x = Zname(!z)

for !kk = 1 to 4 
!lag= @val(ind(!kk))


group control UNRATE(-!lag) WTI(-!lag) M2G(-!lag) FEDFUNDS(-!lag) dcpi(-!lag) 



smpl 1978:1 2022:11
genr Q = prob2



smpl spred
genr rest_Qhat = 0.0
genr unrest_Qhat = 0.0

scalar window = @datediff(!pend,!pstart,"mm")




'Rolling estimation with FOR Loop
for !k = 0 to window
if !scheme = 1 then
smpl 1978:01+!k !rend+!k
else    
smpl 1978:01 1986:12+!k
endif
equation rest.ls Q c {%x}(-!lag) 
equation unrest.ls Q c mich(-!lag) 

smpl !pstart+!k !pstart+!k
rest.fit restf 
unrest.fit unrestf 
genr rest_Qhat =  rest_Qhat + restf
genr unrest_Qhat =  unrest_Qhat + unrestf
next

smpl spred

'MSE
genr u_rest = (Q - rest_Qhat)^2
genr u_unrest = (Q - unrest_Qhat)^2
genr uu = u_rest - (Q - rest_Qhat)*(Q - unrest_Qhat)
scalar mse_rest = @mean(u_rest)
scalar mse_unrest = @mean(u_unrest)
scalar mse_uu = @mean(uu)
scalar msperatio = mse_rest/mse_unrest


' Test Statistics
scalar CM = @obs(u_rest)*mse_uu/mse_unrest
genr d_dm = u_rest   - u_unrest
genr d_cw = u_rest - (u_unrest - (rest_Qhat - unrest_Qhat)^2)
equation DMtest.ls(cov=hac, covbwint) d_dm c
scalar DM = @tstats(1)

!gg1 = 2*!z-1
!gg2 =2*!z
oos(!kk,!gg1)=msperatio
oos(!kk,!gg2)=DM
next

next

