% CAPITAL CONTROL PROJECT
% Author: Muratori Umberto
% Date: July, 18th 2013

%PURPOSE: CALCULATE BOOM AND BUST AND CONSTRUCT CHARTS AND TABLES FOR
%OVERALL INDEX, INDEX SPLIT FOR COUNTRIES REER


format long g

%list of tasks made in the code:
% TASK 1= IMPORT AND MANIPULATE REER DATA
% TASK 2= QUADRATIC DETREND, PLOTS AND DESCRIPTIVE STATISTICS FOR REER
% TASK 3= IMPORT AND MANIPULATE SCHINDLER INDEX
% TASK 4= LINEAR DETREND FOR SCHINDLER INDEX
% TASK 5= IDENTIFY BOOM AND BUST AND DESCRIPITVE STATISTICS
% TASK 6= BOOM/BUST AND DETRENDED INDEX
% TASK 7= NEW FIGURE 6 (MERGED OLD FIGURES 8 AND 9 -REER AND CA TO GDP RATIO-)
% TASK 8= NEW FIGURE 6b
% TASK 9= ONLINE APPENDIX FIGURES 6.A.3, 6.A.4
% TASK 10= ONLINE APPENDIX FIGURES 6.A.1, 6.A.2

%data path
BasePath=pwd;

global num_c vect_adv num_adv vect_mid num_mid vect_poor num_poor schindler_years Countries Years

%# of years of the Schindler Index
schindler_years=17;

num_index=3;
num_reer=1;
num_subindex=6;

% Definition country groups and # of member in each group (no Bahamas,
% Barbados and Trinidad/Tobago)
vect_adv=[1 2 3 6 12 15 16 17 18 20 21 23 24 29 30 31 35 39 40 41 44 45];
num_adv=size(vect_adv,2);
vect_mid=[5 7 8 9 11 13 14 19 22 25 26 27 36 37 38 43 46 47];
num_mid=size(vect_mid,2);
vect_poor=[4 10 28 32 33 34 42 48];
num_poor=size(vect_poor,2);

switch variable

    case 1

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%IMPORT AND MANIPULATE REER DATA%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Import data on REER
if mac_user==1
    [vardat,textdata, raw]=xlsread(strcat(BasePath, '\ReeR.xls') ,char('DATA_1')); 
else
    [vardat,textdata, raw]=xlsread(strcat(BasePath, '/ReeR.xls') ,char('DATA_1'));
end

%Definition of the time span
Years = raw(1,2:end);
num_years=size(Years,2);
%Names of countries in full sample
Countries = raw(2:end,1);

%# of countries in the full sample
Full_sample=size(vardat,1); 

%Creates a index with teh position of the first non NaN value for each cuntry
for i = 1:Full_sample;
idx_first(i,1) = find(sum(~isnan(vardat(i,:)),1) > 0, 1 ,'first');
end

%Eliminate from the sample the countries that have less than 25 yeasr observation
 period_cutoff=num_years-24;
 count=1;                                % Brief Description of the command:
 for i=1:Full_sample                     % for each country, if the nn NaN value
    if idx_first(i,1)<=period_cutoff      % starts before period 25, I keep the country
    vardat_1(count,:)=vardat(i,:);       % series and name and the # of NaN Observations.
    Countries_1(count,1)=Countries(i,1); % Rearrange all in matrices with new dimensions
    idx_first_1(count,1)=idx_first(i,1); % Finally, substitute the old vectors with
    count=count+1;                       % the new ones    
    end
 end
 clear vardat Countries idx_first
 vardat=vardat_1;
 Countries=Countries_1;
 idx_first=idx_first_1;
 clear vardat_1 Countries_1 idx_first_1
 
% %CHANGE MANUALLY!!!!!!!!!
% %ELIMINATE BAHRAIN because there are missing observation for the Schindler Index in 2007
vardat=[vardat(1:2,:); vardat(4:end,:)]; 
Countries=[Countries(1:2,:); Countries(4:end,:)];
idx_first=[idx_first(1:2,:); idx_first(4:end,:)];

%# of countries used in the research (full sample minus countries with less than 25 years)
num_c=size(vardat,1);

%Effective # of valid observations for each country
for i = 1:num_c;
lenght(i,1) = size((vardat(2,:)),2)- idx_first(i,1) +1;
end

%Create Log of REER
log_REER = log(vardat);

save  case_REER1 log_REER lenght Countries idx_first num_c Years Full_sample

    case 2
        
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%QUADRATIC TREND FOR REER AND PLOTS%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load case_REER1

%% Quadratic Trend and Plot real exchange rate and quadratic trend

% To make the plots unocmment the part of script in the function
% quadratic_trend
ylabel={'Real Effective Exchange Rate'};

[reert] = quadratic_trend(lenght, idx_first, log_REER, ylabel);

%Create matrices for the output gap divided by country groups
[reert_adv, reert_mid, reert_poor] = country_groups(num_reer, reert);

save case_REER2 reert reert_adv reert_mid reert_poor

    case 3

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%IMPORT AND MANIPULATE SCHINDLER INDEX%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load case_REER1
load case_REER2

%% Import data on Schindler Index
if mac_user==1
[vardat_2,textdata_2, raw_2]=xlsread(strcat(BasePath, '\FRU_dataset.xls') ,char('index_paper'));
else
[vardat_2,textdata_2, raw_2]=xlsread(strcat(BasePath, '/FRU_dataset.xls') ,char('index_paper'));    
end

%Import Schindler index data
index_data= vardat_2;

%Create matrix only with cumulative indexes (ka,kai, kao) and reshape the
%matrix
for i=1:Full_sample
     for j=1:schindler_years
         for k=1:num_index
             index_matrix(i,j,k) = vardat_2(j+(17*(i-1)), 1+k);
         end
     end
end

%CHANGE MANUALLY!!!!!!!!!
%ELIMINATE COUNTRIES WITH LESS THAN 25 YEAR OBSERVATIONS FOR REER AND THE BAHRAIN THAT HAS LESS THAN 17 OBSERVATION FOR INDEX
index_matrix=[index_matrix(3:4,:,:); index_matrix(7:9,:,:); index_matrix(13:18,:,:);index_matrix(20:22,:,:);index_matrix(25:26,:,:);index_matrix(28,:,:);index_matrix(30,:,:);
index_matrix(33:34,:,:);index_matrix(37:39,:,:);index_matrix(41,:,:);index_matrix(49:50,:,:);index_matrix(52,:,:);index_matrix(54:56,:,:);index_matrix(58,:,:); 
index_matrix(60,:,:); index_matrix(62,:,:);index_matrix(64:65,:,:);index_matrix(69:70,:,:);index_matrix(72:73,:,:);index_matrix(76:77,:,:);
index_matrix(80:81,:,:);index_matrix(85:87,:,:);index_matrix(89,:,:);index_matrix(91,:,:)];

%Create matrices for the index divided by country groups
[index_adv, index_mid, index_poor] = country_groups(num_index, index_matrix);

save case_REER3 index_matrix index_data

    case 4

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%LINEAR DETREND FOR SCHINDLER INDEX%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load case_REER1
load case_REER2
load case_REER3

%% Remove the country specific linear trend and divide by income groups

% Linear detrending for the Schindler Index
[indext] = linear_trend(num_index, schindler_years, index_matrix);

%Create vector with detrended index for the different inocme groups
[indext_adv, indext_mid, indext_poor] = country_groups(num_index, indext);

save case_REER4 indext indext_adv indext_mid indext_poor

 case 5

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%IDENTIFY BOOM AND BUST AND DESCRIPITVE STATISTICS%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load case_REER1
load case_REER2
load Result_CA5

%% Identify Boom and Bust

%Create the matrices for booms and bustes
[boom_matrix, bust_matrix] = identify_boom_bust(reert);

% Calculate the descriptive statistics for booms and bustes
[num_boom, num_bust, duration_boom, duration_bust, amplitude_average_boom, amplitude_average_bust, average_boom, average_bust] = des_stats_boom_bust(num_c, boom_matrix, bust_matrix);

%% Boom and bust episodes divided by income groups

%Divide booms in country groups 
[boom_adv, boom_mid, boom_poor] = country_groups_boom_bust(num_reer, num_boom, boom_matrix, boom_matrix);

%Divide bustes in country groups
[bust_adv, bust_mid, bust_poor] = country_groups_boom_bust(num_reer,num_bust, bust_matrix, bust_matrix);

%% Calculate number, diration and amplitude for boom and bust divided by country groups

%ADVANCED
% Calculate the descriptive statistics for booms and bustes
[num_boom_adv, num_bust_adv, duration_boom_adv, duration_bust_adv, amplitude_average_boom_adv, amplitude_average_bust_adv, average_boom_adv, average_bust_adv] = des_stats_boom_bust(num_adv, boom_adv, bust_adv);

%MIDDLE
% Calculate the descriptive statistics for booms and bustes
[num_boom_mid, num_bust_mid, duration_boom_mid, duration_bust_mid, amplitude_average_boom_mid, amplitude_average_bust_mid, average_boom_mid, average_bust_mid] = des_stats_boom_bust(num_mid, boom_mid, bust_mid);

%POOR
% Calculate the descriptive statistics for booms and bustes
[num_boom_poor, num_bust_poor, duration_boom_poor, duration_bust_poor, amplitude_average_boom_poor, amplitude_average_bust_poor, average_boom_poor, average_bust_poor] = des_stats_boom_bust(num_poor, boom_poor, bust_poor);

%% Make Tables for descriptive statistics boom and bust

%Define labels for descriptive statistics table
Labels_v = {'Number of episodes'; 'Episodes per country'; 'Average Duration (years)'; 'Average Amplitude'};
Labels_h = {'','Total', 'Advanced Economies', 'Emerging Markets', 'Low-Income Countries'};
empty_vector={'','','','',''};

%Create vectors for title for descriptive statistics on Boom and bust
title_des_ca_reer={'Table A.9. Real Effective Exchange Rate and Current Account: Descriptive Statistics','','','',''};
subtitle_boom_des={'(a) Real Effective Exchange Rate-Boom','','','',''};
subtitle_bust_des={'(b) Real Effective Exchange Rate-Bust','','','',''};

%Create tables for descriptive statistics of the boom
boom_des=[num_boom num_boom_adv num_boom_mid num_boom_poor
    average_boom average_boom_adv average_boom_mid average_boom_poor
    duration_boom duration_boom_adv duration_boom_mid duration_boom_poor
    amplitude_average_boom amplitude_average_boom_adv amplitude_average_boom_mid amplitude_average_boom_poor];

%Report only two decimals
boom_des=roundTo(boom_des,2);

boom_des1 = num2cell(boom_des);
table_boom_des_reer = [subtitle_boom_des; Labels_v  boom_des1];

%Create tables for descriptive statistics of the bust
bust_des=[num_bust num_bust_adv num_bust_mid num_bust_poor
    average_bust average_bust_adv average_bust_mid average_bust_poor
    duration_bust duration_bust_adv duration_bust_mid duration_bust_poor
    amplitude_average_bust amplitude_average_bust_adv amplitude_average_bust_mid amplitude_average_bust_poor];

%Report only two decimals
bust_des=roundTo(bust_des,2);

bust_des1=num2cell(bust_des);
table_bust_des_reer = [subtitle_bust_des; Labels_v  bust_des1];

Table_boom_bust_ca_reer=[title_des_ca_reer; 
    Labels_h; 
    table_boom_des_reer; 
    empty_vector;
    table_bust_des_reer;
    empty_vector;
    table_boom_des_ca;
    empty_vector;
    table_bust_des_ca];

save case_REER5 boom_matrix bust_matrix boom_adv boom_mid boom_poor bust_adv bust_mid bust_poor num_boom num_bust
save Result_REER5 Table_boom_bust_ca_reer

    case 6

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%BOOM/BUST AND DETRENDED INDEX%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load case_REER1
load case_REER2
load case_REER3
load case_REER4
load case_REER5

%% Create average boom and bust values for t=-2/-1/0/+1/+2 (reference point the boom/bust)

%BOOM
%Calculate average, standard deviation and confidence interval
[average_boom_values,~,~,~, boom_values] = average_boom_bust(num_reer, num_boom, boom_matrix, reert);

%BUST
%Calculate average, standard deviation and confidence interval
[average_bust_values,~,~,~, bust_values] = average_boom_bust(num_reer, num_bust, bust_matrix, reert);

%% Create average detrended index and confidence interval for t=-2/-1/0/+1/+2 (refrence point the boom/bust)

%BOOM
%Calculate average, standard deviation and confidence interval
[average_indext_values_boom, std_indext_values_boom, upper_bound_boom, lower_bound_boom, indext_values_boom] = average_boom_bust(num_index, num_boom, boom_matrix, indext);

%BUST
%Calculate average, standard deviation and confidence interval
[average_indext_values_bust, std_indext_values_bust, upper_bound_bust, lower_bound_bust, indext_values_bust] = average_boom_bust(num_index, num_bust, bust_matrix, indext);

%% Make plots with average detrended index and average real exchnage rate (reference point boom/bust)

x=[-2 -1 0 1 2];
label_x=[-2 -1 0 +1 +2];

save case_REER7 average_boom_values average_bust_values boom_values bust_values average_indext_values_boom std_indext_values_boom upper_bound_boom lower_bound_boom indext_values_boom average_indext_values_bust std_indext_values_bust upper_bound_bust lower_bound_bust indext_values_bust

%% Create matrices as above divided by country groups

%BOOM
%Divide ouput gap in country groups
[boom_values_adv,  boom_values_mid,  boom_values_poor] = country_groups_boom_bust(num_reer, num_boom, boom_matrix, boom_values);

%Divide detrended index in country groups
[indext_values_boom_adv,  indext_values_boom_mid,  indext_values_boom_poor] = country_groups_boom_bust1(num_index, num_boom, boom_matrix, indext_values_boom);

%Create matrix for each group and index
indext_values_boom_adv_overall=indext_values_boom_adv(:,:,1);
indext_values_boom_adv_inflow=indext_values_boom_adv(:,:,2);
indext_values_boom_adv_outflow=indext_values_boom_adv(:,:,3);
indext_values_boom_mid_overall=indext_values_boom_mid(:,:,1);
indext_values_boom_mid_inflow=indext_values_boom_mid(:,:,2);
indext_values_boom_mid_outflow=indext_values_boom_mid(:,:,3);
indext_values_boom_poor_overall=indext_values_boom_poor(:,:,1);
indext_values_boom_poor_inflow=indext_values_boom_poor(:,:,2);
indext_values_boom_poor_outflow=indext_values_boom_poor(:,:,3);
 
%Eliminate all rows with zero values
indext_values_boom_adv_overall(~any(indext_values_boom_adv_overall,2),:,:)=[];
indext_values_boom_adv_inflow(~any(indext_values_boom_adv_inflow,2),:,:)=[];
indext_values_boom_adv_outflow(~any(indext_values_boom_adv_outflow,2),:,:)=[];
indext_values_boom_mid_overall(~any(indext_values_boom_mid_overall,2),:,:)=[];
indext_values_boom_mid_inflow(~any(indext_values_boom_mid_inflow,2),:,:)=[];
indext_values_boom_mid_outflow(~any(indext_values_boom_mid_outflow,2),:,:)=[];
indext_values_boom_poor_overall(~any(indext_values_boom_poor_overall,2),:,:)=[];
indext_values_boom_poor_inflow(~any(indext_values_boom_poor_inflow,2),:,:)=[];
indext_values_boom_poor_outflow(~any(indext_values_boom_poor_outflow,2),:,:)=[];

%BUST
%Divide output gap in country groups
[bust_values_adv, bust_values_mid, bust_values_poor] = country_groups_boom_bust(num_reer, num_bust, bust_matrix, bust_values);

%Divide detrended index in country groups
[indext_values_bust_adv, indext_values_bust_mid, indext_values_bust_poor] = country_groups_boom_bust1(num_index, num_bust, bust_matrix, indext_values_bust);

%Create matrix for each group and index
indext_values_bust_adv_overall=indext_values_bust_adv(:,:,1);
indext_values_bust_adv_inflow=indext_values_bust_adv(:,:,2);
indext_values_bust_adv_outflow=indext_values_bust_adv(:,:,3);
indext_values_bust_mid_overall=indext_values_bust_mid(:,:,1);
indext_values_bust_mid_inflow=indext_values_bust_mid(:,:,2);
indext_values_bust_mid_outflow=indext_values_bust_mid(:,:,3);
indext_values_bust_poor_overall=indext_values_bust_poor(:,:,1);
indext_values_bust_poor_inflow=indext_values_bust_poor(:,:,2);
indext_values_bust_poor_outflow=indext_values_bust_poor(:,:,3);

%Eliminate all rows with zero values
indext_values_bust_adv_overall(~any(indext_values_bust_adv_overall,2),:,:)=[];
indext_values_bust_adv_inflow(~any(indext_values_bust_adv_inflow,2),:,:)=[];
indext_values_bust_adv_outflow(~any(indext_values_bust_adv_outflow,2),:,:)=[];
indext_values_bust_mid_overall(~any(indext_values_bust_mid_overall,2),:,:)=[];
indext_values_bust_mid_inflow(~any(indext_values_bust_mid_inflow,2),:,:)=[];
indext_values_bust_mid_outflow(~any(indext_values_bust_mid_outflow,2),:,:)=[];
indext_values_bust_poor_overall(~any(indext_values_bust_poor_overall,2),:,:)=[];
indext_values_bust_poor_inflow(~any(indext_values_bust_poor_inflow,2),:,:)=[];
indext_values_bust_poor_outflow(~any(indext_values_bust_poor_outflow,2),:,:)=[];

%% Calculate average and standard deviations for income group

%BOOM
% Average output gap for the different time horizons
for i=1:5
    average_boom_values_adv(1,i)=nanmean(boom_values_adv(:,i+1),1);
    average_boom_values_mid(1,i)=nanmean(boom_values_mid(:,i+1),1);
    average_boom_values_poor(1,i)=nanmean(boom_values_poor(:,i+1),1);
end    

% Average, standard deviations and confidence intervals of the detrended index for the different
% time horizons: ADVANCED COUNTRIES
[average_indext_values_boom_adv, std_indext_values_boom_adv, upper_bound_boom_adv, lower_bound_boom_adv] = average_boom_bust_country_group(num_index, indext_values_boom_adv_overall, indext_values_boom_adv_inflow, indext_values_boom_adv_outflow);

% Average, standard deviations and confidence intervals of the detrended index for the different
% time horizons: MIDDLE INCOME COUNTRIES
[average_indext_values_boom_mid, std_indext_values_boom_mid, upper_bound_boom_mid, lower_bound_boom_mid] = average_boom_bust_country_group(num_index, indext_values_boom_mid_overall, indext_values_boom_mid_inflow, indext_values_boom_mid_outflow);

% Average, standard deviations and confidence intervals of the detrended index for the different
% time horizons: POOR COUNTRIES
[average_indext_values_boom_poor, std_indext_values_boom_poor, upper_bound_boom_poor, lower_bound_boom_poor] = average_boom_bust_country_group(num_index, indext_values_boom_poor_overall, indext_values_boom_poor_inflow, indext_values_boom_poor_outflow);

%BUST
% Average output gap for the different time horizons
for i=1:5
    average_bust_values_adv(1,i)=nanmean(bust_values_adv(:,i+1),1);
    average_bust_values_mid(1,i)=nanmean(bust_values_mid(:,i+1),1);
    average_bust_values_poor(1,i)=nanmean(bust_values_poor(:,i+1),1);
end    

% Average, standard deviations and confidence intervals of the detrended index for the different
% time horizons: ADVANCED COUNTRIES
[average_indext_values_bust_adv, std_indext_values_bust_adv,  upper_bound_bust_adv, lower_bound_bust_adv] = average_boom_bust_country_group(num_index, indext_values_bust_adv_overall, indext_values_bust_adv_inflow, indext_values_bust_adv_outflow);
 
% Average, standard deviations and confidence intervals of the detrended index for the different
% time horizons: MIDDLE INCOME COUNTRIES
[average_indext_values_bust_mid, std_indext_values_bust_mid,  upper_bound_bust_mid, lower_bound_bust_mid] = average_boom_bust_country_group(num_index, indext_values_bust_mid_overall, indext_values_bust_mid_inflow, indext_values_bust_mid_outflow);
 
% Average, standard deviations and confidence intervals of the detrended index for the different
% time horizons: POOR COUNTRIES
[average_indext_values_bust_poor, std_indext_values_bust_poor,  upper_bound_bust_poor, lower_bound_bust_poor] = average_boom_bust_country_group(num_index, indext_values_bust_poor_overall, indext_values_bust_poor_inflow, indext_values_bust_poor_outflow);
 
%% Make plots for country groups

% BOOM
figure(2)
% Inflow-Advanced countries
subplot(3,2,1)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom_adv(1,:,2), 'b', 100*average_boom_values_adv ,'r-*','plot');
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_boom_adv(1,:,2)),2) roundTo(1.75*max(upper_bound_boom_adv(1,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_adv(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_boom_adv(1,:,2)),2)], 'YTickLabel',[roundTo(1.75*min(lower_bound_boom_adv(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_boom_adv(1,:,2)),2)],'FontSize',10);
t=title(' Inflows','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_adv(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_adv(1,:,2),'k-.');
box off
hold off
% Inflows-Middle Income countries
subplot(3,2,3)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom_mid(1,:,2), 'b', 100*average_boom_values_mid ,'r-*','plot');
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_boom_mid(1,:,2)),2) roundTo(1.75*max(upper_bound_boom_mid(1,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_mid(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_boom_mid(1,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_mid(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_boom_mid(1,:,2)),2)],'FontSize',10);
t=title('Inflows','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_mid(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_mid(1,:,2),'k-.');
box off
hold off
% Inflows-Developing Countries
subplot(3,2,5)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom_poor(1,:,2), 'b', 100*average_boom_values_poor ,'r-*','plot');
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_boom_poor(1,:,2)),2) roundTo(1.75*max(upper_bound_boom_poor(1,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_poor(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_boom_poor(1,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_poor(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_boom_poor(1,:,2)),2)],'FontSize',10);
t=title('Inflows','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_poor(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_poor(1,:,2),'k-.');
box off
hold off
% Outflow-Advanced countries
subplot(3,2,2)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom_adv(1,:,3), 'b', 100*average_boom_values_adv ,'r-*','plot');
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_boom_adv(1,:,3)),2) roundTo(1.75*max(upper_bound_boom_adv(1,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_adv(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_boom_adv(1,:,3)),2)], 'YTickLabel',[roundTo(1.75*min(lower_bound_boom_adv(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_boom_adv(1,:,3)),2)],'FontSize',10);
t=title(' Outflows','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_adv(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_adv(1,:,3),'k-.');
box off
hold off
% Outflows-Middle Income countries
subplot(3,2,4)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom_mid(1,:,3), 'b', 100*average_boom_values_mid ,'r-*','plot');
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_boom_mid(1,:,3)),2) roundTo(1.75*max(upper_bound_boom_mid(1,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_mid(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_boom_mid(1,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_mid(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_boom_mid(1,:,3)),2)],'FontSize',10);
t=title('Outflows','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_mid(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_mid(1,:,3),'k-.');
box off
hold off
% Outflows-Developing Countries
subplot(3,2,6)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom_poor(1,:,3), 'b', 100*average_boom_values_poor ,'r-*','plot');
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_boom_poor(1,:,3)),2) roundTo(1.75*max(upper_bound_boom_poor(1,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_poor(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_boom_poor(1,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_poor(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_boom_poor(1,:,3)),2)],'FontSize',10);
t=title('Outflows','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_poor(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_poor(1,:,3),'k-.');
box off
hold off
legend1 = legend([hline1 hline2 hline3],'Average Index(lhs)','Average Real Effective Exchange Rate(rhs)','Two Standard Deviation for the Index(lhs)');
set(legend1,'Orientation','horizontal','Position',[0.360807291666667 0.0177845528455285 0.336979166666667 0.0274390243902439],'FontSize',8,'Box','off');
ha = axes('Position',[0 0 1 1],'Xlim',[0 1],'Ylim',[0.1 1],'Box','off','Visible','off','Units','normalized', 'clipping' , 'off');
text(0.5, 1,'\bf Figure A.6.1. Boom episodes in Real Exchange Rates and Capital Controls by Country Category','Position',[0.508203125 0.978604294478528 9.16025403784439],'HorizontalAlignment','center')
sub_ha1 = axes('Visible','off','Position',[0 0 1 1]);
text('Parent',sub_ha1,'String','\bf (a)Advanced Economies','Position',[0.514453125 0.954180064308682 9.16025403784439],'HorizontalAlignment','center');
sub_ha2 = axes('Visible','off','Position',[0 0 1 1]);
text('Parent',sub_ha2,'String','\bf (b) Emerging Markets','Position',[0.519921875 0.660239361702128 9.16025403784439],'HorizontalAlignment','center');
sub_ha3 = axes('Visible','off','Position',[0 0 1 1]);
text('Parent',sub_ha3,'String','\bf (c) Low-Income Countries','Position',[0.517578125 0.358702640642939 9.16025403784439],'HorizontalAlignment','center');


% BUST
figure(3)
%Inflow-Advanced countries
subplot(3,2,1)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust_adv(1,:,2), 'b', 100*average_bust_values_adv ,'r-*','plot');
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_bust_adv(1,:,2)),2) roundTo(1.75*max(upper_bound_bust_adv(1,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust_adv(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_bust_adv(1,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust_adv(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_bust_adv(1,:,2)),2)],'FontSize',10);
t=title('Inflows','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_bust_adv(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust_adv(1,:,2),'k-.');
box off
hold off
% Inflow-Middle Income countries
subplot(3,2,3)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust_mid(1,:,2), 'b', 100*average_bust_values_mid ,'r-*','plot');
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_bust_mid(1,:,2)),2) roundTo(1.75*max(upper_bound_bust_mid(1,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust_mid(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_bust_mid(1,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust_mid(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_bust_mid(1,:,2)),2)],'FontSize',10);
t=title('Inflows','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_bust_mid(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust_mid(1,:,2),'k-.');
box off
hold off
% Inflows-Low Income Countries
subplot(3,2,5)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust_poor(1,:,2), 'b', 100*average_bust_values_poor ,'r-*','plot');
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_bust_poor(1,:,2)),2) roundTo(1.75*max(upper_bound_bust_poor(1,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust_poor(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_bust_poor(1,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust_poor(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_bust_poor(1,:,2)),2)],'FontSize',10);
t=title('Inflows','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_bust_poor(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust_poor(1,:,2),'k-.');
box off
hold off
%Outflow-Advanced countries
subplot(3,2,2)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust_adv(1,:,3), 'b', 100*average_bust_values_adv ,'r-*','plot');
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_bust_adv(1,:,3)),2) roundTo(1.75*max(upper_bound_bust_adv(1,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust_adv(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_bust_adv(1,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust_adv(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_bust_adv(1,:,3)),2)],'FontSize',10);
t=title('Outflows','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_bust_adv(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust_adv(1,:,3),'k-.');
box off
hold off
% Outflow-Middle Income countries
subplot(3,2,4)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust_mid(1,:,3), 'b', 100*average_bust_values_mid ,'r-*','plot');
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_bust_mid(1,:,3)),2) roundTo(1.75*max(upper_bound_bust_mid(1,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust_mid(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_bust_mid(1,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust_mid(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_bust_mid(1,:,3)),2)],'FontSize',10);
t=title('Outflows','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_bust_mid(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust_mid(1,:,3),'k-.');
box off
hold off
% Outflows-Low Income Countries
subplot(3,2,6)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust_poor(1,:,3), 'b', 100*average_bust_values_poor ,'r-*','plot');
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_bust_poor(1,:,3)),2) roundTo(1.75*max(upper_bound_bust_poor(1,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust_poor(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_bust_poor(1,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust_poor(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_bust_poor(1,:,3)),2)],'FontSize',10);
t=title('Outflows','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_bust_poor(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust_poor(1,:,3),'k-.');
box off
hold off
legend1 = legend([hline1 hline2 hline3],'Average Index(lhs)','Average Real Exchange Rate(rhs)','Two Standard Deviation for the Index(lhs)');
set(legend1,'Orientation','horizontal','Position',[0.360807291666667 0.0177845528455285 0.336979166666667 0.0274390243902439],'FontSize',8, 'Box','off');
ha = axes('Position',[0 0 1 1],'Xlim',[0 1],'Ylim',[0.1 1],'Box','off','Visible','off','Units','normalized', 'clipping' , 'off');
text(0.5, 1,'\bf Figure A.6.2. Bust episodes in Real Exchange Rates and Capital Controls by Country Category','Position',[0.508203125 0.978604294478528 9.16025403784439],'HorizontalAlignment','center')
sub_ha1 = axes('Visible','off','Position',[0 0 1 1]);
text('Parent',sub_ha1,'String','\bf (a)Advanced Economies','Position',[0.514453125 0.954180064308682 9.16025403784439],'HorizontalAlignment','center');
sub_ha2 = axes('Visible','off','Position',[0 0 1 1]);
text('Parent',sub_ha2,'String','\bf (b) Emerging Markets','Position',[0.519921875 0.660239361702128 9.16025403784439],'HorizontalAlignment','center');
sub_ha3 = axes('Visible','off','Position',[0 0 1 1]);
text('Parent',sub_ha3,'String','\bf (c) Low-Income Countries','Position',[0.517578125 0.358702640642939 9.16025403784439],'HorizontalAlignment','center');


    case 7
%% JAVIER Load all relevant cases for the constuction of new Figure 6. Note that results froma  different script are used (i.e. CA results)

load case_REER7
load case_CA7
        
%% JAVIER This is going to be the new Figure 6

x=[-2 -1 0 1 2];
label_x=[-2 -1 0 +1 +2];

% ROW 1: INFLOWS BOOM AND BUST FOR THE REER

% INFLOW AND OUTFLOW INDEXES
figure(1)
%Boom Inflow
subplot(2,2,1)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom(1,:,2), 'b', 100*average_boom_values ,'r-*','plot');
set(haxes, 'xTickLabel','')
set(hline1, 'LineWidth',2)
set(hline2, 'LineWidth',2)
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_boom(1,:,2)),2) roundTo(1.75*max(upper_bound_boom(1,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_boom(1,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_boom(1,:,2)),2)],'FontSize',10);
t=title('Boom','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom(1,:,2),'k-.');
box off
hold off

% Bust Inflow
subplot(2,2,2)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust(1,:,2), 'b', 100*average_bust_values ,'r-*','plot');
set(hline1, 'LineWidth',2)
set(hline2, 'LineWidth',2)
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_bust(1,:,2)),2) roundTo(1.75*max(upper_bound_bust(1,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_bust(1,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_bust(1,:,2)),2)],'FontSize',10);
t=title('Bust','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_bust(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust(1,:,2),'k-.');
box off
hold off

% ROW 2: INFLOWS FOR BOOM AND BUST FOR THE CURRENT ACCOUNT TO GDP RATIO

%Boom Inflow
subplot(2,2,3)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom_ca(1,:,2), 'b',average_boom_values_ca ,'r-*','plot');
set(haxes, 'xTickLabel','')
set(hline1, 'LineWidth',2)
set(hline2, 'LineWidth',2)
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_boom_ca(1,:,2)),2) roundTo(1.75*max(upper_bound_boom_ca(1,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_ca(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_boom_ca(1,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_ca(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_boom_ca(1,:,2)),2)],'FontSize',10);
t=title('Boom','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_ca(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_ca(1,:,2),'k-.');
box off
hold off

% Bust Inflow
subplot(2,2,4)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust_ca(1,:,2), 'b',average_bust_values_ca ,'r-*','plot');
set(haxes, 'xTickLabel','')
set(hline1, 'LineWidth',2)
set(hline2, 'LineWidth',2)
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_bust_ca(1,:,2)),2) roundTo(1.75*max(upper_bound_bust_ca(1,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust_ca(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_bust_ca(1,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust_ca(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_bust_ca(1,:,2)),2)],'FontSize',10);
t=title('Bust','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_bust_ca(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust_ca(1,:,2),'k-.');
box off
hold off

legend1 = legend([hline1 hline2 hline3],'Average Inflow Index(lhs)','Average Rate(rhs)','Two Standard Deviation for the Index(lhs)');
set(legend1,'Orientation','horizontal','Position',[0.360807291666667 0.0177845528455285 0.336979166666667 0.0274390243902439],'FontSize',8, 'Box','off');
ha = axes('Position',[0 0 1 1],'Xlim',[0 1],'Ylim',[0.1 1],'Box','off','Visible','off','Units','normalized', 'clipping' , 'off');
text(0.5, 1,'\bf  Figure 6. Capital Controls During Booms and Busts In The Real Exchange Rate and the Current Account','Position',[0.508203125 0.978604294478528 9.16025403784439],'HorizontalAlignment','center')
sub_ha1 = axes('Visible','off','Position',[0 0 1 1]);
text('Parent',sub_ha1,'String','\bf (a) Real Exchange Rate','Position',[0.514453125 0.954180064308682 9.16025403784439],'HorizontalAlignment','center');
sub_ha2 = axes('Visible','off','Position',[0 0 1 1]);
text('Parent',sub_ha2,'String','\bf (b) Current-Account-To-GDP Ratio','Position',[0.517578125 0.508702640642939 9.16025403784439],'HorizontalAlignment','center');


    case 8
%% JAVIER Load all relevant cases for the constuction of new Figure 6. Note that results froma  different script are used (i.e. CA results)

load case_REER7
load case_CA7
        
%% JAVIER This is going to be the new Figure 6b

x=[-2 -1 0 1 2];
label_x=[-2 -1 0 +1 +2];

% ROW 1: OUTFLOWS BOOM AND BUST FOR THE REER

% OUTFLOW INDEXES
figure(1)
%Boom Outflow
subplot(2,2,1)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom(1,:,3), 'b', 100*average_boom_values ,'r-*','plot');
set(haxes,'xLim', [1 5])
set(haxes, 'xTickLabel','')
set(hline1, 'LineWidth',2)
set(hline2, 'LineWidth',2)
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_boom(1,:,3)),2) roundTo(1.75*max(upper_bound_boom(1,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_boom(1,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_boom(1,:,3)),2)],'FontSize',10);
t=title('Boom','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom(1,:,3),'k-.');
box off
hold off

% Bust Outflow
subplot(2,2,2)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust(1,:,3), 'b', 100*average_bust_values ,'r-*','plot');
set(haxes,'xLim', [1 5])
set(hline1, 'LineWidth',2)
set(hline2, 'LineWidth',2)
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_bust(1,:,3)),2) roundTo(1.75*max(upper_bound_bust(1,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_bust(1,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_bust(1,:,3)),2)],'FontSize',10);
t=title('Bust','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_bust(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust(1,:,3),'k-.');
box off
hold off

% ROW 2: OUTFLOWS FOR BOOM AND BUST FOR THE CURRENT ACCOUNT TO GDP RATIO

%Boom Outflow
subplot(2,2,3)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom_ca(1,:,3), 'b',average_boom_values_ca ,'r-*','plot');
set(haxes,'xLim', [1 5])
set(haxes, 'xTickLabel','')
set(hline1, 'LineWidth',2)
set(hline2, 'LineWidth',2)
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_boom_ca(1,:,3)),2) roundTo(1.75*max(upper_bound_boom_ca(1,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_ca(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_boom_ca(1,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_ca(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_boom_ca(1,:,3)),2)],'FontSize',10);
t=title('Boom','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_ca(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_ca(1,:,3),'k-.');
box off
hold off

% Bust Outflow
subplot(2,2,4)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust_ca(1,:,3), 'b',average_bust_values_ca ,'r-*','plot');
set(haxes,'xLim', [1 5])
set(haxes, 'xTickLabel','')
set(hline1, 'LineWidth',2)
set(hline2, 'LineWidth',2)
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_bust_ca(1,:,3)),2) roundTo(1.75*max(upper_bound_bust_ca(1,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust_ca(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_bust_ca(1,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust_ca(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_bust_ca(1,:,3)),2)],'FontSize',10);
t=title('Bust','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_bust_ca(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust_ca(1,:,3),'k-.');
box off
hold off

legend1 = legend([hline1 hline2 hline3],'Average Outflow Index(lhs)','Average Rate(rhs)','Two Standard Deviation for the Index(lhs)');
set(legend1,'Orientation','horizontal','Position',[0.360807291666667 0.0177845528455285 0.336979166666667 0.0274390243902439],'FontSize',8, 'Box','off');
ha = axes('Position',[0 0 1 1],'Xlim',[0 1],'Ylim',[0.1 1],'Box','off','Visible','off','Units','normalized', 'clipping' , 'off');
sub_ha1 = axes('Visible','off','Position',[0 0 1 1]);
text('Parent',sub_ha1,'String','\bf (a) Real Exchange Rate','Position',[0.514453125 0.954180064308682 9.16025403784439],'HorizontalAlignment','center');
sub_ha2 = axes('Visible','off','Position',[0 0 1 1]);
text('Parent',sub_ha2,'String','\bf (b) Current-Account-To-GDP Ratio','Position',[0.517578125 0.508702640642939 9.16025403784439],'HorizontalAlignment','center');


    
    
    case 9
%%        

load case_REER7
load case_CA1bis
load case_CA2bis
load case_CA3bis
load case_CA4bis
load case_CA5bis
load case_CA7bis
     
num_ca=1;
num_index=3;

%BOOM
%Calculate average, standard deviation and confidence interval
[average_boom_values_ca,~,~,~, boom_values_ca] = average_boom_bust(num_ca, num_boom, boom_matrix, cat);

%BUST
%Calculate average, standard deviation and confidence interval
[average_bust_values_ca,~,~,~, bust_values_ca] = average_boom_bust(num_ca, num_bust, bust_matrix, cat);

%BOOM
%Calculate average, standard deviation and confidence interval
[average_indext_values_boom_ca, std_indext_values_boom_ca, upper_bound_boom_ca, lower_bound_boom_ca, indext_values_boom_ca] = average_boom_bust(num_index, num_boom, boom_matrix, indext_eq);

%BUST
%Calculate average, standard deviation and confidence interval
[average_indext_values_bust_ca, std_indext_values_bust_ca, upper_bound_bust_ca, lower_bound_bust_ca, indext_values_bust_ca] = average_boom_bust(num_index, num_bust, bust_matrix, indext_eq);

%BOOM
%Calculate average, standard deviation and confidence interval
[average_indext_values_boom, std_indext_values_boom, upper_bound_boom, lower_bound_boom, indext_values_boom] = average_boom_bust(num_index, num_boom, boom_matrix, indext_eq);

%BUST
%Calculate average, standard deviation and confidence interval
[average_indext_values_bust, std_indext_values_bust, upper_bound_bust, lower_bound_bust, indext_values_bust] = average_boom_bust(num_index, num_bust, bust_matrix, indext_eq);


x=[-2 -1 0 1 2];
label_x=[-2 -1 0 +1 +2];

% ROW 1: OUTFLOWS BOOM AND BUST FOR THE REER

% OUTFLOW INDEXES
figure(1)
%Boom Outflow
subplot(2,2,1)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom(1,:,3), 'b', 100*average_boom_values ,'r-*','plot');
set(haxes,'xLim', [1 5])
set(haxes, 'xTickLabel','')
set(hline1, 'LineWidth',2)
set(hline2, 'LineWidth',2)
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_boom(1,:,3)),2) roundTo(1.75*max(upper_bound_boom(1,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_boom(1,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_boom(1,:,3)),2)],'FontSize',10);
t=title('Boom','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom(1,:,3),'k-.');
box off
hold off

% Bust Outflow
subplot(2,2,2)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust(1,:,3), 'b', 100*average_bust_values ,'r-*','plot');
set(haxes,'xLim', [1 5])
set(hline1, 'LineWidth',2)
set(hline2, 'LineWidth',2)
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_bust(1,:,3)),2) roundTo(1.75*max(upper_bound_bust(1,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_bust(1,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_bust(1,:,3)),2)],'FontSize',10);
t=title('Bust','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_bust(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust(1,:,3),'k-.');
box off
hold off

% ROW 2: OUTFLOWS FOR BOOM AND BUST FOR THE CURRENT ACCOUNT TO GDP RATIO

%Boom Outflow
subplot(2,2,3)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom_ca(1,:,3), 'b',average_boom_values_ca ,'r-*','plot');
set(haxes,'xLim', [1 5])
set(haxes, 'xTickLabel','')
set(hline1, 'LineWidth',2)
set(hline2, 'LineWidth',2)
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_boom_ca(1,:,3)),2) roundTo(1.75*max(upper_bound_boom_ca(1,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_ca(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_boom_ca(1,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_ca(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_boom_ca(1,:,3)),2)],'FontSize',10);
t=title('Boom','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_ca(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_ca(1,:,3),'k-.');
box off
hold off

% Bust Outflow
subplot(2,2,4)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust_ca(1,:,3), 'b',average_bust_values_ca ,'r-*','plot');
set(haxes,'xLim', [1 5])
set(haxes, 'xTickLabel','')
set(hline1, 'LineWidth',2)
set(hline2, 'LineWidth',2)
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_bust_ca(1,:,3)),2) roundTo(1.75*max(upper_bound_bust_ca(1,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust_ca(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_bust_ca(1,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust_ca(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_bust_ca(1,:,3)),2)],'FontSize',10);
t=title('Bust','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_bust_ca(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust_ca(1,:,3),'k-.');
box off
hold off

legend1 = legend([hline1 hline2 hline3],'Average Outflow Index(lhs)','Average Rate(rhs)','Two Standard Deviation for the Index(lhs)');
set(legend1,'Orientation','horizontal','Position',[0.360807291666667 0.0177845528455285 0.336979166666667 0.0274390243902439],'FontSize',8, 'Box','off');
ha = axes('Position',[0 0 1 1],'Xlim',[0 1],'Ylim',[0.1 1],'Box','off','Visible','off','Units','normalized', 'clipping' , 'off');
text(0.5, 1,'\bf  Figure 6.A.4 Capital-Outflow Controls in Equity During Booms and Busts In The Real Exchange Rate and the Current Account','Position',[0.508203125 0.978604294478528 9.16025403784439],'HorizontalAlignment','center')
sub_ha1 = axes('Visible','off','Position',[0 0 1 1]);
text('Parent',sub_ha1,'String','\bf (a) Real Exchange Rate','Position',[0.514453125 0.954180064308682 9.16025403784439],'HorizontalAlignment','center');
sub_ha2 = axes('Visible','off','Position',[0 0 1 1]);
text('Parent',sub_ha2,'String','\bf (b) Current-Account-To-GDP Ratio','Position',[0.517578125 0.508702640642939 9.16025403784439],'HorizontalAlignment','center');


% ROW 1: INFLOWS BOOM AND BUST FOR THE REER

% INFLOW INDEXES
figure(2)
%Boom INFLOW
subplot(2,2,1)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom(1,:,2), 'b', 100*average_boom_values ,'r-*','plot');
set(haxes,'xLim', [1 5])
set(haxes, 'xTickLabel','')
set(hline1, 'LineWidth',2)
set(hline2, 'LineWidth',2)
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_boom(1,:,2)),2) roundTo(1.75*max(upper_bound_boom(1,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_boom(1,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_boom(1,:,2)),2)],'FontSize',10);
t=title('Boom','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom(1,:,2),'k-.');
box off
hold off

% Bust INFLOW
subplot(2,2,2)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust(1,:,2), 'b', 100*average_bust_values ,'r-*','plot');
set(haxes,'xLim', [1 5])
set(hline1, 'LineWidth',2)
set(hline2, 'LineWidth',2)
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_bust(1,:,2)),2) roundTo(1.75*max(upper_bound_bust(1,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_bust(1,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_bust(1,:,2)),2)],'FontSize',10);
t=title('Bust','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_bust(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust(1,:,2),'k-.');
box off
hold off

% ROW 2: INFLOWS FOR BOOM AND BUST FOR THE CURRENT ACCOUNT TO GDP RATIO

%Boom INFLOW
subplot(2,2,3)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom_ca(1,:,2), 'b',average_boom_values_ca ,'r-*','plot');
set(haxes,'xLim', [1 5])
set(haxes, 'xTickLabel','')
set(hline1, 'LineWidth',2)
set(hline2, 'LineWidth',2)
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_boom_ca(1,:,2)),2) roundTo(1.75*max(upper_bound_boom_ca(1,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_ca(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_boom_ca(1,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_ca(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_boom_ca(1,:,2)),2)],'FontSize',10);
t=title('Boom','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_ca(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_ca(1,:,2),'k-.');
box off
hold off

% Bust INFLOW
subplot(2,2,4)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust_ca(1,:,2), 'b',average_bust_values_ca ,'r-*','plot');
set(haxes,'xLim', [1 5])
set(haxes, 'xTickLabel','')
set(hline1, 'LineWidth',2)
set(hline2, 'LineWidth',2)
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_bust_ca(1,:,2)),2) roundTo(1.75*max(upper_bound_bust_ca(1,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust_ca(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_bust_ca(1,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust_ca(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_bust_ca(1,:,2)),2)],'FontSize',10);
t=title('Bust','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_bust_ca(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust_ca(1,:,2),'k-.');
box off
hold off

legend1 = legend([hline1 hline2 hline3],'Average Inflow Index(lhs)','Average Rate(rhs)','Two Standard Deviation for the Index(lhs)');
set(legend1,'Orientation','horizontal','Position',[0.360807291666667 0.0177845528455285 0.336979166666667 0.0274390243902439],'FontSize',8, 'Box','off');
ha = axes('Position',[0 0 1 1],'Xlim',[0 1],'Ylim',[0.1 1],'Box','off','Visible','off','Units','normalized', 'clipping' , 'off');
text(0.5, 1,'\bf  Figure 6.A.3 Capital-Inflow Controls in Equity During Booms and Busts In The Real Exchange Rate and the Current Account','Position',[0.508203125 0.978604294478528 9.16025403784439],'HorizontalAlignment','center')
sub_ha1 = axes('Visible','off','Position',[0 0 1 1]);
text('Parent',sub_ha1,'String','\bf (a) Real Exchange Rate','Position',[0.514453125 0.954180064308682 9.16025403784439],'HorizontalAlignment','center');
sub_ha2 = axes('Visible','off','Position',[0 0 1 1]);
text('Parent',sub_ha2,'String','\bf (b) Current-Account-To-GDP Ratio','Position',[0.517578125 0.508702640642939 9.16025403784439],'HorizontalAlignment','center');


    case 10
%% Online Appendix Figures 6.A.1, 6.A.2


load case_REER7
load case_CA1bis
load case_CA2bis
load case_CA3bis
load case_CA4bis
load case_CA5bis
load case_CA7bis
     
num_ca=1;
num_index=3;

%BOOM
%Calculate average, standard deviation and confidence interval
[average_boom_values_ca,~,~,~, boom_values_ca] = average_boom_bust(num_ca, num_boom, boom_matrix, cat);

%BUST
%Calculate average, standard deviation and confidence interval
[average_bust_values_ca,~,~,~, bust_values_ca] = average_boom_bust(num_ca, num_bust, bust_matrix, cat);

%BOOM
%Calculate average, standard deviation and confidence interval
[average_indext_values_boom_ca, std_indext_values_boom_ca, upper_bound_boom_ca, lower_bound_boom_ca, indext_values_boom_ca] = average_boom_bust(num_index, num_boom, boom_matrix, indext_bo);

%BUST
%Calculate average, standard deviation and confidence interval
[average_indext_values_bust_ca, std_indext_values_bust_ca, upper_bound_bust_ca, lower_bound_bust_ca, indext_values_bust_ca] = average_boom_bust(num_index, num_bust, bust_matrix, indext_bo);

%BOOM
%Calculate average, standard deviation and confidence interval
[average_indext_values_boom, std_indext_values_boom, upper_bound_boom, lower_bound_boom, indext_values_boom] = average_boom_bust(num_index, num_boom, boom_matrix, indext_bo);

%BUST
%Calculate average, standard deviation and confidence interval
[average_indext_values_bust, std_indext_values_bust, upper_bound_bust, lower_bound_bust, indext_values_bust] = average_boom_bust(num_index, num_bust, bust_matrix, indext_bo);


x=[-2 -1 0 1 2];
label_x=[-2 -1 0 +1 +2];

% ROW 1: OUTFLOWS BOOM AND BUST FOR THE REER

% OUTFLOW INDEXES
figure(4)
%Boom Outflow
subplot(2,2,1)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom(1,:,3), 'b', 100*average_boom_values ,'r-*','plot');
set(haxes,'xLim', [1 5])
set(haxes, 'xTickLabel','')
set(hline1, 'LineWidth',2)
set(hline2, 'LineWidth',2)
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_boom(1,:,3)),2) roundTo(1.75*max(upper_bound_boom(1,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_boom(1,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_boom(1,:,3)),2)],'FontSize',10);
t=title('Boom','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom(1,:,3),'k-.');
box off
hold off

% Bust Outflow
subplot(2,2,2)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust(1,:,3), 'b', 100*average_bust_values ,'r-*','plot');
set(haxes,'xLim', [1 5])
set(hline1, 'LineWidth',2)
set(hline2, 'LineWidth',2)
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_bust(1,:,3)),2) roundTo(1.75*max(upper_bound_bust(1,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_bust(1,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_bust(1,:,3)),2)],'FontSize',10);
t=title('Bust','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_bust(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust(1,:,3),'k-.');
box off
hold off

% ROW 2: OUTFLOWS FOR BOOM AND BUST FOR THE CURRENT ACCOUNT TO GDP RATIO

%Boom Outflow
subplot(2,2,3)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom_ca(1,:,3), 'b',average_boom_values_ca ,'r-*','plot');
set(haxes,'xLim', [1 5])
set(haxes, 'xTickLabel','')
set(hline1, 'LineWidth',2)
set(hline2, 'LineWidth',2)
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_boom_ca(1,:,3)),2) roundTo(1.75*max(upper_bound_boom_ca(1,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_ca(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_boom_ca(1,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_ca(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_boom_ca(1,:,3)),2)],'FontSize',10);
t=title('Boom','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_ca(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_ca(1,:,3),'k-.');
box off
hold off

% Bust Outflow
subplot(2,2,4)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust_ca(1,:,3), 'b',average_bust_values_ca ,'r-*','plot');
set(haxes,'xLim', [1 5])
set(haxes, 'xTickLabel','')
set(hline1, 'LineWidth',2)
set(hline2, 'LineWidth',2)
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_bust_ca(1,:,3)),2) roundTo(1.75*max(upper_bound_bust_ca(1,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust_ca(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_bust_ca(1,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust_ca(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_bust_ca(1,:,3)),2)],'FontSize',10);
t=title('Bust','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_bust_ca(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust_ca(1,:,3),'k-.');
box off
hold off

legend1 = legend([hline1 hline2 hline3],'Average Outflow Index(lhs)','Average Rate(rhs)','Two Standard Deviation for the Index(lhs)');
set(legend1,'Orientation','horizontal','Position',[0.360807291666667 0.0177845528455285 0.336979166666667 0.0274390243902439],'FontSize',8, 'Box','off');
ha = axes('Position',[0 0 1 1],'Xlim',[0 1],'Ylim',[0.1 1],'Box','off','Visible','off','Units','normalized', 'clipping' , 'off');
text(0.5, 1,'\bf  Figure 6.A.2 Capital-Outflow Controls in Bonds During Booms and Busts In The Real Exchange Rate and the Current Account','Position',[0.508203125 0.978604294478528 9.16025403784439],'HorizontalAlignment','center')
sub_ha1 = axes('Visible','off','Position',[0 0 1 1]);
text('Parent',sub_ha1,'String','\bf (a) Real Exchange Rate','Position',[0.514453125 0.954180064308682 9.16025403784439],'HorizontalAlignment','center');
sub_ha2 = axes('Visible','off','Position',[0 0 1 1]);
text('Parent',sub_ha2,'String','\bf (b) Current-Account-To-GDP Ratio','Position',[0.517578125 0.508702640642939 9.16025403784439],'HorizontalAlignment','center');

% ROW 1: INFLOWS BOOM AND BUST FOR THE REER

% INFLOW INDEXES
figure(5)
%Boom INFLOW
subplot(2,2,1)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom(1,:,2), 'b', 100*average_boom_values ,'r-*','plot');
set(haxes,'xLim', [1 5])
set(haxes, 'xTickLabel','')
set(hline1, 'LineWidth',2)
set(hline2, 'LineWidth',2)
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_boom(1,:,2)),2) roundTo(1.75*max(upper_bound_boom(1,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_boom(1,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_boom(1,:,2)),2)],'FontSize',10);
t=title('Boom','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom(1,:,2),'k-.');
box off
hold off

% Bust INFLOW
subplot(2,2,2)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust(1,:,2), 'b', 100*average_bust_values ,'r-*','plot');
set(haxes,'xLim', [1 5])
set(hline1, 'LineWidth',2)
set(hline2, 'LineWidth',2)
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_bust(1,:,2)),2) roundTo(1.75*max(upper_bound_bust(1,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_bust(1,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_bust(1,:,2)),2)],'FontSize',10);
t=title('Bust','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_bust(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust(1,:,2),'k-.');
box off
hold off

% ROW 2: INFLOWS FOR BOOM AND BUST FOR THE CURRENT ACCOUNT TO GDP RATIO

%Boom INFLOW
subplot(2,2,3)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom_ca(1,:,2), 'b',average_boom_values_ca ,'r-*','plot');
set(haxes,'xLim', [1 5])
set(haxes, 'xTickLabel','')
set(hline1, 'LineWidth',2)
set(hline2, 'LineWidth',2)
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_boom_ca(1,:,2)),2) roundTo(1.75*max(upper_bound_boom_ca(1,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_ca(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_boom_ca(1,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_ca(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_boom_ca(1,:,2)),2)],'FontSize',10);
t=title('Boom','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_ca(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_ca(1,:,2),'k-.');
box off
hold off

% Bust INFLOW
subplot(2,2,4)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust_ca(1,:,2), 'b',average_bust_values_ca ,'r-*','plot');
set(haxes,'xLim', [1 5])
set(haxes, 'xTickLabel','')
set(hline1, 'LineWidth',2)
set(hline2, 'LineWidth',2)
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(lower_bound_bust_ca(1,:,2)),2) roundTo(1.75*max(upper_bound_bust_ca(1,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust_ca(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_bust_ca(1,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust_ca(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_bust_ca(1,:,2)),2)],'FontSize',10);
t=title('Bust','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_bust_ca(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust_ca(1,:,2),'k-.');
box off
hold off

legend1 = legend([hline1 hline2 hline3],'Average Inflow Index(lhs)','Average Rate(rhs)','Two Standard Deviation for the Index(lhs)');
set(legend1,'Orientation','horizontal','Position',[0.360807291666667 0.0177845528455285 0.336979166666667 0.0274390243902439],'FontSize',8, 'Box','off');
ha = axes('Position',[0 0 1 1],'Xlim',[0 1],'Ylim',[0.1 1],'Box','off','Visible','off','Units','normalized', 'clipping' , 'off');
text(0.5, 1,'\bf  Figure 6.A.1 Capital-Inflow Controls in Bonds During Booms and Busts In The Real Exchange Rate and the Current Account','Position',[0.508203125 0.978604294478528 9.16025403784439],'HorizontalAlignment','center')
sub_ha1 = axes('Visible','off','Position',[0 0 1 1]);
text('Parent',sub_ha1,'String','\bf (a) Real Exchange Rate','Position',[0.514453125 0.954180064308682 9.16025403784439],'HorizontalAlignment','center');
sub_ha2 = axes('Visible','off','Position',[0 0 1 1]);
text('Parent',sub_ha2,'String','\bf (b) Current-Account-To-GDP Ratio','Position',[0.517578125 0.508702640642939 9.16025403784439],'HorizontalAlignment','center');

end