%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%High-Frequency Analysis and Moment-Matching Estimation%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%of the Baseline New-Keynesian Model%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%by Stephen Sacht (Doctoral Thesis/Chapters 2 & 3)%%%%%%%%%%%%%%
%%%%%%%%%%%%%%Matlab Source Code (c) by Stephen Sacht (2013)%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%Impulse Response Functions%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%(Requires the file 'h_Econ_master.m')%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
syms b1c
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
h=1/w; % Length of the period (relative to a quarter, where h=1)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
rhovi=(1-h*(1-avi)); % ONLY Shock to be considered in the (hybrid) TR Case
rhovt=(1-h*(1-avt)); % ONLY Shock to be considered in the (hybrid) TR Case
rhovc=(1-h*(1-avc)); % ONLY Shock considered in the (non-hybrid) OMP Case
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Specification of composite parameters
thetah=1-h*(1-theta);
betah=1/(1+h*rho);
gamma_f=betah/(1+betah*alpha);
gamma_b=alpha/(1+betah*alpha);
kappah=(1/h)*((1-thetah)*(1-thetah*betah))/(thetah*(1+betah*alpha))*(sigma+eta);
delta_f=1/(1+chi);
delta_b=chi/(1+chi);
nu=(h*(1-chi))/(sigma*(1+chi));
nu2=(h*(1-chi)*(1+eta))/((1+chi)*(sigma+eta));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% AR(1) Shock Process: E_0 v_t = rhov^t*eps_0
for t=1:rf
vi(t)=(rhovi^(t-1))*epsi; % Shock considered in the (hybrid) TR Case ONLY
vt(t)=(rhovt^(t-1))*epst; % Shock considered in the (hybrid) TR Case ONLY
vc(t)=(rhovc^(t-1))*epsc; % ONLY Shock considered in the (non-hybrid) OMP Case
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%A*x_t+B*x_{t+h}+C*x_{t-h}+D*v_t=0 with x_t=[y_t; pi_t];
%v_t=N*v_t-h+eps=0 with v_t=[v^(interest); v^{demand}_t; v^{cost}_t];
A=[1+nu*phi_y nu*phi_pi; -kappah 1]; % System
B=[-delta_f -nu; 0 -gamma_f]; % Matrices
C=[-delta_b 0; 0 -gamma_b]; % for the
N=[rhovi 0 0; 0 rhovt 0; 0 0 rhovc]; % hybrid
D=[nu -nu2*(rhovt-1) 0; 0 0 -1]; % NKM model
%CLAIM: x_t=Omega+x_{t-h}+Phi*v_t;
for i=2:rf; % i=Iterations
Omega{1}=0.8*eye(2,2);
Omega{i}=-inv(B*Omega{i-1}+A)*C;
end;
for ii=2:rf; % ii=Iterations
Phi{1}=0.8*ones(2,3);
Phi{ii}=-inv(A+B*Omega{10000})*(B*Phi{ii-1}*N+D);
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
d0=-alpha2/(h*kappah*alpha1); % Solution of the model
d1=kappah-d0; % under the monetary
d2=betah*d0; % policy regime
d3=-1/(d1+d2*rhovc); % 'Discretion'
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
c0=(h*alpha1*kappah)/alpha2; % Solution of
c1=1+(h*alpha1*(kappah^2))/alpha2+betah^(2-h); % the model
c2=-betah^(1-h); % under the
c3=-betah; % monetary
b1c = solve(b1c^2 + (c1/c3)*b1c + (c2/c3),b1c); % policy regime
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
b1c1=double(b1c(1));
b1c2=double(b1c(2));
if abs(b1c1)<1 && abs(b1c2)>1
cbk=b1c1;
elseif abs(b1c1)>1 && abs(b1c2)<1
cbk=b1c2;
else
disp('Blancher-Kahn condition is not satisfied: Both Eigenvalues lie outside the unit circle!')
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
b2c = -c0/(c1+c3*(cbk+rhovc)); % 'Commitment'
cs=(b2c*(cbk-betah^(1-h)))/(cbk-rhovc); % (code lines 67-76 choose stable eigenvalue)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Dynamics in the state variables (TAYLOR RULE; hybrid NKM)
x(rf,2)=zeros;
x(1,:)=Phi{10000}*[epsi;epst;epsc];
ir(rf)=zeros;
re(rf)=zeros;
xf(rf)=zeros;
xf(1:rf-1)=x(2:rf,2);
p(rf)=zeros;
p(1)=x(1,2);
for t=2:rf
x(t,:)=Omega{10000}*x(t-1,:)'+Phi{10000}*[vi(t);vt(t);vc(t)]; % [Ouput Gap, Inflation Rate]
ir(t-1)=phi_pi*x(t-1,2)+phi_y*x(t-1,1)+vi(t-1); % Interest Rate
re(t-1)=ir(t-1)-xf(t-1); % Real Interest Rate
p(t)=x(t,2)+p(t-1); % Price Level
end;
%ytr(rf)=zeros;
%pitr(rf)=zeros;
%re(rf)=zeros;
%ir(rf)=zeros;
%pitrf(rf)=zeros;
%pitrf(1:rf-1)=pitr(2:rf);
%p(rf)=zeros;
%p(1)=pitr(1);
%for t=1:rf
%ytr(t)=Phi{rf}(1,3)*rhovi^(t-1)*epsi; % Ouput Gap (TR), change epsi/T/c
%pitr(t)=Phi{rf}(2,3)*rhovi^(t-1)*epsi; % Inflation Rate (TR), change epsi/T/c
%ir(t)=phi_pi*pitr(t)+phi_y*ytr(t); % Interest Rate (TR)
%re(t)=ir(t)-pitrf(t); % Real Interest Rate (TR)
%end;
%for t=2:rf
%p(t)=pitr(t)+p(t-1); % Price Level (TR)
%end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Dynamics in the state variables (DISCRETION; forward-looking only)
yd(rf)=zeros;
pid(rf)=zeros;
ird(rf)=zeros;
red(rf)=zeros;
yfd(rf)=zeros;
yfd(1:rf-1)=yd(2:rf);
pifd(rf)=zeros;
pifd(1:rf-1)=pid(2:rf);
pd(rf)=zeros;
for t=1:rf
yd(t)=d3*rhovc^(t-1)*epsc; % Ouput Gap (D)
pid(t)=d0*d3*rhovc^(t-1)*epsc; % Inflation Rate (D)
ird(t)=((delta_f*yfd(t)-yd(t))/nu)+pifd(t); % Interest Rate (D)
red(t)=ird(t)-pifd(t); % Real Interest Rate (D)
end;
for t=2:rf
pd(1)=pid(1);
pd(t)=pid(t)+pd(t-1); % Price Level (D)
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Dynamics in the state variables (COMMITMENT; forward-looking only)
yc(rf)=zeros;
pic(rf)=zeros;
irc(rf)=zeros;
rec(rf)=zeros;
yfc(rf)=zeros;
yfc(1:rf-1)=yc(2:rf);
pifc(rf)=zeros;
pifc(1:rf-1)=pic(2:rf);
pc(rf)=zeros;
for t=1:rf
yc(t)=(b2c*(cbk^(t)-rhovc^(t)))/(cbk-rhovc)*epsc; % Output Gap (COM)
pic(t)=-1/c0*(cs*cbk^(t-1)-(cs-b2c)*rhovc^(t-1))*epsc; % Inflation Rate (COM)
irc(t)=((delta_f*yfc(t)-yc(t))/nu)+pifc(t); % Interest Rate (COM)
rec(t)=irc(t)-pifc(t); % Real Interest Rate (COM)
end;
for t=2:rf
pc(1)=pic(1);
pc(t)=pic(t)+pc(t-1); % Price Level (COM)
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
aa(rf)=zeros; % Adjustment of the x-axis for h<1
for k=1:rf;
aa(k)=(k-1)*h;
end;