//***************************************************************************************************************************************************************************************************     
//     A TWO COUNTRY MONETARY UNION MODEL 
//***************************************************************************************************************************************************************************************************
//
//
// Observational variables
//
// gy       : domestic gdp
// gyea     : union gdp
// gcpi     : domestic inflation
// gcpiea   : union inflation
// gc       : consumption growth
// gi       : investment growth
// gl       : employment growth
// gw       : wage inflation
// 
// Model variables
//
// LY_A     : log gdp over technology
// LK_A     : log capital over technology
// TB_Z     : log trade balance over domestic absorption 
// LI       : log investment
// GUC      : consumtpion utility growth
// LC       : log consumtion
// Q        : Tobin's Q
// FApf     : foreign recursive price variable A
// FBpf     : foreign recursive price variable B
// MC_star  : union  marginal cost
// LPfcop   : optimal foreign real price  
// RK       : return on capital
// LY       : log gdp
// FAph     : domestic recursive price variable A
// FBph     : domestic recursive price variable BA
// LPhcop   : optimal foreign real price 
// LA       : technological level
// PIwop    : inflation of optimal wage
// Ld       : labor demand
// LWop     : optimal wage
// FAw      : domestic recursive wage variable A
// FBw      : domestic recursive wage variable B
// Sw       
// lamda    : lagrange multiplier
// PI       : doemstic inflation
// PIh      : domestic producer inflation
// PIf      : foreign producer inflation and foreign inflation
// LPhc     : domestic real price
// LPfc     : foreign real price
// gyea : foreign output growth
// G_C      : consumption growth
// G_I      : investment growth
// G_Z      : absorption growth
// G_K      : capital growth
// G_L      : employment growth
// G_A      : technological growth
// G_Y      : output growth
// PIw      : wage inflation
// LY_Z     : log gdp to absorption ratio
// LC_Z     : log consumption to absorption ratio
// LZ_Z_star: log domestic over foreign absorption
// LI_K     : log investment to capital ratio
// LI_Z     : log investment to absorption ratio
// LZ       : log absorption
// LK       : log capital
// LY_star  : log foreign gdp
// LW       : log real wage
// LL       : log employment
// MC       : log marginal cost
// MUL      ; log marginal utility of labor
// MUC_Z    ; log MUc over absorptio n
// R        : interest rate
//
//***************************************************************************************************************************************************************************************************     
//      VARIABLES DEFINITION
//***************************************************************************************************************************************************************************************************


var G_Y_star LY_A LK_A TB_Z LI GUC LC Q FApf FBpf MC_star LPfcop RK LY  FAph FBph LPhcop  LA PIwop Ld LWop FAw FBw Sw lamda lamda_star PI PIh PIf LPhc LPfc  G_C G_I G_Z G_K G_L G_A G_Y PIw LY_Z LC_Z LZ_Z_star LI_K LI_Z LZ LK LY_star  LW LL  MC MUL R E_K E_PI E_P E_M  E_L E_PIF  E_Y_star E_TB_Z LD_Z G_D;
// r gy gyea gcpi gcpiea gc gi gl gw 
varexo EPS_A EPS_PI EPS_R EPS_K  EPS_P EPS_MC_star  EPS_PIF EPS_TB_Z EPS_Y_star EPS_L; //

parameters betae alphae h sigmae phi eta  
           rhoA rhoL  rhoPIF rhoPI  rhoR rhoM rhoK rhoW  rhoP rhoY_star rhoTB_Z rhoMC_star
            phi1 phi2 phi3 phi4 
           gamai deltae gamap G G_star Infl alphae_star xi viw etaw viph etaph etapf vipf
           MC_ss LW_ss LL_ss MUC_Z_ss R_ss RK_ss I_Y C_Y LY_star_ss LWop_ss lamda_ss MUL_ss LPhcop_ss LPhc_ss LPfc_ss Ld_ss Sw_ss LC_Z_ss LZ_Z_star_ss MC_star_ss LY_ss LPfcop_ss;
            
gamai   =   2;                  //InveStment adjuStment coSt parameter
deltae  =   0.025 ;             //DEprECIATION RATE
viw     =   0.6 ;               //Calvo parameters for wage
etaw    =   3.5 ;               // elaSticity of subStitution of labor service / elasticity of substitution between labor varieties
gamap   =   0.55 ;              //wage share in output
viph    =   0.5 ;               // fraction of firms with non-reoptimizing price
etaph   =   2 ;                 //price elasticity of demand for a specific varieties of good
vipf    =   0.7 ;
etapf   =   2.5 ;

alphae_star = 0.005;             // Home bias of the foreign economy
alphae  =   0.85;                // Home bias 
betae   =   0.9925;              //9926 to match observations Rate of time preferences
h       =   0.51;                // Habit persiStence
sigmae  =   2.5;                 // coefficient of relative risk aversion 
phi     =   2;                   // Inverse labor elaSticity    
eta     =  1.5;                  // ElaSticity of subStitution between domeStic and foreign goods        

//Growth rate Steady Swate
G_star = 0.0025;                 // Union steady_state growth
G  =   0.0025;                   // Home country steady state growth   
Infl    =   0.004;               // Steadystate inflation

//AR parameters
rhoA    =   0.9;    
rhoPI   =   0.9;    
rhoPIF  =   0.7659;    
rhoL    =   0.3371;
rhoR    =   0.9043;    
rhoM    =   0;    
rhoK    =   0.99;
rhoW    =   0.85;
rhoP    =   0.99;
rhoY_star    =   0.8963;
rhoTB_Z = 0;
rhoMC_star = 0.9;

//Monetary policy parameters
phi1    =   0;//1.7906 ;
phi2    =   0;//.3259;   
phi3    =   0;//.1059;     
phi4    =   0;//0.0471;    


etaws = 2:0.5:5;
viws = 0.6:0.05:0.8;
etaphs = 2:0.5:5;
viphs = 0.5:0.05:0.8;
etapfs = 2:0.5:5;
vipfs = 0.6:0.05:0.8;
etas = 1.5:0.5:5;
hs=0.5:0.075:0.8;
//Results=[];
//for i=1:length(etaws)
//    etaw = etaws(i);
//    for j=1:length(viws)
//        viw = viws(j);
//          for k=1:size(etaphs) 
//            etaph=etaphs(k);
//            for l=1:size(viphs)
//              viph=viphs(l);
//              for m=1:size(etapfs)
//                  etapf=etapfs(m);
//                  for n=1:size(vipfs)
//                      viph=viphs(n);
//                      for o=1:size(etas)
//                            eta=etas(o);
//                            for p=1:size(hs)
//                                h=hs(p);

//Steady-State value
R_ss        = 1/betae-1+Infl;                                                           //(1-gamap)*(deltae+G)*(1/I_Y)+(1-deltae);
RK_ss       = 1/betae-1;
LPhcop_ss   = log( ( (1-viph*(1+Infl)^(etaph-1)) / (1-viph))^(1/(1-etaph)) );
LPhc_ss     =   0 ;                                                                 
LPfc_ss     =   0 ; 
LY_ss       =   1 ;                                                               
MC_ss       = (etaph-1)/etaph * exp(LPhcop_ss)*(1-viph*betae*(1+G)^(1-sigmae) * (1+Infl)^etaph) / (1-viph*betae * (1+G)^(1-sigmae) * (1+Infl)^(etaph-1));
I_Y         = ((1-gamap) * MC_ss * (deltae+G)) / ((1/betae - 1+deltae+G)) ;             // Investment share
C_Y         = 1 - I_Y ;                                                                 // Consumption share
LC_Z_ss     = log(C_Y)  ;
LZ_Z_star_ss= log(alphae_star/alphae);
Sw_ss       = 1 ;
LK_ss       = LY_ss + log(I_Y) - log(deltae+G);
Ld_ss       = LY_ss/gamap - (1-gamap)/gamap*LK_ss; //1/gamap - (1-gamap)/gamap*( log(1-gamap)+1-log(1/betae-(1-deltae-G)) ) ;
LL_ss       = log(Sw_ss) + Ld_ss ;
LW_ss       = log(gamap)+log(MC_ss) + LY_ss - Ld_ss ;
MUC_Z_ss    = -sigmae * ( LC_Z_ss+log(1-h) ) ;
lamda_ss    = exp( LC_Z_ss+LY_ss+log(1-h) )^-sigmae ;
LWop_ss     = LW_ss ;
xi          = (1-viw*betae)/(1-viw*betae*(1+G)^-sigmae*(1+Infl))*(etaw-1)/etaw*exp(LWop_ss)*lamda_ss/exp(LL_ss)^phi ;              //Value implied by the model to ensure a steady state
LY_star_ss  = LY_ss + log(alphae/alphae_star) + (LPfc_ss-LPhc_ss)*(1-eta) ;
MUL_ss      = -xi*exp(LL_ss)^phi ;
LPfcop_ss   = log( ( ( 1 - vipf*(1+Infl)^(etapf-1) ) / (1-vipf) )^(1/(1-etapf)) ) ;
MC_star_ss  = (etapf-1)/etapf * exp(LPfcop_ss) * ( 1 - vipf*betae* (1+G_star)^(1-sigmae) * (1+Infl)^etapf ) / ( 1 - vipf*betae* (1+G_star)^(1-sigmae) * (1+Infl)^(etapf-1) );

    model;
//***************************************************************************************************************************************************************************************************     
//      UTILITY BLOCK                                                       (Equation 1 to 5)
//***************************************************************************************************************************************************************************************************
    // Marginal utility of labor
    MUL = -xi*(exp(LL))^phi * exp(E_L);

    //Marginal utility of consumption
    lamda = (exp(LC)*(1-h/(1+G_C-G)))^-sigmae * exp(E_P);

    // Marginal utility growth, not usaed in the model
    GUC = -sigmae*G_C + log(lamda) - log(lamda(-1));

    // Union Marginal utility of consumption
    lamda_star= log(((C_Y  * (exp(LY_star)-h*exp(LY_star(-1)))))^-sigmae);//*exp(E_P)
 
//***************************************************************************************************************************************************************************************************     
//     prODUCTION BLOCK                                                     (Equation 6 to 7)
//***************************************************************************************************************************************************************************************************
    //Home Aggregate price index
    1 = ((1-alphae)*(exp(LPhc))^(1-eta) + alphae*(exp(LPfc))^(1-eta))^(1/(1-eta)) ;
    
    // Production per unit of technology
    LY_A = (LK_A)*(1-gamap) + (Ld)*gamap;

//***************************************************************************************************************************************************************************************************     
//      CONSUMPTION BLOCK                                                   (Equation 8 to 10)
//***************************************************************************************************************************************************************************************************
    //Euler eq 
    (lamda)  =(lamda(+1))/((betae)*(1+R-PI(+1)));
    
    // Perfect risk sharing equation combined with Euler consumption of the foreign economy, domestic househoLds are taking into expectations how the foreign economy is evolving to adjust their consumption
    //(lamda) = (alphae_star/alphae)^-sigmae*exp(lamda_star(+1))/(betae*(1+R-PIf(+1)))*exp(LPfc);
    // Euler equation for the foreign economy
    //exp(lamda_star)=  exp(lamda_star(+1))/(betae*(1+R-PIf(+1)));

//***************************************************************************************************************************************************************************************************     
//     INVESTMENT BLOCK                                                     (Equation 11 to 13)
//***************************************************************************************************************************************************************************************************    
    // Tobin's Q equation  
    Q = 1/((1+R-PI(+1)))*( 1+RK(+1)-(1-deltae-G)+(1-deltae-G)*Q(+1)); 
    1 = Q*exp(E_K)*(1-gamai/2*(G_I - G)^2-gamai*(G_I - G)*G_I) + betae*Q(+1)*exp(E_K(+1))*gamai*(G_I(+1) - G)*G_I(+1)^2;
    G_K - G = exp(LI_K)*(1-gamai/2*(G_I - G)^2)*exp(E_K) - deltae-G;//

//***************************************************************************************************************************************************************************************************     
//      DOMESTIC CALVO prICE BLOCK                                          (Equation 14 to 17)
//***************************************************************************************************************************************************************************************************
    FAph = ((1-alphae)*exp(LZ)+alphae_star*exp(LY_star)*exp(LPhc-LPfc)^-eta)* MC * exp(LPhcop)^(-etaph-1) + viph*betae*lamda(+1)*(1+G)^-sigmae/(lamda)*exp(LPhcop-LPhcop(+1))^(-etaph-1)*(1/(1+PI(+1)))^-etaph*(1+G)*FAph(+1) ;
    FBph = ((1-alphae)*exp(LZ)+alphae_star*exp(LY_star)*exp(LPhc-LPfc)^-eta) * exp(LPhcop)^(-etaph) + viph*betae*lamda(+1)*(1+G)^-sigmae/(lamda)*exp(LPhcop-LPhcop(+1))^(-etaph)*(1/(1+PI(+1)))^(1-etaph)*(1+G)*FBph(+1) ;
    etaph * FAph= (etaph-1)* FBph *exp(E_PI);
    1 = viph*(1+PIh)^(etaph-1) + (1-viph)*(exp(LPhcop)*exp(E_TB_Z))^(1-etaph) ;

//***************************************************************************************************************************************************************************************************
//      UNION CALVO prICE BLOCK                                             (Equation 18 to 21)
//***************************************************************************************************************************************************************************************************
    FApf = exp(LY_star)*MC_star*exp(LPfcop)^(-etapf-1) + vipf*betae*lamda_star(+1)/(lamda_star)*exp(LPfcop-LPfcop(+1))^(-etapf-1)*(1/(1+PIf(+1)))^-etapf*(1+G_star)^(1-sigmae)*FApf(+1);
    FBpf = exp(LY_star)*exp(LPfcop)^(-etapf) + vipf*betae*lamda_star(+1)/(lamda_star)*exp(LPfcop-LPfcop(+1))^(-etapf)*(1/(1+PIf(+1)))^(1-etapf)*(1+G_star)^(1-sigmae)*FBpf(+1) ;
    //FApf = exp(LY_star)*MC_star*exp(LPfcop)^(-etapf-1) + vipf*betae*(betae*(1+R-PIf(+1)))*exp(LPfcop-LPfcop(+1))^(-etapf-1)*(1/(1+PIf(+1)))^-etapf*(1+G_star)^(1-sigmae)*FApf(+1) ;
    //FBpf = exp(LY_star)*exp(LPfcop)^(-etapf) + vipf*betae*(betae*(1+R-PIf(+1)))*exp(LPfcop-LPfcop(+1))^(-etapf)*(1/(1+PIf(+1)))^(1-etapf)*(1+G_star)^(1-sigmae)*FBpf(+1) ;
    etapf * FApf = (etapf-1) * FBpf*exp(E_PIF);
    1 = vipf*(1+PIf)^(etapf-1) + (1-vipf)*(exp(LPfcop))^(1-etapf) ;
    
//***************************************************************************************************************************************************************************************************
//      MC BLOCK                                                            (Equation 20 to 22)
//***************************************************************************************************************************************************************************************************
    MC * (1-gamap)     * exp(LK(-1))^ -gamap   * exp(Ld+LA)^  gamap   = 1+RK - (1-deltae-G) ;
    MC * gamap*exp(LA) * exp(LK(-1))^(1-gamap) * exp(Ld+LA)^(gamap-1) = exp(LW) ;
    MC_star = (1-rhoMC_star)*MC_star_ss + rhoMC_star*MC_star(-1) +EPS_MC_star;//+E_TB_Z;

//***************************************************************************************************************************************************************************************************
//      CALVO WAGE SETTING BLOCK                                            (Equation 23 to 28)
//***************************************************************************************************************************************************************************************************
    FAw = (etaw-1)/etaw * exp(LWop)*(lamda)*exp(LW-LWop)^etaw*exp(Ld) + viw*betae*((1+PI(+1))/((1+G)*(1+PI)))^(etaw-1)*(1+PIwop)^(etaw-1)*FAw(+1)*((1+Infl)*(1+G)^-sigmae);
    FBw = - MUL*exp(LWop-LW)^-etaw*exp(Ld) + viw*betae*((PIwop(+1)+1)*(1+PI(+1))/((1+G)*(1+PI)))^etaw*FBw(+1) ;
    FAw = FBw ;
    exp(LL) = Sw*exp(Ld) ;
    Sw = (1-viw)*(exp(LWop-LW))^(-etaw) + viw*((1+PIw-G) * (1+PI(-1))/(1+PI))^(-etaw) * Sw(-1) ;
    1 = (1-viw)*exp(LWop-LW)^(1-etaw) + viw*( (1+G)/(1+PIw) * (1+PI(-1))/(1+PI) ) ^(1-etaw) ;

//***************************************************************************************************************************************************************************************************     
//      RESSOURCE CONSTRAINT BLOCK                                           (Equation 29 to 31)
//***************************************************************************************************************************************************************************************************
    //gdp/absorption = (home production + export)
    exp(LY_Z) = ( (1-alphae)*exp(LPhc)^(1-eta) + alphae_star/exp(LZ_Z_star)*exp((LPhc-LPfc))^-eta);
    
    //gdp/absorption= 1 + TradeBalance/absorption
    exp(LY_Z)=TB_Z + 1  ;
    // 1 = invest/absorption + consump/absorption
    1 = exp(LI_Z)  + exp(LC_Z) ;
    
    //Trqde bqlqnce definition
    TB_Z = -alphae*exp(LPfc)^(1-eta) + alphae_star*exp(LPhc)*exp(LPhc- LPfc)^(-eta)/exp(LZ_Z_star);    
    
    //Current account condition requiered to close the model following Schmitt-Grohe 2001, equation divided by debt/absorption
    TB_Z/exp(LD_Z) =(1+G_D)/(1+R-0.000742*((exp(LD_Z)-0.6))) -1;
    //real debt growth relative to absorption growth
    G_D -G_Z = R_ss - G + LD_Z - LD_Z(-1);
       
//***************************************************************************************************************************************************************************************************
//     DEFINITION BLOCK                                                     (Equation 32 to 51)       
//***************************************************************************************************************************************************************************************************    
       // Euro area gdp
      G_Y_star - G_star= + (LY_star - LY_star(-1));
      LY_star     =  (1-rhoY_star)*LY_star_ss + rhoY_star* LY_star(-1) +(E_Y_star); 

    //Log Technology level
    LA      =   rhoA *LA(-1)+ EPS_A;// LA(-1)   ;//+ pr * E_Y_star
    //G_A      =   G +  (rhoA -1)* LA(-1)+EPS_A;//+ EPS_A;

    //Dom GDP growth
    G_Y     =   G + LY - LY(-1) ;
    G_C     =   G + LC - LC(-1) ;   //Domest consumption     growth
    G_K     =   G + LK - LK(-1) ;   //D     capital         g
    G_I     =   G + LI - LI(-1) ;   //D     investment      g
    G_Z     =   G + LZ - LZ(-1) ;   //D     absorption      g
    G_L     =   Ld - Ld(-1) ;       //D     labor demand    g    
    PIwop   =   G + LWop - LWop(-1) ;//D    optimal real wage inflation
    PIw     =   G + LW - LW(-1) ;   //D     real wage       inflation
    PIh - PI =  LPhc - LPhc(-1) ;   //D     home price      i
    PIf - PI =  LPfc - LPfc(-1) ;   //D     foreign price   i
    LI_K    =   LI - LK(-1) ;       //D     log investment to capital ratio    
    LZ - LY_star = LZ_Z_star ;      //Relative absorption (foreign absorption is assumed to equal foreign GDP)
    LC_Z = LC - LZ ;                //Log   consumption     to absorption rqtio
    LI_Z = LI - LZ ;                //L     investment      t
    LY_Z = LY - LZ ;                //L     gdp             t
    LY_A = LY-LA;                   //L     g               to technology ratio               
    LK_A = LK(-1)-LA;               //L     capital         t
        
//***************************************************************************************************************************************************************************************************
//      SHOCKS                                                                (Equation 52 to 59)
//***************************************************************************************************************************************************************************************************
   
    E_TB_Z  =   rhoTB_Z*E_TB_Z(-1) + EPS_TB_Z;
    E_K     =   rhoK*E_K(-1) + EPS_K;
    E_PI    =   rhoPI*E_PI(-1) + EPS_PI;    
    E_PIF   =   rhoPIF*E_PIF(-1) + EPS_PIF; 
    E_M     =   rhoM*E_M(-1) + EPS_R;
    E_P     =   rhoP*E_P(-1) + EPS_P;
    E_L     =   rhoL*E_L(-1) + EPS_L;
    E_Y_star=   rhoY_star*E_Y_star(-1) + EPS_Y_star;
     
//***************************************************************************************************************************************************************************************************
//      MONETARY POLICY BLOCK                                                (Equation 60)
//***************************************************************************************************************************************************************************************************

    R = rhoR*R(-1) + (1-rhoR)*(R_ss +phi1*(PIf-Infl) + phi2*(LY_star-LY_star_ss))+ phi3*(PIf-PIf(-1))  + phi4*(LY_star-LY_star(-1)) + E_M;

//***************************************************************************************************************************************************************************************************
end;

// CALCULATION OF THE StEADY StTE
steady;
check;

shocks;
var EPS_A;   
stderr 0;
var EPS_PI;    
stderr 0.0011;
var EPS_PIF;    
stderr 0.0151;
var EPS_R;    
stderr 0;
var EPS_K;    
stderr 0;
var EPS_P;    
stderr 0;
var EPS_L;    
stderr 0;
var EPS_MC_star ;
stderr 0;
var EPS_TB_Z;   
stderr 0;
var EPS_Y_star;   
stderr 0.0021;

end;

//OBSERVED VARIABLES
//options_.ftol=1.e-6;

estimated_params;
phi1, normal_pdf, 1.7, 0.1; 
//phi2, normal_pdf, 0.125, 0.1;        
//phi3, normal_pdf, 0.3, 0.06;
//phi4, normal_pdf, 0.0625, 0.05;
stderr EPS_MC_star, GAMMA_PDF, 0.01, 0.006, 0, inf;
stderr EPS_A, GAMMA_PDF, 0.01, 0.006, 0, inf;
stderr EPS_PIF, GAMMA_PDF, 0.01, 0.006, 0, inf;
stderr EPS_R, GAMMA_PDF, 0.01, 0.006, 0, inf;
stderr EPS_K, GAMMA_PDF, 0.01, 0.006, 0, inf;
stderr EPS_P, GAMMA_PDF, 0.01, 0.006, 0, inf;
stderr EPS_L, GAMMA_PDF, 0.01, 0.006, 0, inf;
stderr EPS_PI, GAMMA_PDF, 0.01, 0.006, 0, inf;
stderr EPS_TB_Z, GAMMA_PDF, 0.005, 0.003, 0, inf;
stderr EPS_Y_star, GAMMA_PDF,0.01, 0.006, 0, inf;           //set to the observed relative variance of gdp growth

rhoA, BETA_PDF, 0.85, 0.075,0,0.99;  
rhoR , BETA_PDF, 0.85, 0.075,0,0.99;
//rhoM , BETA_PDF, 0.85, 0.1,0,0.99;
rhoPI, BETA_PDF, 0.85, 0.1,0,0.99;
rhoPIF, BETA_PDF, 0.85, 0.1,0,0.99;
rhoL, BETA_PDF, 0.85, 0.1,0,0.99;
rhoY_star, BETA_PDF, 0.85, 0.1,0,0.99;
rhoK, BETA_PDF, 0.85, 0.1,0,0.99;
rhoP, BETA_PDF, 0.85, 0.1,0,0.99;
rhoTB_Z, BETA_PDF, 0.5, 0.2,0,0.99;
//gamai, UNIFORM_PDF, 5, 4, 0, inf;
end;

varobs G_Y G_Y_star PI PIf G_I G_C PIw R G_L;

                                close all;
                                
                                 estimation(datafile=datae ,
                                 //mode_file = MME7_mode,
                                 mode_compute=5, mode_check,
                                 mh_replic=0//nograph,
                                 );
        
                               // Results=[Results; oo_.MarginalDensity];
                                
//stoch_simul(order=1,nograph); 
//                            end;
//                        end;
//                    end;
//                end;
//            end;
//        end;
//    end;
//end;


//shock_decomposition G_Y G_Y_star PI PIf G_I G_C PIw R G_L;

Stoch_simul(order=1,nograph); 