% I. This code generates the steady state for the baseline model with 2
% sectors

function flag = noLFPnoSItwoSmodel
x0_wH = 0.8;
x0_wL= 0.5;
x0_beta=0.7;
x0=[x0_wH, x0_wL, x0_beta];
finalW = fsolve(@RunnoLFPnoSItwoSmodel,x0);
w_H_ss = finalW(1);
w_L_ss = finalW(2);
beta   = finalW(3);
[test, ~,vecSS] = RunnoLFPnoSItwoSmodel(finalW);
flag = 'done';


function [test, par, vecSS] = RunnoLFPnoSItwoSmodel(GuessW)

%parameters 

alpham     = 1;    % matching efficiency
alpha      = 0.5;  % matching elasticity
vartheta   = 0.5;  % bargaining power
theta      = 0.2;  % price rigidity
psi        = 0.5;  
varrho     = 0.6;   %high sector firms' weight in production 
mu         = 0.589; % mass of high-skill workers 0.589

rho_H_ss   = 0.984; %0.984
rho_L_ss   = 0.967; %0.967
rho_rH     = 0.8437; %0.8437
sigma_rH   = 0.0008;  
rho_rL     = 0.3389; %0.3389
sigma_rL   = 0.0028; %0.0028
rho_b      = 0.9448;
sigma_b    = 0.0038;
rho_z      = 0.9752;
sigma_z    = 0.0031;
rho_i      = 0.8527;
sigma_i    = 0.0013;
sigma_tauH  = 0.01;
sigma_tauL  = 0.01;

% make a guess
w_H_ss = GuessW(1);
w_L_ss = GuessW(2);
beta   = GuessW(3);

% SOLVE FOR STEADY STATE 

z_ss=1;
i_bar=0.003;

% Labor market 

u_H_ss=0.023; %0.023
u_L_ss=0.068; %0.068

e_H_ss=mu-u_H_ss;
e_L_ss=1-mu-u_L_ss;
s_H_ss=mu-rho_H_ss*e_H_ss;
s_L_ss=1-mu-rho_L_ss*e_L_ss;

fs_H_ss=(e_H_ss*(1-rho_H_ss))/(mu-rho_H_ss*e_H_ss); 
fs_L_ss=(e_L_ss*(1-rho_L_ss))/(1-mu-rho_L_ss*e_L_ss); 
tightnessH=(fs_H_ss/alpham)^(1/(1-alpha));
tightnessL=(fs_L_ss/alpham)^(1/(1-alpha));
v_H_ss=tightnessH*s_H_ss;
v_L_ss=tightnessL*s_L_ss;
fv_H_ss=alpham*(tightnessH^(-alpha));  
fv_L_ss=alpham*(tightnessL^(-alpha));

%output and prices
Y_ss=(e_H_ss^varrho)*(e_L_ss^(1-varrho));
q_H_ss=(varrho*Y_ss)/e_H_ss;
q_L_ss=((1-varrho)*Y_ss)/e_L_ss;
chi_bar_H   = (1/2)*q_H_ss;
chi_bar_L   = (1/2)*q_L_ss;

%discount factor
Lambda=beta;

%unemployment benefits
tau_uH_ss=0.3*w_H_ss; 
tau_uL_ss=0.3*w_L_ss; 

% vacancy costs
k_H=(fv_H_ss*(q_H_ss-w_H_ss))/(1-beta*rho_H_ss); % from optimal hiring condition
k_L=(fv_L_ss*(q_L_ss-w_L_ss))/(1-beta*rho_L_ss); % from optimal hiring condition

% dividends
dw_H_ss=(q_H_ss-w_H_ss)*e_H_ss-k_H*v_H_ss;
dw_L_ss=(q_L_ss-w_L_ss)*e_L_ss-k_L*v_L_ss;
D_ss=dw_H_ss+dw_L_ss;

% consumption target: c_u/c_e=0.72 
tau_ss=(u_H_ss*tau_uH_ss+u_L_ss*tau_uL_ss)/(e_H_ss*w_H_ss+e_L_ss*w_L_ss+D_ss);
c_eH_ss=((1-tau_ss)*(e_H_ss*w_H_ss+D_ss)+tau_uH_ss*(1-e_H_ss))/(0.72+0.28*e_H_ss);
b_bar_ss=0.72*c_eH_ss-tau_uH_ss;
c_uH_ss=0.72*c_eH_ss;
c_eL_ss=b_bar_ss+(1-tau_ss)*w_L_ss;
c_uL_ss=b_bar_ss+tau_uL_ss;

GammaH_ss=c_eH_ss-b_bar_ss+ c_eH_ss*(log(c_uH_ss/c_eH_ss)+chi_bar_H);
GammaL_ss=c_eL_ss-b_bar_ss+ c_eH_ss*(log(c_uL_ss/c_eL_ss)+chi_bar_L);
x_ss=b_bar_ss;
b_ss=b_bar_ss;
a_ss=b_ss;
C=e_H_ss*c_eH_ss+e_L_ss*c_eL_ss+u_H_ss*c_uH_ss+u_L_ss*c_uL_ss;


test1=vartheta*(q_H_ss+beta*rho_H_ss*fs_H_ss*(k_H/fv_H_ss))+GammaH_ss*((1-vartheta)/(1-tau_ss))-w_H_ss;
test2=vartheta*(q_L_ss+beta*rho_L_ss*fs_L_ss*(k_L/fv_L_ss))+GammaL_ss*((1-vartheta)/(1-tau_ss))-w_L_ss;
test3=1/((1+i_bar)*(e_H_ss+(u_H_ss/0.72)+e_L_ss*(c_eH_ss/c_eL_ss)+u_L_ss*(c_eH_ss/c_uL_ss)))-beta;


test=[test1 test2 test3];


par = [beta alpham alpha vartheta varrho chi_bar_H chi_bar_L mu theta psi z_ss i_bar  ...
            rho_H_ss rho_rH sigma_rH rho_L_ss rho_rL sigma_rL rho_b sigma_b rho_z sigma_z rho_i sigma_i sigma_tauH sigma_tauL];

vecSS=[c_eH_ss c_eL_ss c_uH_ss c_uL_ss e_H_ss e_L_ss u_H_ss u_L_ss tau_uH_ss tau_uL_ss D_ss dw_H_ss dw_L_ss ...
       fs_H_ss fs_L_ss fv_H_ss fv_L_ss w_H_ss w_L_ss b_bar_ss s_H_ss s_L_ss k_H k_L v_H_ss v_L_ss ...  
       x_ss a_ss b_ss GammaH_ss GammaL_ss q_H_ss q_L_ss tau_ss Y_ss ];

save vecSS_2sectorsA beta alpham alpha vartheta varrho chi_bar_H chi_bar_L ...
                  theta psi z_ss mu tau_uH_ss tau_uL_ss i_bar fs_H_ss fs_L_ss fv_H_ss fv_L_ss ...
                  rho_H_ss rho_rH sigma_rH rho_L_ss rho_rL sigma_rL rho_b sigma_b rho_z sigma_z rho_i sigma_i sigma_tauH sigma_tauL ...
                  w_H_ss w_L_ss b_bar_ss e_H_ss e_L_ss u_H_ss u_L_ss s_H_ss s_L_ss k_H k_L v_H_ss v_L_ss D_ss dw_H_ss dw_L_ss...
                  x_ss a_ss b_ss c_eH_ss c_eL_ss c_uH_ss c_uL_ss ...
                  GammaH_ss GammaL_ss q_H_ss q_L_ss tau_ss Y_ss 

