% The RBC Model with informal sector

% Informal goods: consumption only
% Formal goods: consumption and investment
% GHH Preference
% deterministic model, simplified case
close all;

%----------------------------------------------------------------------
%1. Defining Variables
%----------------------------------------------------------------------

var  lU  lT  lY lY_f lY_i lC_f lC_i lC_g ld lK lKg lXg lH lh_f lh_i llambda lx r lG p  lC ltb lca lTBY lCAY lDY ;
% old variables  lU tau lT kappa lY lY_f lY_i lC_f lC_i lC_g ld lK lKg lXg lH lh_f lh_i llambda lx r A_f A_i lG p  lC ltb lca lTBY lCAY lDY ;

varexo kappa tau;
% old varexo ef ei eg ekappa etau;

parameters b alpha1 alpha2 etta1 etta2 pi beta theta gamma sigma r_star R  d_bar phi delta omega;
% old parameters b alpha1 alpha2 etta1 etta2 pi beta theta gamma sigma r_star R rho_f rho_i sigma_f sigma_i sigma_g sigma_kappa d_bar phi delta  G_ss rho_G omega rho_tau sigma_tau rho_kappa tau_ss kappa_ss;

%----------------------------------------------------------------------
% 2. Calibration
% Taken from Ihrig & Moe 2004 and Mitra 2013
%----------------------------------------------------------------------

alpha1 = 0.550;  %formal good share of utility
alpha2 = 0.30;
beta = 0.96;  %discount rate

%elasticity of formal output wrt laborgov
theta = 0.33;
pi    = 0.25;
etta1 = 0.33;
etta2 = 0.33;

%elasticity of informal output wrt labor
gamma = .495;

sigma = 1.5;
r_star = 0.04;
R = 0.1042;

%data from J. Fernandez's model
%rho_f = 0.90;
%rho_i = 0.64;
%sigma_f = 0.10;
%sigma_i = 0.10;
%sigma_g = 0.10;
%sigma_kappa = 0.10;

phi = 0.028 ;  %coefficient of capital adjustment
delta = 0.08; %capital depreciation

omega = 3;

%G_ss = 0.20;
%rho_G = 0.8;
%kappa_ss = 0.25;
%rho_kappa = .8;

%rho_tau = .8;
%sigma_tau = .5;
%tau_ss = .2;

d_bar = 0.019707;

b = 0.1;
 
%----------------------------------------------------------------------
%3. Model
%----------------------------------------------------------------------

model;

%Welfare
(lU) = ((exp(lC_f)^(alpha1)*exp(lC_i)^(alpha2)*exp(lC_g)^(1-alpha1-alpha2)-((exp(lh_f)+exp(lh_i))^omega)/omega)^(1-sigma)-1)/(1-sigma);

%Total Consumption
lC = alpha1*lC_f+alpha2*lC_i;

%Total Production
exp(lY)= exp(lY_f)+p*exp(lY_i);

%Discount 
exp(llambda) = exp(llambda(+1))*beta*(1+r);

%FOC wrt formal consumption
alpha1*exp(lC_f)^(alpha1-1)*exp(lC_i)^alpha2*exp(lC_g)^(1-alpha1-alpha2)*(exp(lC_f)^(alpha1)*exp(lC_i)^(alpha2)*exp(lC_g)^(1-alpha1-alpha2)-((exp(lh_f)+exp(lh_i))^omega)/omega)^(-sigma) = exp(llambda);

%FOC wrt informal consumption
(alpha2)*exp(lC_f)^alpha1*exp(lC_i)^(alpha2-1)*exp(lC_g)^(1-alpha1-alpha2)*(exp(lC_f)^(alpha1)*exp(lC_i)^(alpha2)*exp(lC_g)^(1-alpha1-alpha2)-((exp(lh_f) + exp(lh_i))^omega)/omega)^(-sigma) = p*exp(llambda);

%FOC wrt formal labor
(exp(lh_f) + exp(lh_i))^(omega-1)*(exp(lC_f)^(alpha1)*exp(lC_i)^(alpha2)-((exp(lh_f)*exp(lC_g)^(1-alpha1-alpha2) + exp(lh_i))^omega)/omega)^(-sigma) = (1-(tau))*exp(llambda)*theta*exp(lY_f)/exp(lh_f);

%FOC wrt Informal labor
(exp(lh_f) + exp(lh_i))^(omega-1)*(exp(lC_f)^(alpha1)*exp(lC_i)^(alpha2)-((exp(lh_f)*exp(lC_g)^(1-alpha1-alpha2) + exp(lh_i))^omega)/omega)^(-sigma) = (1-(b+tau)*(kappa))*exp(llambda)*p*gamma*exp(lY_i)/exp(lh_i);

%FOC wrt capital
exp(llambda)*(1+phi*(exp(lK)-exp(lK(-1)))) = exp(llambda(+1))*beta*((1-(tau))*(1-theta-pi)*exp(lY_f(+1))^(theta)*exp(lKg)^(pi)/exp(lK)^(pi+theta)+1-delta+phi*exp((lK(+1))-exp(lK)));

%Interest rate
r = r_star + R*(exp(exp(ld)-d_bar)- 1);

%Budget Constraint
exp(ld) = (1+(r(-1)))*exp(ld(-1))-(1-tau)*exp(lY_f)-(1-(b+tau)*kappa)*p*exp(lY_i)+exp(lC_f)+p*exp(lC_i) + exp(lx)+(phi/2)*(exp(lK)-exp(lK(-1)))^2-exp(lG);

%Formal Production
(lY_f) =  theta*lh_f +(1-theta-pi)*lK(-1)+(pi)*(lKg(-1));

%Informal Production
lY_i = gamma*lh_i;

%Investment
exp(lx) = exp(lK) - (1-delta)*exp(lK(-1));

%Government Investment
etta1*exp(lG) = lXg;

lXg = exp(lKg) - (1-delta)*exp(lKg(-1));

lT = lG*(1-etta1-etta2);

%Provision of Publiv Good;
etta2*exp(lG) = lC_g;

%Government Budget Constraint
(b+tau)*kappa*p*exp(lY_i) + tau*exp(lY_f) = exp(lG);

%Total Labor
exp(lh_f) + exp(lh_i) = exp(lH);

%Informal Sector Identity
lY_i = lC_i ;

%Trade Balance
exp(ltb) = exp(lY_f) - exp(lC_f) - exp(lx) - (phi/2)*(exp(lK))^2+phi*exp(lK)*exp(lK(-1))-(phi/2)*exp(lK(-1))^2;

%Current Account
exp(lca) = -(exp(ld)-exp(ld(-1)));

lTBY = ltb - lY;
lCAY = lca - lY;
lDY = ld - lY;

end;


%------------------------------------------------------------------
% 4. Computation
%------------------------------------------------------------------

initval;


lU      =		 -2.630306;
tau     =		 0.2;
kappa   =		 0.25;
lY      =		 -0.903;
lY_f    =		 -1.342;
lY_i    =		 -1.424229;
lC_f    =		 -1.65181;
lC_i    =		 -1.309;
lC_g    =		 0.0125579;
ld      =		 -1.56031;
lK      =		 -0.158418;
lKg     =		 -1.30974;
lH      =		 -1.49786;
lh_f    =		 -1.90225;
lh_i    =		 -2.5496;
llambda =		 2.132;
lx      =		 -2.511;
r       =		 0.0416667;
lG      =		 -2.69135;
p       =		 0.413974;
lC      =		 -1.20764;
ltb     =		 -4.73836;
lca     =		 -13;
lTBY    =		 -3.89111;
lCAY    =		 -12.1527;
lDY     =		 -0.713056;

end;

steady;

shocks;
var tau;
periods 1:12;
values .3;

%var kappa;
%periods 10:19;
%%values .5;
end;
check;

simul(periods=100,stack_solve_algo = 0, maxit=100);











