// Two Country
// BENCHMARK MODEL: OUTPUT GROWTH RULE, DEMEANED DATA

var PI PI_star PI_H PI_Hstar PI_F PI_Fstar 
    LAM LAM_star C C_star calC calC_star 
    R R_star Y Y_star Q Q_star 
    S E_del PSI_F PSI_Hstar 
    A A_star G G_star Z 
    INF_US INF_F R_US R_F Ygr_US Ygr_F ;
 
varexo EPS_R EPS_Rstar EPS_A EPS_Astar EPS_G EPS_Gstar EPS_Z EPS_Edel;
 
parameters theta_H theta_F theta_Hstar theta_Fstar
           tau h alp eta bet
           rhoR psi1 psi2 psi3 
           rhoRstar psi1star psi2star psi3star
           rhoA rhoAstar rhoG rhoGstar rhoZ
           gam_steady rr_steady pi_steady pi_steadystar;

// pi_steady = pi_steadystar
// corr_RRstar = 0

// To be substituted
// bet = 1/(1+rr_steady/400);
// kap_H = (1-theta_H)/theta_H*(1-theta_H*bet);
// kap_F = (1-theta_F)/theta_F*(1-theta_F*bet);
// kap_Hstar = (1-theta_Hstar)/theta_Hstar*(1-theta_Hstar*bet);
// kap_Fstar = (1-theta_Fstar)/theta_Fstar*(1-theta_Fstar*bet);

model(linear); 

        // 1. Domestic firms price setting
        PI_H = 1/(1+rr_steady/400)*PI_H(+1) + (1-theta_H)/theta_H*(1-theta_H/(1+rr_steady/400))*( -LAM - alp*Q - A );
        
        // 2. Domestic habits
        (1-h)*calC = C - h*C(-1) + h*Z;
        
        // 3. Domestic Marginal utility of consumption
        -LAM = tau/(1-h/(1+rr_steady/400))*calC - h/(1+rr_steady/400)/(1-h/(1+rr_steady/400))*(tau*calC(+1)+Z(+1));
             
        // 4. Domestic importers' price setting
        PI_F = 1/(1+rr_steady/400)*PI_F(+1) + (1-theta_F)/theta_F*(1-theta_F/(1+rr_steady/400))*PSI_F;
       
        // 5. Definition of CPI
        PI   = alp*PI_F + (1-alp)*PI_H;
        
        // 6. Terms of Trade Dynamics
        Q    = Q(-1) + PI_H - PI_F;
        
        // 7. Real Exchange Rate Dynamics
        S    = PSI_F - (1-alp)*Q - alp*Q_star;
        
        // 8. Nominal Depreciation Rate
        E_del = PI - PI_star + S - S(-1) + EPS_Edel;
        
        // 9. Home goods market clearing
        Y = C + G -(alp/tau)*S - alp*(1-alp)*eta*(Q-Q_star);
        
        // 10. Risk Sharing Condition
        LAM = LAM_star - S;
        
        // 11. Monetary Policy Rule
        R = rhoR*R(-1) + (1-rhoR)*(psi1*PI +psi2*(Y-Y(-1)+Z) + psi3*E_del) + EPS_R;
        
        // 12. UIP equation
        R - R_star = PI(+1) - PI_star(+1) + S(+1) -S;
        
        // 13. Foreign firms price setting
        PI_Fstar = 1/(1+rr_steady/400)*PI_Fstar(+1) + (1-theta_Fstar)/theta_Fstar*(1-theta_Fstar/(1+rr_steady/400))*( -LAM_star - alp*Q_star - A_star);
        
        // 14. Foreign habits
        (1-h)*calC_star = C_star - h*C_star(-1) + h*Z;

        // 15. Foreign Marginal utility of consumption
        -LAM_star = tau/(1-h/(1+rr_steady/400))*calC_star - h/(1+rr_steady/400)/(1-h/(1+rr_steady/400))*(tau*calC_star(+1)+Z(+1));

        // 16. Foreign importers' price setting
        PI_Hstar = 1/(1+rr_steady/400)*PI_Hstar(+1) + (1-theta_Hstar)/theta_Hstar*(1-theta_Hstar/(1+rr_steady/400))*PSI_Hstar;
        
        // 17. Definition of CPI
        PI_star   = alp*PI_Hstar + (1-alp)*PI_Fstar;
        
        // 18. Terms of Trade Dynamics
        Q_star = Q_star(-1) + PI_Fstar - PI_Hstar;
               
        // 19. Real Exchange Dynamics
        S = -PSI_Hstar + (1-alp)*Q_star + alp*Q;
        
        // 20. Foreign goods market clearing
        Y_star = C_star + G_star -((1-alp)/tau)*S + alp*(1-alp)*eta*(Q-Q_star);
        
        // 21. Foreign Monetary policy rule
        R_star = rhoRstar*R_star(-1) + (1-rhoRstar)*(psi1star*PI_star + psi2star*(Y_star-Y_star(-1)+Z) - psi3star*E_del) + EPS_Rstar;

        // 22. Euler Equation
        -LAM = -LAM(+1) -(R - PI(+1)) + Z(+1);
        
        // Exogenous shocks
        A        = rhoA*A(-1) + EPS_A;
        A_star   = rhoAstar*A_star(-1) + EPS_Astar;
        G        = rhoG*G(-1) + EPS_G;
        G_star   = rhoGstar*G_star(-1) + EPS_Gstar;
        Z        = rhoZ*Z(-1) + EPS_Z;
        
        // Measurement equations
        INF_US = 4*PI;
        R_US   = 4*R;
        Ygr_US = Y - Y(-1) + Z;
        INF_F  = 4*PI_star;
        R_F    = 4*R_star;
        Ygr_F  = Y_star - Y_star(-1) + Z;

end;
initval; 
PI=0;
PI_star=0;
PI_H=0;
PI_Hstar=0;
PI_F=0;
PI_Fstar=0; 
LAM=0;
LAM_star=0;
C=0;
C_star=0;
calC=0;
calC_star=0;
R=0;
R_star=0;
Y=0;
Y_star=0;
Q =0;
Q_star=0;
S=0;
E_del=0;
PSI_F=0;
PSI_Hstar=0;
A=0;
A_star=0;
G=0;
G_star=0;
Z =0;
INF_US=0;
INF_F=0;
R_US=0;
R_F=0;
Ygr_US=0;
Ygr_F=0;
steady;
check;


estimated_params;

    //                 STARTING VALUES &       PRIORS
    theta_H,           0.5,1E-5,0.999,         BETA_PDF,0.5,0.15;
    theta_F,           0.5,1E-5,0.999,         BETA_PDF,0.5,0.15;
    theta_Hstar,       0.5,1E-5,0.999,         BETA_PDF,0.75,0.15;
    theta_Fstar,       0.5,1E-5,0.999,         BETA_PDF,0.75,0.15;

    tau,               2.7,1E-5,10,            GAMMA_PDF,2,0.5;
    h,                 0.3,1E-5,0.999,         BETA_PDF,0.3,0.1;
    alp,               0.1,1E-5,0.999,         BETA_PDF,0.12,0.05;
    eta,               1,1E-5,10,              GAMMA_PDF,1,0.5;

    psi1,              1.5,0.95,10,            GAMMA_PDF,1.5,0.25;
    psi2,              0.5,1E-5,10,            GAMMA_PDF,0.5,0.25;
    psi3,              0.1,1E-5,10,            GAMMA_PDF,0.1,0.05;
    
    psi1star,          1.1,0.95,10,            GAMMA_PDF,1.5,0.25;
    psi2star,          0.5,1E-5,10,            GAMMA_PDF,0.5,0.25;
    psi3star,          0.1,1E-5,10,            GAMMA_PDF,0.1,0.05;

    rhoA,              0.86,1E-5,1,            BETA_PDF,0.8,0.1;
    rhoR,              0.81,1E-5,1,            BETA_PDF,0.5,0.2; 
    rhoG,              0.96,1E-5,1,            BETA_PDF,0.8,0.1;
       
    rhoAstar,          0.97,1E-5,1,            BETA_PDF,0.6,0.2;
    rhoRstar,          0.82,1E-5,1,            BETA_PDF,0.5,0.2; 
    rhoGstar,          0.81,1E-5,1,            BETA_PDF,0.8,0.1;

    rhoZ,              0.54,1E-5,1,            BETA_PDF,0.66,0.15;

    rr_steady,         0.5,0,10,               GAMMA_PDF,0.5,0.5;
    gam_steady,        0.5,-5,5,               NORMAL_PDF,0.4,0.2;
    pi_steady,         3.33,0,10,              GAMMA_PDF,7,2;
//  pi_steadystar,     4.2,0,10,               GAMMA_PDF,8.8,2;

    stderr EPS_A,      2,1E-8,5,               INV_GAMMA_PDF,1.253314,0.655136;  //fs_dyn(1,4)
    stderr EPS_G,      0.6,1E-8,5,             INV_GAMMA_PDF,1.253314,0.655136;  //fs_dyn(1,4)
    stderr EPS_R,      0.15,1E-8,5,            INV_GAMMA_PDF,0.501326,0.262055;  //fs_dyn(0.4,4)

    stderr EPS_Astar,  0.5,1E-8,5,             INV_GAMMA_PDF,0.501326,0.262055;  //fs_dyn(0.4,4)
    stderr EPS_Gstar,  0.7,1E-8,5,             INV_GAMMA_PDF,1.253314,0.655136;  //fs_dyn(1,4)
    stderr EPS_Rstar,  0.1,1E-8,5,             INV_GAMMA_PDF,0.250663,0.131027;  //fs_dyn(0.2,4)

    stderr EPS_Z,      0.34,1E-8,5,            INV_GAMMA_PDF,0.626657,0.327568;  //fs_dyn(0.5,4)
    stderr EPS_Edel,   4,1E-8,10,              INV_GAMMA_PDF,4.386599,2.292977;  //fs_dyn(3.5,4)

end;


varobs Ygr_US INF_US R_US Ygr_F INF_F R_F E_del;

estimation(datafile=us_euro_data_demeaned,mode_compute=4,mh_replic=20000,mh_jscale=0.15,presample=4);

// mode_compute=3
// load_mh_file
// nodiagnostic
// mode_file=MODEL1_mode
