/******************************************************************************************
*******************************************************************************************
**                                                                                       **
**  GVAR Version 1.1  (21 May 2006)                                                       **
**                                                                                       **
**                                                                                       **
**  This code was written by L. Vanessa Smith. Please address any questions, comments    **
**  or bugs to: lvs21@cam.ac.uk                                                          **       
**                                                                                       **
**  The code replicates the results in:                                                  **
**  Stephane Dees, Filippo di Mauro, M. Hashem Pesaran and L. Vanessa Smith (2006),      **  
**  "Exploring the International Linkages of the Euro Area: A Global VAR Analysis",      **
**                                                                                       **
**  It is based on a slightly more general set-up of the GVAR model than that described  **
**  in DdPS(2006), which constitutes a special case.                                     **   
**                                                                                       **
**  Please follow the instructions carefully.                                            ** 
**                                                                                       **
**                                                                                       **                                               
*******************************************************************************************
******************************************************************************************/

new 2000, 128000;

library gvarlb;

format /ld 7,14;

d1=date;

/*********************************************************************************************
     NOTES:
 
(i)  When setting the variables below to 1, the output described is exported to the output
     folder or printed to screen. When set to 0 no output is exported/printed.

(ii) Setting the variables below to 1 slows down the program so it is advisable that these
     are set to 1 only when the relevant output is to be exported and then set back to 0.

    
 out_DataFiles  : Excel data files for the individual country VARX* models are exported to the
                  data folder which is located in the output folder

 out_UnitRoot   : Unit root test statistics for both domestic and foreign(star) variables are
                  exported to the output folder

 out_ECMVARX    : Individual country VARX* error-correction estimates are exported to the output
                  folder along with values for the R_bar square, AIC and SBC of the individual
                  equations.

 out_ECMVAR     : Similar as above for the country-specific VAR models

 out_corrVARX   : Residual cross-section correlations (not averaged) for all variables of 
                  the country-specific VARX* models are exported to the output folder. Average
                  residual cross-section correlations are exported from the program by default.
              
 out_corrVAR    : Similar as above for the residuals of the country-specific VAR models

 print_cointVARX: Prints to screen detailed cointegration results for the country-specific VARX*
                  models

 print_cointVAR : Similar as above for the country-specific VAR models 

 print_VARX     : Prints to screen estimates of the error correction form of the country-
                  specific VARX* models  

*********************************************************************************************/


out_DataFiles=0;
out_UnitRoot=0; 
out_ECMVARX=0; 
out_ECMVAR=0;
out_corrVARX=0; 
out_corrVAR=0; 
print_cointVARX=0;
print_cointVAR=0;
print_VARX=0;


ccut=0.25; @ Percentage of trimming for the structural stability chow test
              for the country-specific VARX* models @

/** The country codes for which over-identified restrictions are imposed.
 The correspoding country names are: 
China,Euro Area,Japan,Australia,Canada,Norway,Sweden,Switzerland,UK,US
This is not relevant for the results in DdPS (2006) **/

ccOR={160,86,158,193,156,196,142,144,146,112,110}; 


/****
Names and numbers of countries/regions as well as country code. The country/regional ordering
as appears in this file is that adopted throughout the program once any regions have been created.
****/

gname1 = "c:\\gauss6.0\\gvar\\ImportFiles\\regcountry.xls";   
range1 = "b2:c34";
indxreg = xlsreadm(gname1, range1, 1, "");   


gname1 = "c:\\gauss6.0\\gvar\\ImportFiles\\regcountry.xls"; 
range2 = "a2:a34";
cnamd = xlsreadsa(gname1, range2, 1, "");

indnumcr=uniqindxstr(cnamd);
cnam=cnamd[indnumcr];


/** Trade - Matrix ***/

gname2 = "c:\\gauss6.0\\gvar\\ImportFiles\\tradematrix8003.xls"; 
range2 = "a2:ai793";
trdmtx = xlsreadm(gname2, range2, 1, "");    


/*** PPP-GDP data over three years 1999-2001, for all countries (33 countries) ***/

gname2 = "c:\\gauss6.0\\gvar\\ImportFiles\\pppgdp9901.xls"; 
range2 = "b2:d34";    
pppgdp = xlsreadm(gname2, range2, 1,"");

/*** PPP-GDP data over three years 1999-2001, for all countries/regions (26 countries/regions) ***/

gname2 = "c:\\gauss6.0\\gvar\\ImportFiles\\pppgdp9901_reg.xls";   
range2 = "b2:d27";
pppgdp_reg = xlsreadm(gname2, range2, 1, "");


/*** country/regional codes that follow the order of the PPP-GDP data given in pppgdp9901_reg **/ 

gname3 = "c:\\gauss6.0\\gvar\\ImportFiles\\idpppgdp9901_reg.xls";  
range3 = "b2:b27";
idrg = xlsreadm(gname3, range3, 1, "");



ypp=idrg~pppgdp_reg;  



/***** PPP-GDP weights plus regions for impulse responses etc ****/ 

gname1 = "c:\\gauss6.0\\gvar\\ImportFiles\\indxregcir_reg.xls";
range1 = "a2:b27";
indxregcir = xlsreadm(gname1, range1, 1, "");

rgcode=indxregcir[.,2]'; 


gname3 = "c:\\gauss6.0\\gvar\\ImportFiles\\EuroDollarRate2000.xls";
range3 = "b2:b5";    
eausfx = xlsreadm(gname3, range3, 1, "");


gname = "c:\\gauss6.0\\gvar\\ImportFiles\\OveridRestrictions.xls";
range = "a3:b63";   
hdOR_epepsus = xlsreadsa(gname, range, 1, ""); 

gname = "c:\\gauss6.0\\gvar\\ImportFiles\\OveridRestrictions.xls";
range = "d3:e62";   
hdOR_epeps = xlsreadsa(gname, range, 1, "");

/**********************************/


nyrs_rwin=3;
startdate_prd=1979;
startdate_trdmtx=1980;
findate=2003;
yr_sq=seqa(1979,1,findate-startdate_prd+1);
numbreg=maxc(indxreg[.,1]);
qini=1;  /*** initial quarter ***/
qfin=4; /*** final quarter ***/
frq=4; /*** frequency quarterly ***/

cs=4;  /*** Case for cointegration: Unrestricted intercepts restricted trends ***/



/********************************************************/


fname0 = "c:\\gauss6.0\\gvar\\ImportFiles\\gdp_interpl_normalz.xls";  

range1_0 = "b2:ah102"; 
range2_0 = "b2:ah31";
range3_0 = "b2:ah4";   

ysa1 = xlsreadm(fname0, range1_0, 1, "");

intp = xlsreadm(fname0, range2_0, 2, "");

normlz = xlsreadm(fname0, range3_0, 3, "");

/** 33 countries **/

fname1 = "c:\\gauss6.0\\gvar\\ImportFiles\\countrydata33.xls"; 
fname3 = "c:\\gauss6.0\\gvar\\ImportFiles\\countrycodes.xls";

range1 = "b2:g101"; 
range3 = "b2:b34";  


cc = xlsreadm(fname3, range3, 1, "");


N=rows(cc);
 id1_call=cc;  /**country codes **/        

{ygdp}=intnormz(ysa1,intp,normlz,startdate_prd,frq);


ygdp=ln(ygdp);


cini = xlsreadm(fname1, range1, 1, "");


/* Set-up for parameters */
  T  = rows(cini);   @Number of observations@ 
  k   = 6;   @Number of endogenous variables, excluding variables such as oil, 
              which are endogenous to some countries and exogenous to others@
  kst = k;   @Number of foreign(star) variables, excluding variables such as oil, 
              which are endogenous to some countries and exogenous to others@
  ex  = 1;   @Number of exogenous variables other than foreign(star) variables, i.e oil@
/*======================================================================*/



namvri=seqa(11,1,k);   

@--- The below implies that the data is imported with the "exogenous" variables added to the end ----@

if ex/=0;
sqex=seqa(30,1,ex);
namvri=namvri|sqex;
vcex=namvri[rows(namvri)-ex+1:rows(namvri)];
else;
vcex=ex;
endif;  


/****** converts to ln ******/

{c}=convertln(cini,k,ex);


c=ygdp[.,1]~c;

i=2;
do while i<=N;

sheet=i;

cb = xlsreadm(fname1, range1, sheet, "");

{ccb}=convertln(cb,k,ex);

ccb=ygdp[.,i]~ccb;

c=c|ccb;

i=i+1;
endo;



/**** add a column with the corresponding index next to each country in c ****/

v=zeros(N*T,1);

i=1;
do while i<=N;
j=1;
do while j<=T;
v[(i-1)*T+j]=id1_call[i];
j=j+1;
endo;
i=i+1;
endo;

xwex=v~c;


x=v~c[.,1:k];  

/***if pppgdp does not have the same order as the country codes  
these are repositioned in order to match **/


{rgcwgts}=ppp_regional_weights(id1_call,pppgdp,indxreg);  



/*** The ordering of the constructed regional domestic variables 
is that specified for the regions. The rgcodes follow also the 
ordering of the regions ***/

{xrg_plus_oil}=regional_variables_plus_oil(rgcode,indxreg,rgcwgts,k,x,T,xwex,ex,namvri,eausfx);  /*** first column of regional x  
                                                                                                   has the country/region code ****/

x=xrg_plus_oil;


/*******************/

/***  reset k and kst now that oil has been appended to regional variables**/

k=k+ex;
kst=k;

/***** regional share matrix *****/

N=cols(rgcode);
id1=rgcode';  


{type_wghts,st_yr_fw,rgshmtx_allyrs,rgshmtx1_allyrs,rgshmtx_yall_cind}=rgshmtx_sc(rgcode,indxreg,nyrs_rwin,startdate_trdmtx,yr_sq,numbreg,trdmtx);



/*** Note that if the countries are re-shuffled only rgshmtx_allyrs follows accordingly ---- 
rgshmtx1_allyrs (that includes interegional trade)  and
rgshmtx_yall_cind have the initial ordering of the countries ****/

if type_wghts==1;
e_fw=(rgshmtx_allyrs[.,1] .eq st_yr_fw);
rgshmtx=selif(rgshmtx_allyrs,e_fw);
cnam_typw=cnam;
else;
rgshmtx=rgshmtx_allyrs;
cnam1=cnam;
j=1;
do while j<=rows(yr_sq)-1;
cnam1=cnam1 $~ cnam;
j=j+1;
endo;
cnam_typw=vec(cnam1);
endif;

rgshmtx1=rgshmtx[.,3:cols(rgshmtx)];
styr_s="" $+ ftocv(rgshmtx[.,1],1,0);

/*** The regional share matrix as exported sums to one down the columns ***/

gname ="c:\\gauss6.0\\gvar\\output\\rgshmtx.xls";
ret = xlsWrite(rgshmtx1, gname,"c3",1,"");
nyrs_rwin_s="" $+ ftocv(nyrs_rwin,1,0);
if type_wghts==1;
hdr="Starting Year" $+ "( " $+ nyrs_rwin_s $+"year Rolling Window)";
elseif type_wghts==2;
hdr="Year" $+ "( " $+ nyrs_rwin_s $+"year Rolling Window)";
endif;
hd1=hdr $~ "Country" $~cnam';
gname ="c:\\gauss6.0\\gvar\\output\\rgshmtx.xls";
ret = xlswritesa(hd1, gname,"a2",1,"");
gname ="c:\\gauss6.0\\gvar\\output\\rgshmtx.xls";
ret = xlswritesa(styr_s $~ cnam_typw, gname,"a3",1,"");



/***Constructs the star variables including oil***/

{cc_xstar,zstr}=star_variables(T,N,namvri,id1,k,x,vcex,qini,qfin,frq,yr_sq,rgshmtx_allyrs);


xstr=cc_xstar[.,2:cols(cc_xstar)];



print"";
print "**********************************************************************************";
print "Select 0, for exact-identifying restrictions on the long run parameters when";
print "          performing cointegration analysis of the VARX* models";
print "Select 1, for over-identifying restrictions using the exchange rate expressed";
print "          viz a viz the US dollar";
print "Select 2, for over-identifying restrictions using the effective exchange rate";
print"";
print "Notes:";
print " (i) Select 0, to replicate the results in DdPS(2006)";
print "(ii) Options 1 and 2 should be used with the fixed weights option. Over-identifying";
print "     restrictions are placed on 11 out of the 26 country/regions";
print "***********************************************************************************";
print"";

type_idbeta=con(1,1); 

if (type_wghts==2 and type_idbeta/=0);
print"";
print "Options 1 and 2 should be used with the fixed weights option.";
print "Please run the gvar program again selecting the fixed weights"; 
print "option at the beginning of the program";
print"";
stop;
endif;

let string vnamurst={"ys", "ps" , "qs", "e*", "rs", "lrs", "poil","eps"};
let string starstr={"ys","Dps","qs","eps","rs","lrs","poil"};
let string dstarstr={"dys", "dDps", "dqs", "deps", "drs", "dlrs", "dpoil"}; 

if type_idbeta==0;
let string vnam={"GDP", "INFL", "EQUITY", "EP", "IR", "LIR", "POIL"};
let string vnamur={"y", "p" , "q", "e", "r", "lr", "poil","ep"};
let string endstr={"y","Dp","q","ep","r","lr","poil"};
let string dendstr={"dy","dDp","dq","dep","dr","dlr","dpoil"};  
elseif type_idbeta==1;
let string vnam={"GDP", "INFL", "EQUITY", "EPEPSUS", "IR", "LIR", "POIL"};
let string vnamur={"y", "p" , "q", "e", "r", "lr", "poil","epepsus"};
let string endstr={"y","Dp","q","epepsus","r","lr","poil"};
let string dendstr={"dy","dDp","dq","d(epepsus)","dr","dlr","dpoil"};  
elseif type_idbeta==2;
let string vnam={"GDP", "INFL", "EQUITY", "EPEPS", "IR", "LIR", "POIL"};
let string vnamur={"y", "p" , "q", "e", "r", "lr", "poil","epeps"};
let string endstr={"y","Dp","q","epeps","r","lr","poil"};
let string dendstr={"dy","dDp","dq","depeps","dr","dlr","dpoil"};  
endif;


{retinfo}=info(startdate_prd,startdate_trdmtx,findate,qini,qfin,frq,nyrs_rwin,type_wghts,
type_idbeta,st_yr_fw,cs);



/***************/


{ccfxend,ccfxstr,zzun}=NominalToRealFX(id1,k,kst,x,xstr,namvri,type_idbeta);


fxend=ccfxend[.,2];
fxstr=ccfxstr[.,2];


/*** Setting domestic and star variables and binary matrices for unit root tests ****/


if k>=4;
namvriur=namvri|99;   /** 99 is the code for es-ps **/ 
else;
namvriur=namvri;
endif;

{binend,binfex,binefe}=endstr_binary_befur(id1,T,k,kst,zzun,namvri,namvriur);


if out_UnitRoot==1; 

@------- Unit root tests--------@

let string namtests={adf,gls,max,ws};

{urendvar,urforvar}=unit_root_tests(cnam,id1,k,T,binefe,binend,binfex,namvriur,vnamur,vnamurst,namtests,zzun,ex,type_idbeta);


fname ="c:\\gauss6.0\\gvar\\output\\urendvar.xls";
ret = xlsWrite(urendvar, fname, "a2",1,"");

fname ="c:\\gauss6.0\\gvar\\output\\urforvar.xls";
ret = xlsWrite(urforvar, fname, "a2",1,"");

endif;


/** setting endogenous- foreign(star) variables (replacing the price level with inflation and e with e-p)
 and binary matrices after unit root tests **/


{zzun1,binend,binfex,binefe}=endstr_binary_aftur(x,namvri,fxend,fxstr,xstr,N,T,k,kst,id1,zstr);

Tn=(rows(zzun1)/N);


{indxret}=data_files(out_DataFiles,namvri,zstr,fxend,fxstr,endstr,starstr,id1,zzun1,yr_sq,frq,qini,qfin,type_idbeta);



/*** Used for the reposition of the beta's in the case of
 structrural impulse response analysis ***/
 
namvri_org=namvri;       
binefe_org=binend~binfex[.,2:cols(binfex)];



@------Determining the dimension of the VARX for each country -------@

kmax=2; /** Maximum lag order for the endogenous and foreign (star) variables of the 
            country-specific VARX* models determined either by AIC or SBC **/  


{cc_Lv,cc_Lvst}=var_order(N,Tn,kmax,id1,cnam,vnam,k,kst,binend,binfex,binefe,zzun1);


/*** Allows the var order of the foreign (star) variables to be changed based on the results 
from the var_order procedure, else is kept the same****/

{cc_Lv_new,cc_Lvst_new}=var_order_new(cnam,cc_Lv,cc_Lvst);

cc_Lv=cc_Lv_new;
cc_Lvst=cc_Lvst_new;

Lv=cc_Lv[.,2];
Lvst=cc_Lvst[.,2];



/**** Computating the number of cointegrating relations for the individual country 
      VARX* models ****/

rep=10000;

type_cnt=2; /** type_cnt=1: cointegration with endogenous variables only including oil,
                type_cnt=2: cointegration between endogenous and/or star variables **/

type_coint_cv=2; /*** In case of the small sample critical values option:
                      if equal to 1, the star variables are created from the x's
                      if equal to 2, the star variables are simulated ***/

{cc_nbcoint,coint_max_tr}=cointANDcv(x,zzun1,Lv,Lvst,cs,namvri,cnam,binend,
binfex,id1,k,kst,vcex,qini,qfin,frq,yr_sq,rgshmtx_allyrs,rep,type_cnt,type_coint_cv,
print_cointVAR,print_cointVARX,type_idbeta);

rcnt=cc_nbcoint[.,2];

fname ="c:\\gauss6.0\\gvar\\output\\coint_max&traceVARX.xls";
ret = xlsWrite(coint_max_tr, fname,"a2",1,"");


{cc_nbcoint_new}=coint_order_new(cnam,cc_nbcoint);

cc_nbcoint=cc_nbcoint_new;

rcnt=cc_nbcoint[.,2];


if type_idbeta==1;
if sumc(rcnt)/=rows(hdOR_epepsus);
print "The number of cointegrating relations are not the appropriate to replicate";
print "existing results. Please try running the program again."; 
endif;
elseif type_idbeta==2;
if sumc(rcnt)/=rows(hdOR_epeps);
print "The number of cointegrating relations are not the appropriate to replicate";
print "existing results. Please try running the program again."; 
endif;
endif;


if type_idbeta/=0;
{cnamescvOR}=cnames_cvexid(cnam,rcnt);
if type_idbeta==1;
hdOR_epepsus[.,1]=cnamescvOR;
elseif type_idbeta==2;
hdOR_epeps[.,1]=cnamescvOR;
endif;
endif;


/*** Computing the number of cointegrating relations for the individual country VAR models.
For these, poil is included as a regressor in all countries (no star variables are included).
Estimating the VAR models is needed so as to compute the cross-section correlation of the
residuals to compare with the VARX* models. 
 ****/

type_cnt_VAR=1; 
type_coint_cv_VAR=1; 

Lvd=Lv;
Lvstd=zeros(rows(Lv),1);

{cc_nbcointVAR,coint_max_trVAR}=cointANDcv(x,zzun1,Lvd,Lvstd,cs,namvri,cnam,binend,binfex,
id1,k,kst,vcex,qini,qfin,frq,yr_sq,rgshmtx_allyrs,rep,type_cnt_VAR,type_coint_cv_VAR,
print_cointVAR,print_cointVARX,type_idbeta);

rcntVAR=cc_nbcointVAR[.,2];

fname ="c:\\gauss6.0\\gvar\\output\\coint_max&traceVAR.xls";
ret = xlsWrite(coint_max_trVAR, fname,"a2",1,"");


@------Testing for Exogeneity -------@

kmaxexg=2; /** Maximum lag order for endogenous and foreign (star) variables in the 
               exogeneity tests when the lag length is determined by AIC***/ 

{exogn}=exogeneity(Tn,Lv,Lvst,rcnt,cs,id1,cnam,vnam,namvri,k,kst,binend,binfex,zzun1,
                        kmaxexg,starstr,type_idbeta,ccOR);

fname ="c:\\gauss6.0\\gvar\\output\\exogeneity_test.xls";
ret = xlsWrite(exogn, fname,"a2",1,"");



/*****************/


N=rows(id1);

print"";
print "Countries:";
print cnam;
print"";
print"";
print "Variables:";
print vnam;
print"";

print"";
print "********************************************************************************";
print "               Reordering of Countries and/or Variables"; 
print"";
print "Select 1 to keep the countries and variables in the order given above";
print "Select 2 to change the ordering of the countries and/or variables";
print"";
print "Note: Select 2 if structural GIRFs (SGIRFs) are to be pursued at a later stage, as";
print "      option 2 requires the US to be placed first in the above country ordering if";
print "      it is not already. GIRFs are invariant to the country and variable ordering."; 
print "      Both, GIRFs and SGIRFs, are computed in DdPS(2006)."; 
print "********************************************************************************";
print"";


 cv_rep=con(1,1); 


if  cv_rep==2;

{id1_rep,cnam_rep,vnam_rep,namvri_rep,binend_rep,binfex_rep,binefe_rep,zzun1_rep,
zstr_rep,endstr_rep,dendstr_rep,starstr_rep,dstarstr_rep,cctorep}=rep_sid(cnam,vnam,id1,k,binend,binfex,namvri,zzun1,zstr,endstr,dendstr,starstr,dstarstr);

cnam=cnam_rep;
vnam=vnam_rep;
namvri=namvri_rep;
binend=binend_rep;
binfex=binfex_rep;
binefe=binefe_rep;
zzun1=zzun1_rep;
zstr=zstr_rep;
endstr=endstr_rep;
dendstr= dendstr_rep;
starstr=starstr_rep;
dstarstr=dstarstr_rep;

/*** repositions fxend & fxstr ***/

ccfxend_rep=reposition(id1_rep,ccfxend,1);
ccfxstr_rep=reposition(id1_rep,ccfxstr,1);

fxend=ccfxend_rep[.,2];
fxstr=ccfxstr_rep[.,2];

/***********************************/

endif;




if  cv_rep==1;
cctorep=0;
endif;


{vk1,vk2,vk1k2,wi,rgshmtx_allyrs_rep,wyear}=link_matricesW(N,T,k,kst,namvri,binend,binfex,zzun1,fxend,fxstr,yr_sq,rgshmtx_allyrs,frq,qini,qfin,id1,cv_rep,cctorep,type_wghts,type_idbeta);


/*** rgshmtx_allyrs_rep is repositioned only of the countries
are re-shuffled else it follows the initial ordering ***/

rgshmtx_allyrs=rgshmtx_allyrs_rep;   


if cv_rep==2;

id1=id1_rep;

{cc_Lvrep}=reposition(id1,cc_Lv,1);
Lv=cc_Lvrep[.,2];

{cc_Lvstrep}=reposition(id1,cc_Lvst,1);
Lvst=cc_Lvstrep[.,2];

{cc_rcntrep}=reposition(id1,cc_nbcoint,1);
rcnt=cc_rcntrep[.,2];

{cc_rcntrepVAR}=reposition(id1,cc_nbcointVAR,1);
rcntVAR=cc_rcntrepVAR[.,2];


if type_idbeta==1;
hdOR_epepsus_rep=reposition_string(cnam,hdOR_epepsus,1);
hdOR_epepsus=hdOR_epepsus_rep;
elseif type_idbeta==2;
hdOR_epeps_rep=reposition_string(cnam,hdOR_epeps,1);
hdOR_epeps=hdOR_epeps_rep;
endif;


endif;



/*** Calcution of average correlations of data and residuals without the star variables --- in this case
oil is endogenous in all individual country models ***/

Lvd=Lv;
Lvstd=zeros(rows(Lv),1);

{std_data_fdif,std_VAR,avgcorr_vall_VAR,corr_v1_VAR,corr_v2_VAR,corr_v3_VAR,
corr_v4_VAR,corr_v5_VAR,corr_v6_VAR,corr_v7_VAR}=avgcorr_end_only(namvri,namvri_org,Tn,id1,zzun1,
cnam,vnam,Lvd,Lvstd,k,kst,cs,binend,binfex,binefe_org,yr_sq,rcnt,endstr,dendstr,type_idbeta,out_ECMVAR); 




fname ="c:\\gauss6.0\\gvar\\output\\avgcorr_vall_VAR.xls";
ret = xlsWrite(avgcorr_vall_VAR, fname,"a2",1,"");

if out_corrVAR==1;
fname ="c:\\gauss6.0\\gvar\\output\\corr_v1_VAR.xls";
ret = xlsWrite(corr_v1_VAR, fname,"a2",1,"");

fname ="c:\\gauss6.0\\gvar\\output\\corr_v2_VAR.xls";
ret = xlsWrite(corr_v2_VAR, fname,"a2",1,"");

fname ="c:\\gauss6.0\\gvar\\output\\corr_v3_VAR.xls";
ret = xlsWrite(corr_v3_VAR, fname,"a2",1,"");

fname ="c:\\gauss6.0\\gvar\\output\\corr_v4_VAR.xls";
ret = xlsWrite(corr_v4_VAR, fname,"a2",1,"");

fname ="c:\\gauss6.0\\gvar\\output\\corr_v5_VAR.xls";
ret = xlsWrite(corr_v5_VAR, fname,"a2",1,"");

fname ="c:\\gauss6.0\\gvar\\output\\corr_v6_VAR.xls";
ret = xlsWrite(corr_v6_VAR, fname,"a2",1,"");

fname ="c:\\gauss6.0\\gvar\\output\\corr_v7_VAR.xls";
ret = xlsWrite(corr_v7_VAR, fname,"a2",1,"");
endif;


/*** Solving the GVAR ***/

btstrp=0;
btstrp_sb=-9999; /** bootstapping indicator for structural break tests, these are bootstrapped separately **/

{covreswm,Gwm,Gdtwm,GBefwm,Greswm,F,betawm,mxpq,Pg,type_id,S0,S1,ChisqDf,errcd,errcd_sb,
kpres1,kpres2,nyres1,nyres2,chres1,chres2,chres3,bkdate,rchres1,rchres2,rchres3}=solve_gvar(zzun1,id1,Lv,
Lvst,rcnt,cs,binend,binfex,namvri,cnam,vnam,k,kst,wi,yr_sq,qini,qfin,frq,endstr,dendstr,starstr,
dstarstr,out_ECMVARX,ccut,btstrp,btstrp_sb,namvri_org,binefe_org,cv_rep,type_idbeta,ccOR,print_VARX,
out_corrVARX);


/***Standard Errors of Growth Rates, residuals from VAR, VARX* and GVAR Model ****/

{stderrors}=StandardErrors(cnam,vnam,id1,namvri,binend,Greswm,covreswm,std_data_fdif,std_VAR,type_id);

fname ="c:\\gauss6.0\\gvar\\output\\Standard_Errors.xls";
ret = xlsWrite(stderrors, fname,"a2",1,"");


/*******************/

screen on;

let string hd={"Country","Chi-Square Statistic","Degrees of Freedom"};
strna=reshape("NA",rows(binend),2);
if type_idbeta/=0;
pindx=packr(miss(indcv(ccOR,id1),-9999));
ChisqDf_s1="" $+ ftocv(ChisqDf[.,1],1,2);
ChisqDf_s2="" $+ ftocv(ChisqDf[.,2],1,0);
strna[pindx,1]=ChisqDf_s1[pindx];
strna[pindx,2]=ChisqDf_s2[pindx];
endif;
ChisqDf_s=hd'$|(cnam $~strna);

fname ="c:\\gauss6.0\\gvar\\output\\ChiSqForOR.xls";
ret = xlsWrite(ChisqDf_s, fname,"a2",1,"");

fname ="c:\\gauss6.0\\gvar\\output\\ChiSqForOR.xls";
ret = xlswritesa("Chi-Square Statistics for Over-identifying Restrictions", fname,"b1",1,"");

/****** Eigenvalues ******/


{geig,modeig}=eigGVAR(F);

screen off;
output file=c:\gauss6.0\gvar\output\Eigenval.csv reset;
print"";
print "Eigenvalues of the GVAR model in descending order";
print geig;
print"";
print "Moduli of eigenvalues of the GVAR model in descending order";
print modeig;

output file=c:\gauss6.0\gvar\output\Eigenval.csv off;


screen on;
print"";
print"";
print"***********************************************************";
print "    Moduli of Eigenvalues of GVAR in decending order";
print"***********************************************************";
print modeig;
print"";
screen on;




if type_wghts==1;
e_fw=(rgshmtx_allyrs[.,1] .eq st_yr_fw);
rgshmtx_rep=selif(rgshmtx_allyrs,e_fw);
else;
rgshmtx_rep=rgshmtx_allyrs;
endif;

print"";
print"";
print "********************************************************************************";
print "Enter yes, to proceed with the effect of shocks on the GVAR cointegrating";
print "           relations, impulse response analysis and variance decomposition";
print "Enter no, to terminate the program here";
print "********************************************************************************";
contind=cons;
if contind $== "no";
d2 = date;
  et = ethsec(d1,d2);
  et = et/100;
  print "Total elapsed time [mins] :" et/60;
  print "";
end;
endif;



@------------------WEIGHTS for CALCULATION OF IMPULSE RESPONSES-------------------------@


/**** Calculates the ppp-GDP weights to be used when aggregating the impulse responses
at a country level and region level  ****/

/***** Regional Codes *****/

idreg={160,86,158,94,76,82,67,92,112,110};
let string cnamrg={China,EuroArea,Japan,LA,ODC,RestAsia,RestWorld,RestWEurope,UK,US};

{indxregcir_rep,ypp1,idreg_rep,cnamrg_rep}=repositioning_codes(indxregcir,ypp,cnamrg,idreg,id1);
indxregcir=indxregcir_rep;
idreg=idreg_rep;
cnamrg=cnamrg_rep;


/**** Calculation of country and regional PPP-GDP weights ****/

/** PPP-GDP Country Weights - For each variable (gdp,equity, etc) the 
weights are reweighted if for some country that variable does not exist ***/


{cwgts,callvindcwgts,cindvallcwgts}=country_weights(ypp1,binend,namvri); 

cwgts=missrv(cwgts,99999);
vls=reshape(error(0),9,1);
vls[2]=99999;
let string hdcw1={"Country Weights Calculated Using PPP-GDP Figures for 1999-2001"};
let string hdcw2={"Regional Weights Calculated Using PPP-GDP Figures for 1999-2001"};
nulcw1=reshape("",cols(cwgts),2);
nulcw1[2,1]=hdcw1;
fname ="c:\\gauss6.0\\gvar\\output\\cwgts.xls";
ret = xlsWrite(cwgts, fname,"a5",1,vls);
fname ="c:\\gauss6.0\\gvar\\output\\cwgts.xls";
ret = xlsWrite(cnam, fname,"a5",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\cwgts.xls";
ret = xlsWrite(nulcw1'$|("Country"$|vnam)', fname,"a2",1,"");


/****** PPP-GDP Regional Weights *****/


{rgwgts,callvindrgwgts,cindvallrgwgts}=regional_weights(indxregcir,ypp1,binend,namvri);

nulcw2=reshape("",cols(rgwgts),2);
nulcw2[3,1]=hdcw2;
fname ="c:\\gauss6.0\\gvar\\output\\rgwgts.xls";
ret = xlsWrite(rgwgts, fname,"a5",1,vls);
fname ="c:\\gauss6.0\\gvar\\output\\rgwgts.xls";
ret = xlsWrite(cnam, fname,"b5",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\rgwgts.xls";
ret = xlsWrite(nulcw2'$|("Regional #"$|"Country"$|vnam)', fname,"a2",1,"");
/********************************/




/*** Horizon for Persistence Profiles (PP), Generalised Impulse
Response Functions (GIRFs) and Generalised Forecast Error Variance 
Decomposition (GFEVDs) ***/

horz=40; 



/*** Effect of Shocks on Cointegrating Relations/Persistence Profiles (PP)***/

{hd_cr,shockvr_cr,typeshock_cr,Ikpos_cr,Ikneg_cr,type_girf_cr,type_pp}=PP_SelectionDialogue(vnam,cnam,id1,
                                   namvri,binend,cindvallcwgts,indxregcir,type_id);


{effect_cr}=per_profile(horz,Greswm,covreswm,Gwm,F,Pg,betawm,S0,S1,rcnt,binend,binfex,
           shockvr_cr,typeshock_cr,Ikpos_cr,Ikneg_cr,type_id,type_girf_cr,type_pp);

let string vamcv={"CV"};
let string nul={""};
hdr=reshape(vamcv,rows(effect_cr),1);
sqpp_s="" $+ ftocv(effect_cr[.,1],1,0);
hdcv=hdr$+sqpp_s;
hdcv1=hdcv[2:rows(hdcv)];
hdcv[1]=nul;


/*** Effect of Shocks on Cointegrating Relations/Persistence Profiles (PP) -Point Estimates  ***/


let string ad={"Horizon"};
fname ="c:\\gauss6.0\\gvar\\output\\Effect_CR.xls";
ret = xlswritesa(hd_cr, fname,"c1",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Effect_CR.xls";
ret = xlsWrite(effect_cr[.,2:cols(effect_cr)]', fname,"a4",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Effect_CR.xls";
ret = xlsWrite(ad$~hdcv1', fname,"a3",1,"");


if type_idbeta==0;
/** adds the country names to the cointegrating relations **/
{cnamescv}=cnames_cvexid(cnam,rcnt); 
fname ="c:\\gauss6.0\\gvar\\output\\Effect_CR.xls";
ret = xlsWrite(cnamescv', fname,"b2",1,"");
elseif type_idbeta==1;
es=hdOR_epepsus[.,2] .$== "NA";
indxpos=indexcat(es,1);
if sumc(es)/=0;
hdOR_epepsus[indxpos,2]=hdcv1[indxpos];
endif;
fname ="c:\\gauss6.0\\gvar\\output\\Effect_CR.xls";
ret = xlsWrite(hdOR_epepsus', fname,"b2",1,"");
elseif type_idbeta==2;
es=hdOR_epeps[.,2] .$== "NA";
if sumc(es)/=0;
indxpos=indexcat(es,1);
hdOR_epeps[indxpos,2]=hdcv1[indxpos];
endif;
fname ="c:\\gauss6.0\\gvar\\output\\Effect_CR.xls";
ret = xlsWrite(hdOR_epeps', fname,"b2",1,"");
endif;


/**** Generalised Impulse Response Functions (GIRF)  ****/

{hd_girf,shockvr,typeshock,Ikpos,Ikneg,type_girf}=GIRF_SelectionDialogue(vnam,cnam,id1,namvri,binend,cindvallcwgts,
                                               indxregcir,type_id);


{cvnamcindvallgirf,cvnamcallvindgirf,cvnamcallvindgirfrg,cvnamcindvallgirfrg,
cvcindvallcallvind,cvcindvallcallvindrg,binendrg}=GIRF(horz,type_id,id1,cnamrg,
idreg,cnam,vnam,binend,binfex,namvri,Greswm,covreswm,Gwm,F,Pg,cindvallcwgts,cindvallrgwgts,
callvindrgwgts,shockvr,typeshock,Ikpos,Ikneg,rgshmtx_allyrs_rep,wyear,type_idbeta,type_girf);

let string nul2={"",""};

/***  GIRFs - Point Estimates ***/

fname ="c:\\gauss6.0\\gvar\\output\\cvnamcindvallgirf.xls";
ret = xlsWrite(cvnamcindvallgirf, fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\cvnamcindvallgirf.xls";
ret = xlswritesa((nul2'$|cvcindvallcallvind[.,1:2]), fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\cvnamcindvallgirf.xls";
ret = xlswritesa(hd_girf, fname,"e1",1,"");


fname ="c:\\gauss6.0\\gvar\\output\\cvnamcallvindgirf.xls";
ret = xlsWrite(cvnamcallvindgirf, fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\cvnamcallvindgirf.xls";
ret = xlsWrite((nul2'$|cvcindvallcallvind[.,3:4]), fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\cvnamcallvindgirf.xls";
ret = xlswritesa(hd_girf, fname,"e1",1,"");


fname ="c:\\gauss6.0\\gvar\\output\\cvnamcindvallgirfrg.xls";
ret = xlsWrite(cvnamcindvallgirfrg, fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\cvnamcindvallgirfrg.xls";
ret = xlsWrite((nul2'$|cvcindvallcallvindrg[.,1:2]), fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\cvnamcindvallgirfrg.xls";
ret = xlswritesa(hd_girf, fname,"e1",1,"");

fname ="c:\\gauss6.0\\gvar\\output\\cvnamcallvindgirfrg.xls";
ret = xlsWrite(cvnamcallvindgirfrg, fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\cvnamcallvindgirfrg.xls";
ret = xlsWrite((nul2'$|cvcindvallcallvindrg[.,3:4]), fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\cvnamcallvindgirfrg.xls";
ret = xlswritesa(hd_girf, fname,"e1",1,"");

/**************************************************/



/***  Generalised Forecast Error Variance Decomposition (GFEVD) ***/


{hd_gevd,jk,epsil_i,type_gevd}=GFEVD_SelectionDialogue(vnam,cnam,id1,namvri,binend,cindvallcwgts,
                                    indxregcir,type_id);


{cvnamcindvallgevd,cvnamcallvindgevd,cvnamcallvindgevdrg,cvnamcindvallgevdrg,
cvcindvallcallvind,cvcindvallcallvindrg}=GFEVD(horz,type_id,id1,cnamrg,idreg,cnam,vnam,binend,
          binfex,namvri,Greswm,covreswm,Gwm,F,Pg,cindvallcwgts,cindvallrgwgts,callvindrgwgts,jk,
                                   rgshmtx_allyrs_rep,wyear,type_idbeta,epsil_i,type_gevd);

/*** GFEVD - Point Estimates ***/

fname ="c:\\gauss6.0\\gvar\\output\\cvnamcindvallgevd.xls";
ret = xlsWrite(cvnamcindvallgevd, fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\cvnamcindvallgevd.xls";
ret = xlswritesa((nul2'$|cvcindvallcallvind[.,1:2]), fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\cvnamcindvallgevd.xls";
ret = xlswritesa(hd_gevd, fname,"e1",1,"");

fname ="c:\\gauss6.0\\gvar\\output\\cvnamcallvindgevd.xls";
ret = xlsWrite(cvnamcallvindgevd, fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\cvnamcallvindgevd.xls";
ret = xlsWrite((nul2'$|cvcindvallcallvind[.,3:4]), fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\cvnamcallvindgevd.xls";
ret = xlswritesa(hd_gevd, fname,"e1",1,"");

fname ="c:\\gauss6.0\\gvar\\output\\cvnamcindvallgevdrg.xls";
ret = xlsWrite(cvnamcindvallgevdrg, fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\cvnamcindvallgevdrg.xls";
ret = xlsWrite((nul2'$|cvcindvallcallvindrg[.,1:2]), fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\cvnamcindvallgevdrg.xls";
ret = xlswritesa(hd_gevd, fname,"e1",1,"");

fname ="c:\\gauss6.0\\gvar\\output\\cvnamcallvindgevdrg.xls";
ret = xlsWrite(cvnamcallvindgevdrg, fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\cvnamcallvindgevdrg.xls";
ret = xlsWrite((nul2'$|cvcindvallcallvindrg[.,3:4]), fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\cvnamcallvindgevdrg.xls";
ret = xlswritesa(hd_gevd, fname,"e1",1,"");


/*** Bootstrap confidence intervals for impulse responses, persistence profiles
 and variance decomposition, aslo chi-square critical values for the case of overidentifying
restrictions***/

print"";
print"";
print "***************************************************************************************";
print "Select 0, if you do not wish to proceed with obtaining bootstrap confidence intervals"; 
print "          of persistence profiles, impulse responses and variance decompositions";
print "Select 1, to obtain 90% bootstrap confidence intervals of persistence profiles, impulse";
print "          responses and variance decompositions";
print "Select 2, to obtain 90% bootstrap confidence intervals of persistence profiles, impulse";
print "          responses and variance decompositions if structural impulse response analysis"; 
print "          was selected earlier";
print"";
print "Note: If options 1 or 2 are selected, chi-square critical values in the case of over-"; 
print "identifying restrictions on the long run (beta) parameters of the individual VARX*"; 
print "equations are also produced"; 
print "****************************************************************************************";

btstrp=con(1,1);


if btstrp/=0;

if (type_id==2 and btstrp==1); 
print "error: Option 2 should have been selected based on earlier input";
end; 
elseif (type_id==1 and btstrp==2);
print "error: Option 1 should have been selected based on earlier input"; 
end;
endif; 

/**** Bootstrapping the GVAR ***/

maxnb=4000; /** Max number of bootstrap replications permitted **/
nb=2000;   /** Number of bootstrap replications **/

btstrp_sb=0;


{ChisqCVs,Lband_girf,Mean_girf,Uband_girf,Lbandrg_girf,Meanrg_girf,
Ubandrg_girf,Lband_gevd,Mean_gevd,Uband_gevd,Lbandrg_gevd,Meanrg_gevd,
Ubandrg_gevd,Lband_cr,Mean_cr,Uband_cr,Median_cr,Median_girf,
Medianrg_girf,Median_gevd,Medianrg_gevd}= BootstrapGVAR(zstr,zzun1,id1,namvri,cnam,vnam,cnamrg,
idreg,k,kst,Lv,Lvst,rcnt,cs,binend,binfex,wi,vcex,qini,qfin,frq,yr_sq,rgshmtx_allyrs,endstr,
dendstr,starstr,dstarstr,mxpq,Gwm,Gdtwm,GBefwm,Greswm,covreswm,cindvallcwgts,cindvallrgwgts,
callvindrgwgts,shockvr_cr,typeshock_cr,Ikpos_cr,Ikneg_cr,type_girf_cr,type_pp,shockvr,typeshock,
Ikpos,Ikneg,type_girf,cvnamcindvallgirfrg,wyear,jk,epsil_i,type_gevd,out_ECMVARX,ccut,namvri_org,
binefe_org,cv_rep,type_idbeta,ccOR,btstrp,btstrp_sb,maxnb,nb,horz,print_VARX,out_corrVARX);



fname ="c:\\gauss6.0\\gvar\\output\\ChiSqForOR.xls";
ret = xlsWrite(ChisqCVs, fname,"h1",1,"");


let string hd_b1m={"Bootstrap Mean Estimates of a "};
let string hd_b2m={"Bootstrap Mean Estimates of the "};
let string hdL_b={"Lower 5% Band for the Bootstrap Estimates "};
let string hdU_b={"Upper 95% Band for the Bootstrap Estimates "};

hd_girfbm=hd_b1m $+ hd_girf;
hd_gevdbm=hd_b2m $+ hd_gevd;
if type_pp==1;
hd_crbm=hd_b2m $+ hd_cr;
elseif type_pp==2;
hd_crbm=hd_b1m $+ hd_cr;
endif;

let string hd_b1md={"Bootstrap Median Estimates of a "};
let string hd_b2md={"Bootstrap Median Estimates of the "};

hd_girfbmd=hd_b1md $+ hd_girf;
hd_gevdbmd=hd_b2md $+ hd_gevd;
if type_pp==1;
hd_crbmd=hd_b2md $+ hd_cr;
elseif type_pp==2;
hd_crbmd=hd_b1md $+ hd_cr;
endif;



/*** Effect of shocks on Cointegrating Relations (CR) ***/

fname ="c:\\gauss6.0\\gvar\\output\\Lband_CR.xls";
ret = xlsWrite(Lband_cr, fname,"b2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Lband_CR.xls";
ret = xlsWrite(hdcv, fname,"b2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Lband_CR.xls";
ret = xlsWrite(hdL_b, fname,"e1",1,"");

fname ="c:\\gauss6.0\\gvar\\output\\Uband_CR.xls";
ret = xlsWrite(Uband_cr, fname,"b2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Uband_CR.xls";
ret = xlsWrite(hdcv, fname,"b2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Uband_CR.xls";
ret = xlsWrite(hdU_b, fname,"e1",1,"");


fname ="c:\\gauss6.0\\gvar\\output\\Mean_CR.xls";
ret = xlsWrite(Mean_cr, fname,"b2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Mean_CR.xls";
ret = xlsWrite(hdcv, fname,"b2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Mean_CR.xls";
ret = xlswritesa(hd_crbm, fname,"e1",1,"");

fname ="c:\\gauss6.0\\gvar\\output\\Median_CR.xls";
ret = xlsWrite(Median_cr, fname,"b2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Median_CR.xls";
ret = xlsWrite(hdcv, fname,"b2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Median_CR.xls";
ret = xlswritesa(hd_crbmd, fname,"e1",1,"");


addr=reshape("",2,1)';
if type_idbeta==0;
{cnamescv}=cnames_cvexid(cnam,rcnt);
fname ="c:\\gauss6.0\\gvar\\output\\Lband_CR.xls";
ret = xlsWrite(cnamescv, fname,"a3",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Mean_CR.xls";
ret = xlsWrite(cnamescv, fname,"a3",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Median_CR.xls";
ret = xlsWrite(cnamescv, fname,"a3",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Uband_CR.xls";
ret = xlsWrite(cnamescv, fname,"a3",1,"");
elseif type_idbeta==1;
es=hdOR_epepsus[.,2] .$== "NA";
indxpos=indexcat(es,1);
if sumc(es)/=0;
hdOR_epepsus[indxpos,2]=hdcv1[indxpos];
endif;
fname ="c:\\gauss6.0\\gvar\\output\\Lband_CR.xls";
ret = xlsWrite(addr $| hdOR_epepsus, fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Mean_CR.xls";
ret = xlsWrite(addr $| hdOR_epepsus, fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Median_CR.xls";
ret = xlsWrite(addr $| hdOR_epepsus, fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Uband_CR.xls";
ret = xlsWrite(addr $| hdOR_epepsus, fname,"a2",1,"");
elseif type_idbeta==2;
es=hdOR_epeps[.,2] .$== "NA";
if sumc(es)/=0;
indxpos=indexcat(es,1);
hdOR_epeps[indxpos,2]=hdcv1[indxpos];
endif;
fname ="c:\\gauss6.0\\gvar\\output\\Lband_CR.xls";
ret = xlsWrite(addr $| hdOR_epeps, fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Mean_CR.xls";
ret = xlsWrite(addr $| hdOR_epeps, fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Median_CR.xls";
ret = xlsWrite(addr $| hdOR_epeps, fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Uband_CR.xls";
ret = xlsWrite(addr $| hdOR_epeps, fname,"a2",1,"");
endif;



/************************* GIRFs ************************/

/*** Countries ***/

fname ="c:\\gauss6.0\\gvar\\output\\Lband_girf.xls";
ret = xlsWrite(Lband_girf, fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Lband_girf.xls";
ret = xlsWrite((nul2'$|cvcindvallcallvind[.,3:4]), fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Lband_girf.xls";
ret = xlsWrite(hdL_b, fname,"e1",1,"");


fname ="c:\\gauss6.0\\gvar\\output\\Uband_girf.xls";
ret = xlsWrite(Uband_girf, fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Uband_girf.xls";
ret = xlsWrite((nul2'$|cvcindvallcallvind[.,3:4]), fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Uband_girf.xls";
ret = xlsWrite(hdU_b, fname,"e1",1,"");


fname ="c:\\gauss6.0\\gvar\\output\\Mean_girf.xls";
ret = xlsWrite(Mean_girf, fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Mean_girf.xls";
ret = xlsWrite((nul2'$|cvcindvallcallvind[.,3:4]), fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Mean_girf.xls";
ret = xlswritesa(hd_girfbm, fname,"e1",1,"");


fname ="c:\\gauss6.0\\gvar\\output\\Median_girf.xls";
ret = xlsWrite(Median_girf, fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Median_girf.xls";
ret = xlsWrite((nul2'$|cvcindvallcallvind[.,3:4]), fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Median_girf.xls";
ret = xlswritesa(hd_girfbmd, fname,"e1",1,"");


/*** Regions ***/

fname ="c:\\gauss6.0\\gvar\\output\\Lbandrg_girf.xls";
ret = xlsWrite(Lbandrg_girf, fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Lbandrg_girf.xls";
ret = xlsWrite((nul2'$|cvcindvallcallvindrg[.,3:4]), fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Lbandrg_girf.xls";
ret = xlsWrite(hdL_b, fname,"e1",1,"");


fname ="c:\\gauss6.0\\gvar\\output\\Ubandrg_girf.xls";
ret = xlsWrite(Ubandrg_girf, fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Ubandrg_girf.xls";
ret = xlsWrite((nul2'$|cvcindvallcallvindrg[.,3:4]), fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Ubandrg_girf.xls";
ret = xlsWrite(hdU_b, fname,"e1",1,"");


fname ="c:\\gauss6.0\\gvar\\output\\Meanrg_girf.xls";
ret = xlsWrite(Meanrg_girf, fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Meanrg_girf.xls";
ret = xlsWrite((nul2'$|cvcindvallcallvindrg[.,3:4]), fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Meanrg_girf.xls";
ret = xlswritesa(hd_girfbm, fname,"e1",1,"");

fname ="c:\\gauss6.0\\gvar\\output\\Medianrg_girf.xls";
ret = xlsWrite(Medianrg_girf, fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Medianrg_girf.xls";
ret = xlsWrite((nul2'$|cvcindvallcallvindrg[.,3:4]), fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Medianrg_girf.xls";
ret = xlswritesa(hd_girfbmd, fname,"e1",1,"");

/******************* GFEVD *******************/

/*** Countries ***/

fname ="c:\\gauss6.0\\gvar\\output\\Lband_gevd.xls";
ret = xlsWrite(Lband_gevd, fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Lband_gevd.xls";
ret = xlsWrite((nul2'$|cvcindvallcallvind[.,3:4]), fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Lband_gevd.xls";
ret = xlsWrite(hdL_b, fname,"e1",1,"");


fname ="c:\\gauss6.0\\gvar\\output\\Uband_gevd.xls";
ret = xlsWrite(Uband_gevd, fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Uband_gevd.xls";
ret = xlsWrite((nul2'$|cvcindvallcallvind[.,3:4]), fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Uband_gevd.xls";
ret = xlsWrite(hdU_b, fname,"e1",1,"");


fname ="c:\\gauss6.0\\gvar\\output\\Mean_gevd.xls";
ret = xlsWrite(Mean_gevd, fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Mean_gevd.xls";
ret = xlsWrite((nul2'$|cvcindvallcallvind[.,3:4]), fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Mean_gevd.xls";
ret = xlswritesa(hd_gevdbm, fname,"e1",1,"");

fname ="c:\\gauss6.0\\gvar\\output\\Median_gevd.xls";
ret = xlsWrite(Median_gevd, fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Median_gevd.xls";
ret = xlsWrite((nul2'$|cvcindvallcallvind[.,3:4]), fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Median_gevd.xls";
ret = xlswritesa(hd_gevdbmd, fname,"e1",1,"");

/** Regions ***/

fname ="c:\\gauss6.0\\gvar\\output\\Lbandrg_gevd.xls";
ret = xlsWrite(Lbandrg_gevd, fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Lbandrg_gevd.xls";
ret = xlsWrite((nul2'$|cvcindvallcallvindrg[.,3:4]), fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Lbandrg_gevd.xls";
ret = xlsWrite(hdL_b, fname,"e1",1,"");

fname ="c:\\gauss6.0\\gvar\\output\\Ubandrg_gevd.xls";
ret = xlsWrite(Ubandrg_gevd, fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Ubandrg_gevd.xls";
ret = xlsWrite((nul2'$|cvcindvallcallvindrg[.,3:4]), fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Ubandrg_gevd.xls";
ret = xlsWrite(hdU_b, fname,"e1",1,"");


fname ="c:\\gauss6.0\\gvar\\output\\Meanrg_gevd.xls";
ret = xlsWrite(Meanrg_gevd, fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Meanrg_gevd.xls";
ret = xlsWrite((nul2'$|cvcindvallcallvindrg[.,3:4]), fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Meanrg_gevd.xls";
ret = xlswritesa(hd_gevdbm, fname,"e1",1,"");


fname ="c:\\gauss6.0\\gvar\\output\\Medianrg_gevd.xls";
ret = xlsWrite(Medianrg_gevd, fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Medianrg_gevd.xls";
ret = xlsWrite((nul2'$|cvcindvallcallvindrg[.,3:4]), fname,"a2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\Medianrg_gevd.xls";
ret = xlswritesa(hd_gevdbmd, fname,"e1",1,"");

/***************************************************/

endif;



print"";
print"";
print "***********************************************************************************";
print "Select 0, to end the program";
print"";
print "Select 1, if you wish to proceed with obtaining structural stability test results";
print "          for the individual equations of the country-specific VARX* models.";
print "          Note that the critical values for these tests will be obtained via";
print "          the bootstrap.";
print "************************************************************************************";


btstrp_sb=con(1,1);

maxnb=4000; /** Max number of bootstrap replications permitted **/
nb=2000;   /** Number of bootstrap replications **/

if btstrp_sb/=0;

if btstrp==0;
if type_id==1;
btstrp=1;
elseif type_id==2;
btstrp=2;
endif;
endif;

{StbTests_summary_all,StbTests_all,bd_sup_chow_all}=BootstrapGVAR_sb(zstr,zzun1,id1,namvri,cnam,vnam,cnamrg,idreg,
k,kst,Lv,Lvst,rcnt,cs,binend,binfex,wi,vcex,qini,qfin,frq,yr_sq,rgshmtx_allyrs,endstr,dendstr,starstr,dstarstr,mxpq,
Gwm,Gdtwm,GBefwm,Greswm,covreswm,kpres1,kpres2,nyres1,nyres2,chres1,chres2,chres3,bkdate,rchres1,rchres2,rchres3,
out_ECMVARX,ccut,namvri_org,binefe_org,cv_rep,type_idbeta,ccOR,btstrp,btstrp_sb,maxnb,nb,print_VARX,out_corrVARX);

fname ="c:\\gauss6.0\\gvar\\output\\SBtests_summary.xls";
ret = xlsWrite(StbTests_summary_all, fname,"a2",1,"");

fname ="c:\\gauss6.0\\gvar\\output\\SBtests_detailed.xls";
ret = xlsWrite(StbTests_all, fname,"a2",1,"");

fname ="c:\\gauss6.0\\gvar\\output\\BreakDate_supchow.xls";
ret = xlsWrite(bd_sup_chow_all, fname,"a2",1,"");


endif;




d2 = date;
  et = ethsec(d1,d2);
  et = et/100;
  print "Total elapsed time [mins] :" et/60;
  print "";


end;