'Example: Forecasting in the presence of cointegration (real data)
'rolling windows forecasts

'data on the coincident and leading indicator for the US


%runpath = @runpath
cd %runpath
wfopen coint_data.wf1


sample estim1 1985 2003
sample fore1  2004 2006

sample pre_crisis 1985 2006
sample crisis 2007 @last

'  data adjustments
genr d_cci=d(cci)
genr d_cli=d(cli)


'--------------------------------------------


smpl @all
group indicators cci cli
freeze(all) indicators.line(m)
freeze(Figure7_10_4) all
smpl estim1
'first step: unit roots 

for %var cci cli

spool analysis_{%var}
output(s) analysis_{%var}

print {%var}.correl(24)
print {%var}.uroot
print {%var}.uroot(none, dif=1)

next

analysis_cci.extract(Table7_10_6) untitled02

'var estimation-----------------------------------

'first, VAR in levels which can be used also for cointegration 

var var0.ls 1 12 indicators 

'specification through info criteria and LR test

freeze(lag_lenght0) var0.laglen(12, vname=v1)
scalar aic_selected=v1(3)
scalar bic_selected=v1(4)
scalar lr_selected=v1(1)

!aic=aic_selected   'notice fpe, aic and hq  select 4!

var var0.ls 1 !aic indicators     'bic picks the right number of lags
freeze(t_var0) var0
'var0.makeresids res_cci res_ccli

'diagnostics


spool res_diagnostics0
output(s) res_diagnostics0
print var0.resids
print var0.arroots(graph)
print var0.correl(12,graph)
print var0.arlm(12)
print var0.white
print var0.jbera

'----------------------cointegration analysis and VECM

!p=!aic-1


spool coint
output(s) coint
'print indicators.coint(s,!p) 
print indicators.coint(s) 

var vecm.ec(b,1) 1 !p  indicators    'estimation of the vecm model,yes 
                                                        'intercept no trend
freeze(t_vecm) vecm

spool res_diagnostics_vecm
output(s) res_diagnostics_vecm
print vecm.resids
print vecm.arroots(graph)
print vecm.correl(12,graph)
print vecm.arlm(12)
print vecm.white
print vecm.jbera

coint.extract(Table7_10_7) untitled01
freeze(Table7_10_8) t_vecm

'------------------------alternative: VAR in differences

group d_indicators d(cci) d(cli)

var var1.ls 1 12 d_indicators 

'specification through info criteria and LR test

freeze(lag_lenght1) var1.laglen(12, vname=v2)
scalar aic_selected=v2(3)
scalar bic_selected=v2(4)
scalar lr_selected=v2(1)

!bic=bic_selected     'here bic is more parsimonious. it has 2 be the previous lenght-1 anyway

var var1.ls 1 !bic d_indicators     'bic picks the right number of lags
freeze(t_var1) var1
 
'diagnostics

spool res_diagnostics1
output(s) res_diagnostics1
print var1.resids
print var1.arroots(graph)
print var1.correl(12,graph)
print var1.arlm(12)
print var1.white
print var1.jbera


'----------------------------------------------------------
'FORECASTING: the three models under comparison
'notice with the var in differences (var1) and for the vecm we forecast the level because we expressed the variables as d(...)


for %type var0 var1 vecm
genr cci_f_{%type}=na
genr cli_f_{%type}=na
next


'dynamic, rolling
for %type var0 var1 vecm '4 eviews the endogenous are always in levels 

'1step ahead rolling (carriero's lines)

%start="1998:1"
'choose step ahead
!sa=1
 

smpl {%start} @last
!sample=120           '60     '@obs(cci)  '+@nas(cci)
!window=60
!numbfor=!sample-!window-!sa

for !t=1 to !numbfor
scalar nf=!numbfor
scalar date=!t

' estimate VAR
smpl {%start}+!t {%start}+!window+!t
var var0.ls 1 4  CCI CLI  @ C
var var1.ls 1 3  D(CCI) D(CLI)  @ C
var vecm.EC(B,1) 1 3  CCI CLI  @ C

' change sample to forecast period
smpl {%start}+!window+!t+1 {%start}+!window+!t+!sa

'forecast simply using the add-in
{%type}.fit(m) _fn _sen

'{%type}.fcast(g,s)f_{!t} ' _se  'saves temporary series 
cci_f_{%type}=cci_fn
cli_f_{%type}=cli_fn

d cli_fn
d cci_fn
'd(noerr) vtemp*



' plot actual and forecasts in each window (redundant)
'smpl  {%start}+!window+!t+1-12 {%start}+!window+!t+!sa'
' 	group gtmp_cci_{%type}  cci ccif_{!t}
'     group gtmp_cli_{%type}  cli clif_{!t}
''spool rolling_{%type}
'output(s) rolling_{%type}
'print gtmp_cci_{%type}.line 
'print gtmp_cli_{%type}.line  

next '''end of dynamic rolling loop

'graphs 2 synthetize the dyn-rolling procedure

smpl fore1
group cci_rol_{%type} cci cci_f_{%type}
group cli_rol_{%type} cli cli_f_{%type}
freeze(rol_{%type}_cci) cci_rol_{%type}.line
freeze(rol_{%type}_cli) cli_rol_{%type}.line

next
graph a_cci_rol.merge rol_var0_cci rol_var1_cci rol_vecm_cci
'a_cci_rol.ALIGN(1, 0.1, 0.5)
a_cci_rol.options size(8,2) 
'a_cci_rol.legend position(0.1,0.1)

graph a_var0_rol.merge rol_var0_cci rol_var0_cli 
'a_var0_rol.ALIGN(1, 0.1, 0.5)
a_var0_rol.options size(8,2) 
'a_var0_rol.legend position(0.1,0.1)

graph a_var1_rol.merge rol_var1_cci rol_var1_cli 
'a_var1_rol.ALIGN(1, 0.1, 0.5)
a_var1_rol.options size(8,2) 
'a_var1_rol.legend position(0.1,0.1)

graph a_vecm_rol.merge rol_vecm_cci rol_vecm_cli 
'a_vecm_rol.ALIGN(1, 0.1, 0.5)
a_vecm_rol.options size(8,2) 
'a_vecm_rol.legend position(0.1,0.1)

graph a_rol.merge a_var0_rol a_var1_rol a_vecm_rol
a_rol.options backcolor(white) linepat -color
a_rol.align(2, 1.5, 1.5)
a_rol.legend font(18)

'clean up

d vgraph*
d vgroup*
d vmod*
d vspool*
d vtable*


'-----------end of rolling procedure----------------------------

for %type var0 var1 vecm 

'static forecasts/1 step
'stochastic

smpl fore1

{%type}.fit(m) _f _se
rename cci_f ccif_1step_sto_{%type}
rename cli_f clif_1step_sto_{%type}
rename cci_se cci_se_1step_sto_{%type}
rename cli_se cli_se_1step_sto_{%type}

'{%type}.fcast(g,s)f_1step_sto_{%type} _se_1step_sto_{%type}


'
freeze(fcst_sta_sto_{%type}) ccif_1step_sto_{%type}.line clif_1step_sto_{%type}.line
'
''freeze(fcst_sta_sto_{%type}) vtemp1 vtemp2
''d(noerr) vtemp*
'
'fcst_sta_sto_{%type}.options backcolor(white) linepat
'fcst_sta_sto_{%type}.legend font(14)
'
'
'fcst_sta_sto_{%type}.options size(8,2)
'fcst_sta_sto_{%type}.legend position(0.1,0.1)
'fcst_sta_sto_{%type}.align(1, 1, .7)
'
'rename vmod01 {%type}_sta
'rename vtable01 t_{%type}_sta_sto
'rename vspool01 s_{%type}_sta_sto


'static forecast, determinstic solution
{%type}.makemodel({%type}_sta)
{%type}_sta.solveopt(s=d,d=s)
{%type}_sta.solve

rename cci_0 ccif_1step_det_{%type}
rename cli_0 clif_1step_det_{%type}

ccif_1step_det_{%type}.displayname ccif_1step_det_{%type}
clif_1step_det_{%type}.displayname clif_1step_det_{%type}

group ccifcst_sta_det_{%type}  cci ccif_1step_det_{%type}
group clifcst_sta_det_{%type}  cli clif_1step_det_{%type}

freeze(gra1) ccifcst_sta_det_{%type}.line
freeze(gra2) clifcst_sta_det_{%type}.line
gra1.options backcolor(white)
gra1.options backcolor(white)
graph fcst_sta_det_{%type}.merge gra1 gra2
'fcst_sta_det_{%type}.template carriero_2

fcst_sta_det_{%type}.options linepat backcolor(white)
fcst_sta_det_{%type}.legend font(14)



fcst_sta_det_{%type}.options size(8,2)
'fcst_sta_det_{%type}.legend position(0.1,0.1)
'fcst_sta_det_{%type}.align(1, 1, .7)

d gra* 


next   'closes the loop  for type

graph fcst_sta_det.merge fcst_sta_det_var0 fcst_sta_det_var1 fcst_sta_det_vecm
fcst_sta_det.options backcolor(white)
fcst_sta_det.align(2, 1.5, 1.5)
fcst_sta_det.legend font(18)
fcst_sta_det.options linepat backcolor(white) -color

graph fcst_sta_sto.merge fcst_sta_sto_var0 fcst_sta_sto_var1 fcst_sta_sto_vecm
fcst_sta_sto.options backcolor(white)
fcst_sta_sto.align(2, 1.5, 2)
fcst_sta_sto.legend font(18)



'clean up
d rol_*
d vgraph*
d vgroup*
d vmod*
d vspool*
d vtable*

'computation of RMSFE and MAE
for %type var0 var1 vecm
                 for %u cci cli
                           for %sce det sto
scalar rm_{%u}_sta_{%sce}_{%type}=@rmse({%u},{%u}f_1step_{%sce}_{%type})
scalar mae_{%u}_sta_{%sce}_{%type}=@mae({%u},{%u}f_1step_{%sce}_{%type})

next
next
next

for %type var0 var1 vecm
                 for %u cci cli
                     
scalar rm_{%u}_rol_{%type}=@rmse({%u},{%u}_f_{%type})
scalar mae_{%u}_rol_{%type}=@mae({%u},{%u}_f_{%type})

next
next

'evaluation table

for %y cci cli
       for %type var0 var1 vecm

table(4,4) eval_{%y}_{%type}


eval_{%y}_{%type}(2,2)="1 step ahead, deterministic"
eval_{%y}_{%type}(2,3)="1 step ahead, stochastic"
eval_{%y}_{%type}(2,4)="1 step ahead, rolling" 
eval_{%y}_{%type}(3,1)="RMSFE"
eval_{%y}_{%type}(4,1)="MAE"

eval_{%y}_{%type}(3,2)=rm_{%y}_sta_det_{%type}
eval_{%y}_{%type}(4,2)=mae_{%y}_sta_det_{%type}

eval_{%y}_{%type}(3,3)=rm_{%y}_sta_sto_{%type}
eval_{%y}_{%type}(4,3)=mae_{%y}_sta_sto_{%type}

eval_{%y}_{%type}(3,4)=rm_{%y}_rol_{%type}
eval_{%y}_{%type}(4,4)=mae_{%y}_rol_{%type}

      next
next


'table1.copytable table2 A10
'copies all of the data in TABLE1 to the 1st column and 10th row of TABLE2.
eval_cli_var0.copytable eval_cci_var0 e1
d eval_cli_var0
eval_cli_var1.copytable eval_cci_var1 e1
d eval_cli_var1
eval_cli_vecm.copytable eval_cci_vecm e1
d eval_cli_vecm

eval_cci_var1(1,1)="VAR1"
eval_cci_var1(1,3)="cci"
eval_cci_var1(1,7)="cli"

eval_cci_vecm(1,1)="VECM"
eval_cci_vecm(1,3)="cci"
eval_cci_vecm(1,7)="cli"

eval_cci_var0(1,1)="VAR0"
eval_cci_var0(1,3)="cci"
eval_cci_var0(1,7)="cli"

eval_cci_var1.copytable eval_cci_var0 a6
d eval_cci_var1
eval_cci_vecm.copytable eval_cci_var0 a11
d eval_cci_vecm

rename eval_cci_var0 eval

d rm*
d mae*

freeze(Figure7_10_5) fcst_sta_det
freeze(Table7_10_9) eval

pagerename Uslei_monthly_data precrisis



'--------------------------------------------------------------------------
'--------------------------------------------------------------------------


'now let's do the same exercise for the period including the crisis

pagecreate(page=crisis) m 1959m01 2013m04
copy coint_data::precrisis\cci   coint_data::crisis\cci
copy coint_data::precrisis\cli   coint_data::crisis\cli
copy coint_data::precrisis\indicators   coint_data::crisis\indicators
copy coint_data::precrisis\pre_crisis   coint_data::crisis\pre_crisis
copy coint_data::precrisis\crisis   coint_data::crisis\crisis


'var estimation-----------------------------------

smpl crisis

freeze(to_be_predicted) indicators.line(m)

'first, VAR in levels which can be used also for cointegration 
smpl pre_crisis

var var0.ls 1 12 indicators 

'specification through info criteria and LR test

freeze(lag_lenght0) var0.laglen(12, vname=v1)
scalar aic_selected=v1(3)
scalar bic_selected=v1(4)
scalar lr_selected=v1(1)

!bic=bic_selected   'notice fpe and aic both select 9!


var var0.ls 1 !bic indicators     'bic picks the right number of lags
freeze(t_var0) var0
'var0.makeresids res_cci res_ccli

'diagnostics

spool res_diagnostics0
output(s) res_diagnostics0
print var0.arroots(graph)
print var0.resids
print var0.correl(12,graph)
print var0.arlm(12)
print var0.white
print var0.jbera

'----------------------cointegration analysis and VECM
!p=!bic-1

spool coint
output(s) coint
print indicators.coint(s,!p) 

var vecm.ec(b,1) 1 !p indicators    'estimation of the vecm model,yes 
                                                        'intercept no trend
freeze(t_vecm) vecm

spool res_diagnostics_vecm
output(s) res_diagnostics_vecm
print vecm.resids
print vecm.arroots(graph)
print vecm.correl(12,graph)
print vecm.arlm(12)
print vecm.white
print vecm.jbera

'------------------------alternative: VAR in differences

group d_indicators d(cci) d(cli)

var var1.ls 1 12 d_indicators 

'specification through info criteria and LR test

freeze(lag_lenght1) var1.laglen(12, vname=v2)
scalar aic_selected=v2(3)
scalar bic_selected=v2(4)
scalar lr_selected=v2(1)

!bic=bic_selected     'here bic is more parsimonious

var var1.ls 1 !p d_indicators     'anyway we use p=bic-1 as lag lenght
freeze(t_var1) var1
 
'diagnostics

spool res_diagnostics1
output(s) res_diagnostics1
print var1.resids
print var1.arroots(graph)
print var1.correl(12,graph)
print var1.arlm(12)
print var1.white
print var1.jbera


'----------------------------------------------------------
'FORECASTING: the three models under comparison
'notice with the var in differences (var1) and for the vecm we forecast the level because we expressed the variables as d(...)


for %type var0 var1 vecm
genr cci_f_{%type}=na
genr cli_f_{%type}=na
next


'dynamic, rolling
for %type var0 var1 vecm '4 eviews the endogenous are always in levels 


'notice start must be 8 years before the actual start of the forecast sample
%start="1999:1"
'choose step ahead
!sa=1
 

smpl {%start} @last
!sample=170                  '95   
!window=60
!numbfor=!sample-!window-!sa

for !t=1 to !numbfor
scalar nf=!numbfor
scalar date=!t

' estimate VAR
smpl {%start}+!t {%start}+!window+!t
var var0.ls 1 !bic  CCI CLI  @ C
var var1.ls 1 !p  D(CCI) D(CLI)  @ C
var vecm.EC(B,1) 1 !p  CCI CLI  @ C

' make model out of estimated VAR
'{%type}.makemodel(model_{%type})

' change sample to forecast period
smpl {%start}+!window+!t+1 {%start}+!window+!t+!sa
' solve model to obtain dynamic forecasts
'model_{%type}.solve

'forecast simply using the add-in
{%type}.fit(m) _fn _sen

'{%type}.fcast(g,s)f_{!t} ' _se  'saves temporary series 
cci_f_{%type}=cci_fn
cli_f_{%type}=cli_fn

d cli_fn
d cci_fn
'd(noerr) vtemp*
next '''end of dynamic rolling loop

'graphs 2 synthetize the dyn-rolling procedure

smpl crisis
group cci_rol_{%type} cci cci_f_{%type}
group cli_rol_{%type} cli cli_f_{%type}
freeze(rol_{%type}_cci) cci_rol_{%type}.line
freeze(rol_{%type}_cli) cli_rol_{%type}.line

next
graph a_var0_rol.merge rol_var0_cci rol_var0_cli
'a_var0_rol.ALIGN(1, 0.1, 0.5)
a_var0_rol.options size(8,2)
'a_var0_rol.legend position(0.1,0.1)

graph a_var1_rol.merge rol_var1_cci rol_var1_cli
'a_var1_rol.ALIGN(1, 0.1, 0.5)
a_var1_rol.options size(8,2)
'a_var1_rol.legend position(0.1,0.1)

graph a_vecm_rol.merge rol_vecm_cci rol_vecm_cli
'a_vecm_rol.ALIGN(1, 0.1, 0.5)
a_vecm_rol.options size(8,2)
'a_vecm_rol.legend position(0.1,0.1)

graph a_rol.merge a_var0_rol a_var1_rol a_vecm_rol
a_rol.options backcolor(white) linepat -color
a_rol.align(2, 1.5, 1.5)
a_rol.legend font(18)


'clean up

d vgraph*
d vgroup*
d vmod*
d vspool*
d vtable*


'-----------end of rolling procedure----------------------------

for %type var0 var1 vecm 

'static forecasts/1 step
'stochastic

smpl crisis

{%type}.fit(m) _f _se
rename cci_f ccif_1step_sto_{%type}
rename cli_f clif_1step_sto_{%type}
rename cci_se cci_se_1step_sto_{%type}
rename cli_se cli_se_1step_sto_{%type}

'{%type}.fcast(g,s)f_1step_sto_{%type} _se_1step_sto_{%type}

freeze(fcst_sta_sto_{%type}) ccif_1step_sto_{%type}.line clif_1step_sto_{%type}.line

'freeze(fcst_sta_sto_{%type}) vtemp1 vtemp2
'd(noerr) vtemp*

fcst_sta_sto_{%type}.options backcolor(white) linepat
fcst_sta_sto_{%type}.legend font(14)



'vgraph01.options backcolor(white)
'rename vgraph01 fcst_sta_sto_{%type}
fcst_sta_sto_{%type}.options size(8,2)
'fcst_sta_sto_{%type}.legend position(0.1,0.1)
'fcst_sta_sto_{%type}.align(1, 1, .7)
'rename vmod01 {%type}_sta
'rename vtable01 t_{%type}_sta_sto
'rename vspool01 s_{%type}_sta_sto
'

'static forecast, determinstic solution
  {%type}.makemodel({%type}_sta)
{%type}_sta.solveopt(s=d,d=s)
{%type}_sta.solve

rename cci_0 ccif_1step_det_{%type}
rename cli_0 clif_1step_det_{%type}

ccif_1step_det_{%type}.displayname ccif_1step_det_{%type}
clif_1step_det_{%type}.displayname clif_1step_det_{%type}

group ccifcst_sta_det_{%type}  cci ccif_1step_det_{%type}
group clifcst_sta_det_{%type}  cli clif_1step_det_{%type}

freeze(gra1) ccifcst_sta_det_{%type}.line
freeze(gra2) clifcst_sta_det_{%type}.line
gra1.options backcolor(white)
gra1.options backcolor(white)
graph fcst_sta_det_{%type}.merge gra1 gra2
'fcst_sta_det_{%type}.template carriero_2

fcst_sta_det_{%type}.options size(8,2)
'fcst_sta_det_{%type}.legend position(0.1,0.1)
'fcst_sta_det_{%type}.align(1, 1, .7)
'
fcst_sta_det_{%type}.options linepat backcolor(white)
fcst_sta_det_{%type}.legend font(14)


d gra* 


next   'closes the loop  for type


graph fcst_sta_det.merge fcst_sta_det_var0 fcst_sta_det_var1 fcst_sta_det_vecm
fcst_sta_det.options backcolor(white)
fcst_sta_det.align(2, 1.5, 1.5)
fcst_sta_det.legend font(18)

graph fcst_sta_sto.merge fcst_sta_sto_var0 fcst_sta_sto_var1 fcst_sta_sto_vecm
fcst_sta_sto.options backcolor(white)
fcst_sta_sto.align(2, 1.5, 2)
fcst_sta_sto.legend font(18)


'clean up
d rol_*
d vgraph*
d vgroup*
d vmod*
d vspool*
d vtable*

'computation of RMSFE and MAE
for %type var0 var1 vecm
                 for %u cci cli
                           for %sce det sto
scalar rm_{%u}_sta_{%sce}_{%type}=@rmse({%u},{%u}f_1step_{%sce}_{%type})
scalar mae_{%u}_sta_{%sce}_{%type}=@mae({%u},{%u}f_1step_{%sce}_{%type})

next
next
next

for %type var0 var1 vecm
                 for %u cci cli
                     
scalar rm_{%u}_rol_{%type}=@rmse({%u},{%u}_f_{%type})
scalar mae_{%u}_rol_{%type}=@mae({%u},{%u}_f_{%type})

next
next


'evaluation table

for %y cci cli
       for %type var0 var1 vecm

table(4,4) eval_{%y}_{%type}


eval_{%y}_{%type}(2,2)="1 step ahead, deterministic"
eval_{%y}_{%type}(2,3)="1 step ahead, stochastic"
eval_{%y}_{%type}(2,4)="1 step ahead, rolling" 
eval_{%y}_{%type}(3,1)="RMSFE"
eval_{%y}_{%type}(4,1)="MAE"

eval_{%y}_{%type}(3,2)=rm_{%y}_sta_det_{%type}
eval_{%y}_{%type}(4,2)=mae_{%y}_sta_det_{%type}

eval_{%y}_{%type}(3,3)=rm_{%y}_sta_sto_{%type}
eval_{%y}_{%type}(4,3)=mae_{%y}_sta_sto_{%type}

eval_{%y}_{%type}(3,4)=rm_{%y}_rol_{%type}
eval_{%y}_{%type}(4,4)=mae_{%y}_rol_{%type}

      next
next


'table1.copytable table2 A10
'copies all of the data in TABLE1 to the 1st column and 10th row of TABLE2.
eval_cli_var0.copytable eval_cci_var0 e1
d eval_cli_var0
eval_cli_var1.copytable eval_cci_var1 e1
d eval_cli_var1
eval_cli_vecm.copytable eval_cci_vecm e1
d eval_cli_vecm

eval_cci_var1(1,1)="VAR1"
eval_cci_var1(1,3)="cci"
eval_cci_var1(1,7)="cli"

eval_cci_vecm(1,1)="VECM"
eval_cci_vecm(1,3)="cci"
eval_cci_vecm(1,7)="cli"

eval_cci_var0(1,1)="VAR0"
eval_cci_var0(1,3)="cci"
eval_cci_var0(1,7)="cli"

eval_cci_var1.copytable eval_cci_var0 a6
d eval_cci_var1
eval_cci_vecm.copytable eval_cci_var0 a11
d eval_cci_vecm

rename eval_cci_var0 eval

freeze(Table7_10_10) eval

'eval.tbl2tex(name=tab6)
d rm*
d mae*

d cci_f_*
d cli_f_*

close @objects

save coint_results



