% SIMPLE TWO - COUNTRY MODEL
var cih cif kih kif bih bif qh qf rkh rkf  muh muf wh wf rbh rbf ah af yh yf ;
varexo nuih nuif;

parameters ALFA BETAi RHO_a SIGMA Kappahs  Kappafs  SIGMA_nuih SIGMA_nuif;

ALFA = 0.36;
BETAi = 0.97;
RHO_a = 0.95;
SIGMA = 2;
SIGMA_nuih = 0.006;
Kappahs = 0.75;
SIGMA_nuif = 0.006;
Kappafs = 0.75;
CORR_nuih_nuif = 0.00;
% Steady states - levels

ah_s = 1;
kih_s =1;
rbh_s = (1/BETAi)*(1 - (0.02*BETAi));
rkh_s = ALFA*kih_s^(ALFA-1);
wh_s = (1-ALFA)*kih_s^ALFA;
yh_s = kih_s^ALFA;
qh_s = (BETAi*rkh_s)/((1-BETAi)-0.02*BETAi*Kappahs);
bih_s =  Kappahs*qh_s*kih_s;
%cih_s = yh_s;
cih_s = wh_s + rkh_s*kih_s + bih_s*(1-rbh_s);

muh_s = 0.02*BETAi*(cih_s)^(-SIGMA);


af_s = 1;
kif_s =1;
rbf_s = rbh_s; 
%(1/BETAi)*(1 - (0.02*BETAi));
rkf_s = ALFA*kif_s^(ALFA-1);
wf_s = (1-ALFA)*kif_s^ALFA;
yf_s = kif_s^ALFA;
qf_s = (BETAi*rkf_s)/((1-BETAi)-0.02*BETAi*Kappafs);
bif_s =  Kappafs*qf_s*kif_s;
%cif_s = yf_s;
cif_s = wf_s + rkf_s*kif_s + bif_s*(1-rbf_s);
muf_s = 0.02*BETAi*(cif_s)^(-SIGMA);



cih_s = log(cih_s);
kih_s = log(kih_s);
yh_s = log(yh_s);
rkh_s = log(rkh_s);
rbh_s = log(rbh_s);
wh_s = log(wh_s);
ah_s = log(ah_s);
bih_s = bih_s;
qh_s= log(qh_s);
muh_s = muh_s;

cif_s = log(cif_s);
kif_s = log(kif_s);
yf_s = log(yf_s);
rkf_s = log(rkf_s);
rbf_s = log(rbf_s);
wf_s = log(wf_s);
af_s = log(af_s);
bif_s = bif_s;
qf_s= log(qf_s);
muf_s = muf_s;

model;
% budget constraint investors
%1
exp(cih)+(exp(qh)*exp(kih)) = exp(wh)+(exp(qh)+exp(rkh))*exp(kih(-1))+ bih- exp(rbh(-1))*bih(-1); 
exp(cif)+(exp(qf)*exp(kif)) = exp(wf)+(exp(qf)+exp(rkf))*exp(kif(-1))+ bif- exp(rbf(-1))*bif(-1); 
%borrowing constraint  
%2

bih = Kappahs*exp(qh)*exp(kih); 
bif = Kappafs*exp(qf)*exp(kif); 
% FOC  wrt to kih 
%3

exp(cih)^(-SIGMA)= BETAi *exp(cih(+1))^(-SIGMA)*((exp(qh(+1))+exp(rkh(+1)))/exp(qh)) + Kappahs*muh; 
exp(cif)^(-SIGMA)= BETAi *exp(cif(+1))^(-SIGMA)*((exp(qf(+1))+exp(rkf(+1)))/exp(qf)) + Kappafs*muf; 
% FOC wrt to bih 
%4
exp(cih)^(-SIGMA) = BETAi * exp(cih(+1))^(-SIGMA)*exp(rbh) + muh; 
exp(cif)^(-SIGMA) = BETAi * exp(cif(+1))^(-SIGMA)*exp(rbf) + muf; 
% FOC wrt to k in the production function 
%5

exp(rkh)= (ALFA)*exp(ah)*(exp(kih(-1)))^(ALFA-1); 
exp(rkf)= (ALFA)*exp(af)*(exp(kif(-1)))^(ALFA-1);
% FOC  wrt to l in the production function equation 15
%6
exp(wh)= (1-ALFA)*(exp(ah)*(exp(kih(-1)))^(ALFA)); 
%exp(wf)= (1-ALFA)*(exp(af)*(exp(kif(-1)))^(ALFA)); 

%exp(cih)^(-SIGMA) = exp(cif)^(-SIGMA); 
%7 AR(1)
ah = RHO_a*ah(-1)- nuih;
af = RHO_a*af(-1)- nuif;
%8 Market clearing condition
%exp(cih)= exp(yh);
%exp(cif)= exp(yf);
exp(cih)+ exp(cif)= exp(yh)+ exp(yf);
%9 production function
exp(yh)= exp(ah)*(exp(kih(-1)))^ALFA;
exp(yf)= exp(af)*(exp(kif(-1)))^ALFA;
%10 bond clearing condition
%bih = 0;
%bif = 0;
%11 labor  clearing condition
%lh =1 ; 
muh = 0.02*BETAi*(exp(cih(+1)))^(-SIGMA);
muf = 0.02*BETAi*(exp(cif(+1)))^(-SIGMA);
% capital market clearing condition 
exp(kih) = 1;
exp(kif) = 1;
end; 

initval;
cih = cih_s ;
kih = kih_s ;
bih = bih_s;  
qh = qh_s;
rkh = rkh_s;
muh = muh_s; 
wh = wh_s;
%lh =lh_s;
rbh  = rbh_s;
ah = ah_s;
yh =yh_s;
nuih = 0;
ah = ah_s;

cif = cih_s ;
kif = kih_s ;
bif = bih_s;  
qf = qh_s;
rkf = rkh_s;
muf = muh_s; 
wf = wh_s;
%lf =lh_s;
rbf  = rbh_s;
yf =yf_s;
nuif = 0;
af = af_s;
end;

steady;
resid (1);
check;
shocks;
var nuih =SIGMA_nuih^2;
var nuif =SIGMA_nuif^2;
var nuih,nuif =SIGMA_nuih*SIGMA_nuif*CORR_nuih_nuif;
end;
stoch_simul(dr_algo=0, order =1,irf=0, hp_filter=1600);