%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%By Marco Bernardini & Gert Peersman (June 2017)
%Figure 2
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clearvars;close all;clc
% close all
%% Load Data
% Private debt data (ratio of GDP)
[data,text]=xlsread('figMATLAB.xls','Fig1-2-8');
text=text(1,2:end);
time=(1919:0.25:2013.75)';
%Colors
pl=[153,0,153]./255;
dg=[0,102,0]./255;
do=[204,102,0]./255;

pry=100.*(data(:,strmatch('PRY_cycle',text,'exact')))./data(:,strmatch('PRY_trend',text,'exact')); %household debt service ratio
%% Figure 2A
hds=100.*(data(:,strmatch('HDS',text,'exact'))-nanmedian(data(:,strmatch('HDS',text,'exact'))))./nanmedian(data(:,strmatch('HDS',text,'exact'))); %household debt service ratio
hfo=100.*(data(:,strmatch('HFO',text,'exact'))-nanmedian(data(:,strmatch('HFO',text,'exact'))))./nanmedian(data(:,strmatch('HFO',text,'exact'))); %household debt service ratio
%
figure('visible','on')
[ax,h1,p2]=plotyy(time,data(:,strmatch('PRO',text,'exact')),time,pry,'bar','plot');box off;
set(h1,'facecolor',[215 215 215]./255,'EdgeColor',[215 215 215]./255);hold(ax(1),'on');hold(ax(2),'on'),
plot(ax(2),time,zeros(size(data(:,strmatch('PRY_cycle',text,'exact')))),'-r','linewidth',1)
l1=plot(ax(2),time,pry,'-b','linewidth',2);
l2=plot(ax(2),time,hds,'linestyle','--','color',pl,'linewidth',2);
l3=plot(ax(2),time,hfo,time(1:12:end),hfo(1:12:end,:),'^','color',dg,'linewidth',2);
% l4=plot(ax(2),time,prd,'-.k','linewidth',1);
set(gcf, 'CurrentAxes', ax(1)) ,ylabel('Percent deviation from median')
set(ax(1),'xlim',[1978,time(end)],'YTick',[],'YTicklabel',[]),
set(ax(2),'xlim',[1978,time(end)],'ylim',[-20,20],'YTick',-100:5:100,'YTicklabel',-100:5:100,'YColor','k'),
hl=legend([l1;l2;l3(2)],{'Benchmark','Household DSR','Household FOR'},'location','southoutside','orientation','horizontal','FontSize',10,'edgecolor','none');
set(findall(gcf,'type','text'),'FontSize',11) 
set(findall(gcf,'type','axes'),'FontSize',11)
set(gcf,'PaperUnits', 'inches','PaperPosition',[0,0,7,4]);
print(gcf,'fig2_A','-depsc')
%% Figure 2B
pre=[data(:,strmatch('MA1520lo',text,'exact')),data(:,strmatch('MA1520up',text,'exact'))];
pre(sum(isnan(pre),2)==1,2)=data(sum(isnan(pre),2)==1,strmatch('MA1520lo',text,'exact')); %replace the relative nan values with the ones of the LB
pre=[time,pre];
pre(sum(isnan(pre),2)>0,:)=[]; %remove the absolute nan values
grpyat=[pre(:,1),pre(:,2);flipud(pre(:,1)),flipud(pre(:,3))];
hp7=100.*(data(:,strmatch('PRYhp7_cycle',text,'exact')))./data(:,strmatch('PRYhp7_trend',text,'exact')); %household debt service ratio
b3=100.*(data(:,strmatch('PRYb3_cycle',text,'exact')))./data(:,strmatch('PRYb3_trend',text,'exact')); %household debt service ratio
%
figure('visible','on')
[ax,h1,p2]=plotyy(time,data(:,strmatch('PRO',text,'exact')),time,pry,'bar','plot');box off;
set(h1,'facecolor',[215 215 215]./255,'EdgeColor',[215 215 215]./255);hold(ax(1),'on');hold(ax(2),'on'),
set(gcf, 'CurrentAxes',ax(2))
l4=patch(grpyat(:,1),grpyat(:,2),pl,'edgecolor',pl);hold on,
plot(ax(2),time,zeros(size(data(:,strmatch('PRY_cycle',text,'exact')))),'-r','linewidth',1)
l1=plot(ax(2),time,pry,'-b','linewidth',2);
l2=plot(ax(2),time,hp7,time(1:36:end),hp7(1:36:end,:),'^','color',dg,'linewidth',2);
l3=plot(ax(2),time,b3,'--','color',do,'linewidth',2);
set(gcf, 'CurrentAxes', ax(1)) ,ylabel('Percent deviation from trend')
set(ax(1),'xlim',[time(1),time(end)],'YTick',[],'YTicklabel',[]),
set(ax(2),'xlim',[time(1),time(end)],'ylim',[-65,85],'YTick',-100:20:100,'YTicklabel',-100:20:100,'YColor','k'),
legend([l1;l2(2);l3(1);l4],{'Benchmark','Higher smoothness','Lower smoothness','MA band'},'location','southoutside','orientation','horizontal','FontSize',10,'edgecolor','none');
set(findall(gcf,'type','text'),'FontSize',11) 
set(findall(gcf,'type','axes'),'FontSize',11)
set(gcf,'PaperUnits', 'inches','PaperPosition',[0,0,7,4]);
print(gcf,'fig2_B','-depsc')
%% Figure 2C
hhd=100.*(data(:,strmatch('PRYhhd_cycle',text,'exact')))./data(:,strmatch('PRYhhd_trend',text,'exact')); %household debt service ratio
pfd=100.*(data(:,strmatch('PRYpfd_cycle',text,'exact')))./data(:,strmatch('PRYpfd_trend',text,'exact')); %household debt service ratio
figure('visible','on')
[ax,h1,p2]=plotyy(time,data(:,strmatch('PRO',text,'exact')),time,pry,'bar','plot');box off;
set(h1,'facecolor',[215 215 215]./255,'EdgeColor',[215 215 215]./255);hold(ax(1),'on');hold(ax(2),'on'),
plot(ax(2),time,zeros(size(data(:,strmatch('PRY_cycle',text,'exact')))),'-r','linewidth',1)
l1=plot(ax(2),time,pry,'-b','linewidth',2);
l2=plot(ax(2),time,hhd,'linestyle','--','color',pl,'linewidth',2);
l3=plot(ax(2),time,pfd,time(1:36:end),pfd(1:36:end,:),'^','color',dg,'linewidth',2);
% l4=plot(ax(2),time,prd,'-.k','linewidth',1);
set(gcf, 'CurrentAxes', ax(1)) ,ylabel('Percent deviation from trend')
set(ax(1),'xlim',[time(1),time(end)],'YTick',[],'YTicklabel',[]),
set(ax(2),'xlim',[time(1),time(end)],'ylim',[-65,85],'YTick',-100:20:100,'YTicklabel',-100:20:100,'YColor','k'),
legend([l1;l3(2);l2],{'Benchmark','Total private debt-to-GDP','Household debt-to-GDP'},'location','southoutside','orientation','horizontal','FontSize',10,'edgecolor','none');
set(findall(gcf,'type','text'),'FontSize',11) 
set(findall(gcf,'type','axes'),'FontSize',11)
set(gcf,'PaperUnits', 'inches','PaperPosition',[0,0,7,4]);
print(gcf,'fig2_C','-depsc')
