%Small open eco
%Define variables
%Endogenous variables
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;
% Define parameters
parameters alp1 alp2 bet sig vphi etha thet1 thet2 h gam1 gam2 xi vsign vsigh vsigf epsih epsin epsif lamh lamn lamf rhor phiy phipi phie rhozh rhozn rhoy_w rhopi_w rhor_w;
%setting primary parameters
alp1=0.32;
alp2=0.45;
bet=0.99;
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;
lamh=((1-epsih)*(1-bet*epsih))/epsih;
lamn=((1-epsin)*(1-bet*epsin))/epsin;
lamf=((1-epsif)*(1-bet*epsif))/epsif;
rhor=0.8;
phiy=0.5;
phipi=1.5;
phie=0.5;
rhozh=0.44;
rhozn=0.40;
rhoy_w=0.65;
rhopi_w=0.50;
rhor_w=0.56;
%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*1-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*1+l_n-y_n-mu)+xi*alp2*(v+m(-1)-y_n-mu));
%mc_n=(sig/(1-h))*(c-h*c(-1))+vphi*1-z_h+gam2*(1-gam1)*v+gam1*mu;
%Domestic tradable inflation
pi_h=bet*pi_h(+1)+lamh*mc_h;
%Domestic non-tradable inflation
pi_n=bet*pi_n(+1)+lamn*mc_n;
%Imported goods inflation
pi_f=bet*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;
% Evolution of terms of trade
v=v(-1)+pi_f-pi_h+etot;
%The relationship between real exchange rate and terms of trade
q=psi+(1-gam2*(1-gam1))*v-gam1*mu;
%International risk sharing
c-h*c(-1)=y_w-h*y_w(-1)+((1-h)/sig)*q;
%Uncovered interest parity
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;
%Initialization of endog vars
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;
m=0;
mc_h=0;
mc_n=0;
pi_h=0;
pi_n=0;
pi_f=0;
pi_t=0;
pi=0;
psi=0;
r=0;
v=0;
de=0;
q=0;
y_w=0;
pi_w=0;
r_w=0;
mu=0;
end;
shocks;
var etot;
stderr 0.01;
var euip;
stderr 0.01;
end;

steady;
check;
stoch_simul(irf=20,order=2);

