%--------------------------------------------------------------------------
% ROT model with distortionary taxes and debt
% All adjust where both agents receieve/pay transfers 
% Closed economy 
% Uniform wage and labour tax rate 
%--------------------------------------------------------------------------

%--------------------------------------------------------------------------
% 27 Endogenous variables 
%--------------------------------------------------------------------------
 
var     y_h c_h c_h_o c_h_r l_h l_h_o l_h_r i_h k_h b_h  w_h r_h r_k_h
                g_h tau_k_h tau_c_h tau_l_h z_h
        u_a_h u_g_h u_tk_h  u_tc_h  u_tl_h  u_z_h       
        tk_h   tc_h   tl_h; 

%--------------------------------------------------------------------------
% 7 Exogenous shocks (error terms)
%--------------------------------------------------------------------------

varexo  e_u_a_h e_u_g_h e_u_tk_h e_u_tc_h e_u_tl_h e_u_z_h;

%--------------------------------------------------------------------------
% 45 parameters
%--------------------------------------------------------------------------

parameters BETA DELTA ALPHA GAMMA KAPPA H THETA PHI 

Y_h_bar G_h_bar B_h_bar Z_h_bar 
TAU_K_h_bar TAU_C_h_bar TAU_L_h_bar 
TK_h_bar TC_h_bar TL_h_bar 
RHO_A RHO_G RHO_TK RHO_TC RHO_TL RHO_Z 
SIGMA_A_H SIGMA_G_H SIGMA_TK_H SIGMA_TC_H SIGMA_TL_H SIGMA_Z_H
EPSI_G  EPSI_K  EPSI_L  EPSI_Z 
GAMMA_G GAMMA_K GAMMA_L GAMMA_Z PHI_KL PHI_KC PHI_LC;

BETA   = 0.99;
DELTA  = 0.025;
ALPHA  = 0.30;
GAMMA  = 5.0;
KAPPA  = -7.047;
H      = 0.50;
THETA  = 0.50;
PHI    = 10.0;

Y_h_bar  = 0.956;
B_h_bar  = 0.324;
G_h_bar  = 0.088;
Z_h_bar  = 0.016;

% Steady state capital tax rate
TAU_K_h_bar = 0.184;

% Steady state consumption tax rate
TAU_C_h_bar = 0.028;

% Steady state labour tax rate
TAU_L_h_bar = 0.223;


TK_h_bar  = 0.058;
TL_h_bar  = 0.030;
TC_h_bar  = 0.019;

% Government spending debt coefficient
GAMMA_G     = 0.23;

% Capital tax rate debt coefficient
GAMMA_K     = 0.39;

% Labour tax rate debt coefficient
GAMMA_L     = 0.049;

% Transfers debt coefficient
GAMMA_Z     = 0.5;

% Capital tax rate output coefficient
EPSI_K     = 1.7;

% Labour tax rate output coefficient
EPSI_L     = 0.36;

% Government spending output coefficient
EPSI_G     = 0.34;

% Transfers output coefficient
EPSI_Z     = 0.13;

% Capital-Labour co-term
PHI_KL     = 0.19;

% Capital-Consumption co-term
PHI_KC     = 0.024;

% Labour-Consumption co-term
PHI_LC     = -0.028;


% Parameters for the exogenous shocks 

RHO_A            = 0.9609;
SIGMA_A_H        = 0.6241;

RHO_G            = 0.9691;
SIGMA_G_H        = 3.0582;

RHO_TK           = 0.9349;
SIGMA_TK_H       = 4.3819;

RHO_TC           = 0.9321;
SIGMA_TC_H       = 4.0445;

RHO_TL          = 0.9725;
SIGMA_TL_H      = 3.0029;

RHO_Z            = 0.9437;
SIGMA_Z_H        = 3.3809;


%--------------------------------------------------------------------------
% Model
%--------------------------------------------------------------------------

model;

% Aggregation
exp(c_h)     = THETA * exp(c_h_r) + (1-THETA) * exp(c_h_o)  ;
exp(l_h)     = THETA * exp(l_h_r) + (1-THETA) * exp(l_h_o)  ;

% Production function
exp(y_h) = exp(u_a_h)*(exp(k_h(-1)))^ALPHA * exp(l_h)^(1-ALPHA);

% Law of motion
exp(k_h)   = (1-DELTA)*exp(k_h(-1)) + exp(i_h);

% MPL and MPK
exp(w_h)   =             (1-ALPHA)* exp(y_h)/exp(l_h)  ;
exp(r_k_h) =                ALPHA * exp(y_h)/(exp(k_h(-1))) ;

% intertemporal conditions
(exp(c_h_o) - H*exp(c_h_o(-1)) )^(-GAMMA)/(1+exp(tau_c_h)) = BETA * (exp(c_h_o(+1))- H*exp(c_h_o) )^(-GAMMA) / (1+exp(tau_c_h(+1))) *  exp(r_h(+1));
(exp(c_h_o) - H*exp(c_h_o(-1)) )^(-GAMMA)/(1+exp(tau_c_h)) = BETA * (exp(c_h_o(+1))- H*exp(c_h_o) )^(-GAMMA) / (1+exp(tau_c_h(+1))) *((1-exp(tau_k_h(+1))) * exp(r_k_h(+1)) + 1 - DELTA);

% intratemporal conditions

exp(w_h)  = PHI*(1-exp(l_h_o))^ KAPPA /(1-exp(tau_l_h)) * (1+exp(tau_c_h))/ (exp(c_h_o)     - H*exp(c_h_o(-1)) )^(-GAMMA); 
exp(w_h)  = PHI*(1-exp(l_h_r))^ KAPPA /(1-exp(tau_l_h)) * (1+exp(tau_c_h))/ (exp(c_h_r)     - H*exp(c_h_r(-1)) )^(-GAMMA); 

% GBC 
exp(b_h) +  exp(tk_h) + exp(tc_h) + exp(tl_h)   = exp(r_h(-1)) * exp(b_h(-1)) + exp(g_h) + exp(z_h);


% Optimizing agents BC
(1-exp(tau_l_h)) * exp(w_h) * exp(l_h_o) + (1-exp(tau_k_h)) * exp(r_k_h) * exp(k_h(-1))  +  exp(r_h) * exp( b_h(-1)) +   exp(z_h)  = 
 (1+exp(tau_c_h))*exp(c_h_o) +  exp(i_h) + exp(b_h);

% ROT constraint
(1+exp(tau_c_h)) * exp(c_h_r) = (1 - exp(tau_l_h)) * exp(l_h_r)* exp(w_h) ;

% Total capital tax revenue 
exp(tk_h)  = exp(tau_k_h) * exp(r_k_h)  *  exp(k_h(-1));

% Total labor tax revenue 
exp(tl_h) = exp(tau_l_h) * exp(w_h) * exp(l_h_o);

% Total consumption tax revenue 
exp(tc_h) = exp(tau_c_h) * exp(c_h);

%--------------------------------------------------------------------------
% Fiscal rules
%--------------------------------------------------------------------------

% Government spending policy rule
g_h = - EPSI_G*y_h - GAMMA_G*b_h(-1) + u_g_h  + ( EPSI_G*log(Y_h_bar) + GAMMA_G*log(B_h_bar) + log(G_h_bar) ) ;

% Capital tax policy rule
tau_k_h =  EPSI_K*y_h + GAMMA_K*b_h(-1) + PHI_KL*u_tl_h + PHI_KC*u_tc_h + u_tk_h + (-EPSI_K*log(Y_h_bar) - GAMMA_K*log(B_h_bar) + log(TAU_K_h_bar) ) ;

% Labour tax policy rule
tau_l_h =  EPSI_L*y_h + GAMMA_L*b_h(-1) + PHI_KL*u_tk_h + PHI_LC*u_tc_h + u_tl_h + (-EPSI_L*log(Y_h_bar) - GAMMA_L*log(B_h_bar) + log(TAU_L_h_bar) ) ;

% Consumption tax policy rule
tau_c_h = PHI_KC*u_tk_h + PHI_LC*u_tl_h + u_tc_h +  log(TAU_C_h_bar)  ;

% Transfers tax policy rule
z_h = - EPSI_Z*y_h - GAMMA_Z*b_h(-1) + u_z_h  + ( EPSI_Z*log(Y_h_bar) + GAMMA_Z*log(B_h_bar) + log(Z_h_bar) ) ;

%--------------------------------------------------------------------------
%  Exogenous shocks
%--------------------------------------------------------------------------

u_a_h = RHO_A * u_a_h(-1) +  e_u_a_h;

u_g_h   = RHO_G   * u_g_h(-1)   +  e_u_g_h;
u_tk_h  = RHO_TK  * u_tk_h(-1)  +  e_u_tk_h;
u_tc_h  = RHO_TC  * u_tc_h(-1)  +  e_u_tc_h;
u_tl_h  = RHO_TL  * u_tl_h(-1)  +  e_u_tl_h;
u_z_h   = RHO_Z   * u_z_h(-1)   +  e_u_z_h;

end;

%--------------------------------------------------------------------------
% Initial values 
%--------------------------------------------------------------------------


initval;
y_h     = -0.046;
c_h     = -0.366;
c_h_o   = -0.009;
c_h_r   = -0.926;
l_h     = -1.109;
l_h_o   = -2.124;
l_h_r   = -0.615;
i_h     = -1.057;
k_h     = 2.806;
b_h     = -1.126;
w_h     = -0.030;
r_h     = 0.010;
r_k_h   = -3.267;

u_a_h   = 0;
u_g_h   = 0;
u_tk_h  = 0;
u_tc_h  = 0;
u_tl_h  = 0;
u_z_h   = 0;

tk_h    = -2.8;
tc_h    = -3.9;
tl_h    = -2.6;

tau_k_h  = log(TAU_K_h_bar);
tau_l_h  = log(TAU_L_h_bar);
tau_c_h  = log(TAU_C_h_bar);
g_h      = log(G_h_bar);
z_h      = log(Z_h_bar);

end;

%--------------------------------------------------------------------------
% shocks
%--------------------------------------------------------------------------

shocks;
var e_u_a_h   = SIGMA_A_H    ^2;
var e_u_g_h   = SIGMA_G_H    ^2;
var e_u_z_h   = SIGMA_Z_H    ^2;
var e_u_tk_h  = SIGMA_TK_H   ^2;
var e_u_tl_h  = SIGMA_TL_H   ^2;
var e_u_tc_h  = SIGMA_TC_H   ^2;

end;

%--------------------------------------------------------------------------
% Compute 
%--------------------------------------------------------------------------

steady(solve_algo=1);
resid;
check;
//model_info ;


stoch_simul(order=1,hp_filter=1600,irf=0);
