%% Historical Decomposition 
% Starting date: 
% Panel 1: hh=1 (1979M12-1981M11(Recession:1980M01-1980M07))
% Panel 2: hh=16 (1981M03-1984M02 (Recession: 1981M07-1982M11))
% Panel 3: hh=125 (1990M04-1993M03 (Recession: 1990M07-1991M03))
% Panel 4: hh=253 (2000M12-2003M11 (Recession: 2001M03-2001M11))
% Panel 5: hh=336 (2007M11-2011M11 (Recession: 2007M12-2009M06))
clear all;
load result_baseline_3prm.mat

%% Compute the correlation among the epsilons

for t=1:T-1
ups_wL(t,1) = sxi(1,t+1)-sxi(1,t);
ups_wH(t,1) = sxi(2,t+1)-sxi(2,t);
ups_xL(t,1) = sxi(3,t+1)-sxi(3,t);
ups_xH(t,1) = sxi(4,t+1)-sxi(4,t);
end

ups = [ups_wL, ups_wH, ups_xL, ups_xH];

c1 = corrcoef(ups_wL, ups_wH); 
c2 = corrcoef(ups_wL, ups_xL);
c3 = corrcoef(ups_wL, ups_xH);
c4 = corrcoef(ups_wH, ups_xL);
c5 = corrcoef(ups_wH, ups_xH);
c6 = corrcoef(ups_xL, ups_xH);

coef = [c1(1,2), c2(1,2), c3(1,2), c4(1,2), c5(1,2), c6(1,2)];

%% historical decomposition

clear all;
load result_base_3prm_fc.mat

hh_data = [1;16;125;253;336];

global T; global F;global ftau;global beg;
load LF; %Civilian labor force, SA, FRED
%IND = ind65;
for nn=1:5
    hh = hh_data(nn);
    [hist_Ulevel] = hisdcmp(hh,sxi,F,D);
for c=1:6
hist_Ulevel_rec(:,c,nn) = hist_Ulevel(:,c)./(LF(47+hh:47+hh+48,1)/100000);
end
end

U1980r = hist_Ulevel_rec(:,:,1);
U1981r = hist_Ulevel_rec(:,:,2);
U1990r = hist_Ulevel_rec(:,:,3);
U2001r = hist_Ulevel_rec(:,:,4);
U2007r = hist_Ulevel_rec(:,:,5);

%% plots

for i=1:12
    tt(i,1)=1976+i/13;
end
for i=1:37*16;
    tt(i+12,1)=tt(i,1)+1;
end

t1980r=tt(48:71,1); % timeline 1979M12-1981M11 (Recession: 1980M01-1980M07)
t1981r=tt(63:98,1); % timeline 1981M03-1984M02 (Recession: 1981M07-1982M11)
t1990r=tt(172:207,1); % timeline 1990M04-1993M03 (Recession: 1990M07-1991M03)
t2001r=tt(300:335,1); % timeline 2000M12-2003M11 (Recession: 2001M03-2001M11)
t2007r=tt(383:431,1); % timeline 2007M11-2011M11 (Recession: 2007M12-2009M06)

% 1980 recession
b = t1980r(1,1);
e = t1980r(18,1);

g1980r = figure;
hold on
plot(t1980r(1:18,1),U1980r(1:18,1),'-','LineWidth',2,'Color',[0 0 0]) % black
plot(t1980r(1:18,1),U1980r(1:18,2),':','LineWidth',3,'Color',[0 0 0]) % black
plot(t1980r(1:18,1),U1980r(1:18,3),'-p','LineWidth',1,'Color',[1 0 0]) % red
plot(t1980r(1:18,1),U1980r(1:18,4),'-','LineWidth',2,'Color',[1 0 1]) % magenta
plot(t1980r(1:18,1),U1980r(1:18,5),'-o','LineWidth',1,'Color',[0 0 1]) % blue 
plot(t1980r(1:18,1),U1980r(1:18,6),'--','LineWidth',2,'Color',[0.3 0.7 0.2]) % green
hleg1 = legend('U_{base}','U_{all}','W_L','W_H','P_L^{ux}','P_H^{ux}');
set(hleg1,'Location', 'Northwest') 
axis([b,e,5,10])
set(gca,'XTick',[1980.1, 1981.1])
xlabel('1980 Recession (correlated shocks)');
colorstr=[159 182 190]/205;
shade(t1980r(2,1),t1980r(8,1),colorstr);
hold off
saveas(g1980r,'hist_fac_a','epsc');


% 1981 recession
b = t1981r(1,1);
e = t1981r(36,1);

g1981r = figure;
hold on
plot(t1981r(1:36,1),U1981r(1:36,1),'-','LineWidth',2,'Color',[0 0 0]) % black
plot(t1981r(1:36,1),U1981r(1:36,2),':','LineWidth',3,'Color',[0 0 0]) % black
plot(t1981r(1:36,1),U1981r(1:36,3),'-p','LineWidth',1,'Color',[1 0 0]) % red
plot(t1981r(1:36,1),U1981r(1:36,4),'-','LineWidth',2,'Color',[1 0 1]) % magenta
plot(t1981r(1:36,1),U1981r(1:36,5),'-o','LineWidth',1,'Color',[0 0 1]) % blue 
plot(t1981r(1:36,1),U1981r(1:36,6),'--','LineWidth',2,'Color',[0.3 0.7 0.2]) % green
hleg1 = legend('U_{base}','U_{all}','W_L','W_H','P_L^{ux}','P_H^{ux}');
set(hleg1,'Location', 'Northwest') 
axis([b,e,6,11])
set(gca,'XTick',[1982.1, 1983.1, 1984.1])
xlabel('1981 Recession (correlated shocks)');
colorstr=[159 182 190]/205;
shade(t1981r(5,1),t1981r(21,1),colorstr);
hold off
saveas(g1981r,'hist_fac_b','epsc');

% 1990 recession
b = t1990r(1,1);
e = t1990r(36,1);

g1990r = figure;
hold on
plot(t1990r(1:36,1),U1990r(1:36,1),'-','LineWidth',2,'Color',[0 0 0]) % black
plot(t1990r(1:36,1),U1990r(1:36,2),':','LineWidth',3,'Color',[0 0 0]) % black
plot(t1990r(1:36,1),U1990r(1:36,3),'-p','LineWidth',1,'Color',[1 0 0]) % red
plot(t1990r(1:36,1),U1990r(1:36,4),'-','LineWidth',2,'Color',[1 0 1]) % magenta
plot(t1990r(1:36,1),U1990r(1:36,5),'-o','LineWidth',1,'Color',[0 0 1]) % blue 
plot(t1990r(1:36,1),U1990r(1:36,6),'--','LineWidth',2,'Color',[0.3 0.7 0.2]) % green
hleg1 = legend('U_{base}','U_{all}','W_L','W_H','P_L^{ux}','P_H^{ux}');
set(hleg1,'Location', 'Northwest') 
axis([b,e,4.5,9.5])
set(gca,'XTick',[1991.1, 1992.1, 1993.1])
xlabel('1990 Recession (correlated shocks)');
colorstr=[159 182 190]/205;
shade(t1990r(4,1),t1990r(12,1),colorstr);
hold off
saveas(g1990r,'hist_fac_c','epsc');

% 2001 recession
b = t2001r(1,1);
e = t2001r(36,1);

g2001r = figure;
hold on
plot(t2001r(1:36,1),U2001r(1:36,1),'-','LineWidth',2,'Color',[0 0 0]) % black
plot(t2001r(1:36,1),U2001r(1:36,2),':','LineWidth',3,'Color',[0 0 0]) % black
plot(t2001r(1:36,1),U2001r(1:36,3),'-p','LineWidth',1,'Color',[1 0 0]) % red
plot(t2001r(1:36,1),U2001r(1:36,4),'-','LineWidth',2,'Color',[1 0 1]) % magenta
plot(t2001r(1:36,1),U2001r(1:36,5),'-o','LineWidth',1,'Color',[0 0 1]) % blue 
plot(t2001r(1:36,1),U2001r(1:36,6),'--','LineWidth',2,'Color',[0.3 0.7 0.2]) % green
hleg1 = legend('U_{base}','U_{all}','W_L','W_H','P_L^{ux}','P_H^{ux}');
set(hleg1,'Location', 'Northeast') 
axis([b,e,3.5,8.5])
set(gca,'XTick',[2001.1, 2002.1, 2003.1])
xlabel('2001 Recession (correlated shocks)');
colorstr=[159 182 190]/205;
shade(t2001r(4,1),t2001r(12,1),colorstr);
hold off
saveas(g2001r,'hist_fac_d','epsc');

% 2007 recession
b = t2007r(1,1);
e = t2007r(49,1);

g2007r = figure;
hold on
plot(t2007r,U2007r(:,1),'-','LineWidth',2,'Color',[0 0 0]) % black
plot(t2007r,U2007r(:,2),':','LineWidth',3,'Color',[0 0 0]) % black
plot(t2007r,U2007r(:,3),'-p','LineWidth',1,'Color',[1 0 0]) % red
plot(t2007r,U2007r(:,4),'-','LineWidth',2,'Color',[1 0 1]) % magenta
plot(t2007r,U2007r(:,5),'-o','LineWidth',1,'Color',[0 0 1]) % blue 
plot(t2007r,U2007r(:,6),'--','LineWidth',2,'Color',[0.3 0.7 0.2]) % green
hleg1 = legend('U_{base}','U_{all}','W_L','W_H','P_L^{ux}','P_H^{ux}');
set(hleg1,'Location', 'Northwest') 
axis([b,e,4,9])
set(gca,'XTick',[2007.1, 2008.1, 2009.1, 2010.1, 2011.1, 2012.1])
xlabel('2007 Recession (correlated shocks)');
colorstr=[159 182 190]/205;
shade(t2007r(2,1),t2007r(20,1),colorstr);
hold off
saveas(g2007r,'hist_fac_e','epsc');
