var 

Lame Lamc                             % SDF
Ae Ac                               % TFP
Ce Cc Ge Gc Ke Kc ue uc         % Consumption, Government Spending, Capital, Utilization
Ie Ic Ihe Ihc                            % Investment, Effective Investment
Ye Yc Ywe Ywc                             % Aggregate demand, Aggregate supply
Le Lc Lde Ldc                            % Labor supply, Labor demand
we wc wre wrc                            % Wage, Optimal wage
mrse mrsc                            % Marginal Rate of Substitution of labor
Pie Pic Pire Pirc                          % Inflation, Inflation set by optimal price setting
pke pkc                              % Capital price
pwe pwc                              % Wholesale good price

f1e f1c                              % Wage setting f1 
f2e f2c                              % Wage setting f2
vwe vwc                             % Wage dispersion 
x1e x1c                              % Price setting x1 
x2e x2c                              % price setting x2                             
vpe vpc                              % Price dispersion

M1e M1c M2e M2c                           % Auxiliary variables for intermediary problem
Omegae Omegac                           % Auxiliary variable for intermediary problem
mue muc                              % Lagrangian multiplier of budget constraint (marginal utility)
lambdae lambdac                          %

fwe fwc                              % Total private bond issurance
fe fc                               % Private bond held by intermediary
fcbe fcbc                             % Private bond held by central bank
Qe Qc                               % Bond price 
ne nc                               % Net worth
dc                               % Deposit
be 
V
thetae thetac                           
phie phic                               % Leverage ratio
 
Rfe Rfc                             % Private bond yield
Rde 
Rdc                              % Deposit rate
Rtre Rtrc                             % Policy rate
Sp1e 
Sp1c                             % Spread between Rf and Rd

rer Pe Pc Cpie Cpic Rv S FX
   
logYe logYc logCe logCc logIe logIc logLde logLdc logCpie logCpic logRdc 
exre 
logRde exrc logQe logQc;

varexo 
eAe eAc                              % Technology shock
ere erc                              % Monetary policy shock
ete etc                              % Credit shock 1
et2e et2c                             % Credit shock 2
efe efc                              % QE shock                           
;

parameters

beta                            % Discount coefficient
kappa                           % Bonds decaying factor 
spF                             % Spread of private bonds set
psi                             % Incentive constraint 
epsp                            % Elasticity of substitution for price setting
epsw                            % Elasticity of substitution for wage
alpha                           % Production input factor
delta0                          % Depreciation parameter 1
delta1                          % Depreciation parameter 2
delta2                          % Depreciation parameter 3
g                               % Government spending ratio
bb                              % Habit formation
sigma                           % Bank survival probability
upse upsc
v
Delta
deltaT 

etap
m
phip phiw                       % Calvo price for price and wage setting
psik                            % Investment adjustment cost coefficient


eta                             % Frisch elasticity
rhor                            % Coefficient for AR(1) Taylor rule
phipi                           % Coefficient for taylor rule on inflation
phiy                            % Coefficient for taylor rule on output gap                          
sr                              % Standard deviation of monetary policy shock

rhof                            % Coefficient for AR(1) central bank private bond holding
sf                              % Standard deviation of QE shock
rhoA                            % Coefficient for AR(1) technology shock
sA                              % Standard deviation of techonology shock 
rhot                            % Coefficient for AR(1) credit shock
st                              % Standard deviation of credit shock
indp indw 


Rds                             % Gross rate of deposit
Rfs                             % Gross rate of private bonds
Rvs
Rtrs
Qs                              % Bond price
M1s                             % Auxiliary variable for intermediary problem
M2s                             % Auxiliary variable for intermediary problem                           

pws 
Ks Ys Is Gs ws fws fcbs mrss fs 
Pes nes phies lambdaes thetaes 
Omegaes rers
Pcs Vs ncs dcs phics lambdacs 
thetacs Omegacs Ccs Ces bes
mues mucs
chie chic;      



% load param_sw

set_param_value('beta',beta);
set_param_value('kappa',kappa);
set_param_value('spF',spF);
set_param_value('psi',psi);
set_param_value('epsp',epsp);
set_param_value('epsw',epsw);
set_param_value('alpha',alpha);
set_param_value('delta0',delta0);
set_param_value('delta1',delta1);
set_param_value('delta2',delta2);
set_param_value('g',g);
set_param_value('bb',bb);
set_param_value('sigma',sigma);
set_param_value('upse',upse);
set_param_value('upsc',upsc);
set_param_value('v',v);
set_param_value('Delta',Delta);
set_param_value('deltaT',deltaT);
set_param_value('etap',etap);
set_param_value('m',m);
set_param_value('phip',phip);
set_param_value('phiw',phiw);
set_param_value('psik',psik);
set_param_value('eta',eta);
set_param_value('rhor',rhor);
set_param_value('phipi',phipi);
set_param_value('phiy',phiy);
set_param_value('sr',sr);
set_param_value('rhof',rhof);
set_param_value('sf',sf);
set_param_value('rhoA',rhoA);
set_param_value('sA',sA);
set_param_value('rhot',rhot);
set_param_value('st',st);
set_param_value('eta',eta);
set_param_value('indp',indp);
set_param_value('indw',indw);


set_param_value('Rds',Rds);
set_param_value('Rfs',Rfs);
set_param_value('Rvs',Rvs);
set_param_value('Rtrs',Rtrs);
set_param_value('Qs',Qs);
set_param_value('M1s',M1s);
set_param_value('M2s',M2s);
set_param_value('pws',pws);
set_param_value('Ks',Ks);
set_param_value('Ys',Ys);
set_param_value('Is',Is);
set_param_value('Gs',Gs);
set_param_value('ws',ws);
set_param_value('fws',fws);
set_param_value('fcbs',fcbs);
set_param_value('mrss',mrss);
set_param_value('fs',fs);
set_param_value('Pes',Pes);
set_param_value('nes',nes);
set_param_value('phies',phies);
set_param_value('lambdaes',lambdaes);
set_param_value('thetaes',thetaes);
set_param_value('Omegaes',Omegaes);
set_param_value('rers',rers);
set_param_value('Pcs',Pcs);
set_param_value('Vs',Vs);
set_param_value('ncs',ncs);
set_param_value('dcs',dcs);
set_param_value('phics',phics);
set_param_value('lambdacs',lambdacs);
set_param_value('thetacs',thetacs);
set_param_value('Omegacs',Omegacs);
set_param_value('Ccs',Ccs);
set_param_value('Ces',Ces);
set_param_value('bes',bes);
set_param_value('mues',mues);
set_param_value('mucs',mucs);
set_param_value('chie',chie);
set_param_value('chic',chic);

model;
%%%%%%
% Household
%%%%%%

% (1) SDF
Lame = beta*mue/mue(-1);
Lamc = beta*muc/muc(-1);

% (2) mu
mue = (Ce - bb*Ce(-1))^(-1) - beta*bb*(Ce(+1) - bb*Ce)^(-1);
muc = (Cc - bb*Cc(-1))^(-1) - beta*bb*(Cc(+1) - bb*Cc)^(-1);

% (3) Labor supply
chie*Le^(eta) = mrse*mue;
chic*Lc^(eta) = mrsc*muc;

% EME Euler Equation
 %1 = Rde*Lame(1)*Cpie(1)^(-1);
 Rde/Cpie(1) = Rdc*rer(1)/(rer*Cpic(1));

% (4) Bonds
 1 = (Rdc(-1)*Lame*rer)/(rer(-1)*Cpic);
%1 = (Rdc*Lame(1)*rer(1))/(rer*Cpic(1));
 1 = Rdc*Lamc(1)*Cpic(1)^(-1);

% (5) Wage-setting
wre = (epsw/(epsw-1))*f1e/f2e;
wrc = (epsw/(epsw-1))*f1c/f2c;

% (6) f1
f1e = mrse*we^(epsw)*Lde + phiw*Lame(+1)*(Pie(+1)/Cpie^indp)^(epsw)*f1e(+1);
f1c = mrsc*wc^(epsw)*Ldc + phiw*Lamc(+1)*(Pic(+1)/Cpic^indp)^(epsw)*f1c(+1);

% (7) f2
f2e = we^(epsw)*Lde + phiw*Lame(+1)*(Pie(+1)/Cpie^indp)^(epsw-1)*f2e(+1);
f2c = wc^(epsw)*Ldc + phiw*Lamc(+1)*(Pic(+1)/Cpic^indp)^(epsw-1)*f2c(+1);

%%%%%
% Investment firm
%%%%%

% (8) Ihat
Ihe = (1 - (psik/2)*(Ie/Ie(-1) - 1)^2)*Ie;
Ihc = (1 - (psik/2)*(Ic/Ic(-1) - 1)^2)*Ic;

% (9) FOC I
1 = pke*(1 - (psik/2)*(Ie/Ie(-1) - 1)^2 - psik*(Ie/Ie(-1) - 1)*(Ie/Ie(-1))) 
+ Lame(+1)*pke(+1)*psik*(Ie(+1)/Ie - 1)*(Ie(+1)/Ie)^2;
1 = pkc*(1 - (psik/2)*(Ic/Ic(-1) - 1)^2 - psik*(Ic/Ic(-1) - 1)*(Ic/Ic(-1))) 
+ Lamc(+1)*pkc(+1)*psik*(Ic(+1)/Ic - 1)*(Ic(+1)/Ic)^2;

%%%%%%
% Retail firm
%%%%%%

% (10) Reset inflation
Pire = (epsp/(epsp - 1))*x1e/x2e;
Pirc = (epsp/(epsp - 1))*x1c/x2c;

% (11) x1
x1e = pwe*Ye + phip*Lame(+1)*(Pie(+1)/Cpie^indw)^(epsp)*x1e(+1);
x1c = pwc*Yc + phip*Lamc(+1)*(Pic(+1)/Cpic^indw)^(epsp)*x1c(+1);

% (12) x2
x2e = Ye + phip*Lame(+1)*(Pie(+1)/Cpie^indw)^(epsp-1)*x2e(+1);
x2c = Yc + phip*Lamc(+1)*(Pic(+1)/Cpic^indw)^(epsp-1)*x2c(+1);

%%%%%%%
% Wholesale firm
%%%%%%%%%

% (13) Labor demand
we = (1-alpha)*pwe*Ae*(ue*Ke(-1))^(alpha)*Lde^(-alpha);
wc = (1-alpha)*pwc*Ac*(uc*Kc(-1))^(alpha)*Ldc^(-alpha);

% (14) Utilization
pke*M2e*(delta1 + delta2*(ue - 1)) = alpha*pwe*Ae*(ue*Ke(-1))^(alpha-1)*Lde^(1-alpha);
pkc*M2c*(delta1 + delta2*(uc - 1)) = alpha*pwc*Ac*(uc*Kc(-1))^(alpha-1)*Ldc^(1-alpha);

% (15) Capital
pke*M2e = Lame(+1)*(alpha*pwe(+1)*Ae(+1)*(ue(+1)*Ke)^(alpha-1)*ue(+1)*Lde(+1)^(1-alpha) 
+ (1-delta0-delta1*(ue(+1) - 1)-(delta2/2)*(ue(+1) - 1)^2)*pke(+1)*M2e(+1));

pkc*M2c = Lamc(+1)*(alpha*pwc(+1)*Ac(+1)*(uc(+1)*Kc)^(alpha-1)*uc(+1)*Ldc(+1)^(1-alpha) 
+ (1-delta0-delta1*(uc(+1) - 1)-(delta2/2)*(uc(+1) - 1)^2)*pkc(+1)*M2c(+1));

% (16) Bonds
Qe*M1e = Lame(+1)*Cpie(+1)^(-1)*(1+kappa*Qe(+1)*M1e(+1));
Qc*M1c = Lamc(+1)*Cpic(+1)^(-1)*(1+kappa*Qc(+1)*M1c(+1));

% (17) M1 and M2
(M1e-1)/(M2e-1) = 1/psi;
(M1c-1)/(M2c-1) = 1/psi;

% (18) Production
Ywe = Ae*(ue*Ke(-1))^(alpha)*Lde^(1-alpha);
Ywc = Ac*(uc*Kc(-1))^(alpha)*Ldc^(1-alpha);

% (19) Capital acumulation
Ke = Ihe + (1-delta0-delta1*(ue-1)-(delta2/2)*(ue-1)^2)*Ke(-1);
Kc = Ihc + (1-delta0-delta1*(uc-1)-(delta2/2)*(uc-1)^2)*Kc(-1);

% (20) Loan in advance
psi*pke*Ihe = Qe*(fwe - kappa*Cpie^(-1)*fwe(-1));
psi*pkc*Ihc = Qc*(fwc - kappa*Cpic^(-1)*fwc(-1));




%%%%%%%%%%%%%%%
% Financial Intermediaries
%%%%%%%%%%%%%%%

%% EME country (Local Bank)

% FOC to f
 Lame(1)*Omegae(+1)*(Rfe(1)/Cpie(1)-rer(1)/rer*Rv/Cpic(1)) = thetae*lambdae/(1+lambdae);

% Omega
 Omegae = 1- sigma + sigma*phie*thetae;

% Phi
 phie = (1+lambdae)/thetae*Lame(1)*Omegae(1)*rer(1)/rer*Rv/Cpic(1);

% Balance sheet
Qe*fe = rer*V + ne;

% Net Worth Evolvement;
 ne = sigma*((Rfe/Cpie-rer/rer(-1)*Rv(-1)/Cpic)*Qe(-1)*fe(-1)+rer/rer(-1)*Rv(-1)/Cpic*ne(-1)) + deltaT*Qe(-1)*fe(-1);
% ne(1) = sigma*((Rfe(1)/Cpie(1)-rer(1)/rer*Rv/Cpic(1))*Qe*fe+rer(1)/rer*Rv/Cpic(1)*ne) + deltaT*Qe*fe;

phie = Qe*fe/ne;

%% Centre Country (Global Bank)

% FOC to f
Lamc(1)/Cpic(1)*Omegac(1)*(Rfc(1)-Rdc) = thetac*lambdac/(1+lambdac);

% FOC to V
Lamc(1)/Cpic(1)*Omegac(1)*(Rv(1)-Rdc) = Delta*thetac*lambdac/(1+lambdac);

% Omega
Omegac = 1-sigma+sigma*thetac*phic;

% Phi
phic = (1+lambdac)/thetac*Lamc(1)*Omegac(1)/Cpic(1)*Rdc;

% Balance Sheet
(m/(1-m))*V + Qc*fc = nc + dc;

% Net Worth evolvement
 nc = sigma/Cpic*((Rfc-Rdc(-1))*Qc(-1)*fc(-1)+(m/(1-m))*(Rv-Rdc(-1))*V(-1)+Rdc(-1)*nc(-1)) + deltaT*Qc(-1)*fc(-1);
% nc(1) = sigma/Cpic(1)*((Rfc(1)-Rdc)*Qc*fc+(Rv(1)-Rdc)*V+Rdc*nc) + deltaT*Qc*fc;

phic = (Delta*V*m/(1-m)+Qc*fc)/nc;

%%%%%%
%% Central Bank
%%%%%%%%%

% (27) Taylor rule
log(Rtre) = (1-rhor)*log(Rds) + rhor*log(Rtre(-1)) + (1-rhor)*(phipi*log(Cpie) + phiy*(log(Ye) - log(Ye(-1)))) + sr*ere;
log(Rtrc) = (1-rhor)*log(Rds) + rhor*log(Rtrc(-1)) + (1-rhor)*(phipi*log(Cpic) + phiy*(log(Yc) - log(Yc(-1)))) + sr*erc;

% (28)
Rtre = Rde;
Rtrc = Rdc;

% (29) Private holdings
fcbe = (1-rhof)*fcbs + rhof*fcbe(-1) + sf*efe;
fcbc = (1-rhof)*fcbs + rhof*fcbc(-1) + sf*efc;

%%%%%%%%%%%
% Aggregate conditions
%%%%%%%%%%

% (30) Price evolution
1 = (1-phip)*Pire^(1-epsp) + phip*Pie^(epsp-1);
1 = (1-phip)*Pirc^(1-epsp) + phip*Pic^(epsp-1);

% (31) Wage evolution
we^(1-epsw) = (1-phiw)*wre^(1-epsw) + phiw*Pie^(epsw-1)*we(-1)^(1-epsw);
wc^(1-epsw) = (1-phiw)*wrc^(1-epsw) + phiw*Pic^(epsw-1)*wc(-1)^(1-epsw);

% (32) Aggregate output
Ywe = Ye*vpe;
Ywc = Yc*vpc;

% (33) Price dispersion
vpe = (1-phip)*Pire^(-epsp) + phip*Pie^(epsp)*vpe(-1);
vpc = (1-phip)*Pirc^(-epsp) + phip*Pic^(epsp)*vpc(-1);

% (34) Labor supply / demand
Le = Lde*vwe;
Lc = Ldc*vwc;

% (35) Wage dispersion
vwe = (1-phiw)*(wre/we)^(-epsw) + phiw*(we/we(-1))^(epsw)*Cpie^(epsw)*vwe(-1);
vwc = (1-phiw)*(wrc/wc)^(-epsw) + phiw*(wc/wc(-1))^(epsw)*Cpic^(epsw)*vwc(-1);

% (36) Market-clearing private bonds
fwe = fe + fcbe;
fwc = fc + fcbc;

% (37) Aggregate resource constraint
% Ye = Ce + Ihe + Ge;
% Yc = Cc + Ic + Gc;

% (38) Return private bonds
Rfe = (1+kappa*Qe)/Qe(-1);
Rfc = (1+kappa*Qc)/Qc(-1);

% (39) Productivity shock
log(Ae) = rhoA*log(Ae(-1)) + sA*eAe;
log(Ac) = rhoA*log(Ac(-1)) + sA*eAc;

% (40) Credit shock
log(thetae) = (1-rhot)*log(thetaes) + rhot*log(thetae(-1)) + st*ete + st*et2e;
log(thetac) = (1-rhot)*log(thetacs) + rhot*log(thetac(-1)) + st*etc + st*et2c;

% (41) Government Spending
Ge = g*Ye;
Gc = g*Yc;

% (42) excess return
 exre = 400*(log(Rfe(+1)) - log(Rde));
 exrc = 400*(log(Rfc(+1)) - log(Rdc));

% (43) log output
logYe = log(Ye);
logYc = log(Yc);

% (44) log consumption
logCe = log(Ce);
logCc = log(Cc);

% (45) log investment
logIe = log(Ie);
logIc = log(Ic);

% (46) Log Labor
logLde = log(Lde);
logLdc = log(Ldc);

% (47) Net inflation
logCpie = 400*log(Cpie);
logCpic = 400*log(Cpic);

% (48) Net deposit rate
logRde = 400*log(Rde);
logRdc = 400*log(Rdc);

% (49) Log Q
 logQe = log(Qe);
 logQc = log(Qc);

% (50) Spread of Rf and Rd
 Sp1e = Rfe(1)-Rde;
 Sp1c = Rfc(1)-Rdc;

1 = upse*Pe^(1-etap) + (1-upse)*(Pc*rer)^(1-etap);
1 = upsc*Pc^(1-etap) + (1-upsc)*(Pe/rer)^(1-etap);

 Pe*Cpie = Pe(-1)*Pie;
 Pc*Cpic = Pc(-1)*Pic;

% Pe(1)*Cpie(1) = Pe*Pie(1);
% Pc(1)*Cpic(1) = Pc*Pic(1);

% Aggregate demand
%  Cc(1) + dc(1) = mrsc(1)*Lc(1) + dc*Rdc/(Cpic(1))-deltaT*(Qc*fc);
%  Ce(1) + rer(1)*be(1) = mrse(1)*Le(1) + rer(1)*Rdc*be/(Cpic(1))-deltaT*(Qe*fe);

 % Ce + rer*be = mrse*Le + rer*be(-1)*Rdc(-1)/(Cpic)-deltaT*Qe(-1)*fe(-1);

  Ce + rer*be = mrse*Le + rer*be(-1)*Rdc(-1)/(Cpic)-deltaT*Qe*fe+(1-sigma)*ne;

 %Cc + dc = mrsc*Lc + dc(-1)*Rdc(-1)/Cpic-deltaT*Qc(-1)*fc(-1);
   %            +(1-sigma)*(Rfc*Qc(-1)*fc(-1)+Rv*V(-1)-Rdc*dc(-1))

 Ye = upse*Pe^(-etap)*(Ce+Ihe+Ge) + (1-upsc)*((1-m)/m)*(Pe/rer)^(-etap)*(Cc+Ihc+Gc);
 Yc = (1-upse)*(m/(1-m))*(Pc*rer)^(-etap)*(Ce+Ihe+Ge)+upsc*Pc^(-etap)*(Cc+Ihc+Gc);

S = rer*Pe/Pc;

FX = log(S(1))-log(S);

end;

initval;

Rdc = Rds; Rde = Rds;
Rfe = Rfs; Rfc = Rfs;
Rv = Rvs; 
Rtre = Rtrs; Rtrc = Rtrs;
Qe = Qs; Qc = Qs;
M1e = M1s; M1c = M1s;
M2e = M2s; M2c = M2s;
pwe = pws; pwc = pws;
Ke = Ks; Kc = Ks;
Ye = Ys; Yc = Ys;
Ie = Is; Ic = Is;
Ge = Gs; Gc = Gs;
we = ws; wc = ws;
fwe = fws; fwc = fws;
fcbe = fcbs; fcbc = fcbs;
mrse = mrss; mrsc = mrss;
fe = fs; fc = fs;
Pe = Pes; 
ne = nes;
phie = phies;
lambdae = lambdaes;
thetae = thetaes;
Omegae = Omegaes;
rer = rers;
Pc = Pcs;
V = Vs;
nc = ncs;
dc = dcs;
phic = phics;
lambdac = lambdacs;
thetac = thetacs;
Omegac = Omegacs;
Cc = Ccs; Ce = Ces;
% be = bes;
mue = mues; muc = mucs;

Lame = beta; Lamc = beta;
Ywe = Ys; Ywc = Ys;
Le = 1; Lc = 1;
Lde = 1; Ldc = 1;
vpe = 1; vpc = 1;
vwe = 1; vwc = 1;
Ihe = Is; Ihc = Is;
ue = 1; uc = 1;
wre = ws; wrc = ws;
Pie = 1; Pic = 1;
Cpie = 1; Cpic = 1;
Pire = 1; Pirc = 1;
pke = 1; pkc = 1;
f1e = (mrss*ws^(epsw))/(1-phiw*beta); f1c = (mrss*ws^(epsw))/(1-phiw*beta);
f2e = (ws^(epsw)/(1-phiw*beta)); f2c = (ws^(epsw)/(1-phiw*beta));
x1e = (pws*Ys)/(1-phip*beta); x1c = (pws*Ys)/(1-phip*beta);
x2e = Ys/(1-phip*beta); x2c = Ys/(1-phip*beta);
Rtre = Rds; Rtrc = Rds;

Ae = 1; Ac = 1;
% exre = 400*(log(Rfs) - log(Rds)); 
exrc = 400*(log(Rfs) - log(Rds));
logYe = log(Ys); logYc = log(Ys);
logIe = log(Is); logIc = log(Is);
logCe = log(Ces); logCc = log(Ccs);
logLde = 0; logLdc = 0;
logCpie = 0; logCpic = 0;
logRde = 400*log(Rds); 
logRdc = 400*log(Rds);
logQe = log(Qs); logQc = log(Qs);

end;

steady;

shocks;
 %var eAe = 1;
 %var eAc = .1;
 var ere = 0.1;
 var erc = 0.1;
 %var et = 1;
 %var efe = 1;
 %var efc = 1;
 %var et2 = 1;
end;

stoch_simul(order=1,irf=20,ar=1) Ye Yc Rtre Rtrc Rfe Rfc Qe Qc fe fc be dc rer S FX V phie phic 
Cpie Cpic Ke Kc Rv logRde logRdc ne nc;