// A TWO COUNTRY MONETARY UNION MODEL


var R LZ_Z_star LYhf_Z_star LYhh_Z LYfh_Z LYhf LYhh LYfh LY_Z LC_Z LI_Z LZ LY LC LI LI_K LA LL LMUC_Z LMRS LW MC LRK Q G_Y G_K G_C G_Z G_A G_L GUC PIw LPh LPf PI PIh PIf  LY_star E_A E_PI E_R E_K E_D E_P E_W PI_obs Y_obs;//LZ_star
//R: Nominal interest rate; LZ_Z_star: Home demand to foreign demand ratio;  LYhf_Z_star;Export to foreign demand ratio; LYhh_Z;Home production for the dometic economy to home demand ratio; LYfh_Z:Import to home demand ratio; LYhf:Export; LYhh:Home production for the home economy; LYfh:Import; LY_Z:GDP to demand ratio; LC_Z:Consumption to demand ratio; LI_Z:Investment to demand ratio; LZ:real demand; LY:real gdp; LC:real consumption; LI:investment; LI_K:investment to capital ratio; LA:technology; LL:labor; MUC_Z:marginal utility of consumption to demand ratio; LW;real wage; MC:marginal cost; LRK:Real return on capital; Q:Tobin's Q; G_Y:GDP growth; G_K:Capital growth; G_C:consumption growth; G_Z:demand growth; G_A:technology growth; G_Llabor growth; GUCmarginal consumption utility growth; PIw:Nominal wage growth; LPh:home prices;  LPf:foreign prices; PI:home inflation; PIh:local producer inflation; PIf:foreign inflation; LZ_star:foreign demand; LY_star:foreign gdp; E_A E_PI E_R E_K E_D PI_obs Y_obs  

varexo EPS_A EPS_PI EPS_R EPS_K EPS_D EPS_P EPS_W;

parameters betae alphae h sigmae phi eta theta_h delta_h rhoA rhoPI rhoS rhoR rhoK rhoW rhoD rhoP phi1 phi2 phi3 phi4 theta_st delta_st
           gamai gamaw delta viw tetaw gamap Tech_G Tech_G_star gamaw_star Infl alphae_st 
           MC_ss LW_ss LL_ss LMRS_ss I_Y C_Y; 

gamai = 30;                 //Investment adjustment cost parameter
gamaw = 1;                // degree of wage indexation to the CPI
delta = 0.025;              //DEPRECIATION RATE
viw = 0.6;                  //Calvo parameters for wage
tetaw = 6;                  // elasticity of substitution of labor service
gamap = 0.588;               //wage share in output

//Growth rate steady state
Tech_G_star = 0.005;
Tech_G = Tech_G_star;
Infl = 0.005;


alphae_st = 0.6;            //degree of openess of the foreign economy
betae    =   0.99;          // rate of time preferences
alphae   =   0.65;          //degree of openness 
h       =   0.65;           //habit persistence
sigmae   =   2;           // inverse elasticity of substitution 
phi     =   3;           //Inverse labor elasticity    
eta     =   0.8;              //Elasticity of substitution between domestic and foreign goods        

//Philips curve parameters
theta_h =   0.75;       
delta_h =   0.50;      
theta_st=   0.93;      
delta_st=   0.29;      

//Shocks parameters
rhoA    =   0.75;    
rhoPI   =   0.5;    
rhoD    =   0.25;    
rhoS    =   0.5;    
rhoR    =   0.5;    
rhoK    =   0.5;
rhoW    =   0.5;
rhoP    =   0.5;


//Monetary policy parameters
phi1    =   1.70;    
phi2    =   0.32;   
phi3    =   0.2;     
phi4    =   0.13;    

//Steady-state value
I_Y = 0.15;
C_Y = 0.85;
MC_ss = gamap*(1 - 1/(gamap)*(1-(1-gamap)*(log(delta + Tech_G_star)-log(I_Y)-1)) + log(alphae)) + (1-gamap)* (log(1/betae-1 + delta+Tech_G_star)) -(1-gamap)*log(1-gamap) + gamap*log(gamap);
LL_ss = 1/(gamap)*(1-(1-gamap)*(log(delta + Tech_G_star)-log(I_Y)-1));
LW_ss = 1-LL_ss+ log(alphae);
LMRS_ss = sigmae*log(C_Y*(1-h))-sigmae + phi*LL_ss;


// MODEL EQUATIONS
    model;

    #mu_f_d = betae/(1+delta_h*betae);
    #mu_b_d = delta_h/(1+delta_h*betae);
    #lambda_d = (1-theta_h)*(1-theta_h*betae)/(theta_h*(1+delta_h*betae));

   
    //DOMESTIC ECONOMY
//------------------------------------------------------------------------------------------------------------------
    // 1 (A) Demand definition 
    1 = exp(LI_Z) + exp(LC_Z)+1/(2*gamai)*(exp(LI_K(+1))-Tech_G_star-delta)^2*exp(LI_Z-LI_K(-1))/(1+G_Z); 
    
    // 2 (B) Export demand 
    //exp(LYhf_Z_star) = alphae_st*exp(LPh-LPf)^-eta;
    LYhf_Z_star = log(alphae_st) - (LPh-LPf)*eta;

    // 3 (C) Home production demand 
    //exp(LYhh_Z) = (1-alphae)* exp(LPh)^-eta;
    LYhh_Z = log(1-alphae) - (LPh)*eta;
      
    // 4 (D) Import demand 
    //exp(LYfh_Z) = alphae*exp(LPf)^-eta;
    LYfh_Z = log(alphae) -(LPf)*eta;

    // 5 (F)  GDP def, exp approach
    exp(LY_Z) = 1 + exp(LYfh_Z)*exp(LPf) - exp(LYhf_Z_star)*exp(-LZ_Z_star)*exp(LPh);
       
    // 6  (H) Intermediate goods production
    G_Y = (1-gamap)*G_K(-1) + gamap*(G_A+G_L);
    
    // 7 (I) Technology
    G_A = Tech_G + LA - LA(-1) + E_A;

    // 8 (J) Inputs market condition
    LRK - LRK(-1) = G_L + PIw-PI - G_K(-1);
    
     // 9 (K) Log real Marginal cost 
    MC = gamap*LW + (1-gamap)* LRK -(1-gamap)*log(1-gamap) + gamap*log(gamap) + E_PI;

    // 10 (L) Philips curve for home prices
    (PIh) =Infl+ mu_f_d*(PIh(+1)-Infl) + mu_b_d*(PIh(-1)-Infl) + lambda_d*(MC-MC_ss);

    // 11 (N) Wages curve
    (PIw-Infl-Tech_G_star) = betae*(PIw(+1)-Infl-Tech_G_star) + gamaw*((PI(-1)-Infl)-betae*(PI-Infl)) + (1-viw)*(1-betae*viw)* (LMRS-(-phi*LL_ss+sigmae-sigmae*log(C_Y*(1-h))) +LW_ss-LW + E_W) /(viw*(1+phi*tetaw));

    // 12 (O) Capital accumulation equation 
    G_K - Tech_G_star= exp(LI_K) - delta-Tech_G_star ;
 
    // 13 (Definition) Marginal utility of consumption + Add preference shock ?
     1/betae-1= GUC(+1) + (R - (PI(+1)-Infl));

    // 14 (R) Tobin's Q equation
    //Q = 1/(((1+R-PI(+1)+Infl)))*(exp(LRK(+1)+(1-delta-Tech_G_star)*Q(+1)); //-0.5/gamai*((exp(LI_K(+1)))^2-(Tech_G+delta)^2)
    Q = 1/(((1+R-PI(+1)+E_K+Infl)))*(exp(LRK(+1) -1/(2*gamai)*((exp(LI_K(+1)))^2-(Tech_G_star+delta)^2))+(1-delta-Tech_G_star)*Q(+1));
     
    //15 Log mqrginql utility of consumtion
    LMUC_Z = -sigmae*log(exp(E_P)*exp(LC_Z)*(1-h/(1+G_C-G_Z)));
    //Q = 1 - 1/gamai*(exp(LI_K)-delta-Tech_G_star);

    // 16 Marginal rate of substitution LMUC - LMUL
    //LMRS =(LMUC_Z+sigmae*log(C_Y*(1-h))) + sigmae*(LZ-1)  - phi*(LL-LL_ss);
    LMRS =(LMUC_Z) + sigmae*(LZ)  - phi*(LL);


//DOMESTIC VARIABLE DEFINITION 
//--------------------------------------------------------------------------------------------------------------------
    //17
    LYhh_Z = LYhh - LZ;
    //18
    LYfh_Z = LYfh - LZ;
    //19
    LYhf_Z_star = LYhf - LY_star; //LZ_star
    //20
    LZ_Z_star = LZ - LY_star; //LZ_star
    //21
    LY_Z = LY - LZ;
    //22
    LI_Z = LI - LZ;
    //23
    LC_Z = LC - LZ;
    //24
    G_L = LL - LL(-1);
    //25
    G_C = Tech_G_star + LC - LC(-1);
    //26
    G_Z = Tech_G_star + LZ - LZ(-1);
    //27
    G_Y   = Tech_G_star + LY - LY(-1); 
    //28
    G_A   = Tech_G_star + LA- LA(-1)+ E_A;
   //29 Marginal utility of consumtion grozth
    GUC = LMUC_Z - LMUC_Z(-1)  + sigmae*(G_Z-Tech_G_star);  
    //30
    LI_K - LI_K(-1) = LI-LI(-1) - (G_K(-1)-Tech_G_star);

    // 31 Ph definition ( )
    PIh - PI =  LPh - LPh(-1);

    // 32 Wage definition
    PIw  =  PI+ Tech_G_star + LW - LW(-1);

    // 33 CPI
    PIh - PI= -(1-alphae)/alphae * (PIf- PI);
        
    // 34 Foreign price definition
    PIf - PI = LPf - LPf(-1);    

    // DOMESTIC SHOCKS
//---------------------------------------------------------------------------------------------------------------------
    // 35 
    E_K = rhoK*E_K(-1) + EPS_K;

    // 36 Technology shock
    E_A = rhoA*E_A(-1) + EPS_A;

    // 37 Cost-push shock
    E_PI  = rhoPI*E_PI(-1) + EPS_PI;

    //38 Monetary policy shock
    E_R = rhoR*E_R(-1) +EPS_R;

  //39 preference shock policy shock
    E_P = rhoP*E_P(-1) +EPS_P;

    //40  wage shock
    E_W = rhoW*E_W(-1) +EPS_W;

//COMMON BLOCK    
//---------------------------------------------------------------------------------------------------------------------
    //41 (S) Risk sharing condition (In deviation from steady state)
    LMUC_Z+sigmae*log(C_Y*(1-h)) = (LY_star-(1+log(alphae/alphae_st))) - h*(LY_star(-1)-(1+log(alphae/alphae_st))) + (1-h)/sigmae*LPf; 

    // MONETARY POLICY
    // 42
    R = rhoR*R(-1) +(1-rhoR)*(1/betae-1) + (1-rhoR)*(phi1*(PIf-Infl) + phi2*(LY_star-LY_star(-1))) + phi3*(PIf-PIf(-1)) + phi4*(LY_star-LY_star(-1)) + E_R;
 
    //FOREIGN BLOCK
//---------------------------------------------------------------------------------------------------------------------
    #lambda_st= ((1-theta_st)*(1-theta_st*betae))/(theta_st*(1-delta*theta_st));

    //43 
    LY_star = (1/(1+h))*LY_star(+1) + (h/(1+h))*LY_star(-1) - ((1-h)/(sigmae*(1+h)))*(R-(1/betae-1) - (PIf(+1)-Infl)) + E_D;

    //44 Philips curve
    PIf =Infl+ (delta/(1+delta*betae))*(PIf(-1)-Infl) + (betae/(1+delta*betae))*(PIf(+1)-Infl) + lambda_st*(LY_star-(1+log(alphae/alphae_st)));
    
    //LZ_star = LY_star;

//45
    E_D = rhoD*E_D(-1)+ EPS_D;

//END ----------------------------------------------------------------------------------------------------------------

//OBSERVATION EQUATIONS

    PI_obs  =   PI ;
    Y_obs-Y_obs(-1) = LY-LY(-1);
   

end;

// CALCULATION OF THE STEADY STATE

//resid(1);
steady;
check;

// SHOCK STRUCTURE
shocks;
var EPS_A;   
stderr 0.015;
var EPS_PI;    
stderr 0.005;
var EPS_R;    
stderr 0.0035;
var EPS_K;    
stderr 0.005;//.1063;
var EPS_P;    
stderr 0.08;
var EPS_D;    
stderr 0.04;
var EPS_W;    
stderr 0.3;
end;

varobs Y_obs PI_obs ; 

observation_trends;
Y_obs (Tech_G_star);
end;

// STOCHASTIC SIMULATION

Stoch_simul(periods=1000,order = 2) PI Q G_A G_Y G_Z;  
//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.1;
//phi4, normal_pdf, 0.0625, 0.05;

//rhoA, BETA_PDF, 0.85, 0.075,0,0.99;  
//rhoPI, BETA_PDF, 0.85, 0.075,0,0.99;
//rhoD, BETA_PDF, 0.85, 0.075,0,0.99;
rhoS, BETA_PDF, 0.85, 0.075,0,0.99;
//rhoR , BETA_PDF, 0.85, 0.075,0,0.99;
rhoK, BETA_PDF, 0.85, 0.075,0,0.99;
//rhoW, BETA_PDF, 0.85, 0.075,0,0.99;
//rhoP, BETA_PDF, 0.85, 0.075,0,0.99;

stderr EPS_A, GAMMA_PDF, 0.01, 0.006, 0, inf;
stderr EPS_PI, GAMMA_PDF, 0.01, 0.0006, 0, inf;
stderr EPS_R, GAMMA_PDF, 0.01, 0.006, 0, inf;
stderr EPS_K, GAMMA_PDF, 0.001, 0.00006, 0, inf;
stderr EPS_P, GAMMA_PDF, 0.01, 0.006, 0, inf;
stderr EPS_W, GAMMA_PDF, 0.01, 0.006, 0, inf;
end;

//estimation(datafile=data, mode_compute=5,mh_replic=20000, mh_nblocks=2, mh_drop=0.30, mh_jscale=0.60, filtered_vars, bayesian_irf) PI;
//shock_decomposition PI R G_Y G_Z Q; 