% Two sector model with on-the-job search, labor market frictions
% (exogenous separations)  and price rigidities

% Steady state parameters are provided by a corresponding steady state file 
%(see 'ss_fun.m' which uses fsolve to solve for the steady state. All steady
% steady state definitions are finally collected in the m.file 'twosecexonew_steadystate')


% Steffen Esser (November 2014)


%--------------------------------------------------------------------------
% 1. Defining the variables
%--------------------------------------------------------------------------

var ug ub u ng nb n vg vb v mg mb m qb qg thetag thetab wg wb epsilong epsilonb epsilon yg yb y pb pg 
qr s e bettast R c pi A Yb Yg;

varexo eA;


%--------------------------------------------------------------------------
% 2. Parameter choice
%--------------------------------------------------------------------------

parameters betta rhox alphha sigma tau kappa zeta gammma nu eta xi cg cb chig chib 
rhopi rhoy rhor rhoA b ug_ss ub_ss u_ss nb_ss ng_ss n_ss vg_ss vb_ss v_ss mg_ss mb_ss m_ss q_ss qr_ss e_ss s_ss
thetag_ss thetab_ss pg_ss pb_ss qb_ss qg_ss epsilonb_ss epsilong_ss  c_ss y_ss wb_ss wg_ss Yb_ss Yg_ss yb_ss yg_ss Ass;

betta=0.99;                    // Discount factor
rhox=0.1;                     // Exogenous separation rate
alphha=0.4;                    // Share of intermediate good from low wage sector
sigma=2;                      // Relative risk aversion
tau=1.1;                      // Search cost elasticity
kappa=0.04;                   // Scaling parameter search cost
zeta=betta*(1-rhox);
gammma=11;                     // Market power retail firms
nu=0.75;                      // Calvo parameter
eta=0.5;                      // Relative bargaining power worker
xi=0.7;                       // Elasticity of the matching function
cg=0.16;                      // Vacancy posting costs (high wage sector)                     
cb=0.04;                      // Vacancy posting costs (low wage sector)
chig=0.6;                     // Matching efficiency (high wage sector)                     
chib=0.6;                     // Matching efficiency (low wage sector)
rhoA=0.94;
Ass=1;
u_ss=0.12;

// Taylor Rule

rhopi=1.5;
rhoy=0.5;
rhor=0.32; 




%--------------------------------------------------------------------------
% 3. Model
%--------------------------------------------------------------------------


model(linear);

%--------------------------------------------------------------------------
% 3.1 Good jobs
%--------------------------------------------------------------------------


pg=(1-xi)*thetag;               //Matching rate (worker)


qg=-xi*thetag;                  // Matching rate (firm)


thetag=vg-(1/(ug_ss+e_ss))*(ug_ss*ug+e_ss*e);	// Labor market tightness


ng=(1-rhox)*ng(-1)+mg_ss/ng_ss*mg(-1);      // Sectoral employment


mg=(xi/(ug_ss+e_ss))*(ug_ss*ug+e_ss*e)+(1-xi)*vg;   // Matching function


wg_ss*wg=eta*(Ass*epsilong_ss*(A+epsilong)+cg*thetag_ss*thetag);    //Real wage


-qg=bettast-zeta*qg(+1)+betta*qg_ss/cg*(Ass*epsilong_ss*(A(+1)+epsilong(+1))-wg_ss*wg(+1)); // Job creation condition


yg=A+ng;    // Sectoral output


%--------------------------------------------------------------------------
% 3.2 Bad jobs
%--------------------------------------------------------------------------


qb=-xi*thetab;      // Matching rate (worker)


pb=(1-xi)*thetab;   // Matching rate (firm)	


mb=xi*ub+(1-xi)*vb;	


(tau-1)*s=pg-((cg/qg_ss)/(cg/qg_ss-cb/qb_ss))*qg+(cb/qb_ss/(cg/qg_ss-cb/qb_ss))*qb;   // Optimal search intensity


nb=(1-rhox)*(1-s_ss*pg_ss)*nb(-1)-(1-rhox)*s_ss*pg_ss*(s+pg)-mb_ss/nb_ss*mb(-1); //Employment


thetab=vb-ub;       // LM tightness	


-qb=bettast-zeta*(1-s_ss*pg_ss)*qb(+1)+betta*qb_ss/cb*(epsilonb_ss*Ass*(epsilonb(+1)+A(+1))-wb_ss*wb(+1)-(1-rhox)*s_ss*pg_ss*(s(+1)+pg(+1)));          // JC condition


wb_ss*wb=eta*(epsilonb_ss*Ass*(epsilonb+A)+cg*thetag_ss*(1-(1-rhox)*s_ss)*thetag)+((1-eta)*kappa*s_ss^(tau)*tau-eta*(1-rhox)*s_ss*cg*thetag_ss)*s;  //Real wage


yb=A+nb;        // Sectoral output


qr=e+pg-n;  // Quit rate       


e=s+nb;


%--------------------------------------------------------------------------
% 3.3 Other
%--------------------------------------------------------------------------


bettast=sigma*(c-c(+1));     // Stochastic discount factor


sigma*c=pi(+1)-R+sigma*c(+1);	 // Euler equation


u_ss*u=ug_ss*ug+ub_ss*ub;	// Unemployment


u_ss*u=-(ng_ss*ng + nb_ss*nb);  // Normalization


n_ss*n=ng_ss*ng+nb_ss*nb;          // Total employment


v_ss*v=vb_ss*vb+vg_ss*vg;          // Total vacancies


R=rhor*R(-1)+rhoy*y+rhopi*pi;      // Taylor rule


A=rhoA*A(-1)+eA;            // Technology


thetag-thetab=0;            // No Arbitrage


epsilon=alphha*epsilonb+(1-alphha)*epsilong;      // Real marginal cost


pi=betta*pi(+1)+((1-nu)*(1-betta*nu))/nu*epsilon;   // NKPC


epsilonb=epsilon+c-Yb;         // Intermediate good price (bad jobs)


epsilong=epsilon+c-Yg;         // Intermediate good price (good jobs)


c_ss*c=y_ss*y-cg*vg_ss*vg-cb*vb_ss*vb; // Aggregate consumption


Yb_ss*Yb=yb_ss*yb-cb*vb_ss*vb;                    // Intermediate good market clearing (bad jobs)


Yg_ss*Yg=yg_ss*yg-cg*vg_ss*vg;                      // Intermediate good market clearing (good jobs)

y=alphha*yb+(1-alphha)*yg;

end;


%--------------------------------------------------------------------------
% 4. Computation
%--------------------------------------------------------------------------

initval;

R=0;
A=0;
pi=0;
c=0;
y=0;
bettast=0;
u=0;
ug=0;
ub=0;
n=0;
ng=0;
nb=0;
m=0;
mg=0;
mb=0;
v=0;
vg=0;
vb=0;
epsilon=0;
epsilong=0;
epsilonb=0;
thetag=0;
thetab=0;
Yg=0;
Yb=0;
yg=0;
yb=0;
pg=0;
pb=0;
qg=0;
qb=0;
qr=0;
e=0;
s=0;
wg=0;
wb=0;

end;

steady;
check;

shocks;
var eA;
stderr 0.0049;
end;

stoch_simul(order=1,irf=40);