% program1.m
%
% By Dr. Tai-kuang Ho
% Department of Quantitative Finance
% National Tsing Hua University, Taiwan
% ****** Benutzung auf eigene Gefahr! ******
%
% This program solves the small open economy model of Monacelli (2005).
% Monacelli (2005), JMCB
% complete pass-through
% flexible exchange rates
% I use Paul Klein's code (solab.m) to solve the model.
%
% Monacelli (2005) has only domestic and foreign productivity shocks.
% Here I add into his model domestic and foreign monetary policy shocks:
% zm =rhozm *zm(-1) +xim
% zms=rhozms*zms(-1)+xims


% variables order

%1  rb   rb(+1) =r
%2  sb   sb(+1) =s
%3  rsb  rsb(+1)=rs
%4  z
%5  zs
%6  zm
%7  zms
%8  c
%9  y
%10 r
%11 q
%12 s
%13 pii
%14 piiH
%15 x
%16 mc
%17 ys
%18 piis
%19 rs
%20 xs
%21 mcs

disp('clear');
clear;

% parameters values
betta=0.99;
sigma=1;
eta=1.5;
vphi=3;
thetaH=0.75;
theta =0.75;
rho=0.9;
gama=0.4;

rhor=0.8;
phipii=1.2;
phix=0.5;

rhors=0.8;
phipiis=1.2;
phixs=0.5;

lambdaH=(1-thetaH)*(1-betta*thetaH)/thetaH;
omegas=1+gama*(2-gama)*(sigma*eta-1);

rhozm=0.8;
rhozms=0.8;

% eqs
nvar=21;
nk=7;

A=zeros(nvar,nvar);
B=zeros(nvar,nvar);

A(1,1)=1;
B(1,10)=1;

A(2,2)=1;
B(2,12)=1;

A(3,3)=1;
B(3,19)=1;

A(4,4)=1;
B(4,4)=rho;

A(5,5)=1;
B(5,5)=rho;

A(6,6)=1;
B(6,6)=rhozm;

A(7,7)=1;
B(7,7)=rhozms;

B(8,8)=-(1-gama);
B(8,9)=1;
B(8,12)=-(2-gama)*gama*eta;
B(8,17)=-gama;

B(9,11)=-1;
B(9,12)=1-gama;

A(10,14)=betta;
B(10,14)=1;
B(10,16)=-lambdaH;

B(11,16)=-1;
B(11,8)=sigma;
B(11,9)=vphi;
B(11,12)=gama;
B(11,4)=-(1+vphi);

B(12,8)=-1;
B(12,17)=1;
B(12,12)=(1/sigma)*(1-gama);

A(13,13)=1;
A(13,18)=-1;
A(13,11)=1;
B(13,10)=1;
B(13,19)=-1;
B(13,11)=1;

B(14,10)=-1;
B(14,13)=(1-rhor)*phipii;
B(14,15)=(1-rhor)*phix;
B(14,1)=rhor;
B(14,6)=1;

B(15,13)=-1;
B(15,14)=1;
B(15,12)=gama;
B(15,2)=-gama;

B(16,15)=-1;
B(16,9)=1;
B(16,4)=-(omegas*(1+vphi)/(sigma+vphi*omegas));
B(16,17)=-(sigma*(1-omegas)/(sigma+vphi*omegas));

A(17,17)=1;
A(17,18)=1/sigma;
B(17,17)=1;
B(17,19)=1/sigma;

A(18,18)=betta;
B(18,18)=1;
B(18,21)=-(1-theta)*(1-betta*theta)/theta;

B(19,21)=-1;
B(19,17)=(sigma+vphi);
B(19,5)=-(1+vphi);

B(20,19)=-1;
B(20,18)=(1-rhors)*phipiis;
B(20,20)=(1-rhors)*phixs;
B(20,3)=rhors;
B(20,7)=1;

B(21,20)=-1;
B(21,17)=1;
B(21,5)=-(1+vphi)/(sigma+vphi);


[f,p]=solab(A,B,nk);

disp('policy     function'); disp(f);
disp('transition function'); disp(p);


% impulse responses
% use ir.m to compute impulse response function (IRF)
gx=f;
hx=p;

x0=zeros(size(hx,1),1);
x0(end-3)=1; % domestic productivity shock
%x0(end-2)=1; % foreign  productivity shock
%x0(end-1)=1; % domestic monetary shock
%x0(end)=1;   % foreign  monetary shock
IRF=ir(gx,hx,x0,20);

t=(0:size(IRF,1)-1)';
c   =IRF(:,1);
y   =IRF(:,2);
r   =IRF(:,3);
q   =IRF(:,4);
s   =IRF(:,5);
pii =IRF(:,6);
piiH=IRF(:,7);
x   =IRF(:,8);
mc  =IRF(:,9);
ys  =IRF(:,10);
piis=IRF(:,11);
rs  =IRF(:,12);
xs  =IRF(:,13);
mcs =IRF(:,14);
rb  =IRF(:,15);
sb  =IRF(:,16);
rsb =IRF(:,17);
z   =IRF(:,18);
zs  =IRF(:,19);
zm  =IRF(:,20);
zms =IRF(:,21);


subplot(3,6,1)
plot(t,c)
title('consumption')

subplot(3,6,2)
plot(t,y)
title('output')

subplot(3,6,3)
plot(t,r)
title('interest rate')

subplot(3,6,4)
plot(t,q)
title('real exchange rate')

subplot(3,6,5)
plot(t,s)
title('terms of trade')

subplot(3,6,6)
plot(t,pii)
title('CPI inflation')

subplot(3,6,7)
plot(t,piiH)
title('producer inflation')

subplot(3,6,8)
plot(t,x)
title('output gap')

subplot(3,6,9)
plot(t,mc)
title('real marginal cost')

subplot(3,6,10)
plot(t,ys)
title('foreign output')

subplot(3,6,11)
plot(t,piis)
title('foreign inflation')

subplot(3,6,12)
plot(t,rs)
title('foreign interest rate')

subplot(3,6,13)
plot(t,xs)
title('foreign output gap')

subplot(3,6,14)
plot(t,mcs)
title('foreign real marginal cost')

subplot(3,6,15)
plot(t,z)
title('domestic productivity shock')

subplot(3,6,16)
plot(t,zs)
title('foreign productivity shocks')

subplot(3,6,17)
plot(t,zm)
title('domestic monetary shock')

subplot(3,6,18)
plot(t,zms)
title('foreign monetary shocks')

shg


%{
1. The trick is to add 3 auxillary variables rb, sb, and rsb.
2. The 3 variables are one-period-lagged variables.
3. The auxillary variables are treated as state variables (as they should be).
4. Corespondently, we add 3 equations describing the auxillary variables (dynamics).
5. For both domestic and foreign blocks, we employ a version of output gap that does not
   involve forward-looking variables.
6. Another important notice is that the IRF produced by ir.m of Schmitt-Grohe and Uribe
   put the control variables at the first ordering, followed by
   the state variables (state and exogenou shocks).
   So it is important not to confuse the IRF of one for another.
%}



