%slides_1b_210C_v2
%Matlb code for creating figures 9 and 10
%Economics 210C, Spring 2019, Garey Ramey, UCSD

clear all
close all

%Import NIPA, NBER, CPS data
rawdat = xlsread('data_1_210C.xlsx');
GDP = rawdat(1:288,1);
Cons = rawdat(1:288,2);
Inv = rawdat(1:288,3);
Cons_Def = rawdat(1:288,10);
EmpPop = rawdat(5:288,16);  %First four quarters are missing

%Import Shiller data
rawdat2 = xlsread('Shiller_ie_data.xls');
m_price = rawdat2(913:1776,2);
N = size(m_price,1)/3;
q_price = zeros(N,1);
for i = 1:N
    q_price(i) = m_price(3*i);
end

%Compute quarterly growth rates
dGDP = log(GDP(2:end))-log(GDP(1:end-1));
dCons = log(Cons(2:end))-log(Cons(1:end-1));
dInv = log(Inv(2:end))-log(Inv(1:end-1));
dEmpPop = log(EmpPop(2:end))-log(EmpPop(1:end-1));
dSandP = log(q_price(2:end))-log(q_price(1:end-1));

%Calculate cross-correlations and bootstrap error bands
n = 1000;   conf = .95;
cc_Cons = cc_boot(dCons,dGDP,4,n,conf);
cc_Inv = cc_boot(dInv,dGDP,4,n,conf);
cc_EmpPop = cc_boot(dEmpPop,dGDP(5:end),4,n,conf);
cc_SandP = cc_boot(dSandP,dGDP,4,n,conf);

%Plot medians and 95% error bands for cross-correlations of dGDP with
%   dCons, dInv, dEmpPop, dSandP
figure(9)
subplot(2,2,1)
plot(1:9,cc_Cons(:,1),'b:',1:9,cc_Cons(:,2),'b', ...
    1:9,cc_Cons(:,3),'b:',1:9,zeros(1,9),'k', ...
    1:9,[-1000*ones(1,4),0,1e20*ones(1,4)],'k')
axis([1,9,-0.3,1])
title('Corr(dCons at t, dGDP at t-k)')
set(gca,'Xtick',1:1:9)
set(gca,'Xticklabel',{'-4','-3','-2','-1', ...
    '0','1','2','3','4'})

subplot(2,2,2)
plot(1:9,cc_Inv(:,1),'r:',1:9,cc_Inv(:,2),'r', ...
    1:9,cc_Inv(:,3),'r:',1:9,zeros(1,9),'k',...
    1:9,[-1000*ones(1,4),0,1e20*ones(1,4)],'k')
axis([1,9,-0.3,1])
title('Corr(dInv at t, dGDP at t-k)')
set(gca,'Xtick',1:1:9)
set(gca,'Xticklabel',{'-4','-3','-2','-1', ...
    '0','1','2','3','4'})

subplot(2,2,3)
plot(1:9,cc_EmpPop(:,1),'g:',1:9,cc_EmpPop(:,2),'g', ...
    1:9,cc_EmpPop(:,3),'g:',1:9,zeros(1,9),'k', ...
    1:9,[-1000*ones(1,4),0,1e20*ones(1,4)],'k')
axis([1,9,-0.3,1])
title('Corr(dEmpPop at t, dGDP at t-k)')
set(gca,'Xtick',1:1:9)
set(gca,'Xticklabel',{'-4','-3','-2','-1', ...
    '0','1','2','3','4'})

subplot(2,2,4)
hold on
plot(1:9,cc_SandP(:,1),'r:',1:9,cc_SandP(:,2),'r', ...
    1:9,cc_SandP(:,3),'r:','Color',[.9,.5,.1])
plot(1:9,zeros(1,9),'k',...
    1:9,[-1000*ones(1,4),0,1e20*ones(1,4)],'k')
axis([1,9,-0.3,1])
title('Corr(dS&P at t, dGDP at t-k)')
set(gca,'Xtick',1:1:9)
set(gca,'Xticklabel',{'-4','-3','-2','-1', ...
    '0','1','2','3','4'})
hold off

%Calculate dGDP autocorrelations and bootstrap error bands
ac_GDP = ac_boot(dGDP,8,n,conf);

%Plot median dGDP autocorrelations and 95% error bands
figure(10)
plot(1:8,ac_GDP(:,1),'k:',1:8,ac_GDP(:,2),'k', ...
    1:8,ac_GDP(:,3),'k:',1:8,zeros(1,8),'k')
%axis([1,9,-0.3,1])
title('Autocorrelations of GDP Growth Rates')
set(gca,'Xtick',1:1:8)
set(gca,'Xticklabel',{'1','2','3','4', ...
    '5','6','7','8'})
