
// Endogenous variables: 
var C_T     // Tradable consumption
    C_N     // Nontradable consumption
    I_T     // Investment in capital for Tradable sector		
    I_N     // Investment in capital for Nontradable sector  
    I_C     // Investment in capital for Commodity sector 
    L_T     // Tradable sector Labor
    L_N     // Nontradable sector Labor
    L_C     // Commodity sector Labor
    lambda  // Lagrange multiplier
    D       // debt stock 
    
    A_N     // Productivity factor of Nontradable sector
    K_N     // Capital for Nontradable sector
    Y_N     // Output of Nontradable sector

    A_C     // Productivity factor of Commodity sector 
    K_C     // Capital  for Commodity sector
    Y_C     // Output of Commodity sector
    TB_C    // Trade balance of nontradable sector

    A_T     // Productivity factor of Tradable sector
    K_T     // Capital for Tradable sector
    CM_T    // Demand for Commodity goods from Tradable sector
    Y_T     // Output of tradable sector
    TB_T    // Trade Balance of tradable sector

    I       // Total Investment
    C       // Consumption 
    TB      // Total Trade balance
    Y       // Total Production
    
    P_N     // Price of Nontradable good
    P_C     // Price of commodity goods
    R       // Interest rate of borrowing
    mu_T    // rental rate of capital for Tradable sector
    mu_N    // rental rate of capital for nontradable sector
    mu_C    // rental rate of capital for Commodity sector
    W_T     // Wage for Tradable sector
    W_N     // Wage for Nontradable sector
    W_C;    // Wage for Commodity sector
    

// Exogenous variables: 
varexo eps_C   // Shock in Commodity price process
       eps_R   // Shock in Risk premium process
       eps_AT  // Shock in productivity factor in Tradable sector
       eps_AN  // Shock in productivity factor in Tradable sector
       eps_AC; // Shock in productivity factor in Tradable sector


parameters betaa       // Subjective discount factor
           varphi      // Elasticity of substitution 
           sigma       // relative risk coeficient 
           omega_T     // Frisch elasticity of labor supply for Tradable sector
           omega_N     // Frisch elasticity of labor supply for Nontradable sector
           omega_C     // Frisch elasticity of labor supply for Commodity sector
           chi         // a number between 0 and 1 used in the CES aggregator
           phi_T       // parameter in the capital adjustment cost for Tradable sector
           phi_N       // parameter in the capital adjustment cost for Nradable sector
           phi_C       // parameter in the capital adjustment cost for Commodity sector
          
           delta       // depreciation rate
           alpha_T     // output elasticity of capital for Tradable sector
           alpha_N     // output elasticity of capital for nontradable sector
           alpha_C     // output elasticity of capital for Commodity sector
           gammaa_T    // output elasticity of commodity goods for Tradable sector

           v_C         // Coefficient used in the price commodity process
           v_D         // Coefficient used in the price commodity process
                
           rho_C       // autoregressive coefficient for commodity price process
           rho_AT      // autoregressive coefficient for productivity factor process in the Tradable sector
           rho_AN      // autoregressive coefficient for productivity factor process in the nontradable sector
           rho_AC      // autoregressive coefficient for productivity factor process in the commodity sector
           Rss         // Interest rate in the steady state used in the exogenous process
           Dss         // Debt in the steady state used in the exogenous process
           Pss_C;      // Commodity price in the steady state used in the exogenous process


varphi = 0.5;  
sigma = 2;
omega_T = 1.455;
omega_N = 1.455;
omega_C = 1.455;
chi = 0.35;
delta = 0.025; 
alpha_T = 0.35;
alpha_N = 0.25;
alpha_C = 0.35;
gammaa_T = 0.05;
phi_T = 4.6;
phi_N = 9.3;
phi_C =10.3;

rho_C = 0.95;

rho_AC = 0.88;
rho_AT = 0.87;
rho_AN = 0.84;

v_D = 0.077;
v_C = -0.014;
Pss_C = 0.69;
Dss = 0.12;
Rss = 1.02;
betaa = 1/Rss;


model;
// Households:
//   1. Price of nontradable goods:
P_N = ((1-chi)/chi)*((C_T/C_N)^(1/varphi));

//   2-4. Labor supply for each sector:  
(L_T^(omega_T - 1)) = chi*((C/C_T)^(1/varphi))*W_T;
(L_N^(omega_N - 1)) = chi*((C/C_T)^(1/varphi))*W_N;
(L_C^(omega_C - 1)) = chi*((C/C_T)^(1/varphi))*W_C;

//   5. Euler equation:
lambda = betaa*R*lambda(+1);

//   6. CES aggregator: 
C = (chi*(C_T^((varphi-1)/varphi)) + (1-chi)*(C_N^((varphi - 1)/varphi)))^(varphi/(varphi-1));

//   7. Lagrange multiplier:
lambda = ((C - ((L_T^omega_T)/omega_T) - ((L_N^omega_N)/omega_N) - ((L_C^omega_C)/omega_C))^(-sigma))*chi*((C/C_T)^(1/varphi));
 
//   8-10. Euler equation: 
lambda*(1 + phi_T*(K_T - K_T(-1))) = betaa*lambda(+1)*(1- delta + mu_T(+1) - phi_T*(K_T(+1) - K_T));
lambda*(1 + phi_N*(K_N - K_N(-1))) = betaa*lambda(+1)*(1- delta + mu_N(+1) - phi_N*(K_N(+1) - K_N));
lambda*(1 + phi_C*(K_C - K_C(-1))) = betaa*lambda(+1)*(1- delta + mu_C(+1) - phi_C*(K_C(+1) - K_C));

//   11-13. Law of motion of capital:
K_T = I_T + (1-delta)*K_T(-1) - phi_T*(K_T - K_T(-1));
K_N = I_N + (1-delta)*K_N(-1) - phi_N*(K_N - K_N(-1));
K_C = I_C + (1-delta)*K_C(-1) - phi_C*(K_C - K_C(-1));

// Firms FOC:
//   14-17. Tradable sector FOC:
Y_T = A_T*(K_T(-1)^alpha_T)*(CM_T^gammaa_T)*(L_T^(1 - gammaa_T - alpha_T));
L_T = (1 - alpha_T - gammaa_T)*(Y_T/W_T);
K_T(-1) = alpha_T*(Y_T/mu_T);
CM_T = gammaa_T*(Y_T/P_C);

//   18-20. Nontradable Sector:
Y_N = A_N*(K_N(-1)^alpha_N)*(L_N^(1 - alpha_N));
L_N = (1-alpha_N)*(Y_N*P_N/W_N);
K_N(-1) = alpha_N*(Y_N*P_N/mu_N);

//    21-23. Commodity Sector:
Y_C = A_C*(K_C(-1)^alpha_C)*(L_C^(1 - alpha_C));
L_C = (1 - alpha_C)*((Y_C*P_C)/W_C);
K_C(-1) = alpha_C*((Y_C*P_C)/mu_C);

// Market Clearing
//    24. Nontradable Sector:
C_N = Y_N;

//    25. Tradable Sector:
Y_T = C_T + I_T + I_N + I_C + TB_T;

//    26. Commodity Sector:
P_C*Y_C = P_C*CM_T + TB_C;

//    27. Balance of payments: 
TB_T + TB_C = D(+1) + D*(R-2);

//    28. Total trade balance:
TB = TB_T + TB_C;

//    29. Total Investment:
I = I_T + I_N + I_C;

//    30. Total Output:
Y = Y_T + P_N*Y_N + P_C*Y_C;

// Exogenous Process:
//    31. Commodity Price
ln(P_C) = rho_C*ln(P_C(-1)) + eps_C;

//    32. Risk Premium:
R = Rss + v_D*(exp(D - Dss) - 1) + v_C*(exp(P_C - Pss_C) - 1) + eps_R;

//    33-35. Productivity sector:
ln(A_T) = rho_AT*ln(A_T(-1)) + eps_AT;
ln(A_N) = rho_AN*ln(A_N(-1)) + eps_AN;
ln(A_C) = rho_AC*ln(A_C(-1)) + eps_AC;

end;



steady(solve_algo = 1, maxit=50000);

check;


shocks;
var eps_C; 
stderr 0.01;
//var eps_R; stderr 0.02;
//var eps_AT; stderr ;
//var eps_AN; stderr ;
//var eps_AC; stderr ;
end;


stoch_simul(order=1,irf=40) Y I C TB P_N;

