%%%%%%%%%%%%%%%%% Master Thesis %%%%%%%%%%%%%%%%% Hui-Lan Chang %%%%%%%%%%%
%%%                                                                                                                                                         %%%\
%%% Title: Explaining Backus-Smith puzzle in a                                                                              %%%\
%%%           stochastic dynamic general equilibrium model                                                      %%%\
%%%                                                                                                                                                         %%%\
%%% Supervisor: Prof. Almuth Scholl                                                                                                %%%\
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% University of Konstanz %%%%
%%%\
%%% Refenerce: G. Corsetti, L. Dedola, and S. Leduc (2008),
%%%            "International Risk Sharing and the Transmission of
%%%            Productivity Shocks", The Review of Economic Studies,
%%%            75, 443-473.
%%%
%%% Create: 25.06.2014
%%%

%%% With endogenous discount factor from average capita consumption and leisure.
%%% Ref. Schmitt-Grohe and Uribe (2003)

%----------------------------------------------------------------------------------------------------\
% 0. Housekeeping (close all graphic windows and clear the workspace)
%----------------------------------------------------------------------------------------------------\

close all; 

%----------------------------------------------------------------------------------------------------\
% 1. Defining variables
%----------------------------------------------------------------------------------------------------\

% list of endogenous variables
var  GDP1 GDP2 c1 c2 c_ht1 c_ht2 c_ft1 c_ft2 c_t1 c_t2 c_n1 c_n2 ih1 if2  
     bh1 bh2 k_ht1 k_ft2 k_n1 k_n2 k1 k2 y_ht1 y_ft2 y_n1 y_n2 l1 l2
     l_ht1 l_ft2 l_n1 l_n2 p_wht1 p_wht2 p_wft1 p_wft2 p_ht1 p_ht2 
     p_ft1 p_ft2 p_n1 p_n2 p_t1 p_t2 wage_1 wage_2 R_1 R_2 r_b  p1 p2
     TOT RER nx1 nx2 
     z_ht1 z_ft2 z_n1 z_n2 
     discount_ONEperiod1 discount_ONEperiod2
     MU1_l MU2_l MU1_c MU2_c MC1_ct MC2_ct MC1_cn MC2_cn 
     MCT1_ch MCT1_cf MCT2_ch MCT2_cf; 

% the following exogenous variables are shocked
varexo e_ht1 e_ft2 e_n1 e_n2;

parameters sigma alpha rho elasTN aHT aFT aN aT psi diM
           LsT LsN KsT KsN delta ps11 ps12 ps13 ps14 ps21 ps22 ps23 ps24 
           ps31 ps32 ps33 ps34 ps41 ps42 ps43 ps44 vv11 vv12 vv13 vv14 
           vv21 vv22 vv23 vv24 vv31 vv32 vv33 vv34 vv41 vv42 vv43 vv44
           u_bar l_bar r_bar p_n1_bar p_wh_bar p_h_bar  
           c_bar c_f_bar c_n_bar c_h_bar c_t_bar y_n_bar wage1_bar 
           R_bar K_bar y_h_bar LH_bar LN_bar KH_bar KN_bar ZH_bar ZN_bar;

%----------------------------------------------------------------------------------------------------\
% 2. Calibration
%----------------------------------------------------------------------------------------------------\

%% Baseline Model 
%% With estimate by Stockman and Tesar (1995) from U.S. relative to a set of OECD countries on annual data

%% Table 2

% For Preferences and technology
sigma = 2; %Risk aversion 
alpha = 0.34; %Consumption share 
rho = 1-(1/0.85); % 1/(1-rho)=0.85; Home and Foreign-traded goods of Elasticity of substitution 
elasTN = 1-(1/0.74); % 1/(1-elasTN)=0.74; Traded and non-traded goods of Elasticity of Substitution 
aHT = 0.72; %Share of Home-traded goods -->imports in steady state is 5% of aggregate output 
aFT = 1-aHT; %Share of Foreign-traded goods 
aN = 0.45; %Share of non-traded goods in consumption
aT = 1-aN; %Share of traded goods in consumption
psi = 0.08; %Elasticity of the discount factor with respect to C and L 
diM = 1.09; %Distribution margin 
LsT = 0.61; %Labor share in tradables 
KsT = 1-LsT; %Capital share in tradables
LsN = 0.56; %Labor share in non-tradables 
KsN = 1-LsN; %Capital share in non-tradables
delta = 0.1; %Depreciation rate
r_bar = 0.04; %Hidden in the text real interest rate is 4% per annuam in steady state.
%%DoNotFit Steady State:importshareY = 0.05; % Hidden in the text. In steady state imports is 5% of aggregate output.
%%DoNotFit Steady State:l_bar = 1/3; %Hidden in the text. In steady state 1/3 of time endownment is spent working.

% Steady state algorithm
%*_
%%*_Approximation value_*
l_bar=0.298851047; %Calibration
u_bar=0.499504184; %Calibration
p_wh_bar=1; %Calibrate price for Investment

%*_Price of goods_*
p_n1_bar=p_wh_bar/(diM/u_bar-diM);
p_h_bar=diM/u_bar*p_n1_bar; 
p_f_bar=p_h_bar;

%*_Consumption_*
c_bar=((r_bar/psi)/((1-l_bar)^(1-alpha)))^(1/alpha);
c_f_bar=aT*aFT*(aT+aN*(p_n1_bar/p_h_bar)^(elasTN/(elasTN-1)))^(-1/elasTN)*c_bar;
c_n_bar=aN/aT/aFT*(p_n1_bar/p_h_bar)^(1/(elasTN-1))*c_f_bar;
c_h_bar=aHT/aFT*c_f_bar;
c_t_bar=1/aFT*c_f_bar;

%*_Returns of inputs and captial stock_*
wage1_bar=(1-alpha)*p_h_bar*(aT+aN*(p_n1_bar/p_h_bar)^(elasTN/(elasTN-1)))*c_f_bar/(1-l_bar)/alpha/aT/aFT;
R_bar=(r_bar+delta)*p_wh_bar;

%*_Total output, inputs, and productility_*
y_n_bar=c_n_bar+diM*c_h_bar+diM*c_f_bar; %Total output
KN_bar=KsN*p_n1_bar*y_n_bar/R_bar;
LN_bar=LsN*p_n1_bar*y_n_bar/wage1_bar;

KH_bar=(KN_bar+c_h_bar/delta+c_f_bar/delta)/((r_bar+delta)/KsT/delta-1);
K_bar=KH_bar+KN_bar;
y_h_bar=c_h_bar+c_f_bar+delta*K_bar;
LH_bar=LsT*p_wh_bar*y_h_bar/wage1_bar;

ZH_bar=y_h_bar/(KH_bar^KsT)/(LH_bar^LsT);
ZN_bar=y_n_bar/(KN_bar^KsN)/(LN_bar^LsN);
%_*

% For Productivity shocks
ps11 =  0.82; ps12 = -0.06; ps13 =  0.10; ps14 =  0.24;
ps21 = -0.06; ps22 =  0.82; ps23 =  0.24; ps24 =  0.10;
ps31 = -0.02; ps32 =  0.02; ps33 =  0.96; ps34 =  0.01;
ps41 =  0.02; ps42 = -0.02; ps43 =  0.01; ps44 =  0.96;

% Variance-covariance matrix (in per cent)
vv11 =  0.047; vv12 =  0.022; vv13 =  0.009; vv14 =  0.004;
vv21 =  0.022; vv22 =  0.047; vv23 =  0.004; vv24 =  0.009;
vv31 =  0.009; vv32 =  0.004; vv33 =  0.009; vv34 = -0.001;
vv41 =  0.004; vv42 =  0.009; vv43 = -0.001; vv44 =  0.009;

%----------------------------------------------------------------------------------------------------\
% 3. Model--From Social Planner's perspective.
%----------------------------------------------------------------------------------------------------\

model;

%% CES aggregator for consumption
exp(c_t1) = (aHT^(1-rho)*exp(c_ht1)^(rho)+aFT^(1-rho)*exp(c_ft1)^(rho))^(1/rho); %Traded good consumption in country 1
exp(c_t2) = (aHT^(1-rho)*exp(c_ft2)^(rho)+aFT^(1-rho)*exp(c_ht2)^(rho))^(1/rho); %Traded good consumption in country 2
exp(c1) = (aT^(1-elasTN)*exp(c_t1)^(elasTN)+aN^(1-elasTN)*exp(c_n1)^(elasTN))^(1/elasTN); %Total consumption in country 1
exp(c2) = (aT^(1-elasTN)*exp(c_t2)^(elasTN)+aN^(1-elasTN)*exp(c_n2)^(elasTN))^(1/elasTN); %Total consumption in country 2

% First order conditions with uninternalized endogenous discount factor 
exp(MU1_l) = ((exp(c1)^(alpha)*(1-exp(l1))^(1-alpha))^(-sigma))*(1-alpha)*exp(c1)^(alpha)*(1-exp(l1))^(-alpha); %marginal utility w.r.t. labor in country 1
exp(MU2_l) = ((exp(c2)^(alpha)*(1-exp(l2))^(1-alpha))^(-sigma))*(1-alpha)*exp(c2)^(alpha)*(1-exp(l2))^(-alpha); %marginal utility w.r.t. labor in country 2
exp(MU1_c) = ((exp(c1)^(alpha)*(1-exp(l1))^(1-alpha))^(-sigma))*alpha*exp(c1)^(alpha-1)*(1-exp(l1))^(1-alpha); %marginal utility w.r.t. consumption in country 1
exp(MU2_c) = ((exp(c2)^(alpha)*(1-exp(l2))^(1-alpha))^(-sigma))*alpha*exp(c2)^(alpha-1)*(1-exp(l2))^(1-alpha); %marginal utility w.r.t. consumption in country 2
exp(MC1_ct) = (exp(c1)^elasTN)^(-1+1/elasTN)*(aT^(1-elasTN)*exp(c_t1)^(elasTN-1)); %marginal consumption w.r.t. traded goods in country 1
exp(MC2_ct) = (exp(c2)^elasTN)^(-1+1/elasTN)*(aT^(1-elasTN)*exp(c_t2)^(elasTN-1)); %marginal consumption w.r.t. traded goods in country 2
exp(MC1_cn) = (exp(c1)^elasTN)^(-1+1/elasTN)*(aN^(1-elasTN)*exp(c_n1)^(elasTN-1)); %marginal consumption w.r.t. non-traded goods in country 1
exp(MC2_cn) = (exp(c2)^elasTN)^(-1+1/elasTN)*(aN^(1-elasTN)*exp(c_n2)^(elasTN-1)); %marginal consumption w.r.t. non-traded goods in country 2
exp(MCT1_ch) = exp(c_ht1)^(rho-1)*aHT^(1-rho)*((aHT^(1-rho)*exp(c_ht1)^rho)+(aFT^(1-rho)*exp(c_ft1)^rho))^(-1+1/rho); %marginal traded good consumption w.r.t. Home traded goods in country 1
exp(MCT2_cf) = exp(c_ft2)^(rho-1)*aHT^(1-rho)*((aHT^(1-rho)*exp(c_ft2)^rho)+(aFT^(1-rho)*exp(c_ht2)^rho))^(-1+1/rho); %marginal traded good consumption w.r.t. Foreign traded goods in country 2
exp(MCT1_cf) = exp(c_ft1)^(rho-1)*aFT^(1-rho)*((aHT^(1-rho)*exp(c_ht1)^rho)+(aFT^(1-rho)*exp(c_ft1)^rho))^(-1+1/rho); %marginal traded good consumption w.r.t. Foreign traded goods in country 1
exp(MCT2_ch) = exp(c_ht2)^(rho-1)*aFT^(1-rho)*((aHT^(1-rho)*exp(c_ft2)^rho)+(aFT^(1-rho)*exp(c_ht2)^rho))^(-1+1/rho); %marginal traded good consumption w.r.t. Home traded goods in country 2

exp(MCT1_ch)/exp(p_ht1) = exp(MCT1_cf)/exp(p_ft1); %Trade off between Home traded good and Foreign traded good in country 1
exp(MCT2_ch)/exp(p_ht2) = exp(MCT2_cf)/exp(p_ft2); %Trade off between Home traded good and Foreign traded good in country 2
exp(MC1_ct)*exp(MCT1_ch)/exp(p_ht1) = exp(MC1_cn)/exp(p_n1); %Trade off between Home traded good and non-traded good in country 1
exp(MC2_ct)*exp(MCT2_ch)/exp(p_ht2) = exp(MC2_cn)/exp(p_n2); %Trade off between Home traded good and non-traded good in country 2
exp(MU1_l)/(LsT*exp(y_ht1)*exp(p_wht1)/exp(l_ht1)) = exp(MU1_c)*exp(MC1_cn)/exp(p_n1); %Trade off between labor input in traded section and non-traded good consumption in country 1
exp(MU2_l)/(LsT*exp(y_ft2)*exp(p_wft2)/exp(l_ft2)) = exp(MU2_c)*exp(MC2_cn)/exp(p_n2); %Trade off between labor input in traded section and non-traded good consumption in country 2
exp(MU1_l)/(LsN*exp(y_n1)*exp(p_n1)/exp(l_n1)) = exp(MU1_c)*exp(MC1_cn)/exp(p_n1); %Trade off between labor input in non-traded section and non-traded good consumption in country 1
exp(MU2_l)/(LsN*exp(y_n2)*exp(p_n2)/exp(l_n2)) = exp(MU2_c)*exp(MC2_cn)/exp(p_n2); %Trade off between labor input in non-traded section and non-traded good consumption in country 2

%Euler equations
exp(discount_ONEperiod1) = 1/(1+(psi*(exp(c1))^(alpha)*(1-exp(l1))^(1-alpha))); % discountF_t+1 / discountF_t
exp(discount_ONEperiod2) = 1/(1+(psi*(exp(c2))^(alpha)*(1-exp(l2))^(1-alpha))); % discountF_t+1 / discountF_t
exp(MU1_c)*exp(MC1_cn)*exp(p_wht1)/exp(p_n1) = exp(discount_ONEperiod1)*exp(MU1_c(+1))*exp(MC1_cn(+1))/exp(p_n1(+1))*(exp(p_wht1(+1))*KsT*exp(y_ht1(+1))/exp(k_ht1)+(1-delta)*exp(p_wht1(+1))); %Euler equation for non-traded good consumption in country 1
exp(MU2_c)*exp(MC2_cn)*exp(p_wft2)/exp(p_n2) = exp(discount_ONEperiod2)*exp(MU2_c(+1))*exp(MC2_cn(+1))/exp(p_n2(+1))*(exp(p_wft2(+1))*KsT*exp(y_ft2(+1))/exp(k_ft2)+(1-delta)*exp(p_wft2(+1))); %Euler equation for non-traded good consumption in country 2
exp(MU1_c)*exp(MC1_cn)*exp(p_wht1)/exp(p_n1) = exp(discount_ONEperiod1)*exp(MU1_c(+1))*exp(MC1_cn(+1))/exp(p_n1(+1))*(exp(p_n1(+1))*KsN*exp(y_n1(+1))/exp(k_n1)+(1-delta)*exp(p_wht1(+1))); %Euler equation for non-traded good consumption in country 1
exp(MU2_c)*exp(MC2_cn)*exp(p_wft2)/exp(p_n2) = exp(discount_ONEperiod2)*exp(MU2_c(+1))*exp(MC2_cn(+1))/exp(p_n2(+1))*(exp(p_n2(+1))*KsN*exp(y_n2(+1))/exp(k_n2)+(1-delta)*exp(p_wft2(+1))); %Euler equation for non-traded good consumption in country 2
exp(MU1_c)*exp(MC1_cn)/exp(p_n1) = exp(discount_ONEperiod1)*exp(MU1_c(+1))*exp(MC1_cn(+1))/exp(p_n1(+1))*(1+exp(r_b)); %Euler equation for non-traded good consumption in country 1
exp(MU2_c)*exp(MC2_cn)/exp(p_n2) = exp(discount_ONEperiod2)*exp(MU2_c(+1))*exp(MC2_cn(+1))/exp(p_n2(+1))*(1+exp(r_b)); %Euler equation for non-traded good consumption in country 2

% Resource budget constraints in both countries
exp(p_ht1)*exp(c_ht1)+exp(p_ft1)*exp(c_ft1)+exp(p_n1)*exp(c_n1)+bh1+exp(p_wht1)*exp(ih1) = exp(p_wht1)*exp(y_ht1)+exp(p_n1)*exp(y_n1)+(1+exp(r_b(-1)))*bh1(-1); 
exp(p_ft2)*exp(c_ft2)+exp(p_ht2)*exp(c_ht2)+exp(p_n2)*exp(c_n2)+bh2+exp(p_wft2)*exp(if2) = exp(p_wft2)*exp(y_ft2)+exp(p_n2)*exp(y_n2)+(1+exp(r_b(-1)))*bh2(-1); 

% Production function of traded and non-traded goods
exp(y_ht1) = exp(z_ht1)*exp(k_ht1(-1))^KsT*exp(l_ht1)^LsT; %Output of traded goods in country 1
exp(y_ft2) = exp(z_ft2)*exp(k_ft2(-1))^KsT*exp(l_ft2)^LsT; %Output of traded goods in country 2
exp(y_n1) = exp(z_n1)*exp(k_n1(-1))^KsN*exp(l_n1)^LsN; %Output of non-traded goods in country 1
exp(y_n2) = exp(z_n2)*exp(k_n2(-1))^KsN*exp(l_n2)^LsN; %Output of non-traded goods in country 2

%% Market clearing condition
exp(l1) = exp(l_ht1)+exp(l_n1); %Total labor supply in country 1
exp(l2) = exp(l_ft2)+exp(l_n2); %Total labor supply in country 2
exp(k1) = exp(ih1)+(1-delta)*exp(k1(-1)); %Law of motion for captial stock in country 1
exp(k2) = exp(if2)+(1-delta)*exp(k2(-1)); %Law of motion for captial stock in country 2
exp(k1) = exp(k_ht1)+exp(k_n1); %Total capital stock in country 1
exp(k2) = exp(k_ft2)+exp(k_n2); %Total capital stock in country 2
bh1 = -bh2; %Bond market clear
exp(y_n1) = exp(c_n1)+diM*exp(c_ht1)+diM*exp(c_ft1); %Non-traded goods market clear in country 1
exp(y_n2) = exp(c_n2)+diM*exp(c_ft2)+diM*exp(c_ht2); %Non-traded goods market clear in country 2
exp(y_ht1) = exp(ih1)+exp(c_ht1)+exp(c_ht2); %Traded goods market clear in country 1
exp(y_ft2) = exp(if2)+exp(c_ft2)+exp(c_ft1); %Traded goods market clear in country 2

%% Price in equilibrium
exp(wage_1) = exp(p_n1)*LsN*exp(y_n1)/exp(l_n1); %Wage in country 1
exp(wage_2) = exp(p_n2)*LsN*exp(y_n2)/exp(l_n2); %Wage in country 2
exp(R_1) = exp(p_n1)*KsN*exp(y_n1)/exp(k_n1(-1)); %Return of captial in country 1
exp(R_2) = exp(p_n2)*KsN*exp(y_n2)/exp(k_n2(-1)); %Return of captial in country 2

% Law of one price hold at the wholesale level.
exp(p_wht1) = exp(p_wht2);
exp(p_wft1) = exp(p_wft2);

% Distribution cost added for tradable goods
exp(p_ht1) = exp(p_wht1) + diM*exp(p_n1); %Retrail price for Home traded good in country 1
exp(p_ft1) = exp(p_wft1) + diM*exp(p_n1); %Retrail price for Foreign traded good in country 1
exp(p_ht2) = exp(p_wht2) + diM*exp(p_n2); %Retrail price for Home traded good in country 2
exp(p_ft2) = exp(p_wft2) + diM*exp(p_n2); %Retrail price for Foreign traded good in country 2

%Utility-based CPIs
exp(p_t1) = (aHT*exp(p_ht1)^(rho/(rho-1))+aFT*exp(p_ft1)^(rho/(rho-1)))^((rho-1)/rho);
exp(p_t2) = (aHT*exp(p_ft2)^(rho/(rho-1))+aFT*exp(p_ht2)^(rho/(rho-1)))^((rho-1)/rho);
exp(p1) = (aT*exp(p_t1)^(elasTN/(elasTN-1))+aN*exp(p_n1)^(elasTN/(elasTN-1)))^((elasTN-1)/elasTN);
exp(p2) = (aT*exp(p_t2)^(elasTN/(elasTN-1))+aN*exp(p_n2)^(elasTN/(elasTN-1)))^((elasTN-1)/elasTN);

%Indexes
GDP1=exp(p_wht1)*exp(y_ht1)+exp(p_n1)*exp(y_n1);
GDP2=exp(p_wft2)*exp(y_ft2)+exp(p_n2)*exp(y_n2);
nx1=(exp(p_wft1)*exp(c_ft1)-exp(p_wht2)*exp(c_ht2))/GDP1; %net exports ratio of GDP
nx2=(exp(p_wht2)*exp(c_ht2)-exp(p_wft1)*exp(c_ft1))/GDP2; %net exports ratio of GDP
TOT=exp(p_ft1)/exp(p_ht2); %terms of trade: imported goods relative to exported goods
RER=exp(p2)/exp(p1); %realative price of consumption: the foreign CPI in terms of home CPI

%% Productivity Shocks with log form
z_ht1=ps11*z_ht1(-1)+ps12*z_ft2(-1)+ps13*z_n1(-1)+ps14*z_n2(-1)+e_ht1;
z_ft2=ps21*z_ht1(-1)+ps22*z_ft2(-1)+ps23*z_n1(-1)+ps24*z_n2(-1)+e_ft2;
z_n1=ps31*z_ht1(-1)+ps32*z_ft2(-1)+ps33*z_n1(-1)+ps34*z_n2(-1)+e_n1;
z_n2=ps41*z_ht1(-1)+ps42*z_ft2(-1)+ps43*z_n1(-1)+ps44*z_n2(-1)+e_n2;

end;

%----------------------------------------------------------------------------------------------------\
% 4. Computation
%----------------------------------------------------------------------------------------------------\

initval;

%Steady state


c1 = log(c_bar);
c_ht1 = log(c_h_bar);
c_ft1 = log(c_f_bar);
c_t1 = log(c_t_bar);
c_n1 = log(c_n_bar);
k_ht1 = log(KH_bar);
k_n1 = log(KN_bar);
k1 = log(K_bar);
ih1 = log(delta*K_bar);
y_ht1 = log(y_h_bar);
y_n1 = log(y_n_bar);
l1=log(l_bar);
l_ht1 = log(LH_bar);
l_n1 = log(LN_bar);
p_wht1 = log(p_wh_bar);
p_wft2 = log(p_wh_bar);
p_ht1 = log(p_h_bar); 
p_ft1 = log(p_h_bar); 
p_n1 = log(p_n1_bar);
wage_1 = log(wage1_bar);
R_1 = log(R_bar);
r_b = log(r_bar);
bh1 = 0;

p_t1 = log((aHT*exp(p_ht1)^(rho/(rho-1))+aFT*exp(p_ft1)^(rho/(rho-1)))^((rho-1)/rho)); 
p1 = log((aT*exp(p_t1)^(elasTN/(elasTN-1))+aN*exp(p_n1)^(elasTN/(elasTN-1)))^((elasTN-1)/elasTN));
GDP1 = exp(p_wh_bar)*exp(y_h_bar)+exp(p_n1_bar)*exp(y_n_bar);

GDP2 = GDP1;
c2 = c1;
c_ht2 = c_ft1;
c_ft2 = c_ht1;
c_t2 = c_t1; 
c_n2 = c_n1;
if2 = ih1;
bh2 = -bh1;
k_ft2 = k_ht1; 
k_n2 = k_n1; 
k2 = k1;
y_ft2 = y_ht1; 
y_n2 = y_n1; 
l2 = l1;
l_ft2 = l_ht1; 
l_n2 = l_n1;
p_wht2 = p_wht1;
p_wft1 = p_wft2;
p_ft2 = p_ht1;
p_ht2 = p_ft1; 
p_n2 = p_n1;
p_t2 = p_t1; 
wage_2 = wage_1; 
R_2 = R_1; 
p2 = p1;

TOT = exp(p_h_bar)/exp(p_f_bar);
RER = exp(p2)/exp(p1);

discount_ONEperiod1 = log(1/(1+(psi*(exp(c1))^(alpha)*(1-exp(l1))^(1-alpha)))); % discountF_t+1 / discountF_t
MU1_l = log(((exp(c1)^(alpha)*(1-exp(l1))^(1-alpha))^(-sigma))*(1-alpha)*exp(c1)^(alpha)*(1-exp(l1))^(-alpha)); %marginal utility w.r.t. labor
MU1_c = log(((exp(c1)^(alpha)*(1-exp(l1))^(1-alpha))^(-sigma))*alpha*exp(c1)^(alpha-1)*(1-exp(l1))^(1-alpha)); %marginal utility w.r.t. consumption
MC1_ct = log((exp(c1)^elasTN)^(-1+1/elasTN)*(aT^(1-elasTN)*exp(c_t1)^(elasTN-1))); %marginal consumption w.r.t. traded goods
MC1_cn = log((exp(c1)^elasTN)^(-1+1/elasTN)*(aN^(1-elasTN)*exp(c_n1)^(elasTN-1))); %marginal consumption w.r.t. non-traded goods
MCT1_ch = log(exp(c_ht1)^(rho-1)*aHT^(1-rho)*((aHT^(1-rho)*exp(c_ht1)^rho)+(aFT^(1-rho)*exp(c_ft1)^rho))^(-1+1/rho)); %marginal traded good consumption w.r.t. Home traded goods
MCT1_cf = log(exp(c_ft1)^(rho-1)*aFT^(1-rho)*((aHT^(1-rho)*exp(c_ht1)^rho)+(aFT^(1-rho)*exp(c_ft1)^rho))^(-1+1/rho)); %marginal traded good consumption w.r.t. Foreign traded goods

nx1=(exp(p_wft1)*exp(c_ft1)-exp(p_wht2)*exp(c_ht2))/GDP1; 
nx2=(exp(p_wht2)*exp(c_ht2)-exp(p_wft1)*exp(c_ft1))/GDP2;


discount_ONEperiod2= discount_ONEperiod1;
MU2_l = MU1_l; 
MU2_c = MU1_c; 
MC2_ct = MC1_ct; 
MC2_cn = MC1_cn; 
MCT2_cf = MCT1_ch; 
MCT2_ch = MCT1_cf; 

z_ht1=1; z_ft2=1; z_n1=1; z_n2=1;
e_ht1=0; e_ft2=0; e_n1=0; e_n2=0;


end;


shocks;

var e_ht1 =  0.047;
var e_ft2 =  0.047;
var e_n1 =  0.009; 
var e_n2 =  0.009;
var e_ht1,e_ft2 =  0.022; 
var e_ht1,e_n1 =  0.009; 
var e_ht1,e_n2 =  0.004;
var e_ft2,e_n1 =  0.004; 
var e_ft2,e_n2 =  0.009;
var e_n1,e_n2 = -0.001;

end;

steady;

% Eigenvalue 0/0 does not pass initial threshold, Modification Line is from https://gist.github.com/stepan-a/5757263 (Date:12Jul2014)
check(qz_zero_threshold=1e-12); // Need to reduce the value of the threshold option to get it work...

stoch_simul(periods=20200, drop=200,hp_filter=1600,order=1, irf=40); %Use Collard & Juillard (2001) algorithm

%----------------------------------------------------------------------------------------------------\
% 5. Some Results
%----------------------------------------------------------------------------------------------------}

% To calculate standard deviation of deviations from steady state divide by steady state
% Relative standard deviations

statistic1 = 100*sqrt(diag(oo_.var(1:55,1:55)))./oo_.mean(1:55);    
dyntable('Relative standard deviations in %',strvcat('VARIABLE','REL. S.D.'),M_.endo_names(1:53,:),statistic1,10,8,4);

% Standard deviations relative to output
statistic21 = statistic1./statistic1(1);
dyntable('standard deviations relative to output of country 1',strvcat('VARIABLE','S.D.'),M_.endo_names(1:53,:),statistic21,10,8,4);

% Standard deviations relative to output
statistic22 = statistic1./statistic1(2);
dyntable('standard deviations relative to output of country 2',strvcat('VARIABLE','S.D.'),M_.endo_names(1:53,:),statistic22,10,8,4);


% plot the impulse responses to a productivity shock
% in percentage deviations from steady state

shock_GDP1 = 100* oo_.irfs.GDP1_e1/oo_.mean(1);
shock_GDP2 = 100* oo_.irfs.GDP2_e1/oo_.mean(2);
shock_c1 = 100* oo_.irfs.c1_e1/oo_.mean(3);
shock_c2 = 100* oo_.irfs.c2_e1/oo_.mean(4);
shock_c_ht1 = 100* oo_.irfs.c_ht1_e1/oo_.mean(5);
shock_c_ht2 = 100* oo_.irfs.c_ht2_e1/oo_.mean(6);
shock_c_ft1 = 100* oo_.irfs.c_ft1_e1/oo_.mean(7);
shock_c_ft2 = 100* oo_.irfs.c_ft2_e1/oo_.mean(8);
shock_c_t1 = 100* oo_.irfs.c_t1_e1/oo_.mean(9);
shock_c_t2 = 100* oo_.irfs.c_t2_e1/oo_.mean(10);
shock_c_n1 = 100* oo_.irfs.c_n1_e1/oo_.mean(11);
shock_c_n2 = 100* oo_.irfs.c_n2_e1/oo_.mean(12);
shock_ih1 = 100* oo_.irfs.ih1_e1/oo_.mean(13);
shock_if2 = 100* oo_.irfs.if2_e1/oo_.mean(14);
shock_bh1 = 100* oo_.irfs.bh1_e1/oo_.mean(15);
shock_bh2 = 100* oo_.irfs.bh2_e1/oo_.mean(16);
shock_k_ht1 = 100* oo_.irfs.k_ht1_e1/oo_.mean(17);
shock_k_ft2 = 100* oo_.irfs.k_ft2_e1/oo_.mean(18);
shock_k_n1 = 100* oo_.irfs.k_n1_e1/oo_.mean(19);
shock_k_n2 = 100* oo_.irfs.k_n2_e1/oo_.mean(20);
shock_k1 = 100* oo_.irfs.k1_e1/oo_.mean(21);
shock_k2 = 100* oo_.irfs.k2_e1/oo_.mean(22);
shock_y_ht1 = 100* oo_.irfs.y_ht1_e1/oo_.mean(23);
shock_y_ft2 = 100* oo_.irfs.y_ft2_e1/oo_.mean(24);
shock_y_n1 = 100* oo_.irfs.y_n1_e1/oo_.mean(25);
shock_y_n2 = 100* oo_.irfs.y_n2_e1/oo_.mean(26);
shock_l1 = 100* oo_.irfs.n1_l1/oo_.mean(27);
shock_l2 = 100* oo_.irfs.n2_l2/oo_.mean(28);
shock_l_ht1 = 100* oo_.irfs.l_ht1_e1/oo_.mean(29);
shock_l_ft2 = 100* oo_.irfs.l_ft2_e1/oo_.mean(30);
shock_l_n1 = 100* oo_.irfs.l_n1_e1/oo_.mean(31);
shock_l_n2 = 100* oo_.irfs.l_n2_e1/oo_.mean(32);
shock_p_wht1 = 100* oo_.irfs.p_wht1_e1/oo_.mean(33);
shock_p_wht2= 100* oo_.irfs.p_wht2_e1/oo_.mean(34);
shock_p_wft1 = 100* oo_.irfs.p_wft1_e1/oo_.mean(35);
shock_p_wft2 = 100* oo_.irfs.p_wft2_e1/oo_.mean(36);
shock_p_ht1 = 100* oo_.irfs.p_ht1_e1/oo_.mean(37);
shock_p_ht2 = 100* oo_.irfs.p_ht2_e1/oo_.mean(38);
shock_p_ft1 = 100* oo_.irfs.p_ft1_e1/oo_.mean(39);
shock_p_ft2 = 100* oo_.irfs.p_ft2_e1/oo_.mean(40);
shock_p_n1 = 100* oo_.irfs.p_n1_e1/oo_.mean(41);
shock_p_n2 = 100* oo_.irfs.p_n2_e1/oo_.mean(42);
shock_p_t1 = 100* oo_.irfs.p_t1_e1/oo_.mean(43);
shock_p_t2 = 100* oo_.irfs.p_t2_e1/oo_.mean(44);
shock_wage_1 = 100* oo_.irfs.wage_1_e1/oo_.mean(45);
shock_wage_2 = 100* oo_.irfs.wage_2_e1/oo_.mean(46);
shock_R_1 = 100* oo_.irfs.R_1_e1/oo_.mean(47);
shock_R_2 = 100* oo_.irfs.R_2_e1/oo_.mean(48);
shock_r_b = 100* oo_.irfs.r_b_e1/oo_.mean(49);
shock_p1 = 100* oo_.irfs.p1_e1/oo_.mean(50);
shock_p2 = 100* oo_.irfs.p2_e1/oo_.mean(51);
shock_TOT = 100* oo_.irfs.TOT_e1/oo_.mean(52);
shock_RER = 100* oo_.irfs.RER_e1/oo_.mean(53);


shock_nx1 = 100* oo_.irfs.nx1_e1;
shock_nx2 = 100* oo_.irfs.nx2_e1;


time = 1:1:40;
figure = plot(time,shock_GDP1,'-*k',time, shock_c1, '-g', time, shock_k1, '--r', time, shock_l1, '-s', time, shock_nx1, 'm-d', time, shock_TOT, 'c-p',time, shock_RER, 'yellow');
title('Impulse Responses to a Shock in Technology', 'FontSize',22);
xlabel('quarters', 'FontSize',18);
ylabel('percentage deviation from steady state', 'FontSize',18);
legend( 'home output GDP1', 'home consumption', 'home capital', 'home labor', 'home net exports', 'terms of trade', 'Relative real exchange rate', 'LOCATION', 'NorthEast');
set(figure,'LineWidth',3);
set(gca, 'FontSize',20);
axis([time(1) time(40) -0.5 1.5]);
print('-dpdf','incomp_bench');



pause;
disp('Press a key to continue ...')


time = 1:1:40;
figure = plot(time,shock_GDP2,'-*k',time, shock_c2, '-g', time, shock_k2, '--r', time, shock_l2, '-s' );
title('Impulse Responses to a Shock in Technology', 'FontSize',22);
xlabel('quarters', 'FontSize',18);
ylabel('percentage deviation from steady state', 'FontSize',18);
legend( 'foreign output GDP2', 'foreign consumption', 'foreign capital', 'foreign labor', 'LOCATION', 'NorthEast');
set(figure,'LineWidth',3);
set(gca, 'FontSize',20);
axis([time(1) time(40) -0.1 0.25]);
print('-dpdf','incomp_bench_fo');
