
@**********************************************************************

EXPERIMENT 1: US DOMINANT IN OUTPUT and INFLATION


TWO COUNTRY VAR

		Gauss code for the project by 

    ORIGINAL CODE:  August 12 2008
	THIS CODE:      

	COPYRIGHT:      Mardi Dungey and Denise Osborn

	FURTHER INFORMATION:  

	SUMMARY

	DETAILS IN BRIEF:
	
    DATA SAMPLE:    March 1971 - March 2008

	KEY VARIABLES:  
        US and Europe
        Output, inflation, short rates, long rates and exchange rate compiled from
        Anderson, Dungey, Osborn and Vahid (2007) on Euro area data. US data from FRED2

    COINTEGRATING RELATIONSHIPS:


    STRUCTURE OF THE MODEL:
            
    STRUCTURE OF CODE

**********************************************************************@

new;
library pgraph;
graphset;
gausset;
rndseed 123457;


@---- set save and load paths for data input and output -----@
    
    
      @---
     spath="c:\\Mydocuments\\Mywork\\Denise\\programs\\output\\";
     loadm path="c:\\Mydocuments\\Mywork\\Denise\\programs\\output\\";
    ---@
    
    @---@
     spath="c:\\visit_July11\\Denise\\programs\\output\\";
     loadm path="c:\\visit_July11\\Denise\\programs\\output\\";
    @---@



     n      = 7;            @-- number of variables in the model                --@
     p      = 3;             @-- lag length in VAR                               --@
     q      = 60;            @-- order of impulse responses                      --@
     
 
    @-- set acronyms for variable labels in the order that the variables appear in the model ---@

/*     vord ="y_us"$|"pi_us"$|"r_us"$|"lr_us"$|"y_eu"$|"pi_eu"$|"r_eu"$|"lr_eu"$|"erate"; */
     vord ="y_us"$|"inf_us"$|"r_us"$|"y_eu"$|"pi_eu"$|"r_eu"$|"erate";


    @---- additional graphics settings ----@
     _ptitlht=0.5;
	 _pdate = "";
	 _pnum = 2;
	 _pnumht = 0.2;
	 _paxht  = 0.3; 
	 fonts("microb");
	 xlabel("");
	 ylabel("");
/*	 _pline={1 1 6 0 30 0 1 9 0};*/


    
@------------------------------------------------------------------------------
                 load DATASETS
------------------------------------------------------------------------------@


  
    load data[149,14]=c:\\visit_July11\\Denise\\programs\\dataset08_2oiladov.txt;



  
    pidum=zeros(rows(data),1);             @ - German reunification dummy for the inflation rate -@
    pidum[112:114,.]=ones(3,1);
    pidum=trimr(pidum,1,0);
    pidum=pidum[49:rows(pidum),.];
    
    @--create the price levels -- @
    p={98, 99, 100, 101};
    p=p~p;
    
    for i(1,rows(data),1);
            p1=p[i,1]*(1+data[i,2]/100);
            p2=p[i,2]*(1+data[i,6]/100);
            p=p|(p1~p2);
    endfor;
    p=p[5:rows(p),.];
    p=p[49:rows(p),.];       @49@  @works from 28 also@
    
    
    
    data=data[49:rows(data),.];  @49@

    y_us=ln(data[.,1]);
    pi_us=data[.,2];
    r_us=data[.,3];
    lr_us=data[.,4];
    y_eu=ln(data[.,5]);
    pi_eu=data[.,14];
    r_eu=data[.,7];
    lr_eu=data[.,8];
    r_eu_AWM=data[.,9];
    lr_eu_AWM=data[.,10];
    erate_nom=ln(100*data[.,9]);
    erate=ln(100*data[.,11])-ln(p[.,1])+ln(p[.,2]);
    oil = data[.,13];

    real_us=r_us-pi_us;
    real_eu=r_eu-pi_eu;
  
    dataord=y_us~pi_us~r_us~lr_us~y_eu~pi_eu~r_eu~lr_eu~erate~oil;

    reundum=zeros(rows(dataord),1);             @ - German reunification dummy for the inflation rate -@
    reundum[79:93,.]=ones(15,1);

    
    /*
    @-- plots of data --@
    begwind;
    window(3,3,0);
    setwind(1);
        for i(1,9,1);
            title(vord[i]);
            xy(rows(dataord),dataord[.,i]);
            nextwind;
        endfor;
    endwind;
*/


    

@------------------------------------------------------------------------------
                3. Data transformations
-------------------------------------------------------------------------------@


    @--- Choose dataset and set trend assumptions here  ---@

     ydata  = dataord;
     
     ttrend  = ones(rows(ydata),1)~seqa(1,1,rows(ydata));    @-- define constant and trend --@
     b      = ydata/ttrend;                                   
    

    @----- Express data in change form which is used in the estimation ---@
     y      = trimr(ydata,1,0)-trimr(ydata,0,1);
    
@------------------------------------------------------------------------------
                4. Specification of long run relationships
------------------------------------------------------------------------------@

    @-- the I(1) variables are: ly and lystar. All others are I(0)
     -- the next step is to take the I(1) variables and establish the cointegrating
     -- vector(s) between them (cointegrating vector is established via the Engle Granger 2 step. 
     -- These are used in constructing the ecm terms  --@

    @-- in the current set up there are 2 I(1) variables and 1 cointegrating vectors
     -- This means that there are 1 transitory shocks and 1 permanent shocks
     -- the variables for the transitory shocks are those which have the most significant
     -- responses to the two ecm terms in a VECM of the 6 I(1) variables with 2 cointegrating
     -- vectors (excluding tax), and one cointegrating vector derived from theory   --@


    @-- Perform the Engle-Granger 2 step procedure to determine the cointegrating vectors --@
        
     @-- The first cointegrating vector consists of y, ystar, erate, (with constant and trend)--@

       @-- step 1: regression --@
        y_eg  = ydata[.,5];    @--- choose the variable to normalise upon. Here it is y_eu --@
        x_eg  = ydata[.,1 9]~ttrend[.,1];
        b_eg1 = y_eg/x_eg;

       @-- step 2: perform ADF test on residuals --@
        e_eg1 = y_eg - x_eg*b_eg1;  
    
        { b,t} = eg(e_eg1);

        print "";
        print "check for cointegration between y_eu and y_us and erate (constant and trend last) ";
        print "";
        print "the first cointegrating vector is" 1~-1*(b_eg1)'; 
        print "";


     ecm1 = trimr(ydata[.,5]-b_eg1[1]*ydata[.,1]-b_eg1[2]*ydata[.,9]-b_eg1[3]*ttrend[.,1],0,1); 
    

    xy(rows(ecm1),ecm1);



    @-- Construct error correction terms corresponding to the I(0) variables ---@
     ecminf_us  =trimr(ydata[.,2],0,1);
     ecmr_us    =trimr(ydata[.,3],0,1);
     ecminf_eu   = trimr(ydata[.,6],0,1);
     ecmr_eu = trimr(ydata[.,7],0,1);

    @-- Construct the beta term         --@
    @-- Note the sign change here too   --@
     beta=zeros(n,5);
     beta[1,1] = -b_eg1[1];  beta[4,1]=1;        beta[7,1]=-b_eg1[2];
     beta[2,2] = 1;             @-- us inflation -@
     beta[3,3] = 1;             @-- us short rate -@ 
     beta[5,4] = 1;             @-- eu inflation-@
     beta[6,5] = 1;             @-- eu short rate -@

@------------------------------------------------------------------------------
                5. OLS parameter estimation

    The structure of each equation is summarised in Table 4 of the document. 

------------------------------------------------------------------------------@
    @-- set up matrices to store parameter estimates --@
     b0 = eye(n);
     b1 = zeros(n,n);
     b2 = zeros(n,n);

     c  = zeros(n,5);    @-- responses to the ecm terms, columns = number of cointegrating relationships + no. I(0) variables --@
     cn = ones(rows(y)-2,1);  @- constant (corresponds to trend term) -@

    @--eqn1 ystar--@
        eqn  = 1;                               @-- equation number              --@
        yvar = trimr(y[.,1],2,0);               @-- dependent variable           --@
         /** xvar1 = trimr(y[.,1 2 3 9],1,1);            @-- AR(1) explanatory variables  --@
          xvar2 = trimr(y[.,1 2 3 9],0,2);            @-- AR(2) explanatory variables  --@
        **/
          xvar1 = trimr(y[.,1],1,1)~trimr(real_us,2,1)~trimr(y[.,5],1,1);            @-- AR(1) explanatory variables  --@
          xvar2 = trimr(y[.,1],0,2)~trimr(real_us,1,2)~trimr(y[.,5],0,2);            @-- AR(2) explanatory variables  --@
        xvar = xvar1~xvar2~cn;                     @-- all explanatory variables    --@
        bvar = yvar/xvar;                       @-- OLS estimation               --@
        e1 = yvar - xvar*bvar;                  @-- residuals                    --@
        b1[eqn,1 2 3 4] = bvar[1:2]'~(-1)*bvar[2]~bvar[3];                   @-- place AR(1) ests. into B1    --@
        b2[eqn,1 2 3 4] = bvar[4:5]'~(-1)*bvar[5]~bvar[6];                   @-- place AR(2) ests. into B2    --@
        bvar1=bvar;
        reg1=xvar;

    @--eqn2 inflation_us--@        
        eqn  = eqn+1;                           @-- equation number               --@
        yvar = trimr(y[.,2],2,0);              @-- dependent variable            --@
          xvar0   = trimr(y[.,1],2,0);        @-- contemp. explanatory variables--@  
          xvar1   = trimr(y[.,1 2 6 9],1,1);  @-- AR(1) explanatory variables   --@
          xvar2   = trimr(y[.,1 2 6 9],0,2);  @-- AR(2) explanatory variables   --@
          xvarecm = trimr(ecm1~ecminf_us~ecminf_eu,2,0); 
          xvarexog = trimr(oil,3,0);         @-- ecm explanatory variables     --@                          
        xvar = xvar0~xvar1~xvar2~xvarecm~cn; @-- all explanatory variables     --@ 
        bvar = yvar/xvar;                       @-- OLS estimation                --@          
        e2 = yvar - xvar*bvar;                 @-- residuals                     --@
        b0[eqn,1]       = -bvar[1]';         @-- place contemp. ests. into B0  --@
        b1[eqn,1 2 5 7] = bvar[2:5]';         @-- place AR(1) ests. into B1     --@
        b2[eqn,1 2 5 7] = bvar[6:9]';        @-- place AR(2) ests. into B2     --@
        c[eqn,1 2 4]          = bvar[10:12]';          @-- place ecm ests. into C        --@                
        bvar2=bvar;
        reg2=xvar;

    @--eqn3 short_us--@        
        eqn  = eqn+1;                           @-- equation number               --@
        yvar = trimr(y[.,3],2,0);              @-- dependent variable            --@
          xvar0 = trimr(y[.,1 2],2,0);         @-- contemp. explanatory variables--@  
          xvar1 = trimr(y[.,1 2 3],1,1);      @-- AR(1) explanatory variables   --@
          xvar2 = trimr(y[.,1 2 3],0,2);      @-- AR(2) explanatory variables   --@           
          xvarecm= trimr(ecm1~ecminf_us~ecmr_us,2,0);  @-- ecm explanatory variables     --@    
        xvar = xvar0~xvar1~xvar2~xvarecm~cn;       @-- all explanatory variables     --@ 
        bvar = yvar/xvar;                       @-- OLS estimation                --@            
        e3  = yvar - xvar*bvar;                @-- residuals                     --@
        b0[eqn,1 2]    = -bvar[1:2]';           @-- place contemp. ests. into B0  --@
        b1[eqn,1 2 3] = bvar[3:5]';           @-- place AR(1) ests. into B1     --@
        b2[eqn,1 2 3] = bvar[6:8]';           @-- place AR(2) ests. into B2     --@
        c[eqn, 1 2 3]    = bvar[9:11]';          @-- place ecm ests. into C        --@                
        bvar3=bvar;
        reg3=xvar;



    @--eqn4 y_eu--@
        eqn  = eqn+1;                           @-- equation number               --@
        yvar = trimr(y[.,5],2,0);               @-- dependent variable            --@
          xvar0 = trimr(y[.,1],2,0);          @-- contemp. explanatory variables--@  
          /*xvar1 = trimr(y[.,1 5 6 7 9],1,1);        @-- AR(1) explanatory variables   --@
          xvar2 = trimr(y[.,1 5 6 7 9],0,2);        @-- AR(2) explanatory variables   --@
          **/
          xvar1 = trimr(y[.,1 5],1,1)~trimr(real_eu,2,1);        @-- AR(1) explanatory variables   --@
          xvar2 = trimr(y[.,1 5],0,2)~trimr(real_eu,1,2);        @-- AR(2) explanatory variables   --@
        xvar=xvar0~xvar1~xvar2~cn;
        bvar = yvar/xvar;                       @-- OLS estimation               --@       
        e4 = yvar - xvar*bvar;                  @-- residuals                    --@
        b0[eqn,1]    = -bvar[1]';          @-- place contemp. ests. into B0 --@
        b1[eqn,1 4:6] = bvar[2:4]'~(-1)*bvar[4];         @-- place AR(1) ests. into B1    --@
        b2[eqn,1 4:6] = bvar[5:7]'~(-1)*bvar[7];        @-- place AR(2) ests. into B2    --@
        bvar4=bvar;
        reg4=xvar;


    @--eqn5 inflation_eu--@        
        eqn  = eqn+1;                           @-- equation number               --@
        yvar = trimr(y[.,6],2,0);              @-- dependent variable            --@
          xvar0   = trimr(y[.,2 5],2,0);        @-- contemp. explanatory variables--@  
          xvar1   = trimr(y[.,2 5 6 9],1,1);  @-- AR(1) explanatory variables   --@
          xvar2   = trimr(y[.,2 5 6 9],0,2);  @-- AR(2) explanatory variables   --@
         /* xvarexog= trimr(oil,3,0);*/
          xvarecm = trimr(ecm1~ecminf_us~ecminf_eu,2,0);          @-- ecm explanatory variables     --@                          
        xvar = xvar0~xvar1~xvar2~xvarecm~cn; @-- all explanatory variables     --@ 
        bvar = yvar/xvar;                       @-- OLS estimation                --@          
        e5 = yvar - xvar*bvar;                 @-- residuals                     --@
        b0[eqn,2 4]       = -bvar[1:2]';         @-- place contemp. ests. into B0  --@
        b1[eqn,2 4 5 7] = bvar[3:6]';         @-- place AR(1) ests. into B1     --@
        b2[eqn,2 4 5 7] = bvar[7:10]';        @-- place AR(2) ests. into B2     --@
        c[eqn,1 2 4]          = bvar[11:13]';          @-- place ecm ests. into C        --@                
        bvar5=bvar;
        reg5=xvar;

    @--eqn6 short--@        
        eqn  = eqn+1;                           @-- equation number               --@
        yvar = trimr(y[.,7],2,0);              @-- dependent variable            --@
          xvar0 = trimr(y[.,5 6],2,0);         @-- contemp. explanatory variables--@  
          xvar1 = trimr(y[.,5 6 7],1,1);      @-- AR(1) explanatory variables   --@
          xvar2 = trimr(y[.,5 6 7],0,2);      @-- AR(2) explanatory variables   --@
          xvarecm= trimr(ecm1~ecminf_eu~ecmr_eu,2,0);  @-- ecm explanatory variables     --@    
        xvar = xvar0~xvar1~xvar2~xvarecm~cn;       @-- all explanatory variables     --@ 
        bvar = yvar/xvar;                       @-- OLS estimation                --@            
        e6  = yvar - xvar*bvar;                @-- residuals                     --@
        b0[eqn,4 5]    = -bvar[1:2]';           @-- place contemp. ests. into B0  --@
        b1[eqn,4 5 6] = bvar[3:5]';           @-- place AR(1) ests. into B1     --@
        b2[eqn,4 5 6] = bvar[6:8]';           @-- place AR(2) ests. into B2     --@
        c[eqn, 1 4 5]    = bvar[9:11]';          @-- place ecm ests. into C        --@                
        bvar6=bvar;
        reg6=xvar;

    @--eqn7 erate--@
        eqn  = eqn+1;                                          @-- equation number               --@
        yvar = trimr(y[.,9],2,0);                             @-- dependent variable            --@
          xvar0  = trimr(y[.,1 2 3 5 6 7],2,0);    @-- contemp. explanatory variables--@   
          xvar1  = trimr(y[.,1 2 3 5 6 7 9],1,1); @-- AR(1) explanatory variables   --@
          xvar2  = trimr(y[.,1 2 3 5 6 7 9],0,2); @-- AR(2) explanatory variables   --@
          xvarecm= trimr(ecm1~ecminf_us~ecmr_us~ecminf_eu~ecmr_eu,2,0);                       @-- ecm explanatory variables     --@    
        xvar = xvar0~xvar1~xvar2~xvarecm;                   @-- all explanatory variables     --@ 
        bvar = yvar/xvar;                                      @-- OLS estimation                --@               
        e7 = yvar - xvar*bvar;                                @-- residuals                     --@
        b0[eqn,1:6] = -bvar[1:6]';                       @-- place contemp. ests. into B0  --@
        b1[eqn,1:7] = bvar[7:13]';                      @-- place AR(1) ests. into B1     --@
        b2[eqn,1:7] = bvar[14:20]';                      @-- place AR(2) ests. into B2     --@
        c[eqn,1 2 3 4 5]        = bvar[21:25]';                      @-- place ecm ests. into C        --@                
        bvar7=bvar;
        reg7=xvar;




    @--- group together residuals ---@
     e = e1~e2~e3~e4~e5~e6~e7;       


@------------------------------------------------------------------------------
                6. Implementation
------------------------------------------------------------------------------@

    @-- Determine the gamma matrix for weights attached to permanent components
        where gamma=I-B1-B2 --@

      gam=eye(n)-B1-B2;


      J  = 0;   @-- initialize J for use in the calculation of impulses in the sign restrictions procedure  --@
      h0 = 0;   @-- initialize h0 for use in the calculation of impulses in the sign restrictions procedure --@





    @-- set up structural ecm's. First two are for the I(1) variables. 
        Then rest are the cofficients on the "own" lag variable of the I(0) variables --@ 

        alpha=inv(b0)*c;

        alphaperp = null(alpha');

        betaperp  = null(beta');

    @-- Now compute the weights on the permenant shocks This is J in the accompanying document in equation 20
        i.e. delta yperm(t)=J*perm shock --@

    @-- calculation shows that the permanent shocks are indeed those for the structural equations for ystar,exports and
        gne i.e. matrix only has ones in those relevant elements --@

    @-- J controls the long run responses.  --@

        J = betaperp*(inv(alphaperp'*gam*betaperp))*alphaperp'*inv(b0);    @--- construct the J matrix  ---@ 
  
        J[1,2:7]=zeros(1,6);                @--- reset these elements to zero to make sure there is no leakage ---@
        J[4, 2 3 5 6 7]=zeros(1,5);
        J[7,2 3 5 6 7]=zeros(1,5);

    @---- Construct the impulse response functions using the method described in Dungey and Pagan (2007) ---@
    @-- impulses are in the order of 
        shock variable 1, response of all other variables in order.         
        shock variable 2, response of all other variables in order etc. ---@

        g_1 = c*beta'+b1+b0;  
        g_2 = b2-b1;       
        g_3 = -b2;
        h0  = eye(n)-b0*j;

        h1  = b1*j;
        h2  = b2*j;

        cc0 = inv(b0)*h0;
        cc1 = inv(b0)*(g_1*cc0+h1);
        cc2 = inv(b0)*(g_1*cc1+g_2*cc0+h2);
        impulse = vec(cc0+J)~vec(cc1+J)~vec(cc2+J);       

        for i(3,q,1);
            imp = inv(b0)*(g_1*cc2+g_2*cc1+g_3*cc0);
                impulse=impulse~vec(imp+J);              
            cc0=cc1; cc1=cc2; cc2=imp;  
        endfor;

        impulse=impulse';

      imp_optimal = impulse;



@--------------------------------------------------------------------------------
                BOOTSTRAP THE ERROR BANDS
--------------------------------------------------------------------------------@

@--------------------------------------------------------------------------------
                BOOTSTRAP THE ERROR BANDS
--------------------------------------------------------------------------------@
/*
imps_array=impulse;                 @-- set up to receive the boostraps --@
*/

boots=1000;       /* number of bootstraps*/

b0b=eye(n);
b1b=zeros(rows(b1),cols(b1));
b2b=zeros(rows(b2),cols(b2));
cb=zeros(rows(c),cols(c));

bvarb_collect=zeros(rows(bvar1)+rows(bvar2)+rows(bvar3)+rows(bvar4)+rows(bvar5)+rows(bvar6)+rows(bvar7),1);

for bloop(1,boots,1);
estar=trunc(rows(e)*rndu(rows(e),cols(e)))+1;

etild=e[estar[.,1],1]~e[estar[.,2],2]~e[estar[.,3],3]~e[estar[.,4],4]~e[estar[.,5],5]~e[estar[.,6],6]~e[estar[.,7],7];


y1tild = reg1*bvar1 + etild[.,1];
y2tild = reg2*bvar2 + etild[.,2];
y3tild = reg3*bvar3 + etild[.,3];
y4tild = reg4*bvar4 + etild[.,4];
y5tild = reg5*bvar5 + etild[.,5];
y6tild = reg6*bvar6 + etild[.,6];
y7tild = reg7*bvar7 + etild[.,7]; 

ytild=y1tild~y2tild~y3tild~y4tild~y5tild~y6tild~y7tild;

@- ystar - @
dep1=ytild[.,1];
bvarb=inv(reg1'reg1)*reg1'dep1;
        b1b[1,1 2 3 4] = bvarb[1:2]'~(-1)*bvarb[2]~bvarb[3];                   @-- place AR(1) ests. into B1    --@
        b2b[1,1 2 3 4] = bvarb[4:5]'~(-1)*bvarb[5]~bvarb[6];                   @-- place AR(2) ests. into B2    --@
        bvarb1 = bvarb;

@- inflation_us -@

dep2=ytild[.,2];
bvarb=inv(reg2'reg2)*reg2'dep2;
        b0b[2,1]       = -bvarb[1]';         @-- place contemp. ests. into B0  --@
        b1b[2,1 2 5 7] = bvarb[2:5]';         @-- place AR(1) ests. into B1     --@
        b2b[2,1 2 5 7] = bvarb[6:9]';        @-- place AR(2) ests. into B2     --@
        cb[2,1 2 4]          = bvarb[10:12]';          @-- place ecm ests. into C        --@                
        bvarb2 = bvarb;

@- short_us -@

dep3=ytild[.,3];
bvarb=inv(reg3'reg3)*reg3'dep3;
        b0b[3,1 2]    = -bvarb[1:2]';           @-- place contemp. ests. into B0  --@
        b1b[3,1 2 3] = bvarb[3:5]';           @-- place AR(1) ests. into B1     --@
        b2b[3,1 2 3] = bvarb[6:8]';           @-- place AR(2) ests. into B2     --@
        cb[3, 1 2 3]    = bvarb[9:11]';          @-- place ecm ests. into C        --@                
         bvarb3 = bvarb;

@- y_eu -@

dep4=ytild[.,4];
bvarb=inv(reg4'reg4)*reg4'dep4;
        b0b[4,1]    = -bvarb[1]';          @-- place contemp. ests. into B0 --@
        b1b[4,1 4:6] = bvarb[2:4]'~(-1)*bvarb[4];         @-- place AR(1) ests. into B1    --@
        b2b[4,1 4:6] = bvarb[5:7]'~(-1)*bvarb[7];        @-- place AR(2) ests. into B2    --@
        bvarb4 = bvarb; 

@- inflation_eu -@

dep5=ytild[.,5];
bvarb=inv(reg5'reg5)*reg5'dep5;
        b0b[5,2 4]       = -bvarb[1:2]';         @-- place contemp. ests. into B0  --@
        b1b[5,2 4 5 7] = bvarb[3:6]';         @-- place AR(1) ests. into B1     --@
        b2b[5,2 4 5 7] = bvarb[7:10]';        @-- place AR(2) ests. into B2     --@
        cb[5,1 2 4]          = bvarb[11:13]';          @-- place ecm ests. into C        --@                
        bvarb5 = bvarb;

@- short_eu -@

dep6=ytild[.,6];
bvarb=inv(reg6'reg6)*reg6'dep6;
        b0b[6,4 5]    = -bvarb[1:2]';           @-- place contemp. ests. into B0  --@
        b1b[6,4 5 6] = bvarb[3:5]';           @-- place AR(1) ests. into B1     --@
        b2b[6,4 5 6] = bvarb[6:8]';           @-- place AR(2) ests. into B2     --@
        cb[6, 1 4 5]    = bvarb[9:11]';          @-- place ecm ests. into C        --@                
        bvarb6 = bvarb;

@- erate -@       

dep7=ytild[.,7];
bvarb=inv(reg7'reg7)*reg7'dep7;
        b0b[eqn,1:6] = -bvarb[1:6]';                       @-- place contemp. ests. into B0  --@
        b1b[eqn,1:7] = bvarb[7:13]';                      @-- place AR(1) ests. into B1     --@
        b2b[eqn,1:7] = bvarb[14:20]';                      @-- place AR(2) ests. into B2     --@
        cb[eqn,1 2 3 4 5]        = bvarb[21:25]';                      @-- place ecm ests. into C        --@                
        bvarb7 = bvarb; 

/**

    gamb=eye(n)-b1b-b2b;

    alphab=inv(b0b)*cb;

    alphaperpb=null(alphab');

        Jb=betaperp*(inv(alphaperpb'*gamb*betaperp))*alphaperpb'*inv(b0b);

        Jb[1,2:7]=zeros(1,6);                @--- reset these elements to zero to make sure there is no leakage ---@
        Jb[4, 2 3 5 6 7]=zeros(1,5);
        Jb[7,2 3 5 6 7]=zeros(1,5);


 @---- Construct the impulse response functions using the method described in Dungey and Pagan (2007) ---@
    @-- impulses are in the order of 
        shock variable 1, response of all other variables in order.         
        shock variable 2, response of all other variables in order etc. ---@

        g_1 = c*beta'+b1b+b0b;  
        g_2 = b2b-b1b;       
        g_3 = -b2b;
        h0  = eye(n)-b0b*jb;

        h1  = b1b*jb;
        h2  = b2b*jb;

        cc0 = inv(b0b)*h0;
        cc1 = inv(b0b)*(g_1*cc0+h1);
        cc2 = inv(b0b)*(g_1*cc1+g_2*cc0+h2);
        impulse = vec(cc0+Jb)~vec(cc1+Jb)~vec(cc2+Jb);       

        for i(3,q,1);
            imp = inv(b0b)*(g_1*cc2+g_2*cc1+g_3*cc0);
                impulse=impulse~vec(imp+J);              
            cc0=cc1; cc1=cc2; cc2=imp;  
        endfor;

        impulseb=impulse';
        
imps_array=aconcat(imps_array,impulseb,3);
**/

bvarb_collect=bvarb_collect~(bvarb1|bvarb2|bvarb3|bvarb4|bvarb5|bvarb6|bvarb7);

endfor;


@ -- bias correction --@
    beta_est = bvar1|bvar2|bvar3|bvar4|bvar5|bvar6|bvar7;
    beta_av_boot=meanc(bvarb_collect');
    beta_tilde = 2*beta_est-beta_av_boot;
    
r1=rows(bvar1); r2=r1+rows(bvar2); r3=r2+rows(bvar3); r4=r3+rows(bvar4); r5=r4+rows(bvar5); r6=r5+rows(bvar6); r7=r6+rows(bvar7);
bvart1=beta_tilde[1:r1];
bvart2=beta_tilde[r1+1:r2];
bvart3=beta_tilde[r2+1:r3];
bvart4=beta_tilde[r3+1:r4];
bvart5=beta_tilde[r4+1:r5];
bvart6=beta_tilde[r5+1:r6];
bvart7=beta_tilde[r6+1:r7];


@--------------------------------------------------------------------------------
                2nd BOOTSTRAP 
--------------------------------------------------------------------------------@


imps_array=impulse;                 @-- set up to receive the boostraps --@

boots=1000;       /* number of bootstraps*/

b0b=eye(n);
b1b=zeros(rows(b1),cols(b1));
b2b=zeros(rows(b2),cols(b2));
cb=zeros(rows(c),cols(c));


for bloop(1,boots,1);
estar=trunc(rows(e)*rndu(rows(e),cols(e)))+1;

etild=e[estar[.,1],1]~e[estar[.,2],2]~e[estar[.,3],3]~e[estar[.,4],4]~e[estar[.,5],5]~e[estar[.,6],6]~e[estar[.,7],7];


y1tild = reg1*bvart1 + etild[.,1];
y2tild = reg2*bvart2 + etild[.,2];
y3tild = reg3*bvart3 + etild[.,3];
y4tild = reg4*bvart4 + etild[.,4];
y5tild = reg5*bvart5 + etild[.,5];
y6tild = reg6*bvart6 + etild[.,6];
y7tild = reg7*bvart7 + etild[.,7]; 

ytild=y1tild~y2tild~y3tild~y4tild~y5tild~y6tild~y7tild;

@- ystar - @
dep1=ytild[.,1];
bvarb=inv(reg1'reg1)*reg1'dep1;
        b1b[1,1 2 3 4] = bvarb[1:2]'~(-1)*bvarb[2]~bvarb[3];                   @-- place AR(1) ests. into B1    --@
        b2b[1,1 2 3 4] = bvarb[4:5]'~(-1)*bvarb[5]~bvarb[6];                   @-- place AR(2) ests. into B2    --@

@- inflation_us -@

dep2=ytild[.,2];
bvarb=inv(reg2'reg2)*reg2'dep2;
        b0b[2,1]       = -bvarb[1]';         @-- place contemp. ests. into B0  --@
        b1b[2,1 2 5 7] = bvarb[2:5]';         @-- place AR(1) ests. into B1     --@
        b2b[2,1 2 5 7] = bvarb[6:9]';        @-- place AR(2) ests. into B2     --@
        cb[2,1 2 4]          = bvarb[10:12]';          @-- place ecm ests. into C        --@                

@- short_us -@

dep3=ytild[.,3];
bvarb=inv(reg3'reg3)*reg3'dep3;
        b0b[3,1 2]    = -bvarb[1:2]';           @-- place contemp. ests. into B0  --@
        b1b[3,1 2 3] = bvarb[3:5]';           @-- place AR(1) ests. into B1     --@
        b2b[3,1 2 3] = bvarb[6:8]';           @-- place AR(2) ests. into B2     --@
        cb[3, 1 2 3]    = bvarb[9:11]';          @-- place ecm ests. into C        --@                

@- y_eu -@

dep4=ytild[.,4];
bvarb=inv(reg4'reg4)*reg4'dep4;
        b0b[4,1]    = -bvarb[1]';          @-- place contemp. ests. into B0 --@
        b1b[4,1 4:6] = bvarb[2:4]'~(-1)*bvarb[4];         @-- place AR(1) ests. into B1    --@
        b2b[4,1 4:6] = bvarb[5:7]'~(-1)*bvarb[7];        @-- place AR(2) ests. into B2    --@
 

@- inflation_eu -@

dep5=ytild[.,5];
bvarb=inv(reg5'reg5)*reg5'dep5;
        b0b[5,2 4]       = -bvarb[1:2]';         @-- place contemp. ests. into B0  --@
        b1b[5,2 4 5 7] = bvarb[3:6]';         @-- place AR(1) ests. into B1     --@
        b2b[5,2 4 5 7] = bvarb[7:10]';        @-- place AR(2) ests. into B2     --@
        cb[5,1 2 4]          = bvarb[11:13]';          @-- place ecm ests. into C        --@                

@- short_eu -@

dep6=ytild[.,6];
bvarb=inv(reg6'reg6)*reg6'dep6;
        b0b[6,4 5]    = -bvarb[1:2]';           @-- place contemp. ests. into B0  --@
        b1b[6,4 5 6] = bvarb[3:5]';           @-- place AR(1) ests. into B1     --@
        b2b[6,4 5 6] = bvarb[6:8]';           @-- place AR(2) ests. into B2     --@
        cb[6, 1 4 5]    = bvarb[9:11]';          @-- place ecm ests. into C        --@                

@- erate -@       

dep7=ytild[.,7];
bvarb=inv(reg7'reg7)*reg7'dep7;
        b0b[eqn,1:6] = -bvarb[1:6]';                       @-- place contemp. ests. into B0  --@
        b1b[eqn,1:7] = bvarb[7:13]';                      @-- place AR(1) ests. into B1     --@
        b2b[eqn,1:7] = bvarb[14:20]';                      @-- place AR(2) ests. into B2     --@
        cb[eqn,1 2 3 4 5]        = bvarb[21:25]';                      @-- place ecm ests. into C        --@                
 

    gamb=eye(n)-b1b-b2b;

    alphab=inv(b0b)*cb;

    alphaperpb=null(alphab');

        Jb=betaperp*(inv(alphaperpb'*gamb*betaperp))*alphaperpb'*inv(b0b);

        Jb[1,2:7]=zeros(1,6);                @--- reset these elements to zero to make sure there is no leakage ---@
        Jb[4, 2 3 5 6 7]=zeros(1,5);
        Jb[7,2 3 5 6 7]=zeros(1,5);


 @---- Construct the impulse response functions using the method described in Dungey and Pagan (2007) ---@
    @-- impulses are in the order of 
        shock variable 1, response of all other variables in order.         
        shock variable 2, response of all other variables in order etc. ---@

        g_1 = c*beta'+b1b+b0b;  
        g_2 = b2b-b1b;       
        g_3 = -b2b;
        h0  = eye(n)-b0b*jb;

        h1  = b1b*jb;
        h2  = b2b*jb;

        cc0 = inv(b0b)*h0;
        cc1 = inv(b0b)*(g_1*cc0+h1);
        cc2 = inv(b0b)*(g_1*cc1+g_2*cc0+h2);
        impulse = vec(cc0+Jb)~vec(cc1+Jb)~vec(cc2+Jb);       

        for i(3,q,1);
            imp = inv(b0b)*(g_1*cc2+g_2*cc1+g_3*cc0);
                impulse=impulse~vec(imp+J);              
            cc0=cc1; cc1=cc2; cc2=imp;  
        endfor;

        impulseb=impulse';
        
imps_array=aconcat(imps_array,impulseb,3);

endfor;

/** make the confidence bands **/

    alf=(1-0.95)/2;
    upp=trunc((1-alf)*boots);
    low=trunc(alf*boots);
    upbnd=zeros(rows(imp_optimal),121);
    lowbnd=zeros(rows(imp_optimal),121);

    for i(1,rows(imp_optimal),1);
    xx=arraytomat(imps_array[1,i,.]);
    for jj(2,boots,1);
        xx=xx|arraytomat(imps_array[jj,i,.]);
        endfor;
     for k(1,n*n,1);
            xx2=xx[.,k]; 
            xx3=sortc(xx2,1);
            upb=xx3[upp,1]; 
            lowb=xx3[low,1]; 
            upbnd[i,k]=xx3[upp,1]; lowbnd[i,k]=xx3[low,1];
      endfor;
 
    endfor;


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





impout=imp_optimal;

upbnd_1=upbnd;
lowbnd_1=lowbnd;
point_1=impout;
save path=^spath upbnd_1, lowbnd_1, point_1;


@--------------------------------------------------------------------@

      @-- scale optimal impulses by standard deviation --@
     
      imp_expt1prefer=imp_optimal;

      scaler=sqrt(diag(vcx(e)));
        orig=impulse';   
        orig[.,1:7]=scaler[1]*orig[.,1:7];   
        orig[.,8:14]=scaler[2]*orig[.,8:14];
        orig[.,15:21]=scaler[3]*orig[.,15:21];
        orig[.,22:28]=scaler[4]*orig[.,22:28];
        orig[.,29:35]=scaler[5]*orig[.,29:35];
        orig[.,36:42]=scaler[6]*orig[.,36:42];
        orig[.,43:49]=scaler[7]*orig[.,43:49];
        
        origup=upbnd_1;
        origup[.,1:7]=scaler[1]*origup[.,1:7];   
        origup[.,8:14]=scaler[2]*origup[.,8:14];
        origup[.,15:21]=scaler[3]*origup[.,15:21];
        origup[.,22:28]=scaler[4]*origup[.,22:28];
        origup[.,29:35]=scaler[5]*origup[.,29:35];
        origup[.,36:42]=scaler[6]*origup[.,36:42];
        origup[.,43:49]=scaler[7]*origup[.,43:49];
        
        origlow=lowbnd_1;
        origlow[.,1:7]=scaler[1]*origlow[.,1:7];   
        origlow[.,8:14]=scaler[2]*origlow[.,8:14];
        origlow[.,15:21]=scaler[3]*origlow[.,15:21];
        origlow[.,22:28]=scaler[4]*origlow[.,22:28];
        origlow[.,29:35]=scaler[5]*origlow[.,29:35];
        origlow[.,36:42]=scaler[6]*origlow[.,36:42];
        origlow[.,43:49]=scaler[7]*origlow[.,43:49];

      imp_scale_expt1prefer=orig; scaler_expt1prefer=scaler; upbnd_scale1prefer=origup; lowbnd_scale1prefer=origlow;
    
save path=^spath imp_scale_expt1prefer, imp_expt1prefer, scaler_expt1prefer, upbnd_scale1prefer, lowbnd_scale1prefer;
    
imp_optimal=imp_scale_expt1prefer;


@------------------------------------------------------------------------------
        8. Generate figures of optimal impulse responses
------------------------------------------------------------------------------@

    xtics(0,q,3,2);
	 _plwidth =5;
     _pltype = 0;

  x=zeros(rows(imp_optimal),1);  
  xy(q+4,x~imp_optimal[.,1]~lowbnd_scale1prefer[.,1]~upbnd_scale1prefer[.,1]);


    x = zeros(rows(imp_optimal),1);
    for m(1,n,1);
        begwind;
        window(4,2,0);
        setwind(1);
            for i(1,n,1);
                title(vord[m]$+" on "$+vord[i]);
                xy(q+4,x~imp_optimal[.,(m-1)*n+i]~lowbnd_scale1prefer[.,(m-1)*n+i]~upbnd_scale1prefer[.,(m-1)*n+i]);
            nextwind;
            endfor;
            wait; 
        endwind;  
    endfor;

     

@--------------------------------------------------------------------------------------
        11. Procedures 
--------------------------------------------------------------------------------------@

    @---------- 11.1  Engle-Granger 2 step procedure ----------@

    proc(2)=eg(yt);
        local dy,y,err,s,se,x,const,trend,c;

    @--------------- ADF test ----------------@
    dy  = trimr(yt,1,0) - trimr(yt,0,1); 
    y = trimr(dy,3,0); 

    x = (trimr(yt,3,1)~trimr(dy,2,1)~trimr(dy,1,2)~trimr(dy,0,3));

    b = y/x;
    err = y - x*b;
    s = (err'err)/(rows(y)-cols(x));
    se = sqrt(diag(s*inv(x'x)));

    t = b./se; 
    print "ADF test on residuals";
    print "";
    print "coefficient and t-stat for unit root paramater" b[1]~t[1];
    print "";
    print "MacKinnon (1996) critical values";
    print "";
    print "10 percent critical value -1.614487";
    print "5  percent critical value -1.944286";
    print "1  percent critical value -2.589795";

    if t[1] .le -1.614487; print "reject null of no unit root at the 10 percent level"; else; endif; 
    if t[1] .le -1.944286; print "reject null of no unit root at the 5 percent level"; else; endif; 
    if t[1] .le -2.589795; print "reject null of no unit root at the 1 percent level"; else; endif; 
    if t[1] .gt -1.614487; print "fail to reject the null of no unit root"; else; endif; 

    print "";

retp(b,t);
endp; 
    


