%-------------------------------------------------------------------
%This is the .mod file - dynare program - of "Small open 
%economy New Keynesian DSGE model for a foreign exchange constrained  
%economy". February 2011. 
%-------------------------------------------------------------------

%-----------------------------------------------------
% 1. Define variables
% Endogenous (var) and exogenous (varexo)
%-----------------------------------------------------
var
y_h,y_n,z_h,z_n,y,c,l,l_h,l_n,m,mc_h,mc_n,pi_h,pi_n,pi_f,pi_t,pi,psi,r,v,de,q,y_w,pi_w,r_w,mu;

varexo
ey_w, epi_w, er_w, ez_h, ez_n, euip, etot; 


%-----------------------------------------------------
% 2. Define parameters and asign parameter values 
%-----------------------------------------------------

parameters

alp1, alp2, bet, sig, vphi, etha, thet1, thet2, h, gam1, 
gam2,xi, 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;

%setting primary parameters

alp1 = 0.32;
alp2 = 0.45;
bet = 0.97;
sig = 2.61;
vphi = 3;
etha = 0.24;
thet1 = 12;
thet2 = 12;
h = 0.25;
gam1 = 0.731;
gam2 = 0.3;
xi = 0.15;
vsign =0.80;
epsin =0.45;
vsigh =0.75;
epsih =0.35;
vsigf = 0.2;
epsif = 0.3;
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.5;
phipi = 0.31;
phie = 0.3;
rhozh = 0.74;
rhozn = 0.90;
rhoy_w =0.75;
rhopi_w =0.60;
rhor_w =0.66;


%-----------------------------------------------------
% 3. Defining the model
%-----------------------------------------------------

model(linear);

%Domestic tradable sector

y_h = gam1*(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;

y_h = z_h+l_h;

y_n = z_n+alp1*l_n+xi*alp2*m(-1);

%y_n = z_n + l_n;

%l = (1-gam1)*l_h+gam1*l_n;

m = psi+thet2*gam2*(1-gam1)*v+thet2*gam1*mu+y_w+thet2*q;

mu = mu(-1)+pi_n-pi_h;

% consumption Euler equation

c = (h/(1+h))*c(-1)+(1/(1+h))*c(+1)-((1-h)/sig*(1+h))*(r - pi(+1));

%Domestic tradable marginal cost

mc_h = (sig/(1-h))*(c-h*c(-1))+vphi*l-z_h+gam2*(1-gam1)*v+gam1*mu;

% Non-tradable marginal cost

mc_n = (1/(alp1+xi*alp2))*((alp1*(sig/(1-h))*(c-h*c(-1))+vphi*l+l_n-y_n-mu)+xi*alp2*(v+m(-1)-y_n-mu));

%mc_n = (sig/(1-h))*(c-h*c(-1))+vphi*l-z_h+gam2*(1-gam1)*v+gam1*mu;

%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;

%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 international risk sharing condition

c-h*c(-1)=y_w-h*y_w(-1)+((1-h)/sig)*q;

%Uncovered interest parity condition

r=r_w+de+euip;

%monetary policy rule

r = rhor*r(-1)+ (1-rhor)*(phipi*pi + phiy*y + phie*de);

%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;

%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;

end;

%-----------------------------------------------------
% 4. Initialization of endogenous variables
% - steady - allows approximate values to be included 
%-----------------------------------------------------
initval;

y_h = 0;
y_n = 0;
z_h = 0;
z_n = 0;
y  = 0;
c = 0;
l=0;
l_h = 0;
l_n = 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;
y_w = 0;
pi_w = 0;
r_w = 0;
mu = 0;

end;

steady;

check;
shocks;
var etot = 0.01;
end;

stoch_simul(linear,hp_filter = 1600, irf = 20)y c pi_h pi_n pi_f pi mc_h mc_n de q mu;




