%************************************************************************
%This is the .mod file - dynare program - of "Introducing Labour Market * 
%Segmentation into a Small Open Economy New Keynesian DSGE model".      *
%Sisay Regassa Senbeta September 2012.                                  *
%************************************************************************

%--------------------------------------------------------------------
% 0. Closing graphics windows
%--------------------------------------------------------------------

close all;

%--------------------------------------------------------------------
% 1. Define variables
% Endogenous (var) and exogenous (varexo)
%--------------------------------------------------------------------

var
c,y_hf,y_hi,y_h,y_n,y,l,l_f,l_i,l_hi,l_ni,tau_i,u,x,w_f,w_i,z_h,z_n,mc_hf,mc_hi,mc_h,mc_n,pi_h,pi_n,pi_f,pi_t,pi,de,psi,v,q,mu,r,y_w,pi_w,r_w;

varexo
ey_w, epi_w, er_w, ez_h, ez_n, etot, er, euip; 


%--------------------------------------------------------------------
% 2. Define parameters and asign parameter values 
%--------------------------------------------------------------------

parameters

alp, bet, sig, vphi, etha, thet0, thet1, thet2, thet3, b, gam1,tau3,tau4,wbf,wbi,wb, 
gam2,gam3,vsign,vsigh,vsigf,epsih,epsin,epsif,kbh,kfh,lamh,kbn,kfn,lamn,kbf,kff,
lamf,rhor,phiy,phipi,phie,rhozh,rhozn,rhoy_w,rhopi_w,rhor_w,del,taui,tau1,tau2,xi,omeg;

%setting primary parameters

alp = 1.5;
bet = 0.9951;
sig = 2.61;
vphi = 3;
etha = 1;
thet0= 1;
thet1 = 1.5;
thet2 =1.5;
thet3 = 1.5;
b = 0.25;
gam1 = 0.73;
gam2 = 0.3;
gam3 = 0.80;
vsign =0.80;
epsin =0.10;
vsigh =0.75;
epsih =0.45;
vsigf = 0.20;
epsif = 0.40;
kbh = vsigh/(epsih+vsigh*(1-epsih*(1-bet)));
kfh = (bet*epsih)/(epsih+vsigh*(1-epsih*(1-bet)));
lamh = ((1-vsigh)*(1-epsih)*(1-bet*epsih))/(epsih+vsigh*(1-epsih*(1-bet)));
kbn = vsign/(epsin+vsign*(1-epsin*(1-bet)));
kfn = (bet*epsin)/(epsin+vsign*(1-epsin*(1-bet)));
lamn = ((1-vsign)*(1-epsin)*(1-bet*epsin))/(epsin+vsign*(1-epsin*(1-bet)));
kbf =vsigf/(epsif+vsigf*(1-epsif*(1-bet)));
kff =(bet*epsif)/(epsif+vsigf*(1-epsif*(1-bet)));
lamf= ((1-vsigf)*(1-epsif)*(1-bet*epsif))/(epsif+vsigf*(1-epsif*(1-bet)));
rhor = 0.8;
phiy = 0.0570;
phipi = 0.3;
phie = 0.8;
rhozh = 0.74;
rhozn = 0.90;
rhoy_w =0.75;
rhopi_w =0.60;
rhor_w =0.66;
del = 0.05;
taui = 0.90;
tau1 = 0.10;
tau2 = 0.60;
tau3 = 0.67;
tau4 = 0.33;
xi = 0.40;
omeg = 0.95;
wbi= 0.95;
wbf=(1/taui)*(xi*thet0*((0.11)^alp)+taui*wbi+bet*(1-del)*xi*0.89*thet0*((0.11)^alp));
wb=(xi*thet0*((0.11)^alp)+taui*wbi+bet*(1-del)*xi*0.89*thet0*((0.11)^alp));


%---------------------------------------------------------------------
% 3. Defining the model
%---------------------------------------------------------------------

model(linear);

//The model is in log-linearized form - log-deviation around a steady-state

//consumption

c = (b/(1+b))*c(-1)+(1/(1+b))*(b*c+y_w(+1)-b*y_w+((1-b)/sig)*q(+1))-((1-b)/sig*(1+b))*(r - pi(+1));


//production of tradable goods formal, tradable informal, and non-tradable informal 

y_hf = l_f+z_h;                                     //Intermediate-type tradable goods produced by the formal sector

y_hi = l_hi+z_h;                                   //Intermediate-type tradable goods produced by the informal sector

y_n = l_ni+z_n;                                     //Non-tradable goods produced by the informal sector

//Total labour - resource constraint

l=tau1*l_f+tau2*l_hi+(1-tau1-tau2)*l_ni;            //Total labour

l_i = tau3*l_hi + tau4*l_ni;                        //Labour in the informal sector

tau_i = l_i-l;                                      //The fraction of workers in the informal sector

//productivity in tradable sector

z_h = rhozh*z_h(-1) + ez_h;

//productivity in non-tradable sector

z_n = rhozn*z_n(-1) + ez_n;

//Final tradable goods production - CES pdn function where the inputs are the intermediate-type tradable goods discussed above

y_h = (1-gam3)*y_hf + gam3*y_hi;

//Marginal cost of the intermediate-type tradable goods

mc_hf=((1-del)*bet*(0.11)^alp*thet0*(-pi- z_h +sig*c+((sig*b)/(1-b))*c(+1)+((sig)/(1-b))*c(-1)-alp*x(+1)-z_h(+1))
+wbf*z_h+wbf*w_f+alp*thet0*((0.11)^alp)*x)/(wbf+thet0*((0.11)^alp)-(1-del)*bet*thet0*((0.11)^alp)); 

//Marginal cost of intermediate-type goods produced by informal sector firms

mc_hi = w_i-z_h;

//Marginal cost of final tradable goods

mc_h = (1-gam3)*mc_hf+gam3*mc_hi;

//Marginal cost of non-tradable goods

mc_n = w_i-z_n;

//Goods market clearing condition

y_h = -gam2*(thet2-thet1*gam1)*v+thet1*gam1*mu+c-thet2*gam2*(1-gam1)*v+thet2*gam1*mu+y_w+thet2*q;

//Domestic non-tradable sector

y_n = -thet1*gam2*(1-gam1)*v+thet1*(gam1-1)*mu+c;

// Total output

y = (1-gam1)*y_h+gam1*y_n;

//Labour market tightness

x = (1/del)*(l_f-(1-del)*l_f(-1))-u;

//Unemployment rate

u = -(3.762)*l;                         //Assuming steady-state unemployment rate of 21 percent

//wage rates in the formal and informal sector

w_f = (1/wb)*((xi*z_h+alp*xi*x+(((sig+sig*b)/(1-b))*c-((sig*b)/(1-b))*c(-1)-((sig)/(1-b))*c(+1)+ z_h(+1))*bet*(1-del)*xi*(0.89)
+bet*(1-del)*xi*(alp-(alp+1)*0.89)*x(+1)+(1+bet*(1-del)*0.89)*xi*tau_i)*thet0*((0.11)^alp)+taui*wbi*w_i);

w_i = w_i(-1)+pi_n-pi+z_n-z_n(-1);

//Uncovered interest parity condition

de(+1)= r -r_w + euip;                          //de is the change in nominal exchange rate

//Domestic tradable inflation

pi_h =kbh*pi_h(-1)+kfh*pi_h(+1)+lamh*mc_h;

//Domestic non-tradable inflation

pi_n =  kbn*pi_n(-1)+kfn*pi_n(+1)+lamn*mc_n;

//imported goods inflation

pi_f =  kbf*pi_f(-1)+kff*pi_f(+1)+lamf*psi;

//tradable inflation;

pi_t=pi_h-gam2*(v-v(-1));

//Overall inflation

pi=(1-gam1)*pi_t+gam1*pi_n;

//Nominal exchange rate

de=q-q(-1)-pi_w+pi;

//the evolution of law of one price gap

psi=psi(-1)+de+pi_w-pi_f;

//The evolution of the terms of trade

v =v(-1)+pi_f-pi_h+etot;

//The relationship between real exchage rate and terms of trade

q = psi-(1-gam2*(1-gam1))*v-gam1*mu;

//the evolution of the price of non-tradable goods relative to the price of domestically produced tradable goods

mu(+1) = mu+pi_n(+1)-pi_h(+1);

//monetary policy rule

r = rhor*r(-1)+ (1-rhor)*(phipi*pi + phiy*y + phie*de)+er;

//External sector

y_w = rhoy_w*y_w(-1) + ey_w;

pi_w= rhopi_w*pi_w(-1) + epi_w;

r_w=rhor_w*r_w(-1) + er_w;

end;

%-----------------------------------------------------
% 4. Initialization of endogenous variables
% - steady - allows approximate values to be included 
%-----------------------------------------------------

initval;

c = 0;
y_hf = 0;
y_hi = 0; 
y_h = 0;
y_n = 0;
y  = 0;
l = 0;
l_f = 0;
l_i = 0;
tau_i = 0;
u = 0;
x = 0;
w_f = 0;
w_i = 0;
z_h = 0;
z_n = 0;
mc_hf = 0;
mc_hi = 0;
mc_h = 0;
mc_n = 0;
pi_h = 0;
pi_n = 0; 
pi_f = 0;
pi_t = 0;
pi = 0;
psi = 0;
v = 0;
de = 0;
q = 0;
r = 0;
y_w = 0;
pi_w = 0;
r_w = 0;
mu = 0;

end;

steady;

check;
shocks;
var ey_w; stderr 1;
end;

stoch_simul(linear,hp_filter = 1600, irf = 40)y c l l_f l_i mc_h mc_n pi r;
%mean(oo_.endo_simul(1,options_.drop+M_.maximum_lag+1:end));



