%slides_1a_210C
%Matlb code for creating figures 1-8
%Uses function PLOT_BANDS.M
%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);
Gov = rawdat(1:288,4);
Exp = rawdat(1:288,5);
Imp = rawdat(1:288,6);
Rec = rawdat(1:288,7);
GDP_Def = rawdat(1:288,9);
Cons_Def = rawdat(1:288,10);
Inv_Def = rawdat(1:288,11);
Gov_Def = rawdat(1:288,12);
EmpPop = rawdat(1:288,16);

%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

%Estimate quadratic time trend for log of GDP
X = [ones(288,1),(1:288)',((1:288)').^2];
gamm = (X'*X)\(X'*log(GDP));
log_trend = gamm(1)+gamm(2)*(1:288)'+gamm(3)*((1:288)').^2;

%Plot levels of GDP and trend
figure(1)
plot_bands(Rec,[0,2*1e4],.3,'g')
hold on
plot(1:288,GDP,'k',1:288,exp(log_trend),'k:')
axis([1,288,0,2*1e4])
set(gca,'Xtick',21:40:288)
set(gca,'Xticklabel',{'1952:1','1962:1','1972:1','1982:1', ...
    '1992:1','2002:1','2012:1'})
title('Gross Domestic Product (GDP)')
ylabel('Billions of Chained 2012 Dollars')
hold off
return
%Plot components of GDP
figure(2)
plot_bands(Rec,[0,2*1e4],.3,'g')
hold on
h(1) = plot(1:288,GDP,'k');
h(2) = plot(1:288,Cons,'b');
h(3) = plot(1:288,Inv,'r');
h(4) = plot(1:288,Gov,'Color',[.9,.5,.1]);
%h(5) = plot(1:288,Exp-Imp,'Color',[.7,.4,.9]);
h(5) = plot(1:288,zeros(1,288),'k');
axis([1,288,0,2*1e4])
set(gca,'Xtick',21:40:288)
set(gca,'Xticklabel',{'1952:1','1962:1','1972:1','1982:1', ...
    '1992:1','2002:1','2012:1'})
title('Components of GDP')
ylabel('Billions of Chained 2012 Dollars')
legend(h([1 2 3 4]),'GDP','Consumption','Investment','Government', ...
    'Location','Northwest')
hold off

%Plot annual growth rate of GDP
figure(3)
dGDP = log(GDP(5:288))-log(GDP(1:284));
plot_bands(Rec(5:288),[-.06,.14],.3,'g')
hold on
plot(1:284,dGDP,'k',1:284,mean(dGDP)*ones(1,284),'k:',...
    1:284,zeros(1,284),'k')
axis([1,284,-.06,.14])
set(gca,'Xtick',17:40:284)
set(gca,'Xticklabel',{'1952:1','1962:1','1972:1','1982:1', ...
    '1992:1','2002:1','2012:1'})
title('Annual GDP Growth Rate')
ylabel('Log Difference')
hold off

%Plot annual growth rates of Consumption and Investment
figure(4)
dCons = log(Cons(5:288))-log(Cons(1:284));
dInv = log(Inv(5:288))-log(Inv(1:284));
plot_bands(Rec(5:288),[-.35,.55],.3,'g')
hold on
h(1) = plot(1:284,dCons,'b');
h(2) = plot(1:284,dInv,'r');
h(3) = plot(1:284,zeros(1,284),'k');
axis([1,284,-.35,.55])
set(gca,'Xtick',17:40:284)
set(gca,'Xticklabel',{'1952:1','1962:1','1972:1','1982:1', ...
    '1992:1','2002:1','2012:1'})
title('Annual Consumption and Investment Growth Rates')
ylabel('Log Difference')
legend(h([1 2]),'Consumption','Investment','Location','Northeast')
hold off

%Plot Employment-Population Ratio
figure(5)
plot_bands(Rec,[52,67],.3,'g')
hold on
plot(1:288,EmpPop,'k')
axis([1,288,52,67])
set(gca,'Xtick',17:40:284)
set(gca,'Xticklabel',{'1952:1','1962:1','1972:1','1982:1', ...
    '1992:1','2002:1','2012:1'})
title('Employment-Population Ratio')
ylabel('Percent')
hold off

%Plot annual growth rate of GDP Deflator
figure(6)
dGDP_Def = log(GDP_Def(5:288))-log(GDP_Def(1:284));
plot_bands(Rec(5:288),[-.02,.11],.3,'g')
hold on
plot(1:284,dGDP_Def,'k',1:284,mean(dGDP_Def)*ones(1,284),'k:',...
    1:284,zeros(1,284),'k')
axis([1,284,-.02,.11])
set(gca,'Xtick',17:40:284)
set(gca,'Xticklabel',{'1952:1','1962:1','1972:1','1982:1', ...
    '1992:1','2002:1','2012:1'})
title('Annual Growth Rate of GDP Deflator')
ylabel('Log Difference')
hold off

%Plot annual growth rate of S&P 500 Index
figure(7)
dSandP = log(q_price(5:288))-log(q_price(1:284));
plot_bands(Rec(5:288),[-.6,.6],.3,'g')
hold on
plot(1:284,dSandP,'k',1:284,mean(dSandP)*ones(1,284),'k:',...
    1:284,zeros(1,284),'k')
axis([1,284,-.6,.6])
set(gca,'Xtick',17:40:284)
set(gca,'Xticklabel',{'1952:1','1962:1','1972:1','1982:1', ...
    '1992:1','2002:1','2012:1'})
title('Annual Growth Rate of S&P 500 Index')
ylabel('Log Difference')
hold off

%Plot mean and standard deviation of 10-year centered moving windows
%   of annual growth rate of GDP
figure(8)
mndGDP = zeros(245,1);
sddGDP = zeros(245,1);
for t = 1:245
    mndGDP(t) = mean(dGDP(t:t+39,1));
    sddGDP(t) = std(dGDP(t:t+39,1));
end
subplot(2,1,1)
plot_bands(Rec(44:288),[0,.06],.3,'g')
hold on
plot(1:245,mndGDP,'k')
axis([1,245,0,.06])
set(gca,'Xtick',21:40:284)
set(gca,'Xticklabel',{'1958:1','1968:1','1978:1','1988:1', ...
    '1998:1','2008:1'})
title('Mean of Annual GDP Growth Rate')
ylabel({'Mean of 10-year', 'Centered Window'})
hold off
subplot(2,1,2)
plot_bands(Rec,[0,.06],.3,'g')
hold on
plot(1:245,sddGDP,'k')
axis([1,245,0,.06])
set(gca,'Xtick',21:40:284)
set(gca,'Xticklabel',{'1958:1','1968:1','1978:1','1988:1', ...
    '1998:1','2008:1'})
title('Standard Deviation of Annual GDP Growth Rate')
ylabel({'St Dev of 10-year', 'Centered Window'})
hold off







