% Open-economy New Keynesian model by Linardi (2016)
%Doesn't work- indeterminacy
%
pkg load io;
pkg load struct;
pkg load optim;


%----------------------------------------------------------------
% 0. Housekeeping (close all graphic windows)
%----------------------------------------------------------------

close all;

%----------------------------------------------------------------
% 1. Defining variables
%----------------------------------------------------------------
var c, pi_h,pi_f,y,q,r,r_for,pi,pi_for,s,s_s,s_r,s_a,s_q,e,de,y_for;
varexo e_q,e_r,e_s,e_a,e_pi_for,e_r_for,e_y_for;
%c-consumption
%pi_h-domestic goods inflation
%s_q-real interest parity shock
%y-home output
%q-real exchange rate
%r-home nominal interest rate
%r_for-foreign nominal interest rate
%pi- total home CPI inflation
%pi_for-foreign CPI inflation
%s-terms of trade
%s_s-terms of trade shock
%y_for-foreign output
%e- nominal exchange rate (inverse definition, higher means depreciation)
%de-nominal depreciation rate
%s_a-technology shock
%----------------------------------------------------------------
% 2. Calibration
%----------------------------------------------------------------
parameters beta,h,sigma, phi, delta_h,delta_f, theta_h, theta_f,a1,b1,c1,rho_a,rho_s,rho_q,rho_r,phi_r,phi_pi,phi_de,phi_y,lambda_h,lambda_f,ni,alpha;
beta    	= 0.994535;				% discount factor calibrated to match realized real interest rate
alpha=0.19; %share of import in domestic consumption
h=0.7; %habit persistence
sigma=1;%inverse elasticity of substitution 
phi=2; %inverse elasticity of labour supply
ni=1; %elasticity of subtitution between home and foreign goods
delta_h=0.8; %degree of indexation of home produced goods (equals 80 % of lagged home inflation)
delta_f=0.8;%degree of indexation of imported goods (equals 80 % of lagged foreign inflation)
theta_h=0.5; %Calvo domestic prices
theta_f=0.5;%Calvo foreign prices
a1=0.5;%Persistence of foreign inflation
b1=0.5;%Persistence of foreign output
c1=0.5;%Persistence of foreign nominal interest rate
rho_a=0.8;%Persistence of technology shock
rho_s=0.8;%Persistence of terms-of-trade shock
rho_q=0.8;%Persistence of exchange rate shock
rho_r=0; % Persistence of monetary policy shock
phi_r=0.5;%Taylor rule smoothing parameter
phi_pi=1.5 ;%Taylor rule coefficient by inflation
phi_de=0.25/4 ; %Taylor rule coefficient by depreciation (adjusted to quarterly interest rate)
phi_y=0.25/4 ;%Taylor rule coefficient by output gap (adjusted to quarterly interest rate)
lambda_h=((1-beta*theta_h)*(1-theta_h))/theta_h;
lambda_f=((1-beta*theta_f)*(1-theta_f))/theta_f;

%----------------------------------------------------------------
% 3. Model
%----------------------------------------------------------------

model(linear); 
c-h*c(-1)=c(+1)-h*c-((1-h)/sigma)*(r-pi(+1)); %Consumption Euler equation (IS curve) eq.34
pi_h=beta*(pi_h(+1)-delta_h*pi_h)+delta_h*pi_h(-1)+lambda_h*(phi*y-(1+phi)*s_a+alpha*s+(sigma/(1-h))*(c-h*c(-1))); %Domestic goods inflation eq.35
pi_f=beta*(pi_f(+1)-delta_f*pi_f)+delta_f*pi_f(-1)+lambda_f*(q-(1-alpha)*s); % Imports inflation eq.36
q(+1)-q=(r-pi(+1))-(r_for-pi_for(+1))+s_q;%real interest rate parity condition eq.37
s-s(-1)=pi_f-pi_h+s_s;
y=(1-alpha)*c+alpha*ni*q+alpha*ni*s+alpha*y_for; %Market clearing condition eq.39
pi=(1-alpha)*pi_h+alpha*pi_f;%Total CPI inflation eq. 40
r=phi_r*r(-1)+(1-phi_r)*(phi_pi*pi+phi_y*y+phi_de*de)+s_r;%Taylor rule equation 26
%Shock processes
s_r=rho_r*s_r(-1)+e_r;
s_s=rho_s*s_s(-1)+e_s;
s_q=rho_q*s_q(-1)+e_q;
s_a=rho_a*s_a(-1)+e_a;
%Foreign variables AR(1) processes
pi_for=a1*pi_for(-1)+e_pi_for;
y_for=b1*y_for(-1)+e_y_for;
r_for=c1*r_for(-1)+e_r_for;
%Variables definition
de=e-e(-1);%Depreciation rate definition
q-q(-1)=de+pi_for-pi; %Link between real and nominal exchange rate	

end;

%----------------------------------------------------------------
% 4. Computation
%----------------------------------------------------------------
shocks;
var e_r;stderr 0.005;
var e_q;stderr 0.005;
var e_a; stderr 0.005;
var e_s;stderr 0.005;
var e_y_for; stderr 0.005;
var e_pi_for; stderr 0.005;
var e_r_for;stderr 0.005;
end;
steady;
check;

stoch_simul(irf=20,periods=0)y pi r q s e de;
%----------------------------------------------------------------
% 5. Estimation
%----------------------------------------------------------------





