%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// Preamble
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

// Name of the endogenous variables
var p c r pi a s y p_h p_f pi_h pi_f q mc nx g e y_f a_f mc_f r_f ;  

// Name of the exogenous variable
varexo  eps_a eps_a_f eps_g;                                   

// Name of the model parameters  
parameters  sigma phi alpha rho_a beta kappa theta gamma_g omega rho_g eta phi_pi_f phi_pi rho_af sigmae sigmae_g sigmae_f;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// Calibration
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

sigma   =  2;
phi     =  1;
alpha   =  0.4; 
rho_a   =  0.9;
rho_g   =  0.85;
rho_af  =  0.9;
beta    =  0.99;
theta   =  0.75;
eta     =  1.5;
kappa   =  ((1-beta*theta)*(1-theta))/theta;
gamma_g =  0.25;
omega   =  sigma*eta + (1-alpha)*(sigma*eta-1);
phi_pi_f   =  1.5;
phi_pi  = 1.5;
sigmae      = 0.02;
sigmae_g    = 0.02;    
sigmae_f    = 0.02;
                                              
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// Description of the model
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

model(linear);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//Home Economy
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

//Domestic Households (Demand)

c = c(+1) - (1/sigma)*(r - pi(+1));             // Euler Equation (AD)

//Domestic Supply

pi_h = beta*pi_h(+1) + kappa*mc;                                // Philip's Curve (AS)
mc = sigma*c + phi*(y-a) - a + alpha*s;                         // Marginal Costs
y = (1 - gamma_g)*(c + ((alpha*omega)/sigma)*s) + gamma_g*g;    // Market Clearing
nx = y -(1 - gamma_g)*(c + alpha*s) - gamma_g*g;                // Trade Balance
c = y_f + ((1 - alpha)/sigma)*s;                                // International Risk Sharing

// Some Identities

pi = p - p(-1);                                     // Cpi Inflation
pi_h = p_h - p_h(-1);                               // Dpi Inflation
pi = pi_h + alpha*(s - s(-1));                      // Link between CPI and DPI
q - q(-1) = e - e(-1) + pi_f - pi_h;                // Terms of trade
s = p_f - p_h;                                      // Terms of Trade
q = (1-alpha)*s;                                    // Real Exchange Rate

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//Foreign Economy
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

y_f   = y_f(+1) - (1/sigma)*(r_f - pi_f(+1));       // Foreign Euler Equation (AD) 
pi_f  = beta * pi_f(+1) + kappa * mc_f;             // Foreign new keynesian Philips Curve (AS) 
mc_f  = (sigma + phi) * y_f - (1 + phi) * a_f;      // Foreign real marginal cost function 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// Monetary Policy Rules
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

// World Economy

   r_f   = phi_pi_f*pi_f;                           // Foreign's Taylor rule
// pi_f = 0;                                        // No CPI deviation

// Home Economy

// pi    = 0;                                       // Strict inflation targeting (CIT)
// e     = 0;                                       // Exchange rate peg (PEG)
 pi_h  = 0;                                       // Domestic inflation targeting (DIT)
//   r = phi_pi*pi_h;                                 // Domestic Taylor Rule

// Shocks

a    = rho_a * a(-1) + eps_a;                       // Technology innovation
a_f  = rho_af * a_f(-1) + eps_a_f;                  // Foreign technology process
g    = rho_g*g(-1) + eps_g;                         // Government Spending Shock

end;

resid;
steady; 
check;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


shocks;
var eps_a   ; stderr sigmae   ;
var eps_g   ; stderr sigmae_g ;
var eps_a_f ; stderr sigmae_f ;
end;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

stoch_simul(irf=25) y c nx q pi_h pi; 