close all
// *****************************************************************************
//  Graduate Institute for Policy Study (GRIPS)_Japan_2017
//  ----------------------------------------------------------------------------
//  Macroeconomic Policy Game in the Multi-Sector Open Economy with Shifting Trend Inflation
//  Le Thanh Ha
// *****************************************************************************
// all model variables are expressed in deviations from steady state
// NOt use:eps_q
@#define Nash =1
@#define Cooperation =0
% define string for saving different policies
@#if Nash == 1
    case_title='The Nash Equilibrium';    
    short_title='Nash';
@#else
    @#if Cooperation ==1
         case_title='The Cooperating Equilibrium';
        short_title='Cooperation';
@#endif
@#endif

// Declaration of variables
//-------------------------------------
var 
//Output
y y_n y_t y_nf y_ni

//Productivity
a_t a_nf

//Labor supply and wages
n n_i n_f n_nf n_t w w_f w_i

//Consumption
c c_t c_tf c_th c_n c_nf c_ni

// Calvo Price Setting
pstar_th no_th de_th s_th
pstar_nf no_nf de_nf s_nf
//pibar 

//Prices and inflation
p p_t p_tf p_th p_n p_nf p_ni pi pi_d pi_t pi_th pi_tf pi_n pi_nf pi_ni r

//Open Economy
e s q c_star p_star y_star_t
dif1 dif2

//L_nash1 L_nash2
;

varexo 
eps_at eps_r eps_an eps_cstar eps_pstar;


// Declaration of parameters
//-------------------------------------

parameters 
lambda omega h sigma phi nu eta mu alpha beta gamma chi
rho_at rho_an sigma_at sigma_an 

kappa tau
//rho_e sigma_ner

rho_r psi_y psi_pi psi_q sigma_r
rho_cstar rho_pstar rho_rstar sigma_cstar sigma_pstar sigma_rstar 
//Steady state shares
iota
//Calvo Parameters
theta_t theta_n chi_t chi_n miu_t miu_n eps
kappa_t kappa_n
//rho_pibar sigma_pibar

rgm
;
rgm=1.02^0.25;
beta    = 0.973;                       // Rate of time preference (subjective discount factor)
lambda  = 0.3;                         // Share of non-tradables in aggregate consumption/output
omega   = 0.67;                        // Share of informal sector in non-tradables consumption/output 0.25
alpha   = 0.88;                        // Share of imports consumption in the consumption of tradables 
kappa   = 0.18;                        // Fraction of households providing labor to formal firms 0.75
chi     = 0.50;                        // Share of tradable labor in formal labor market
iota    = 0.78;                        // Steady state share of exports in tradable output


rho_cstar  = 0.9063;                    // World consumption persistence
rho_pstar  = 0.6745;                     // World inflation persistence
rho_rstar  = 0.8818;                    // World interest rate persistence

// Setting initial values of estimated parameters using prior mean
//----------------------------------------------------------------

h        = 0.85;                        // Habit persistence parameter
sigma    = 3;                           // Inverse of the elasticity of substitution between consumption and labor
nu       = 0.4;                         // Elasticity of subsitution between tradable and non-tradable goods consumption
eta      = 0.7;                         // Elasticity of substitution between domestic tradable and imported goods
mu       = 0.7;                         // Elasticity of substitution between non-tradable formal and informal goods consumption
gamma    = 1;                           // Elasticity of substitution between goods produced in different foreign countries
phi      = 1.5;                         // Inverse labor elasticity
tau      = 2;                           // Inverse elasticity of substitution between non-tradable formal and informal labor
rho_at   = 0.75;                        // Productivity persistence for domestic tradable firms
rho_an   = 0.75;                        // Productivity persistence for formal non-tradable firms
sigma_at = 0.1;                         // Productivity shock for domestic tradable firms
sigma_an = 0.1;                         // Productivity shock for formal non-tradable firms

// Calvo Parameters
theta_t  = 0.5;                         // Calvo parameter for tradables firms (probability of keeping prices unchanged)
theta_n  = 0.5;                         // Calvo parameter for non-tradables firms (probability of keeping prices unchanged)
chi_t    = 0.0;                         // Price Indexation for tradable firms
miu_t    = 1.0;                         // Weight on lagged inflation for tradable firms
chi_n    = 0.0;                         // Price Indexation for nontradable firms
miu_n    = 1.0;                         // Weight on lagged inflation for nontradable firms
eps      = 10;

// Shock Parameteres
psi_pi     = 1.5;                         // Taylor inflation
psi_y      = 0.125;                       // Taylor output
psi_q      = 1.5;                         // Taylor exchange rate
//r_dq     = 1.5;
//r_dy     = 0.125;                       // Taylor output growth
//r_dpi    = 1.5;                         // Taylor inflation growth
rho_r      = 0.95;                        // Taylor interest rate
sigma_r    = 0.01;                        // Monetary policy shock

//rho_pibar = 0.995;                      // Persistence Inflation AR(1)

sigma_cstar   = 0.01;                     // World consumption shock
sigma_pstar   = 0.01;                     // World inflation shock
sigma_rstar   = 0.01;                     // World interest rate shock
sigma_q       = 0.01;                     // Nominal exchange rate shock
sigma_tot     = 0.01;                     // Term of trade shock
//sigma_pibar   = 0.01;                   // Persistence Inflation shock

kappa_t = (1-beta*theta_t)*(1-theta_t)/theta_t;
kappa_n = (1-beta*theta_n)*(1-theta_n)/theta_n;
// Declaration of the log-linearized model
//-----------------------------------------
model(linear);

y = (1 - lambda)*y_t + lambda*y_n ;                                                 // 1. Aggregate output

y_n = (1 - omega)*y_nf + omega*y_ni ;                                               // 2. Non-tradable output

c = w-p ;                                                                           // 3. Aggregate consumption

c(-1) = c+pi-r(-1) ;                                                                // 4. Euler equation

c_t = c + p - p_t ;                                                                 // 5. Tradables consumption

c_tf = (-p_tf + p_t) + c_t ;                                                        // 6. Foreign tradables consumption

c_th = (-p_th + p_t) + c_t ;                                                        // 7. Domestic tradables consumption

c_n = (-p_n + p) + c ;                                                              // 8. Non-tradables consumption

c_nf = (-p_nf + p_n) + c_n ;                                                        // 9. Formal non-tradables consumption

c_ni = (-p_ni + p_n) + c_n ;                                                        // 10. Informal non-tradables consumption

p = (1 - lambda)*p_t + lambda*p_n ;                                                 // 11. CPI Inflation

pi_d = (1 - lambda)*pi_th + lambda*pi_n ;                                           // 12. Domestic inflation

p_t = (1 - alpha)*p_th + alpha*p_tf ;                                               // 13. Tradables inflation

p_n = (1 - omega)*p_nf + omega*p_ni ;                                               // 14. Non-tradables inflation

p_tf = e  + p_star ;                                                                // 15. Foreign tradables inflation (From law of one price when LOP=1

p = p(-1) + pi ;                                                                    // 16. CPI price level

p_t = p_t(-1) + pi_t ;                                                              // 17. Tradables price level

//p_th = p_th(-1)+pi_th ;                                                             // 18. Domestic tradables price level
p=p_th+alpha*s;
p_tf = p_tf(-1) + pi_tf ;                                                           // 19. Foreign tradables price level

p_n = p_n(-1) + pi_n ;                                                              // 20. Non-tradables price level

p_nf = p_nf(-1) + pi_nf ;                                                           // 21. Formal non-tradables price level

p_ni = p_ni(-1) + pi_ni ;                                                           // 22. Informal non-tradables price level

n = n_f + n_i ;                                                              // 23. Aggregate supply of labor

n_f = (1/tau)*(w_f - w) + n ;                                                       // 24. Supply of formal labor

n_i = (1/tau)*(w_i - w) + n ;                                                       // 25. Supply of informal labor

// Foreign Sector


q = (1-alpha)*s - lambda*(p_t-p_n) ;                                                // 26. The relationship bw Real exchange rate and term of trade

//Production Sector
//Traded Firm

y_t = a_t + n_t -s_th;                                                                   // 27. Domestic tradable output

pstar_th = no_th - de_th;                                                                                                                  //29

no_th = (1-theta_t*beta*rgm^((1-chi_t)*eps))*w_f + (beta*theta_t*rgm^((1-chi_t)*eps))*(eps*pi_th(+1)-eps*chi_t*miu_t*pi_th+no_th(+1));     //30
            
de_th = (beta*theta_t*rgm^((1-chi_t)*(eps-1)))*((eps-1)*pi_th(+1)+chi_t*miu_t*(1-eps)*pi_th+de_th(+1));                                    //31 
 
pstar_th = ((theta_t*rgm^((1-chi_t)*(eps-1)))/(1-theta_t*rgm^((1-chi_t)*(eps-1))))*(pi_th-chi_t*miu_t*pi_th(-1));                          //32

s_th = (-eps*(1-theta_t*rgm^((1-chi_t)*eps)))*pstar_th+(theta_t*rgm^((1-chi_t)*eps))*(eps*pi_th+s_th(-1)-chi_t*miu_t*eps*pi_th(-1));       //33


// Non-traded Formal Firm

y_nf = a_nf + n_nf - s_nf ;                                                                          // 34. Formal non-tradable output

pstar_nf = no_nf - de_nf;                                                                                                                  //36

no_nf = (1-theta_n*beta*rgm^((1-chi_n)*eps))*w_f + (beta*theta_n*rgm^((1-chi_n)*eps))*(eps*pi_nf(+1)-eps*chi_n*miu_n*pi_nf+no_nf(+1));     //37

de_nf = (beta*theta_n*rgm^((1-chi_n)*(eps-1)))*((eps-1)*pi_nf(+1)+chi_n*miu_n*(1-eps)*pi_nf+de_nf(+1));                                    //38

pstar_nf = ((theta_n*rgm^((1-chi_n)*(eps-1)))/(1-theta_n*rgm^((1-chi_n)*(eps-1))))*(pi_nf-chi_n*miu_n*pi_nf(-1));                          //39 

s_nf = (-eps*(1-theta_n*rgm^((1-chi_n)*eps)))*pstar_nf+(theta_n*rgm^((1-chi_n)*eps))*(eps*pi_nf+s_nf(-1)-chi_n*miu_n*eps*pi_nf(-1));       //40

// Non-traded informal Firm

y_ni = n_i ;                                                                                     // 41. Informal non-tradable output

p_ni = w_i  ;                                                                                    // 42. Informal non-tradables inflation

// Monetary Authority

r = rho_r*r(-1) + (1 - rho_r)*(psi_y*y + psi_pi*pi + psi_q*q) + eps_r ;                          // 43. Monetary policy under flexible exchange rate

// Market Clearing Condition

y_t = (1 - iota)*(s*(alpha+(1-lambda)*alpha)-p_t+(1-lambda)*p_th+lambda*p_n+c) + iota*((1-lambda)*alpha*s-lambda*(p_th-p_n) + q + c_star) ;             
                                                                                                 // 44. Tradables market clearing condition
y_nf = c_nf ;                  // 45. Formal non-tradables market clearing condition

y_ni = c_ni ;              // 46. Informal non-tradables market clearing condition

dif1 =  p_n - p_t;                                                                               // 47. The evolution of price differential between the tradable and nontradable sector

dif2 =  p_ni - p_nf;                                                                             // 48. The evolution of price differential between the formal and informal sector

n_f =n_t + n_nf ;                                                                                // 49. Formal labor market clearing condition
//p=p_th+alpha*s;

// Shocks
// External Shocks

c_star = rho_cstar*c_star(-1) + eps_cstar ;                                                      // 50. World consumption

p_star = rho_pstar*p_star(-1) + eps_pstar ;                                                      // 51. World inflation
                                                // 52. World interest rate

// Internal Shock

a_t = rho_at*a_t(-1) + eps_at ;                                                                  // 53. Domestic tradable firms productivity

a_nf = rho_an*a_nf(-1) + eps_an ;                                                                // 54. Non-tradable formal firms productivity

q=c-c_star;                                                                                      // 55 Uncovered interest parity condity with a risk premium shock (compute)

s = y_t-y_star_t ;                                                                               // 56. The evolution of term of trade 

c = (1-lambda)*(1-alpha)*y_t + alpha*(1-lambda)*y_star_t+lambda*y_n;                             // 57. 

//c_n = omega*y_ni+ (1-omega)*y_nf;                                                                // 58.


//pibar = rho_pibar*pibar(-1)+eps_pibar;                                                           // 57 . The shifting trend inflation shock 

%compute loss function
//L_nash1 = ((1-lambda)*(1-alpha))*(y_t^2+theta_t/kappa_t*pi_th^2)+lambda*(1-omega)*(y_nf^2+theta_n/kappa_n*pi_nf^2)+ lambda*omega*y_ni^2;
//L_nash2 = ((1-lambda)*(1-alpha)*theta_t/kappa_t*pi_th^2+lambda*(1-omega)*theta_n/kappa_n*pi_nf^2)+(1-lambda)*c^2+(lambda*lambda)*(1-lambda)*dif1^2+lambda*c_n^2+lambda*omega*(1-omega)*dif2^2+(1-lambda)*alpha*(-alpha+1)*s^2;


end;
resid(1);

steady(solve_algo=0);
check;

model_diagnostics;




// Calibration of Shocks
//-------------------------------------
shocks;
var eps_r       = sigma_r^2;                                                                                                     
var eps_an      = sigma_an^2;                                                                                                     
var eps_at      = sigma_at^2;
var eps_cstar   = sigma_cstar^2; 
var eps_pstar  = sigma_pstar^2;
//var eps_rstar   = sigma_rstar^2;
//var eps_q       = sigma_q^2;
//var eps_tot     = sigma_tot^2;
// var eps_pibar  = sigma_pibar^2;


end;

/*
// Simulation of the model
// ----------------------------------------

check;
stoch_simul(order=1,periods=2100,irf=21) y c pi r q e;
*/
estimated_params;
    h, beta_pdf, 0.25, 0.1;
    sigma, normal_pdf, 3, 0.1;
    nu, normal_pdf, 0.4, 0.1;
    eta, normal_pdf, 0.7, 0.1;
    mu, normal_pdf, 0.7, 0.1;
    gamma, normal_pdf, 1, 0.1;
    phi, normal_pdf, 1.5, 0.1;
    tau, normal_pdf, 2, 0.5;

    theta_t,beta_pdf, 0.5, 0.1;                         // Calvo parameter for tradables firms (probability of keeping prices unchanged)
    theta_n,beta_pdf, 0.5, 0.1;                         // Calvo parameter for non-tradables firms (probability of keeping prices unchanged)
    chi_t,normal_pdf, 0.0, 0.1;                       // Price Indexation for tradable firms
    miu_t,normal_pdf, 1.0, 0.1;                         // Weight on lagged inflation for tradable firms
    chi_n,normal_pdf, 0.0, 0.1;                        // Price Indexation for nontradable firms
    miu_n,normal_pdf, 1.0, 0.1;                         // Weight on lagged inflation for nontradable firms
    eps ,normal_pdf, 10, 0.1;

    rho_at, beta_pdf, 0.75, 0.1;
    rho_an, beta_pdf, 0.75, 0.1;
    stderr eps_at, inv_gamma_pdf, 0.1, 2;
    stderr eps_an, inv_gamma_pdf, 0.1, 2;

    
    rho_r, beta_pdf, 0.95, 0.1;
//    stderr eps_r, inv_gamma_pdf, 0.1, 2;
    psi_y, gamma_pdf, 1.5, 0.5;
    psi_pi, gamma_pdf, 0.125, 0.5;
    psi_q, gamma_pdf, 1.5, 0.5;

end;

estimated_params_init(use_calibration);
end;

varobs y pi r q s;
estimation(lik_init=2,plot_priors = 0,bayesian_irf,datafile=data,conf_sig =.95,prefilter = 1,filtered_vars,smoother,first_obs=6,mode_check,mode_compute=9,moments_varendo,mh_nblocks=5,mh_replic=1000,mh_jscale=0.5,mh_init_scale=1);

identification;















