%slides_3c_210C - VERSION 2
%Matlb code for creating figures 22-23
%Calculates empirical moments and compares them to simulated moments
%First run SLIDES_3B_210C.M
%Economics 210C, Spring 2019, Garey Ramey, UCSD

T_ir = 26;
IR_S_z_lo = zeros(T_ir,3);
IR_U_z_lo = zeros(T_ir,1);
IR_v_z_lo = zeros(T_ir,2);
IR_S_z_hi = zeros(T_ir,3);
IR_U_z_hi = zeros(T_ir,1);
IR_v_z_hi = zeros(T_ir,2);
IR_S_g_lo = zeros(T_ir,3);
IR_U_g_lo = zeros(T_ir,1);
IR_v_g_lo = zeros(T_ir,2);
IR_S_g_hi = zeros(T_ir,3);
IR_U_g_hi = zeros(T_ir,1);
IR_v_g_hi = zeros(T_ir,2);

z_shock = .01;
IR_S_z_lo(2,2) = z_shock;
IR_S_z_hi(2,2) = z_shock;
g_shock = .01;
IR_S_g_lo(2,3) = z_shock;
IR_S_g_hi(2,3) = z_shock;
IR_U_z_lo(2,:) = PHI_U_all(:,:,4)*IR_S_z_lo(2,:)';
IR_v_z_lo(2,:) = PHI_v_all(:,:,4)*[IR_U_z_lo(2,:); IR_S_z_lo(2,:)']; 
IR_U_z_hi(2,:) = PHI_U_all(:,:,6)*IR_S_z_hi(2,:)';
IR_v_z_hi(2,:) = PHI_v_all(:,:,6)*[IR_U_z_hi(2,:); IR_S_z_hi(2,:)'];
IR_U_g_lo(2,:) = PHI_U_all(:,:,4)*IR_S_g_lo(2,:)';
IR_v_g_lo(2,:) = PHI_v_all(:,:,4)*[IR_U_g_lo(2,:); IR_S_g_lo(2,:)']; 
IR_U_g_hi(2,:) = PHI_U_all(:,:,6)*IR_S_g_hi(2,:)';
IR_v_g_hi(2,:) = PHI_v_all(:,:,6)*[IR_U_g_hi(2,:); IR_S_g_hi(2,:)'];
for t = 3:T_ir
    IR_S_z_lo(t,:) = PHI_S_all(:,:,4)*IR_S_z_lo(t-1,:)';
    IR_U_z_lo(t,:) = PHI_U_all(:,:,4)*IR_S_z_lo(t,:)';
    IR_v_z_lo(t,:) = PHI_v_all(:,:,4)*[IR_U_z_lo(t,:); IR_S_z_lo(t,:)']; 
    IR_S_z_hi(t,:) = PHI_S_all(:,:,6)*IR_S_z_hi(t-1,:)';
    IR_U_z_hi(t,:) = PHI_U_all(:,:,6)*IR_S_z_hi(t,:)';
    IR_v_z_hi(t,:) = PHI_v_all(:,:,6)*[IR_U_z_hi(t,:); IR_S_z_hi(t,:)'];
    IR_S_g_lo(t,:) = PHI_S_all(:,:,4)*IR_S_g_lo(t-1,:)';
    IR_U_g_lo(t,:) = PHI_U_all(:,:,4)*IR_S_g_lo(t,:)';
    IR_v_g_lo(t,:) = PHI_v_all(:,:,4)*[IR_U_g_lo(t,:); IR_S_g_lo(t,:)']; 
    IR_S_g_hi(t,:) = PHI_S_all(:,:,6)*IR_S_g_hi(t-1,:)';
    IR_U_g_hi(t,:) = PHI_U_all(:,:,6)*IR_S_g_hi(t,:)';
    IR_v_g_hi(t,:) = PHI_v_all(:,:,6)*[IR_U_g_hi(t,:); IR_S_g_hi(t,:)'];
end

figure(22)
subplot(2,1,1)
hold on
plot(1:T_ir,IR_S_z_lo(:,1),'k',1:T_ir,IR_U_z_lo,'g', ...
    1:T_ir,IR_S_z_lo(:,2),'r',1:T_ir,IR_S_z_lo(:,3),'y')
plot(1:T_ir,IR_S_z_hi(:,1),'k--',1:T_ir,IR_U_z_hi,'g--', ...
    1:T_ir,IR_S_z_hi(:,2),'r--',1:T_ir,IR_S_z_hi(:,3),'y--')
plot(1:T_ir,zeros(T_ir,1),'k')
title('Response to One Percent TFP Shock')
axis([1 T_ir -.013 .013])
hold off

subplot(2,1,2)
hold on
h(1) = plot(1:T_ir,IR_S_g_lo(:,1),'k');
h(2) = plot(1:T_ir,IR_U_g_lo,'g');
h(3) = plot(1:T_ir,IR_S_g_lo(:,2),'r');
h(4) = plot(1:T_ir,IR_S_g_lo(:,3),'Color',[.9,.5,.1]);
h(5) = plot(1:T_ir,IR_S_g_hi(:,1),'k--');
h(6) = plot(1:T_ir,IR_U_g_hi,'g--');
h(7) = plot(1:T_ir,IR_S_g_hi(:,2),'r--');
h(8) = plot(1:T_ir,IR_S_g_hi(:,3),'Color',[.9,.5,.1]);
h(9) = plot(1:T_ir,zeros(T_ir,1),'k');
title('Response to One Percent Gov Spending Shock')
legend(h([1 2 5 6]),'Capital, \eta  = .25','MU of wealth, \eta  = .25', ...
    'Capital, \eta  = 10','MU of wealth, \eta  = 10','Location','Southwest')
axis([1 T_ir -.018 .013])
hold off

IR_y_z_hi = IR_S_z_hi(:,2)+alpha*IR_S_z_hi(:,1)+(1-alpha)*IR_v_z_hi(:,2);
IR_i_z_hi = (SSE(6,3)*IR_y_z_hi-SSE(6,1)*IR_v_z_hi(:,1) ...
    -G*IR_S_z_hi(:,3))/(delta*SSE(6,2));
IR_p_z_hi = IR_y_z_hi-IR_v_z_hi(:,2);

figure(23)
subplot(2,1,1)
hold on
plot(1:T_ir,IR_y_z_hi,'k',1:T_ir,IR_v_z_hi(:,1),'b',1:T_ir,IR_i_z_hi,'r')
plot(1:T_ir,zeros(T_ir,1),'k')
title('Response to One Percent TFP Shock, \eta = 10')
legend('GDP','Cons','Inv')
axis([1 T_ir -.011 .10])
hold off

subplot(2,1,2)
hold on
plot(1:T_ir,IR_y_z_hi,'k',1:T_ir,IR_v_z_hi(:,2),'b',1:T_ir,IR_p_z_hi,'r')
plot(1:T_ir,zeros(T_ir,1),'k')
title('Response to One Percent TFP Shock, \eta = 10')
legend('GDP','Hours','Prod','Location','Southwest')
axis([1 T_ir -.015 .02])
hold off
