%------------------------------------------------------------------------------------------%
% MODELO de Economia Abierta
%------------------------------------------------------------------------------------------%
% PUCP
% Tópicos de Econometría
% Junio 2013
%------------------------------------------------------------------------------------------%
% Jose Carlos Gonzales Tanaka                                                                          
%------------------------------------------------------------------------------------------%
%===========================================================================================

%------------------------------------------------------------------------------------------%

% VARIABLES (9)                                                                        
%------------------------------------------------------------------------------------------%

var
y %product
c %consumption
k %capital
h %labor
m %money
b %international assets
p %domestic prices
e %exchange rate
r %rental of capital
w %real wage
rf %return on international assets
x %net exports

pf %foreign prices
prod %productivity
g %gross growth of money
;

varexo e1, e2, e3;

%------------------------------------------------------------------------------------------%
% PARAMETERS                                                                       
%------------------------------------------------------------------------------------------%
parameters

r_ast
beta
delta
theta
a % derivative of international return with respect to international assets
kappa

A
ho
gamma_prod
gamma_g
gamma_pf

sigma1 %Shocks error standars
sigma2
sigma3

B

y_ss 
c_ss 
k_ss
h_ss
m_ss
b_ss
p_ss
e_ss
r_ss
w_ss
rf_ss
x_ss

prod_ss
g_ss
pf_ss
;

%------------------------------------------------------------------------------------------%
% CALIBRATION                                                                      
%------------------------------------------------------------------------------------------%
%1)PREFERENCES
%--------------
A = 1.72;
ho = 0.53;
beta = 0.99;
delta = 0.025;
B = (A*log(1-ho))/ho;

%2)FIRMS
%--------------
theta = 0.36;
kappa = 0.95;   %parametro de ajuste del capital

%3)OPEN ECONOMY
%--------------
a = 0.01; %Derivada de la tasa de interes internacional con respecto a los
          %activos internacionales
r_ast = 0.03;
         
%4)SHOCKS
%--------------
gamma_prod = 0.95;
gamma_g = 0.95;
gamma_pf = 0.95;

sigma1 = 0.00712;
sigma2 = 0.00712;
sigma3 = 0.00712;

%------------------------------------------------------------------------------------------%
% STEADY STATE                                                                   
%------------------------------------------------------------------------------------------%
prod_ss = 1;
g_ss = 1;
pf_ss = 1;
p_ss = 1;
pi = 1;


r_ss = 1/beta - (1 - delta);
rf_ss = 1/beta - 1;
b_ss = (r_ast + 1 - (1/beta))/a;
x_ss = -(rf_ss*b_ss);
w_ss = (1 - theta)*(theta/r_ss)^(theta/(1 - theta));
c_ss = (beta*w_ss)/(-B*pi);
m_ss = p_ss*c_ss;
k_ss = theta*((m_ss/p_ss) - rf_ss*b_ss)/(r_ss - theta*delta);
h_ss = (r_ss*(1 - theta))*k_ss/(w_ss*theta);
y_ss = c_ss + delta*k_ss + x_ss;
e_ss = p_ss;

%================================
% MODEL
%================================
model;
%================================
% Families 
%================================
% 1
e/(p(+1)*c(+1)) = beta*(e(+1)*(1 + rf))/(p(+2)*c(+2));
% 2
(p/(p(+1)*c(+1)))*(1 + kappa*(k - k(-1))) = beta*(p(+1)/(p(+2)*c(+2)))*(r(+1) + (1 - delta) + kappa*(k(+1) - k));
% 3
B/w = -beta*(p/(p(+1)*c(+1)));
% 4
p*c = m;
% 5
m/p + (e*b)/p + k + (kappa/2)*((k - k(-1))^2) = w*h + r*k(-1) + (1 - delta)*k(-1) + (e*(1 + rf(-1))*b(-1))/p;

%================================
% Firms
%================================
% 6
w = (1-theta)*prod*((k(-1))^(theta))*(h^(-theta));
% 7
r = theta*prod*(k(-1)^(theta - 1))*(h^(1 - theta));

%================================
% Open economy conditions
%================================
% 8 
pf*x = b - (1 + rf(-1))*b(-1);
% 9
rf = r_ast - (a*b)/pf;
% 10
e = p/pf;

%================================
% Money condition
%================================
% 11
m = g*m(-1);  

%================================
% Market clearing 
%================================
% 12
y = c + k - (1 - delta)*k(-1) + x;  
%================================
% Uncertainty laws of motion 
%================================
% 13
ln(prod) = (gamma_prod)*ln(prod(-1)) + e1;
% 14
ln(g) = (gamma_g)*ln(g(-1)) + e2;
% 15
ln(pf) = (gamma_pf)*ln(pf(-1)) + e3;

end;
%------------------------------------------------------------------------------------------%
% VALORES INICIALES
%------------------------------------------------------------------------------------------%
initval;
c = c_ss;
k = k_ss;
h = h_ss;
m = m_ss;
b = b_ss;
p = p_ss;
e = e_ss;
y = y_ss;
r = r_ss;
w = w_ss;
rf = rf_ss; 
x = x_ss;

prod = prod_ss;
g = g_ss;
pf = pf_ss;

end;

resid(1);
steady;

%------------------------------------------------------------------------------------------%
% SHOCKS                                                                         
%------------------------------------------------------------------------------------------%
shocks;
var e1;
stderr sigma1;
var e2;
stderr sigma2;
var e3;
stderr sigma3;
end;

check;

%------------------------------------------------------------------------------------------%

%=======Impulse Response Functions of the same gamma values========
stoch_simul(nomoments,nograph,order=1,irf=100); 
oo1=oo_;

%=======Impulse Reponse Functions of different gamma values========
gamma_prod = 0.5;
gamma_g = 0.5;
gamma_pf = 0.5;
stoch_simul(nomoments,nograph,order=1,irf=100);
oo2=oo_;

%=========Timing===================================================
time = 1:1:100;

%==========All Variables in one Graph==================

%==========Variables to shock of Productivity==========
subplot(1,3,1)
plot(time,oo1.irfs.y_e1,time,oo1.irfs.c_e1,time,oo1.irfs.k_e1,...
     time,oo1.irfs.h_e1,time,oo1.irfs.m_e1,time,oo1.irfs.b_e1,...
     time,oo1.irfs.p_e1,time,oo1.irfs.e_e1,time,oo1.irfs.r_e1,...
     time,oo1.irfs.w_e1,time,oo1.irfs.rf_e1,time,oo1.irfs.x_e1,'--')
legend('y','c','k','h','m','b','p','e','r','w','rf','x');
title('Variables to shock e1(prod)')
 
%==========Variables to shock of money growth==========
subplot(1,3,2)
plot(time,oo1.irfs.y_e2,time,oo1.irfs.c_e2,time,oo1.irfs.k_e2,...
     time,oo1.irfs.h_e2,time,oo1.irfs.m_e2,time,oo1.irfs.b_e2,...
     time,oo1.irfs.p_e2,time,oo1.irfs.e_e2,time,oo1.irfs.r_e2,...
     time,oo1.irfs.w_e2,time,oo1.irfs.rf_e2,time,oo1.irfs.x_e2,'--')
legend('y','c','k','h','m','b','p','e','r','w','rf','x');
title('Variables to shock e2(g)')
 
%==========Variables to shock of foreign prices==========
subplot(1,3,3)
plot(time,oo1.irfs.y_e3,time,oo1.irfs.c_e3,time,oo1.irfs.k_e3,...
     time,oo1.irfs.h_e3,time,oo1.irfs.m_e3,time,oo1.irfs.b_e3,...
     time,oo1.irfs.p_e3,time,oo1.irfs.e_e3,time,oo1.irfs.r_e3,...
     time,oo1.irfs.w_e3,time,oo1.irfs.rf_e3,time,oo1.irfs.x_e3,'--')
legend('y','c','k','h','m','b','p','e','r','w','rf','x');
title('Variables to shock e1(pf)')

