% Are Capital Controls Countercyclical?
% Author: Umberto Muratori and Javier Caicedo
% Email: umbertom@iadb.org - caicedo.javier@javeriana.edu.co
% Last update: August 2015

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

% List of tasks made in the code:

% TASK 1= IMPORT AND MANIPULATE GDP DATA
% TASK 2= QUADRATIC DETREND AND DESCRIPTIVE STATISTICS FOR GDP

% TASK 3= IMPORT, MANIPULATE AND PLOT THE SCHINDLER INDEX
% TASK 4= LINEAR DETREND, DESCRIPTIVE STATISTICS, TIME SERIES PLOTS AND BAR PLOT FOR SCHINDLER INDEX

% TASK 5= CALCULATE CORRELATION BETWEEN OUTPUT GAP AND INDEX, METHOD 1: STACKED MATRIX
% TASK 6= CALCULATE CORRELATION BETWEEN OUTPUT GAP AND INDEX, METHOD 2: AVERAGE CORRELATION ACROSS COUNTRIES

% TASK 7= IDENTIFY BOOM AND BUST AND DESCRIPITVE STATISTICS
% TASK 8= BOOM/BUST AND DETRENDED INDEX

% TASK 10= BOOM/BUST AND SUBCOMPONENT OF THE INDEX (FIGURES 4, 4.A.1)
% TASK 11= CALCULATE CORRELATION BETWEEN OUTPUT GAP AND INFLOW SUBINDEX, METHOD 1: STACKED MATRIX

% TASK 12= EXCHANGE RATE REGIME EXERCISE
% TASK 13= INDEBTED COUNTRIES EXERCISE
% TASK 15= ONLY LARGE BOOM/BUST AND DETRENDED INDEX
% TASK 16= CRISIS EXERCISE
% TASK 17= KLEIN EXERCISE
% TASK 18= BRAZIL CASE

% TASK 21= ONLINE APPENDIX FIGURES A.5.1, A.5.2, A.5.3
% TASK 22= ONLINE APPENDIX FIGURES 4.A.1, 4.A.2, 4.A.3

% TASK 24= ONLINE APPENDIX FIGURES 1.A.1, 1.A.2, 2.A.1, 2.A.2
% TASK 25= ONLINE APPENDIX FIGURE 3.A.1
% TASK 26= ONLINE APPENDIX FIGURES 5.A.5, 5.A.6, 5.A.7, 5.A.8 (EQUITY)
% TASK 27= ONLINE APPENDIX FIGURES 5.A.1, 5.A.2, 5.A.3, 5.A.4 (BONDS)
% TASK 28= ONLINE APPENDIX FIGURE 7.A.2 (EQUITY)
% TASK 29= ONLINE APPENDIX FIGURE 7.A.1 (BONDS)
% TASK 30= NEEDED FOR FIGURE 5 AND OTHERS

format long g


% clear all 
% close all
% clc
% 
% variable=4

%Data path from where to import data and find functions
BasePath=pwd;

global num_c vect_adv num_adv vect_mid num_mid vect_poor num_poor schindler_years Countries Years

%% DEFINITION OF PARAMETERS

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

%# of main indexes
num_index=3;

%# of gdp series
num_gdp=1;

%# of subindexes
num_subindex=6;

%Tells Matlab if using the detrended index or the raw data to calculate the
%pooling correlations in task 5, Default detrended index. Matlab uses:
%detrended index when detrended_pooling_correlation=1; 
%raw data in levels (no detrending) when detrended_pooling_correlation=2
detrended_pooling_correlation=1;

%Tells Matlab which percentile to use for Task 13. This value identify the
%countries that have high debt and the countries with low debt
level_threshold_high = 20;
level_threshold_low  = 80;

% Definition of country groups and # of member in each group (no less than 25 data points in GDP and no less than 17 in Schindler)
% The Definition comes from WEO 2013 which catergorized into 3 groups: Advanced/Emerging/Low-Income
vect_adv=[3 4 6 11 17 22 23 24 26 29 32 34 36 46 47 49 56 61 64 65 73 74];
num_adv=size(vect_adv,2);
vect_mid=[2 8 9 12 13 14 16 18 19 21 27 28 30 33 35 38 39 40 41 42 43 44 50 52 54 57 58 59 60 63 67 69 70 72 75 77];
num_mid=size(vect_mid,2);
vect_poor=[1 5 7 10 15 20 25 31 37 45 48 51 53 55 62 66 68 71 76 78];
num_poor=size(vect_poor,2);


switch variable
    
    case 1

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%IMPORT AND MANIPULATE GDP DATA%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Import data on GDP on the 88 countries in the original Schindler index
if mac_user==1
[vardat,textdata, raw]=xlsread(strcat(BasePath, '\WDI_gdp.xls') ,char('Sheet1')); 
else
[vardat,textdata, raw]=xlsread(strcat(BasePath, '/WDI_gdp.xls') ,char('Sheet1')); 
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 an index with the position of the first non NaN value for each country
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 years 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
% This eliminates 12 countries from the total of 91 countries in the
% original Schindler index dataset, leaving then 79 countries.
 
% %
vardat=[vardat(1:4,:); vardat(6:end,:)]; 
Countries=[Countries(1:4,:); Countries(6:end,:)];
idx_first=[idx_first(1:4,:); idx_first(6:end,:)];

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

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

%Calculate Log of GDP
log_GDP = log(vardat);
GDP     = vardat;

save  case_GDP1 GDP log_GDP length Countries idx_first num_c Years Full_sample

    case 2
        
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%QUADRATIC TREND FOR GDP AND PLOTS%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load case_GDP1

%% Quadratic Trend and Plot output gap and quadratic trend

% To make the plots, uncomment the part of script in the function
% quadratic_trend
ylabel={'Log of GDP (thousand of lcu)'};

[xt] = quadratic_trend(length, idx_first, log_GDP, ylabel);

%% Descriptive Statistics on the output gap

%Create matrices for the output gap divided by country groups
[xt_adv, xt_mid, xt_poor] = country_groups(num_gdp, xt);

%Calculate volatility(standard deviation)and persistency of the output gap(correlation between t and t-1) 
%for each country over the period of study
[stats_xt_tot_std, stats_xt_tot_corr] = des_statistics(num_c, num_gdp, xt);

%Calculate volatility(standard deviation)and persistency of the output gap(correlation between t and t-1) 
%over the period of study only for advanced countries
[stats_xt_adv_std, stats_xt_adv_corr] = des_statistics(num_adv, num_gdp, xt_adv);

%Calculate volatility(standard deviation)and persistency of the output gap(correlation between t and t-1) 
%over the period of study only for emerging countries
[stats_xt_mid_std, stats_xt_mid_corr] = des_statistics(num_mid, num_gdp, xt_mid);

%Calculate volatility(standard deviation)and persistency of the output gap(correlation between t and t-1) 
%over the period of study only for low-income countries
[stats_xt_poor_std, stats_xt_poor_corr] = des_statistics(num_poor, num_gdp, xt_poor);

%Create matrix with descriptive statistics for output gap
xt_des_stats=[100*nanmean(stats_xt_tot_std,1) nanmean(stats_xt_tot_corr,1); 
    100*nanmean(stats_xt_adv_std,1) nanmean(stats_xt_adv_corr,1)
    100*nanmean(stats_xt_mid_std,1) nanmean(stats_xt_mid_corr,1)
    100*nanmean(stats_xt_poor_std,1) nanmean(stats_xt_poor_corr,1)];

%Report only teo decimals
xt_des_stats=roundTo(xt_des_stats,2);

%Define labels for descriptive statistics table
Labels_v = {'Standard Deviation (%)'; 'Autocorrelation'};
Labels_h = {'','Total', 'Advanced Economies', 'Emerging Markets', 'Low-Income Countries'};
subtitle_xt_des_stats={'(d) Output','','','',''};

%Create tables for descriptive statistics of the output gap
xt_des_stats1 = num2cell(xt_des_stats);
Table_xt_des_stats = [subtitle_xt_des_stats; Labels_v xt_des_stats1'];

save case_GDP2 xt xt_adv xt_mid xt_poor
save Result_GDP2 Table_xt_des_stats

    case 3

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%IMPORT AND MANIPULATE SCHINDLER INDEX%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load case_GDP1
load case_GDP2

%% 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

%Define Schindler index data
index_data= vardat_2;

%Define years for the Schindler index
schindler_years_list=(1995:1:2011);

%Create a countryxtimexindexes dimension 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+(schindler_years*(i-1)),1+k);
         end
     end
end


%ELIMINATE 12 COUNTRIES WITH LESS THAN 25 YEAR OBSERVATIONS FOR GDP 
index_matrix=[index_matrix(1:4,:,:); index_matrix(6:10,:,:); index_matrix(12:18,:,:); index_matrix(20:26,:,:);index_matrix(28:32,:,:);index_matrix(34:41,:,:);index_matrix(43:45,:,:);
     index_matrix(48:52,:,:);index_matrix(54:66,:,:);index_matrix(69:70,:,:);index_matrix(72: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);

%Calculate the average index for each year over all countries
    %Rows = years
    %Column 1 = ka
    %Column 2 = kai
    %Column 3 = kao
for i=1:schindler_years
    for j = 1:num_index
        year_index_mean(i,j)= nanmean(index_matrix(1:num_c,i,j)); 
        year_index_adv_mean(i,j)=nanmean(index_adv(1:num_adv,i,j));
        year_index_mid_mean(i,j)=nanmean(index_mid(1:num_mid,i,j));
        year_index_poor_mean(i,j)=nanmean(index_poor(1:num_poor,i,j));
    end
end

%Calculate the standard deviation across countries in each year for the index
for j=1:num_index
    std_country(:,j) = nanstd(index_matrix(:,:,j),0,1)';
end

%Compute interval for the standard deviation around the average of the indexes
for j=1:num_index
    interval_std(:,:,j)=[year_index_mean(:,j)-std_country(:,j), year_index_mean(:,j)+std_country(:,j)];
end

%% CREATE TABLE ONE IN THE CORE PAPER

for i=1:num_index
    average(i,:)=roundTo(mean(year_index_mean(:,i),1),2);
    average_adv(i,:)=roundTo(mean(year_index_adv_mean(:,i),1),2);
    average_mid(i,:)=roundTo(mean(year_index_mid_mean(:,i),1),2);
    average_poor(i,:)=roundTo(mean(year_index_poor_mean(:,i),1),2);
end

average=num2cell(average);
average_adv=num2cell(average_adv);
average_mid=num2cell(average_mid);
average_poor=num2cell(average_poor);

label_h={'Capital Control','All Countries','Developed Countries','Emerging Countries','Low-Income Countries'};
label_v={'Overall Index';'Mean';'Mean'};
title_one={'Table 1: Capital Controls: Mean Values','','','',''};

Table_one_paper=[title_one;
    label_h;
    label_v,average,average_adv,average_mid,average_poor];

save case_GDP3 index_matrix index_data index_adv index_mid index_poor year_index_mean
save Result_GDP3 Table_one_paper

    case 4

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%LINEAR DETREND FOR SCHINDLER INDEX AND BAR CHART%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load case_GDP1
load case_GDP2
load case_GDP3

%% 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);

%% Descriptive Statistics

%Calculate volatility(standard deviation) and persistency(correlation between t and t-1)of the index
%for each country over the period of study
[stats_index_std, stats_index_corr] = des_statistics(num_c, num_index, indext);

%Create vector with standard deviation for the different inocme groups
[stats_index_std_adv, stats_index_std_mid, stats_index_std_poor] = country_groups(1, stats_index_std);

%Create vector with autocorrelation for the different income groups
[stats_index_corr_adv, stats_index_corr_mid, stats_index_corr_poor] = country_groups(1, stats_index_corr);

%Create matrix with descriptive statistics for detrended overall index
index_overall_des_stats=[nanmean(stats_index_std(:,1)) nanmean(stats_index_corr(:,1))
    nanmean(stats_index_std_adv(:,1)) nanmean(stats_index_corr_adv(:,1))
    nanmean(stats_index_std_mid(:,1)) nanmean(stats_index_corr_mid(:,1))
    nanmean(stats_index_std_poor(:,1)) nanmean(stats_index_corr_poor(:,1))]';

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

%Create matrix with descriptive statistics for detrended inflow index
index_inflow_des_stats=[nanmean(stats_index_std(:,2)) nanmean(stats_index_corr(:,2))
    nanmean(stats_index_std_adv(:,2)) nanmean(stats_index_corr_adv(:,2))
    nanmean(stats_index_std_mid(:,2)) nanmean(stats_index_corr_mid(:,2))
    nanmean(stats_index_std_poor(:,2)) nanmean(stats_index_corr_poor(:,2))]';

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

%Create matrix with descriptive statistics for detrended outflow index
index_outflow_des_stats=[nanmean(stats_index_std(:,3)) nanmean(stats_index_corr(:,3))
    nanmean(stats_index_std_adv(:,3)) nanmean(stats_index_corr_adv(:,3))
    nanmean(stats_index_std_mid(:,3)) nanmean(stats_index_corr_mid(:,3))
    nanmean(stats_index_std_poor(:,3)) nanmean(stats_index_corr_poor(:,3))]';

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

%Calculate correlation between Inflows and outflows (cyclical components)
[r_in_out, p_in_out] = country_correlation(1, indext(:,:,2), indext(:,:,3));

%Create country group correlation between inflows and outflows
[r_in_out_adv, r_in_out_mid, r_in_out_poor] = country_groups(1, r_in_out);

%Create table with descriptive statistics for correlation inflows and outflows
index_inflow_outflow_des_stats=[nanmean(r_in_out), nanmean(r_in_out_adv),nanmean(r_in_out_mid),nanmean(r_in_out_poor)];

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

%% Make tables for the descriptive statistics of the indexes

%Define labels for descriptive statistics table
Labels_v = {'Standard Deviation'; 'Autocorrelation'};
Labels_h = {'','Total', 'Advanced Economies', 'Emerging Markets', 'Low-Income Countries'};
Labels_v1 = {'Correlation'};

%Create title vectors
title_inflow_outflow_des_stats={'Table A.2: Descriptive Statistics', '','','',''}; 
subtitle_inflow_des_stats={'(a) Inflows', '','','',''};
subtitle_outflow_des_stats={'(b) Outflows', '','','',''};
subtitle_inflow_outflow_des_stats={'(c) Inflows-Outflows', '','','',''};
subtitle_overall_des_stats={'Capital Controls', '','','',''};
vector_nothing={'','','','',''};

%Create tables for descriptive statistics of the overall index
index_overall_des_stats1 = num2cell(index_overall_des_stats);

%Create tables for descriptive statistics of the inflow and outflow index
index_inflow_des_stats1 = num2cell(index_inflow_des_stats);
index_outflow_des_stats1 = num2cell(index_outflow_des_stats);
index_inflow_outflow_des_stats1 = num2cell(index_inflow_outflow_des_stats);

Table_index_inflow_outflow_des_stats = [title_inflow_outflow_des_stats; 
    Labels_h; subtitle_inflow_des_stats; 
    Labels_v  index_inflow_des_stats1; 
    vector_nothing; subtitle_outflow_des_stats; 
    Labels_v  index_outflow_des_stats1; 
    vector_nothing; subtitle_inflow_outflow_des_stats;
    Labels_v1 index_inflow_outflow_des_stats1]; 

%% Calculate correlations between output gap and index for t=0 and make plots

%Calculate correlation and pvalues when output gap t=0 and index t=0
[r, p] = country_correlation(num_index, xt, indext);

%Sort them and associate stars to the country name
[corr_matrix_sort, legend_matrix_star, star_matrix, legend_matrix] = star_zero(num_index, r, p);

%Create vector for overall Index
corr_overall_sort=corr_matrix_sort(:,:,1);
legend_overall_star= legend_matrix_star(:,:,1);

%Create vector for inflow Index
corr_inflow_sort=corr_matrix_sort(:,:,2);
legend_inflow_star= legend_matrix_star(:,:,2);

%Create vector for outflow Index
corr_outflow_sort=corr_matrix_sort(:,:,3);
legend_outflow_star= legend_matrix_star(:,:,3);

%Sort correlation between inflow and outflow cyclical components
[corr_in_out_sort, legend_in_out_star, star_n_out, legend_in_out] = star_zero(1, r_in_out, p_in_out);

figure(1)
%Create a bar chart for the INFLOW CORRELATIONS
subplot(1,2,1)
b_in=barh(corr_inflow_sort(:,2));
set(gca, 'YDir', 'reverse')
set(gca, 'YTick', 1:num_c, 'YTickLabel', legend_inflow_star,'FontSize',5,'FontWeight','bold');
set(gca,'XLim',[-1 1])
t=title('(a) Inflows','FontWeight','bold');
set(t, 'Visible', 'on','Fontsize', 8);
bar_child_in=get(b_in,'Children');
set(bar_child_in,'CData', corr_inflow_sort(:,2));
%Vector of colors for bars: ***blue, **green, white
mycolor=[0 0 1; 0 1 0; 1 1 1];
for iCount_in=1:num_c
    if (corr_inflow_sort(iCount_in,3)<.01)
        index_in(iCount_in)=1;
    elseif and(corr_inflow_sort(iCount_in,3)>.01, corr_inflow_sort(iCount_in,3)<.05)
        index_in(iCount_in)=2;
    else
        index_in(iCount_in)=3;
    end
end
set(bar_child_in, 'CData',index_in);
colormap(mycolor);
box off
%Create a bar chart for the OUTFLOW CORRELATIONS
subplot(1,2,2)
b_out=barh(corr_outflow_sort(:,2));
set(gca, 'YDir', 'reverse')
set(gca, 'YTick', 1:num_c, 'YTickLabel', legend_outflow_star,'FontSize',5,'FontWeight','bold');
set(gca,'XLim',[-1 1])
t=title('(b) Outflows','FontWeight','bold');
set(t, 'Visible', 'on','Fontsize', 8);
bar_child_out=get(b_out,'Children');
set(bar_child_out,'CData', corr_outflow_sort(:,2));
%Vector of colors for bars: ***blue, **green, white
mycolor=[0 0 1; 0 1 0; 1 1 1];
for iCount_out=1:num_c
    if (corr_outflow_sort(iCount_out,3)<.01)
        index_out(iCount_out)=1;
    elseif and(corr_outflow_sort(iCount_out,3)>.01, corr_outflow_sort(iCount_out,3)<.05)
        index_out(iCount_out)=2;
    else
        index_out(iCount_out)=3;
    end
end
set(bar_child_out, 'CData',index_out);
colormap(mycolor);
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 1. Country-By-Country Correlations between Capital Controls and Output','Position',[0.508203125 0.978604294478528 9.16025403784439],'HorizontalAlignment','center')


%Create a bar chart for the INFLOW-OUTFLOW CORRELATIONS
figure(2)
b_in_out=barh(corr_in_out_sort(:,2));
set(gca, 'YDir', 'reverse')
set(gca, 'YTick', 1:num_c, 'YTickLabel', legend_in_out_star,'FontSize',5,'FontWeight','bold');
set(gca,'XLim',[-1 1])
t=title('Figure 2. Country-By-Country Correlations between Controls on Capital Inflows and Outflows','FontWeight','bold');
set(t, 'Visible', 'on','Fontsize', 10);
bar_child_in_out=get(b_in_out,'Children');
set(bar_child_in_out,'CData', corr_in_out_sort(:,2));
%Vector of colors for bars: ***blue, **green, white
mycolor=[0 0 1; 0 1 0; 1 1 1];
for iCount_in_out=1:num_c
    if (corr_in_out_sort(iCount_in_out,3)<.01)
        index_in_out(iCount_in_out)=1;
    elseif and(corr_in_out_sort(iCount_in_out,3)>.01, corr_in_out_sort(iCount_in_out,3)<.05)
        index_in_out(iCount_in_out)=2;
    else
        index_in_out(iCount_in_out)=3;
    end
end
set(bar_child_in_out, 'CData',index_in_out);
colormap(mycolor);
box off


%% Calculate correlations for t=-1 and t=1 and make table

%Create vector for calculating correlations when output gap t=0 and index t=1
xt_plus1=xt(:,1:end-1);
indext_plus1=indext(:,2:end,:);

%Calculate correlation and pvalues when output gap t=0 and index t=1
[r_plus1, p_plus1] = country_correlation(num_index, xt_plus1, indext_plus1);

%Concatenate lead values and stars
[r_plus1_matrix_sort, r_plus1_star_matrix_sort] = star_one(num_index, r_plus1, p_plus1, corr_matrix_sort);

%Create vector for calculating correlations when output gap t=0 and index t=-1
xt_minus1=xt(:,2:end);
indext_minus1=indext(:,1:end-1,:);

%Calculate correlation and pvalues when output gap t=0 and index t=-1
[r_minus1, p_minus1] = country_correlation(num_index, xt_minus1, indext_minus1);

%Concatenate lag values and stars
[r_minus1_matrix_sort, r_minus1_star_matrix_sort] = star_one(num_index, r_minus1, p_minus1, corr_matrix_sort);

%Concatenate values and stars for correlation overall index t=0
r_overall=num2str(corr_matrix_sort(:,2,1));
r_overall_star=strcat(r_overall,star_matrix(:,1,1));

%Concatenate values and stars for correlation inflow index t=0
r_inflow=num2str(corr_matrix_sort(:,2,2));
r_inflow_star=strcat(r_inflow,star_matrix(:,1,2));

%Concatenate values and stars for correlation outflow index t=0
r_outflow=num2str(corr_matrix_sort(:,2,3));
r_outflow_star=strcat(r_outflow,star_matrix(:,1,3));

%Concatenate values and stars for correlation overall index t=-1
r_minus1_overall=num2str(r_minus1_matrix_sort(:,:,1));
r_minus1_overall_star=strcat(r_minus1_overall,r_minus1_star_matrix_sort(:,1));

%Concatenate values and stars for correlation inflow index t=-1
r_minus1_inflow=num2str(r_minus1_matrix_sort(:,:,2));
r_minus1_inflow_star=strcat(r_minus1_inflow,r_minus1_star_matrix_sort(:,2));

%Concatenate values and stars for correlation outflow index t=-1
r_minus1_outflow=num2str(r_minus1_matrix_sort(:,:,3));
r_minus1_outflow_star=strcat(r_minus1_outflow,r_minus1_star_matrix_sort(:,3));

%Concatenate values and stars for correlation overall index t=+1
r_plus1_overall=num2str(r_plus1_matrix_sort(:,:,1));
r_plus1_overall_star=strcat(r_plus1_overall,r_plus1_star_matrix_sort(:,1));
 
%Concatenate values and stars for correlation inflow index t=+1
r_plus1_inflow=num2str(r_plus1_matrix_sort(:,:,2));
r_plus1_inflow_star=strcat(r_plus1_inflow,r_plus1_star_matrix_sort(:,2));
 
%Concatenate values and stars for correlation outflow index t=+1
r_plus1_outflow=num2str(r_plus1_matrix_sort(:,:,3));
r_plus1_outflow_star=strcat(r_plus1_outflow,r_plus1_star_matrix_sort(:,3));
 
%Create and order the matrix with overall correlations at time t=-1,0,1
overall_temporal_corr=[r_minus1_overall_star r_overall_star r_plus1_overall_star];

%Create and order the matrix with inflow correlations at time t=-1,0,1
inflow_temporal_corr=[r_minus1_inflow_star r_inflow_star r_plus1_inflow_star];

%Create and order the matrix with outflow correlations at time t=-1,0,1
outflow_temporal_corr=[r_minus1_outflow_star r_outflow_star r_plus1_outflow_star];

%% These tables report the correlation in three different periods for each country

%Create labels and title vectors
Labels = { '' , 'j=-1', 'j=0', 'j=+1'};
title_inflow_corr={'Table A.3.a. Country Correlations: Output and Capital Controls on Inflows', '', '', ''};
title_outflow_corr={'Table A.3.b. Country Correlations: Output and Capital Controls on Outflows', '', '', ''};

%Create tables for inflow correlations
Table_inflow_corr = [title_inflow_corr; Labels ; legend_matrix(:,:,2) inflow_temporal_corr];

%Create tables for outflow correlations
Table_outflow_corr = [title_outflow_corr; Labels ; legend_matrix(:,:,3) outflow_temporal_corr];

save case_GDP4 indext r p r_minus1 p_minus1 r_plus1 p_plus1 indext_adv indext_mid indext_poor
save Result_GDP4 Table_index_inflow_outflow_des_stats Table_inflow_corr Table_outflow_corr

    case 6

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%CALCULATE CORRELATION BETWEEN OUTPUT GAP AND INDEX, METHOD 2: AVERAGE COUNTRY CORRELATIONS%%% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load case_GDP1
load case_GDP2
load case_GDP3
load case_GDP4
load Result_GDP4
load Result_GDP3

%% Calculate correlation using the average correlations across countries and for income groups (no pvalues in this case)

%Average Output t=0 and index t=0
%Create matrices for the correlations between overall index and output gap (t=0) divided by country groups
[corr_adv, corr_mid, corr_poor] = country_groups(1, r);

%Average over all countries of the correlations between index and output
%gap (t=0)
[average_corr, std_corr, confidence_int] = average_country_correlation(num_index, r, corr_adv, corr_mid, corr_poor);

%Create average and confidence interval for each single index
average_corr_overall=average_corr(:,:,1);
average_corr_inflow=average_corr(:,:,2);
average_corr_outflow=average_corr(:,:,3);

%Average Output t=0 and index t=-1
%Create matrices for the correlations between index and output gap (t=-1) divided by country groups
[corr_adv_minus1, corr_mid_minus1, corr_poor_minus1] = country_groups(1, r_minus1);

%Average over all countries of the correlations between index and output
%gap (t=-1)
[average_corr_minus1, std_corr_minus1, confidence_int_minus1] = average_country_correlation(num_index, r_minus1, corr_adv_minus1, corr_mid_minus1, corr_poor_minus1);

%Create average and confidence interval for each single index
average_corr_overall_minus1=average_corr_minus1(:,:,1);
average_corr_inflow_minus1=average_corr_minus1(:,:,2);
average_corr_outflow_minus1=average_corr_minus1(:,:,3);

%Average Output t=0 and index t=1
%Create matrices for the correlations between index and output gap (t=1) divided by country groups
[corr_adv_plus1, corr_mid_plus1, corr_poor_plus1] = country_groups(1, r_plus1);

%Average over all countries of the correlations between index and output
%gap (t=+1)
[average_corr_plus1,std_corr_plus1,confidence_int_plus1]=average_country_correlation(num_index,r_plus1,corr_adv_plus1,corr_mid_plus1,corr_poor_plus1);
 
%Create average and confidence interval for each single index
average_corr_overall_plus1=average_corr_plus1(:,:,1);
average_corr_inflow_plus1=average_corr_plus1(:,:,2);
average_corr_outflow_plus1=average_corr_plus1(:,:,3);

[interval, interval_minus1, interval_plus1]=confidence_int_average_corr(num_index, confidence_int, confidence_int_minus1, confidence_int_plus1);

%% Create tables for the average correlations divided by country groups

%Define labels and titles
Labels_h = { '' , 'j=-1', 'j=0', 'j=+1'};
Labels_v = {'Total'; ''; 'Advanced Economies';''; 'Emerging Markets';''; 'Low-Income Countries';''};
title_average_corr={'Table A.2. Correlations between Output and Capital Controls: Average across countries', '', '', ''};
subtitle_inflow={'(a) Inflows', '', '', ''};
subtitle_outflow={'(b) Outflows', '', '', ''};
empty_vector={'', '', '', ''};

%Create tables for overall correlations (NOT IN THE PAPER)
average_corr_overall_matrix=[average_corr_overall_minus1 average_corr_overall average_corr_overall_plus1];
average_corr_overall_matrix = num2cell(average_corr_overall_matrix);
average_corr_overall_matrix1=[average_corr_overall_matrix(1,:)
    interval_minus1(1,1,1) interval(1,1,1) interval_plus1(1,1,1)
    average_corr_overall_matrix(2,:)
    interval_minus1(2,1,1) interval(2,1,1) interval_plus1(2,1,1)
    average_corr_overall_matrix(3,:)
    interval_minus1(3,1,1) interval(3,1,1) interval_plus1(3,1,1)
    average_corr_overall_matrix(4,:)
    interval_minus1(4,1,1) interval(4,1,1) interval_plus1(4,1,1)];

%Create tables for inflow correlations
average_corr_inflow_matrix=[average_corr_inflow_minus1 average_corr_inflow average_corr_inflow_plus1];
average_corr_inflow_matrix = num2cell(average_corr_inflow_matrix);
average_corr_inflow_matrix1=[average_corr_inflow_matrix(1,:)
    interval_minus1(1,1,2) interval(1,1,2) interval_plus1(1,1,2)
    average_corr_inflow_matrix(2,:)
    interval_minus1(2,1,2) interval(2,1,2) interval_plus1(2,1,2)
    average_corr_inflow_matrix(3,:)
    interval_minus1(3,1,2) interval(3,1,2) interval_plus1(3,1,2)
    average_corr_inflow_matrix(4,:)
    interval_minus1(4,1,2) interval(4,1,2) interval_plus1(4,1,2)];

%Create tables for outflow correlations
average_corr_outflow_matrix=[average_corr_outflow_minus1 average_corr_outflow average_corr_outflow_plus1];
average_corr_outflow_matrix = num2cell(average_corr_outflow_matrix);
average_corr_outflow_matrix1=[average_corr_outflow_matrix(1,:)
    interval_minus1(1,1,3) interval(1,1,3) interval_plus1(1,1,3)
    average_corr_outflow_matrix(2,:)
    interval_minus1(2,1,3) interval(2,1,3) interval_plus1(2,1,3)
    average_corr_outflow_matrix(3,:)
    interval_minus1(3,1,3) interval(3,1,3) interval_plus1(3,1,3)
    average_corr_outflow_matrix(4,:)
    interval_minus1(4,1,3) interval(4,1,3) interval_plus1(4,1,3)];

Table_average_corr_inflow1_outflow1 = [title_average_corr; Labels_h; subtitle_inflow; Labels_v average_corr_inflow_matrix1; 
                                        empty_vector; subtitle_outflow; Labels_v average_corr_outflow_matrix1];
                                    
%Create TABLE 2 in the Core paper

title_two_paper={'Table 1: Capital Controls: Mean, Standard Deviation, and Correlations','','','',''};
label_h={'Statistic','All Countries','Developed Countries','Emerging Countries','Low-Income Countries'};
label_v={'Controls on Capital Inflows';'Standard Deviation';'Correlation with Output';
    'Controls on Capital Outflows';'Standard Deviation';'Correlation with Output';
    'Correlation between Controls on Inflows and Cotrols on Outflows'};
empty_line={'','','',''};

average_corr_inflow_values=average_corr_inflow';
average_corr_inflow_values = num2cell(average_corr_inflow_values);

average_corr_outflow_values=average_corr_outflow';
average_corr_outflow_values = num2cell(average_corr_outflow_values);

Table_two_paper=[title_two_paper; label_h;
    label_v(1,:),empty_line;
    Table_one_paper(4,:);
    label_v(2,:),Table_index_inflow_outflow_des_stats(4,2:end);
    label_v(3,:),average_corr_inflow_values;
    label_v(4,:),empty_line;
    Table_one_paper(5,:);
    label_v(5,:),Table_index_inflow_outflow_des_stats(8,2:end);
    label_v(6,:),average_corr_outflow_values;
    label_v(7,:),Table_index_inflow_outflow_des_stats(12,2:end)];
    
save Result_GDP6 Table_average_corr_inflow1_outflow1 Table_two_paper

    case 7

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%IDENTIFY BOOM AND BUST AND DESCRIPITVE STATISTICS%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load case_GDP1
load case_GDP2

%% Identify Boom and Bust

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

% 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_gdp, num_boom, boom_matrix, boom_matrix);

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

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

%ADVANCED
[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
[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);

%LOW-INCOME
[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 and titles for descriptive statistics table on booms and busts
Labels_v = {'Number of episodes'; 'Episodes per country'; 'Average Duration (years)'; 'Average Amplitude'};
Labels_h = {'','Total', 'Advanced Economies', 'Emerging Markets', 'Low-Income Countries'};
subtitle_boom_des={'(e) Booms','','','',''};
subtitle_bust_des={'(f) Busts','','','',''};

%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 = [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 = [subtitle_bust_des; Labels_v  bust_des1];

clc 
save case_GDP7 boom_matrix bust_matrix boom_adv boom_mid boom_poor bust_adv bust_mid bust_poor num_boom num_bust...
        num_boom_adv num_bust_adv num_boom_mid num_bust_mid num_boom_poor num_bust_poor
    
save Result_GDP7 Table_boom_des Table_bust_des

    case 8

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%BOOM/BUST AND DETRENDED INDEX%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load case_GDP1
load case_GDP2
load case_GDP3
load case_GDP4
load case_GDP7

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

%BOOM
[average_boom_values,~,~,~,boom_values] = average_boom_bust(num_gdp, num_boom, boom_matrix, xt);

%BUST
[average_bust_values,~,~,~,bust_values] = average_boom_bust(num_gdp, num_bust, bust_matrix, xt);

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

%BOOM
[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
[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);

%BOOM
%Divide ouput gap in country groups
[boom_values_adv, boom_values_mid, boom_values_poor] = country_groups_boom_bust(num_gdp, 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_gdp, 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),:,:)=[];

%BOOM
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    

%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);

%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);

%LOW-INCOME 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
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    

%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);
 
%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);
 
%LOW-INCOME 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 with average detrended index and average output gap (reference point boom/bust)

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

figure(1)
% OVERALL INDEX
% Boom
subplot(3,2,1)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom(1,:,1), 'b', 100*average_boom_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_boom(1,:,1)),2) roundTo(1.75*max(upper_bound_boom(1,:,1)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom(1,:,1)),2) 0 roundTo(1.75*max(upper_bound_boom(1,:,1)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom(1,:,1)),2) 0 roundTo(1.75*max(upper_bound_boom(1,:,1)),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,:,1),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom(1,:,1),'k-.');
box off
hold off
%Bust
subplot(3,2,2)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust(1,:,1), '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,:,1)),2) roundTo(1.75*max(upper_bound_bust(1,:,1)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust(1,:,1)),2) 0 roundTo(1.75*max(upper_bound_bust(1,:,1)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust(1,:,1)),2) 0 roundTo(1.75*max(upper_bound_bust(1,:,1)),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,:,1),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust(1,:,1),'k-.');
box off
hold off
% INFLOW
%Boom
subplot(3,2,3)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom(1,:,2), 'b', 100*average_boom_values ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
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
subplot(3,2,4)
[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',8);
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
%OUTFLOW
% Boom
subplot(3,2,5)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom(1,:,3), 'b', 100*average_boom_values ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
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
subplot(3,2,6)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust(1,:,3), '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',8);
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
legend1 = legend([hline1 hline2 hline3],'Average Index (lhs)','Average Output Gap (rhs)','Two Standard Deviation for the Index (lhs)');
set(legend1,'Orientation','horizontal','Position',[0.271744791666667 0.0203429912151471 0.515104166666667 0.0322147651006711],'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 3. Boom-Bust Episodes and Capital Controls','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) Overall Index','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) Inflows','Position',[0.514453125 0.660651920838184 9.16025403784439],'HorizontalAlignment','center');
sub_ha3 = axes('Visible','off','Position',[0 0 1 1]);
text('Parent',sub_ha3,'String','\bf (c) Outflows','Position',[0.517578125 0.364959254947614 9.16025403784439],'HorizontalAlignment','center');
save case_GDP20 boom_matrix indext_values_boom bust_matrix indext_values_bust num_boom num_bust


save case_GDP8 bust_values boom_values average_boom_values average_bust_values average_indext_values_boom_adv std_indext_values_boom_adv upper_bound_boom_adv lower_bound_boom_adv average_indext_values_boom_mid std_indext_values_boom_mid upper_bound_boom_mid lower_bound_boom_mid average_indext_values_boom_poor std_indext_values_boom_poor upper_bound_boom_poor lower_bound_boom_poor average_boom_values_adv average_boom_values_mid average_boom_values_poor average_indext_values_bust_adv std_indext_values_bust_adv upper_bound_bust_adv lower_bound_bust_adv average_indext_values_bust_mid std_indext_values_bust_mid upper_bound_bust_mid lower_bound_bust_mid average_indext_values_bust_poor std_indext_values_bust_poor upper_bound_bust_poor lower_bound_bust_poor average_bust_values_adv average_bust_values_mid average_bust_values_poor

    case 10

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%BOOM/BUST AND SUBCOMPONENTS OF THE INDEX%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load case_GDP1
load case_GDP2
load case_GDP7
load case_GDP8
load case_GDP1
load case_GDP2
load case_GDP3

%% Import the data

%Create matrix with raw data for the subindexes
subindex_data=[index_data(:,5) index_data(:,12) index_data(:,19) index_data(:,26) index_data(:,33) index_data(:,36)];
subindex_num=size(subindex_data,2);

%Create matrix only with cumulativesubindexes and reshape the matrix
for i=1:Full_sample
     for j=1:schindler_years
         for k=1:subindex_num
                subindex_matrix(i,j,k) = subindex_data(j+(schindler_years*(i-1)), k);
         end
     end
end

%ELIMINATE 12 COUNTRIES WITH LESS THAN 25 YEAR OBSERVATIONS FOR GDP 
subindex_matrix=[subindex_matrix(1:4,:,:); subindex_matrix(6:10,:,:); subindex_matrix(12:18,:,:); subindex_matrix(20:26,:,:);subindex_matrix(28:32,:,:);subindex_matrix(34:41,:,:);subindex_matrix(43:45,:,:);
     subindex_matrix(48:52,:,:);subindex_matrix(54:66,:,:);subindex_matrix(69:70,:,:);subindex_matrix(72:89,:,:);subindex_matrix(91,:,:)];

%Create matrix with raw data for the subindexes: INFLOWS
subindex_inflow_data=[index_data(:,6) index_data(:,13) index_data(:,20) index_data(:,27) index_data(:,34) index_data(:,37)];

%Create matrix only with cumulativesubindexes and reshape the matrix
for i=1:Full_sample
     for j=1:schindler_years
         for k=1:subindex_num
             subindex_inflow_matrix(i,j,k) = subindex_inflow_data(j+(schindler_years*(i-1)),k);
         end
     end
end

%ELIMINATE 12 COUNTRIES WITH LESS THAN 25 YEAR OBSERVATIONS FOR GDP AND THE BAHRAIN THAT HAS LESS THAN 17 OBSERVATION FOR ALL SUBINDEXES
subindex_inflow_matrix=[subindex_inflow_matrix(1:4,:,:); subindex_inflow_matrix(6:10,:,:); subindex_inflow_matrix(12:18,:,:); subindex_inflow_matrix(20:26,:,:);subindex_inflow_matrix(28:32,:,:);subindex_inflow_matrix(34:41,:,:);
     subindex_inflow_matrix(43:45,:,:);subindex_inflow_matrix(48:52,:,:);subindex_inflow_matrix(54:66,:,:);subindex_inflow_matrix(69:70,:,:);subindex_inflow_matrix(72:89,:,:);subindex_inflow_matrix(91,:,:)];

%Create matrix with raw data for the subindexes: OUTFLOWS
subindex_outflow_data=[index_data(:,7) index_data(:,14) index_data(:,21) index_data(:,28) index_data(:,35) index_data(:,39)];

%Create matrix only with cumulativesubindexes and reshape the matrix
for i=1:Full_sample
     for j=1:schindler_years
         for k=1:subindex_num
             subindex_outflow_matrix(i,j,k) = subindex_outflow_data(j+(schindler_years*(i-1)),k);
         end
     end
end

%ELIMINATE 12 COUNTRIES WITH LESS THAN 25 YEAR OBSERVATIONS FOR GDP
subindex_outflow_matrix=[subindex_outflow_matrix(1:4,:,:); subindex_outflow_matrix(6:10,:,:); subindex_outflow_matrix(12:18,:,:); subindex_outflow_matrix(20:26,:,:);subindex_outflow_matrix(28:32,:,:);subindex_outflow_matrix(34:41,:,:);
     subindex_outflow_matrix(43:45,:,:);subindex_outflow_matrix(48:52,:,:);subindex_outflow_matrix(54:66,:,:);subindex_outflow_matrix(69:70,:,:);subindex_outflow_matrix(72:89,:,:);subindex_outflow_matrix(91,:,:)];

%% Remove the country specific means and linear trend

%Lineraly detrend the subcomponents of the index: OVERALL
[subindext] = linear_trend(num_subindex,schindler_years,subindex_matrix);

%Detrend the series of bonds starting from the first valid value (third
%observation)
[subindext_prova] = linear_trend(num_subindex,schindler_years-2,subindex_matrix(:,3:end,:));

for j=1:subindex_num
    for i=1:num_c
        if j==2
            subindext(i,3:end,j)=subindext_prova(i,:,j);
        else
            subindext(i,:,j)=subindext(i,:,j);
        end
    end
end

%Lineraly detrend the subcomponents of the index: INFLOWS
[subindext_inflow] = linear_trend(num_subindex,schindler_years,subindex_inflow_matrix);

%Detrend the series of bonds starting from the first valid value (third
%observation)
%CHANGE MANUALLY!!!!!
[subindext_inflow_prova] = linear_trend(num_subindex,schindler_years-2,subindex_inflow_matrix(:,3:end,:));

for j=1:subindex_num
    for i=1:num_c
        if j==2
            subindext_inflow(i,3:end,j)=subindext_inflow_prova(i,:,j);
        else
            subindext_inflow(i,:,j)=subindext_inflow(i,:,j);
        end
    end
end

%Lineraly detrend the subcomponents of the index: OUTFLOWS
[subindext_outflow] = linear_trend(num_subindex,schindler_years,subindex_outflow_matrix);

%Detrend the series of bonds starting from the first valid value (third
%observation)
%CHANGE MANUALLY!!!!!
[subindext_outflow_prova] = linear_trend(num_subindex,schindler_years-2,subindex_outflow_matrix(:,3:end,:));

for j=1:subindex_num
    for i=1:num_c
        if j==2
            subindext_outflow(i,3:end,j)=subindext_outflow_prova(i,:,j);
        else
            subindext_outflow(i,:,j)=subindext_outflow(i,:,j);
        end
    end
end

%% Calculate average, standard deviation and confidende intervals in Boom/Bust for subindexes

%OVERALL
%BOOM
[average_subindext_values_boom, std_subindext_values_boom, upper_bound_boom_subindext, lower_bound_boom_subindext, indext_values_boom] = average_boom_bust(num_subindex, num_boom, boom_matrix, subindext);
 
%BUST
[average_subindext_values_bust, std_subindext_values_bust, upper_bound_bust_subindext, lower_bound_bust_subindext, indext_values_bust] = average_boom_bust(num_subindex, num_bust, bust_matrix, subindext);

%INFLOWS
%BOOM
[average_subindext_inflow_values_boom, std_subindext_inflow_values_boom, upper_bound_boom_subindext_inflow, lower_bound_boom_subindext_inflow, indext_inflow_values_boom] = average_boom_bust(num_subindex, num_boom, boom_matrix, subindext_inflow);
 
%BUST
[average_subindext_inflow_values_bust, std_subindext_inflow_values_bust, upper_bound_bust_subindext_inflow, lower_bound_bust_subindext_inflow, indext_inflow_values_bust] = average_boom_bust(num_subindex, num_bust, bust_matrix, subindext_inflow);

%OUTFLOWS
%BOOM
[average_subindext_outflow_values_boom, std_subindext_outflow_values_boom, upper_bound_boom_subindext_outflow, lower_bound_boom_subindext_outflow, indext_outflow_values_boom] = average_boom_bust(num_subindex, num_boom, boom_matrix, subindext_outflow);
 
%BUST
[average_subindext_outflow_values_bust, std_subindext_outflow_values_bust, upper_bound_bust_subindext_outflow, lower_bound_bust_subindext_outflow, indext_outflow_values_bust] = average_boom_bust(num_subindex, num_bust, bust_matrix, subindext_outflow);

%% This is going to be needed for Figure 4 and Figure 4.A.1

%Define labels
x=[-2 -1 0 1 2];
label_x=[-2 -1 0 +1 +2];

% Import data once again, this time keeping the subcategories of equity, bonds and direct investment.

if mac_user==1
[data_bogota,text_bogota, raw_bogota]=xlsread(strcat(BasePath,'\FRU_dataset.xls') ,char('index_paper'));
else
[data_bogota,text_bogota, raw_bogota]=xlsread(strcat(BasePath,'/FRU_dataset.xls') ,char('index_paper'));
end    

%COMPONENT INFLOW: siar and plbn, dii ldi
inflow_subindexes=[data_bogota(:,6), data_bogota(:,8), data_bogota(:,11), data_bogota(:,13), data_bogota(:,15), data_bogota(:,18),...
    data_bogota(:,37),data_bogota(:,38), data_bogota(:,40)];
 
%# basic index components inflow
num_subindexes_inflow=size(inflow_subindexes,2);

for i=1:Full_sample
     for j=1:schindler_years
         for k=1:num_subindexes_inflow
             matrix_inflow_shaped(i,j,k) = inflow_subindexes(j+(schindler_years*(i-1)), k);
         end
     end
end


%ELIMINATE 12 COUNTRIES WITH LESS THAN 25 YEAR OBSERVATIONS FOR GDP 
matrix_inflow_shaped=[matrix_inflow_shaped(1:4,:,:); matrix_inflow_shaped(6:10,:,:); matrix_inflow_shaped(12:18,:,:); matrix_inflow_shaped(20:26,:,:);matrix_inflow_shaped(28:32,:,:);matrix_inflow_shaped(34:41,:,:);matrix_inflow_shaped(43:45,:,:);
     matrix_inflow_shaped(48:52,:,:);matrix_inflow_shaped(54:66,:,:);matrix_inflow_shaped(69:70,:,:);matrix_inflow_shaped(72:89,:,:);matrix_inflow_shaped(91,:,:)];

 %Lineraly detrend the subcomponents of the index: INFLOWS
[matrix_inflow_detrended] = linear_trend(num_subindexes_inflow,schindler_years,matrix_inflow_shaped);

%Detrend the series of bonds starting from the first valid value (third
%observation)
%CHANGE MANUALLY!!!!!
[matrix_inflow_prova] = linear_trend(num_subindexes_inflow,schindler_years-2,matrix_inflow_shaped(:,3:end,:));

for j=1:num_subindexes_inflow
    for i=1:num_c
        if j==4
            matrix_inflow_detrended(i,3:end,j)=matrix_inflow_prova(i,:,j);
        elseif j==5
            matrix_inflow_detrended(i,3:end,j)=matrix_inflow_prova(i,:,j);
        elseif j==6
            matrix_inflow_detrended(i,3:end,j)=matrix_inflow_prova(i,:,j);
        else
            matrix_inflow_detrended(i,:,j)=matrix_inflow_detrended(i,:,j);
        end
    end
end
 
%COMPONENT OUTFLOW: siln and pabr, dio
outflow_subindexes=[data_bogota(:,7), data_bogota(:,9), data_bogota(:,10), data_bogota(:,14), data_bogota(:,16), data_bogota(:,17),...
     data_bogota(:,39)];

%# basic index components outflow
num_subindexes_outflow=size(outflow_subindexes,2);

for i=1:Full_sample
     for j=1:schindler_years
         for k=1:num_subindexes_outflow
             matrix_outflow_shaped(i,j,k) = outflow_subindexes(j+(schindler_years*(i-1)), k);
         end
     end
end


%ELIMINATE 12 COUNTRIES WITH LESS THAN 25 YEAR OBSERVATIONS FOR GDP 
matrix_outflow_shaped=[matrix_outflow_shaped(1:4,:,:); matrix_outflow_shaped(6:10,:,:); matrix_outflow_shaped(12:18,:,:); matrix_outflow_shaped(20:26,:,:);matrix_outflow_shaped(28:32,:,:);matrix_outflow_shaped(34:41,:,:);matrix_outflow_shaped(43:45,:,:);
     matrix_outflow_shaped(48:52,:,:);matrix_outflow_shaped(54:66,:,:);matrix_outflow_shaped(69:70,:,:);matrix_outflow_shaped(72:89,:,:);matrix_outflow_shaped(91,:,:)];

%Lineraly detrend the subcomponents of the index: OUTFLOWS
[matrix_outflow_detrended] = linear_trend(num_subindexes_outflow,schindler_years,matrix_outflow_shaped);

%Detrend the series of bonds starting from the first valid value (third
%observation)
%CHANGE MANUALLY!!!!!
[matrix_outflow_prova] = linear_trend(num_subindexes_outflow,schindler_years-2,matrix_outflow_shaped(:,3:end,:));

for j=1:num_subindexes_outflow
    for i=1:num_c
        if j==4
            matrix_outflow_detrended(i,3:end,j)=matrix_outflow_prova(i,:,j);
        elseif j==5
            matrix_outflow_detrended(i,3:end,j)=matrix_outflow_prova(i,:,j);
        elseif j==6
            matrix_outflow_detrended(i,3:end,j)=matrix_outflow_prova(i,:,j);
        else
            matrix_outflow_detrended(i,:,j)=matrix_outflow_detrended(i,:,j);
        end
    end
end
 

%INFLOWS
%BOOM
[average_subindexes_inflow_values_boom, std_subindexes_inflow_values_boom, upper_bound_boom_subindexes_inflow, lower_bound_boom_subindexes_inflow, indexes_inflow_values_boom] = average_boom_bust(num_subindexes_inflow, num_boom, boom_matrix, matrix_inflow_detrended);
%BUST
[average_subindexes_inflow_values_bust, std_subindexes_inflow_values_bust, upper_bound_bust_subindexes_inflow, lower_bound_bust_subindexes_inflow, indexes_inflow_values_bust] = average_boom_bust(num_subindexes_inflow, num_bust, bust_matrix, matrix_inflow_detrended);

%OUTFLOWS
%BOOM
[average_subindexes_outflow_values_boom, std_subindexes_outflow_values_boom, upper_bound_boom_subindexes_outflow, lower_bound_boom_subindexes_outflow, indexes_outflow_values_boom] = average_boom_bust(num_subindexes_outflow, num_boom, boom_matrix, matrix_outflow_detrended);
%BUST
[average_subindexes_outflow_values_bust, std_subindexes_outflow_values_bust, upper_bound_bust_subindexes_outflow, lower_bound_bust_subindexes_outflow, indexes_outflow_values_bust] = average_boom_bust(num_subindexes_outflow, num_bust, bust_matrix, matrix_outflow_detrended);

save case_GDP22 average_subindexes_inflow_values_boom average_subindexes_outflow_values_boom boom_matrix bust_matrix...
    num_subindexes_inflow num_subindexes_outflow num_boom num_bust matrix_inflow_detrended matrix_outflow_detrended...
    average_subindexes_inflow_values_bust std_subindexes_inflow_values_bust upper_bound_bust_subindexes_inflow...
    lower_bound_bust_subindexes_inflow indexes_inflow_values_boom indexes_outflow_values_boom indexes_inflow_values_bust...
    average_subindexes_outflow_values_bust std_subindexes_outflow_values_bust upper_bound_bust_subindexes_outflow...
    lower_bound_bust_subindexes_outflow indexes_outflow_values_bust average_subindext_inflow_values_bust...
    std_subindext_inflow_values_bust upper_bound_bust_subindext_inflow lower_bound_bust_subindext_inflow indext_inflow_values_bust...
    average_subindext_outflow_values_bust std_subindext_outflow_values_bust upper_bound_bust_subindext_outflow...
    lower_bound_bust_subindext_outflow indext_outflow_values_bust average_subindext_values_boom std_subindext_values_boom...
    upper_bound_boom_subindext lower_bound_boom_subindext indext_values_boom...
    average_subindext_outflow_values_boom std_subindext_outflow_values_boom upper_bound_boom_subindext_outflow...
    lower_bound_boom_subindext_outflow indext_outflow_values_boom...
    average_subindexes_outflow_values_boom std_subindexes_outflow_values_boom upper_bound_boom_subindexes_outflow...
    lower_bound_boom_subindexes_outflow indexes_outflow_values_boom average_subindext_inflow_values_boom average_boom_values...
    upper_bound_boom_subindext_inflow lower_bound_boom_subindext_inflow lower_bound_boom_subindexes_inflow upper_bound_boom_subindexes_inflow
%% This is the figure 4
% ROW 1: AGGREGATES INFLOWS (EQI, BOI, DII_LDI)

figure(1)

%Equity

subplot(3,3,1)
[haxes,hline1,hline2] = plotyy(average_subindext_inflow_values_boom(1,:,1), 'b', 100*average_boom_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
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_boom_subindext_inflow(1,:,1)),2) roundTo(1.75*max(upper_bound_boom_subindext_inflow(1,:,1)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_subindext_inflow(1,:,1)),2)  0 roundTo(1.75*max(upper_bound_boom_subindext_inflow(1,:,1)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_subindext_inflow(1,:,1)),2) 0 roundTo(1.75*max(upper_bound_boom_subindext_inflow(1,:,1)),2)],'FontSize',10);
t=title(' eq','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_subindext_inflow(1,:,1),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_subindext_inflow(1,:,1),'k-.');
box off
hold off

% Bonds

subplot(3,3,2)
[haxes,hline1,hline2] = plotyy(average_subindext_inflow_values_boom(1,:,2), 'b', 100*average_boom_values ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
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_boom_subindext_inflow(1,:,2)),2) roundTo(1.75*max(upper_bound_boom_subindext_inflow(1,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_subindext_inflow(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_boom_subindext_inflow(1,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_subindext_inflow(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_boom_subindext_inflow(1,:,2)),2)],'FontSize',10);
t=title('bo','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_subindext_inflow(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_subindext_inflow(1,:,2),'k-.');
box off
hold off

% Direct Investment

subplot(3,3,3)
[haxes,hline1,hline2] = plotyy(average_subindext_inflow_values_boom(1,:,6), 'b', 100*average_boom_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
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_boom_subindext_inflow(1,:,6)),2) roundTo(1.75*max(upper_bound_boom_subindext_inflow(1,:,6)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_subindext_inflow(1,:,6)),2) 0 roundTo(1.75*max(upper_bound_boom_subindext_inflow(1,:,6)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_subindext_inflow(1,:,6)),2) 0 roundTo(1.75*max(upper_bound_boom_subindext_inflow(1,:,6)),2)],'FontSize',10);
t=title('di','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_subindext_inflow(1,:,6),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_subindext_inflow(1,:,6),'k-.');
box off
hold off

% ROW 2: PLBN + DII

%eq_plbn

subplot(3,3,4)
[haxes,hline1,hline2] = plotyy(average_subindexes_inflow_values_boom(1,:,2), 'b', 100*average_boom_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
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_boom_subindexes_inflow(1,:,2)),2) roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_subindexes_inflow(1,:,2)),2)  0 roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_subindexes_inflow(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,2)),2)],'FontSize',10);
t=title(' eq-plbn','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_subindexes_inflow(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_subindexes_inflow(1,:,2),'k-.');
box off
hold off

%bo_plbn

subplot(3,3,5)
[haxes,hline1,hline2] = plotyy(average_subindexes_inflow_values_boom(1,:,5), 'b', 100*average_boom_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
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_boom_subindexes_inflow(1,:,5)),2) roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,5)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_subindexes_inflow(1,:,5)),2)  0 roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,5)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_subindexes_inflow(1,:,5)),2) 0 roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,5)),2)],'FontSize',10);
t=title(' bo-plbn','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_subindexes_inflow(1,:,5),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_subindexes_inflow(1,:,5),'k-.');
box off
hold off

%dii

subplot(3,3,6)
[haxes,hline1,hline2] = plotyy(average_subindexes_inflow_values_boom(1,:,8), 'b', 100*average_boom_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
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_boom_subindexes_inflow(1,:,8)),2) roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,8)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_subindexes_inflow(1,:,8)),2)  0 roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,8)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_subindexes_inflow(1,:,8)),2) 0 roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,8)),2)],'FontSize',10);
t=title(' dii','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_subindexes_inflow(1,:,8),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_subindexes_inflow(1,:,8),'k-.');
box off
hold off

% ROW 3: SIAR + LDI

%eq_siar

subplot(3,3,7)
[haxes,hline1,hline2] = plotyy(average_subindexes_inflow_values_boom(1,:,3), 'b', 100*average_boom_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
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_boom_subindexes_inflow(1,:,3)),2) roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_subindexes_inflow(1,:,3)),2)  0 roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_subindexes_inflow(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,3)),2)],'FontSize',10);
t=title(' eq-siar','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_subindexes_inflow(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_subindexes_inflow(1,:,3),'k-.');
box off
hold off

%bo_siar

subplot(3,3,8)
[haxes,hline1,hline2] = plotyy(average_subindexes_inflow_values_boom(1,:,6), 'b', 100*average_boom_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
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_boom_subindexes_inflow(1,:,6)),2) roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,6)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_subindexes_inflow(1,:,6)),2)  0 roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,6)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_subindexes_inflow(1,:,6)),2) 0 roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,6)),2)],'FontSize',10);
t=title('bo-siar','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_subindexes_inflow(1,:,6),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_subindexes_inflow(1,:,6),'k-.');
box off
hold off

%ldi

subplot(3,3,9)
[haxes,hline1,hline2] = plotyy(average_subindexes_inflow_values_boom(1,:,9), 'b', 100*average_boom_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
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_boom_subindexes_inflow(1,:,9)),2) roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,9)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_subindexes_inflow(1,:,9)),2)  0 roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,9)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_subindexes_inflow(1,:,9)),2) 0 roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,9)),2)],'FontSize',10);
t=title('ldi','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_subindexes_inflow(1,:,9),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_subindexes_inflow(1,:,9),'k-.');
box off
hold off

legend1 = legend([hline1 hline2 hline3],'Average Inflow Index (lhs)','Average Output Gap(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');

    case 11

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%CALCULATE CORRELATION BETWEEN OUTPUT GAP AND INFLOW SUBINDEX, METHOD 1: STACKED MATRIX%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load case_GDP1
load case_GDP2
load case_GDP5
load case_GDP9

%% Calculate correlation for output gap and subindexes both attime t=0

%Create a matrix of aggregated subindexes
for k=1:num_subindex
    for j=1:schindler_years
        for i=1:num_c
            aggregate_subindext_matrix(j+schindler_years*(i-1),k)=subindext_inflow(i,j,k);
        end
    end
end

%Create a vector for the bond because of the first two missing observations
for k=2
    for j=1:schindler_years-2
        for i=1:num_c
            aggregate_bond_matrix(j+(schindler_years-2)*(i-1),1)=subindext_inflow(i,j+2,k);
        end
    end
end

%Create a vector of aggregated ouput gap for bond subindex
for j=1:schindler_years-2
    for i=1:num_c
        aggregate_xt_bond(j+(schindler_years-2)*(i-1),1)=xt(i,j+2,1);
    end
end

%% Aggregate data for income groups at time t=0 for the subindexes

%Create vectors of subindexes for different inocme groups
[subindext_adv, subindext_mid, subindext_poor] = country_groups(num_subindex, subindext_inflow);

%Create vector for bond of the output gap
[xt_bond_adv, xt_bond_mid, xt_bond_poor] = country_groups(1, xt(:,3:end));

%Delete the NaN from the subindex series for bond
bond_adv=subindext_adv(:,3:end,2);
bond_mid=subindext_mid(:,3:end,2);
bond_poor=subindext_poor(:,3:end,2);

%Create aggregated matrix for subindexes
[aggregate_subindext_matrix_adv, aggregate_subindext_matrix_mid, aggregate_subindext_matrix_poor]=aggregate_group(num_subindex, subindext_adv, subindext_mid, subindext_poor);

%Create aggregated matrix for bond case
[aggregate_bond_adv, aggregate_bond_mid, aggregate_bond_poor]=aggregate_group_special_bond(1, bond_adv, bond_mid, bond_poor);

%Create aggregated matrix of output for bond case
[aggregate_xt_bond_adv, aggregate_xt_bond_mid, aggregate_xt_bond_poor]=aggregate_group_special_bond(1, xt_bond_adv, xt_bond_mid, xt_bond_poor);

%% Aggregate data for output gap and index for calculating the lag and lead correlations

%LAG INDEX
%Create a vector of aggregated subindex at time t=-1
for k=1:num_subindex
    for j=1:schindler_years-1
        for i=1:num_c
            aggregate_subindext_aux1(j+(schindler_years-1)*(i-1),k)=subindext_inflow(i,j,k);
        end
    end
end

%Create a vector of aggregated ouput gap at time t=-1
for j=2:schindler_years
    for i=1:num_c
        aggregate_xt_aux1(j-1+(schindler_years-1)*(i-1),1)=xt(i,j);
    end
end

%Create a vector of aggregated bond subindex at time t=-1
for j=3:schindler_years-1
    for i=1:num_c
        aggregate_subindext_aux1_bond(j-2+(schindler_years-3)*(i-1),1)=subindext_inflow(i,j,2);
    end
end

%Create a vector of aggregated ouput gap at time t=-1 for bond case
for j=4:schindler_years
    for i=1:num_c
        aggregate_xt_aux1_bond(j-3+(schindler_years-3)*(i-1),1)=xt(i,j);
    end
end

%LEAD INDEX
%Create a vector of aggregated subindex at time t=+1
for k=1:num_subindex
    for j=2:schindler_years
        for i=1:num_c
            aggregate_subindext_aux2(j-1+(schindler_years-1)*(i-1),k)=subindext_inflow(i,j,k);
        end
    end
end

%Create a vector of aggregated ouput gap at time t=+1
for j=1:schindler_years-1
    for i=1:num_c
        aggregate_xt_aux2(j+(schindler_years-1)*(i-1),1)=xt(i,j);
    end
end

%Create a vector of aggregated subindex at time t=+1
for j=4:schindler_years
    for i=1:num_c
       aggregate_subindext_aux2_bond(j-3+(schindler_years-3)*(i-1),1)=subindext_inflow(i,j,2);
    end
end

%Create a vector of aggregated ouput gap at time t=+1
for j=3:schindler_years-1
    for i=1:num_c
        aggregate_xt_aux2_bond(j-2+(schindler_years-3)*(i-1),1)=xt(i,j);
    end
end

%% Aggregate data for output gap and index for calculating the lag and lead correlations divided by groups

%LAG INDEX
%Create country groups for detrended index and output gap
[aggregate_subindext_aux1_adv, aggregate_subindext_aux1_mid, aggregate_subindext_aux1_poor, aggregate_xt_aux1_adv, aggregate_xt_aux1_mid, aggregate_xt_aux1_poor] = aggregate_group_one(num_subindex, num_gdp, subindext_adv, subindext_mid, subindext_poor, xt_adv, xt_mid, xt_poor);

%Create country groups for detrended index and output gap for the bond case
[aggregate_subindext_aux1_adv_bond, aggregate_subindext_aux1_mid_bond, aggregate_subindext_aux1_poor_bond, aggregate_xt_aux1_adv_bond, aggregate_xt_aux1_mid_bond, aggregate_xt_aux1_poor_bond] = aggregate_group_one_special_bond(1, num_gdp, bond_adv, bond_mid, bond_poor, xt_bond_adv, xt_bond_mid, xt_bond_poor);

%LEAD INDEX
%Create country groups for detrended index and output gap
[aggregate_xt_aux2_adv, aggregate_xt_aux2_mid, aggregate_xt_aux2_poor, aggregate_subindext_aux2_adv, aggregate_subindext_aux2_mid, aggregate_subindext_aux2_poor] = aggregate_group_one(num_gdp, num_subindex, xt_adv, xt_mid, xt_poor, subindext_adv, subindext_mid, subindext_poor);

%Create country groups for detrended index and output gap for the bond case
[aggregate_xt_aux2_adv_bond, aggregate_xt_aux2_mid_bond, aggregate_xt_aux2_poor_bond, aggregate_subindext_aux2_adv_bond, aggregate_subindext_aux2_mid_bond, aggregate_subindext_aux2_poor_bond] = aggregate_group_one_special_bond(num_gdp, 1, xt_bond_adv, xt_bond_mid, xt_bond_poor, bond_adv, bond_mid, bond_poor);

%% Calculate correlations for index t=0 (No inocme group division)

%Calculate correlation for aggregated matrices   
[r_agg_sub_matrix, p_agg_sub_matrix, star_agg_sub_matrix] = aggregate_correlation(num_subindex, aggregate_xt, aggregate_subindext_matrix);

%Calculate correlation for aggregated matrices   
[r_agg_bond_matrix, p_agg_bond_matrix, star_agg_bond_matrix] = aggregate_correlation(1, aggregate_xt_bond, aggregate_bond_matrix);

%Concatenate values and stars
r_agg_equity=num2str(r_agg_sub_matrix(:,1));
r_agg_equity=strcat(r_agg_equity,star_agg_sub_matrix(:,1));

r_agg_bond=num2str(r_agg_bond_matrix(:,1));
r_agg_bond=strcat(r_agg_bond,star_agg_bond_matrix(:,1));

r_agg_money=num2str(r_agg_sub_matrix(:,3));
r_agg_money=strcat(r_agg_money,star_agg_sub_matrix(:,3));

r_agg_cinvestment=num2str(r_agg_sub_matrix(:,4));
r_agg_cinvestment=strcat(r_agg_cinvestment,star_agg_sub_matrix(:,4));

r_agg_credit=num2str(r_agg_sub_matrix(:,5));
r_agg_credit=strcat(r_agg_credit,star_agg_sub_matrix(:,5));

r_agg_dinvestment=num2str(r_agg_sub_matrix(:,6));
r_agg_dinvestment=strcat(r_agg_dinvestment,star_agg_sub_matrix(:,6));

%% Calculate correlations for index t=-1 (No inocme group division)

%Calculate correlation for aggregated matrices
[r_agg_minus1_sub, p_agg_minus1_sub, star_agg_minus1_sub] = aggregate_correlation(num_subindex, aggregate_xt_aux1, aggregate_subindext_aux1);

%Calculate correlation for the bond case
[r_agg_minus1_bond, p_agg_minus1_bond, star_agg_minus1_bond] = aggregate_correlation(1, aggregate_xt_aux1_bond, aggregate_subindext_aux1_bond);

%Concatenate values and stars
r_agg_minus1_equity=num2str(r_agg_minus1_sub(:,1));
r_agg_minus1_equity=strcat(r_agg_minus1_equity,star_agg_minus1_sub(:,1));
 
r_agg_minus1_bond=num2str(r_agg_minus1_bond(:,1));
r_agg_minus1_bond=strcat(r_agg_minus1_bond,star_agg_minus1_bond(:,1));
 
r_agg_minus1_money=num2str(r_agg_minus1_sub(:,3));
r_agg_minus1_money=strcat(r_agg_minus1_money,star_agg_minus1_sub(:,3));

r_agg_minus1_cinvestment=num2str(r_agg_minus1_sub(:,4));
r_agg_minus1_cinvestment=strcat(r_agg_minus1_cinvestment,star_agg_minus1_sub(:,4));

r_agg_minus1_credit=num2str(r_agg_minus1_sub(:,5));
r_agg_minus1_credit=strcat(r_agg_minus1_credit,star_agg_minus1_sub(:,5));

r_agg_minus1_dinvestment=num2str(r_agg_minus1_sub(:,6));
r_agg_minus1_dinvestment=strcat(r_agg_minus1_dinvestment,star_agg_minus1_sub(:,6));

%% Calculate correlations for index t=+1 (No income group division)

%Calculate correlation for aggregated matrices
[r_agg_plus1_sub, p_agg_plus1_sub, star_agg_plus1_sub] = aggregate_correlation(num_subindex, aggregate_xt_aux2, aggregate_subindext_aux2);

%Calculate correlation for the bond case
[r_agg_plus1_bond, p_agg_plus1_bond, star_agg_plus1_bond] = aggregate_correlation(1, aggregate_xt_aux2_bond, aggregate_subindext_aux2_bond);

%Concatenate values and stars
r_agg_plus1_equity=num2str(r_agg_plus1_sub(:,1));
r_agg_plus1_equity=strcat(r_agg_plus1_equity,star_agg_plus1_sub(:,1));
 
r_agg_plus1_bond=num2str(r_agg_plus1_bond(:,1));
r_agg_plus1_bond=strcat(r_agg_plus1_bond,star_agg_plus1_bond(:,1));
 
r_agg_plus1_money=num2str(r_agg_plus1_sub(:,3));
r_agg_plus1_money=strcat(r_agg_plus1_money,star_agg_plus1_sub(:,3));
 
r_agg_plus1_cinvestment=num2str(r_agg_plus1_sub(:,4));
r_agg_plus1_cinvestment=strcat(r_agg_plus1_cinvestment,star_agg_plus1_sub(:,4));
 
r_agg_plus1_credit=num2str(r_agg_plus1_sub(:,5));
r_agg_plus1_credit=strcat(r_agg_plus1_credit,star_agg_plus1_sub(:,5));
 
r_agg_plus1_dinvestment=num2str(r_agg_plus1_sub(:,6));
r_agg_plus1_dinvestment=strcat(r_agg_plus1_dinvestment,star_agg_plus1_sub(:,6));

%Create matrix with all countries for different time horizons
sub_corr(:,:,1)=[r_agg_minus1_equity r_agg_equity r_agg_plus1_equity];
sub_corr(:,:,2)=[r_agg_minus1_bond r_agg_bond r_agg_plus1_bond];
sub_corr(:,:,3)=[r_agg_minus1_money r_agg_money r_agg_plus1_money];
sub_corr(:,:,4)=[r_agg_minus1_cinvestment r_agg_cinvestment r_agg_plus1_cinvestment];
sub_corr(:,:,5)=[r_agg_minus1_credit r_agg_credit r_agg_plus1_credit];
sub_corr(:,:,6)=[r_agg_minus1_dinvestment r_agg_dinvestment r_agg_plus1_dinvestment];

%% Calculate the correlations matrix for country group and different time horizons

%ADVANCED
[adv_sub_corr] = subindext_corr_group( num_subindex, aggregate_xt_adv, aggregate_subindext_matrix_adv, aggregate_xt_bond_adv, aggregate_bond_adv,aggregate_xt_aux1_adv, aggregate_subindext_aux1_adv,aggregate_xt_aux1_adv_bond, aggregate_subindext_aux1_adv_bond, aggregate_xt_aux2_adv, aggregate_subindext_aux2_adv, aggregate_xt_aux2_adv_bond, aggregate_subindext_aux2_adv_bond);

%MIDDLE
[mid_sub_corr] = subindext_corr_group( num_subindex, aggregate_xt_mid, aggregate_subindext_matrix_mid, aggregate_xt_bond_mid, aggregate_bond_mid,aggregate_xt_aux1_mid, aggregate_subindext_aux1_mid,aggregate_xt_aux1_mid_bond, aggregate_subindext_aux1_mid_bond, aggregate_xt_aux2_mid, aggregate_subindext_aux2_mid, aggregate_xt_aux2_mid_bond, aggregate_subindext_aux2_mid_bond);

%POOR
[poor_sub_corr] = subindext_corr_group( num_subindex, aggregate_xt_poor, aggregate_subindext_matrix_poor, aggregate_xt_bond_poor, aggregate_bond_poor,aggregate_xt_aux1_poor, aggregate_subindext_aux1_poor,aggregate_xt_aux1_poor_bond, aggregate_subindext_aux1_poor_bond, aggregate_xt_aux2_poor, aggregate_subindext_aux2_poor, aggregate_xt_aux2_poor_bond, aggregate_subindext_aux2_poor_bond);

%% Make tables for all correlations

%Define labels and titles for all subcomponents
Labels_h = { '' , 'j=-1', 'j=0', 'j=+1'};
Labels_v = {'Advanced Economies'; 'Emerging Markets'; 'Low-Income Countries'};
vector_nothing={'', '', '', ''};
title_average_subindex={'Table A.5: Correlations between Output and Inflows Capital Controls by Asset Category', '', '', ''};
subtitle_average_corr_equity={'(a) Equity: Total'};
subtitle_average_corr_bond={'(b) Bond: Total'};
subtitle_average_corr_money={'(c) Money Market: Total'};
subtitle_average_corr_cinvestment={'(d) Collective Investment: Total'};
subtitle_average_corr_credit={'(e) Financial Credit: Total'};
subtitle_average_corr_dinvestment={'(f) Direct Investment: Total'};

%Create tables for equity subindex
correlation_equity_table=[ sub_corr(:,:,1)
    adv_sub_corr(:,:,1)
    mid_sub_corr(:,:,1)
    poor_sub_corr(:,:,1)]; 

%Create tables for bond subindex
correlation_bond_table=[ sub_corr(:,:,2)
    adv_sub_corr(:,:,2)
    mid_sub_corr(:,:,2)
    poor_sub_corr(:,:,2)]; 
 
%Create tables for money subindex
correlation_money_table=[ sub_corr(:,:,3)
    adv_sub_corr(:,:,3)
    mid_sub_corr(:,:,3)
    poor_sub_corr(:,:,3)];
 
%Create tables for collective investment subindex
correlation_cinvestment_table=[ sub_corr(:,:,4)
    adv_sub_corr(:,:,4)
    mid_sub_corr(:,:,4)
    poor_sub_corr(:,:,4)]; 
 
%Create tables for credit subindex
correlation_credit_table=[ sub_corr(:,:,5)
    adv_sub_corr(:,:,5)
    mid_sub_corr(:,:,5)
    poor_sub_corr(:,:,5)];
 
%Create tables for direct investment subindex
correlation_dinvestment_table=[ sub_corr(:,:,6)
    adv_sub_corr(:,:,6)
    mid_sub_corr(:,:,6)
    poor_sub_corr(:,:,6)];

Table_average_corr_subindex = [title_average_subindex; Labels_h; 
    subtitle_average_corr_equity, correlation_equity_table(1,:); Labels_v correlation_equity_table(2:end,:);
    vector_nothing;
    subtitle_average_corr_bond, correlation_bond_table(1,:); Labels_v correlation_bond_table(2:end,:);
    vector_nothing;
    subtitle_average_corr_money, correlation_money_table(1,:); Labels_v correlation_money_table(2:end,:);
    vector_nothing;
    subtitle_average_corr_cinvestment, correlation_cinvestment_table(1,:); Labels_v correlation_cinvestment_table(2:end,:);
    vector_nothing;
    subtitle_average_corr_credit, correlation_credit_table(1,:); Labels_v correlation_credit_table(2:end,:);
    vector_nothing;
    subtitle_average_corr_dinvestment, correlation_dinvestment_table(1,:); Labels_v correlation_dinvestment_table(2:end,:)];

save Result_GDP11 Table_average_corr_subindex

   case 12

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%EXCHANGE RATE REGIME EXERCISE%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load case_GDP1
load case_GDP2
load case_GDP4
load case_GDP7
load case_GDP8

if mac_user==1
[vardat,textdata, raw]=xlsread(strcat(BasePath, '\exchange_rate_regime.xls') ,char('data')); 
else
[vardat,textdata, raw]=xlsread(strcat(BasePath, '/exchange_rate_regime.xls') ,char('data')); 
end    
    
ex_regime=vardat(2:end,:);    
    

%ELIMINATE 12 COUNTRIES WITH LESS THAN 25 YEAR OBSERVATIONS FOR GDP
ex_regime=[ex_regime(1:4,:,:); ex_regime(6:10,:,:); ex_regime(12:18,:,:); ex_regime(20:26,:,:);ex_regime(28:32,:,:);ex_regime(34:41,:,:);ex_regime(43:45,:,:);
     ex_regime(48:52,:,:);ex_regime(54:66,:,:);ex_regime(69:70,:,:);ex_regime(72:89,:,:);ex_regime(91,:,:)];

%Aggregate exchange regime data
for i=1:num_c
    for j=1:schindler_years
        aggregate_ex_regime(j+schindler_years*(i-1),1)=ex_regime(i,j);
        aggregate_xt(j+schindler_years*(i-1),1)=xt(i,j);
        aggregate_overall_indext(j+schindler_years*(i-1),1)=indext(i,j,1);
        aggregate_inflow_indext(j+schindler_years*(i-1),1)=indext(i,j,2);
        aggregate_outflow_indext(j+schindler_years*(i-1),1)=indext(i,j,3);
    end
end

%Calculate the averages and confidence intervals for output gap and OVERALL
%index divided by different exchange rate regime
[num_regime_boom, num_regime_bust, boom_bust_indicator, xt_average_matrix_boom, xt_average_matrix_bust, overall_average_matrix_boom, overall_average_matrix_bust, overall_interval_matrix_boom, overall_interval_matrix_bust] = exchange_regime_exercise(num_boom, num_bust, boom_matrix, bust_matrix, aggregate_xt, aggregate_ex_regime, aggregate_overall_indext);

%Calculate the averages and confidence intervals for output gap and INFLOW
%index divided by different exchange rate regime
[~, ~, ~, ~, ~, inflow_average_matrix_boom, inflow_average_matrix_bust, inflow_interval_matrix_boom, inflow_interval_matrix_bust] = exchange_regime_exercise(num_boom, num_bust, boom_matrix, bust_matrix, aggregate_xt, aggregate_ex_regime, aggregate_inflow_indext);

%Calculate the averages and confidence intervals for output gap and OUTFLOW
%index divided by different exchange rate regime
[~, ~, ~, ~, ~, outflow_average_matrix_boom, outflow_average_matrix_bust, outflow_interval_matrix_boom, outflow_interval_matrix_bust] = exchange_regime_exercise(num_boom, num_bust, boom_matrix, bust_matrix, aggregate_xt, aggregate_ex_regime, aggregate_outflow_indext);

save case_GDP12 aggregate_xt aggregate_overall_indext aggregate_inflow_indext aggregate_outflow_indext num_regime_boom num_regime_bust inflow_average_matrix_boom xt_average_matrix_boom xt_average_matrix_bust inflow_interval_matrix_boom outflow_average_matrix_boom outflow_average_matrix_bust outflow_interval_matrix_boom outflow_interval_matrix_bust inflow_average_matrix_bust inflow_interval_matrix_bust

    case 30

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%INDEBTED COUNTRIES EXERCISE%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load case_GDP1
load case_GDP2
load case_GDP3
load case_GDP4
load case_GDP7
load case_GDP8
load case_GDP12

%% Import data on nfa and manipulate main variables

if mac_user==1
[vardat,textdata, raw]=xlsread(strcat(BasePath, '\NFA.xls') ,char('data')); 
else
[vardat,textdata, raw]=xlsread(strcat(BasePath, '/NFA.xls') ,char('data'));     
end

%Define level of indebtness
nfa=vardat(2:end,:);
    

%ELIMINATE 12 COUNTRIES WITH LESS THAN 25 YEAR OBSERVATIONS FOR GDP 
nfa=[nfa(1:4,:,:); nfa(6:10,:,:); nfa(12:18,:,:); nfa(20:26,:,:);nfa(28:32,:,:);nfa(34:41,:,:);nfa(43:45,:,:);
     nfa(48:52,:,:);nfa(54:66,:,:);nfa(69:70,:,:);nfa(72:89,:,:);nfa(91,:,:)];

%Create aggregated vector
for i=1:num_c
    for j=1:schindler_years
        aggregate_nfa(j+schindler_years*(i-1),1)=nfa(i,j);
    end
end

%Sort the vector in ascendign order
aggregate_nfa_sort=sort(aggregate_nfa);

% Find the two threshold levels (20% and 80%)
threshold_high = prctile(aggregate_nfa_sort,level_threshold_high);
threshold_low  = prctile(aggregate_nfa_sort,level_threshold_low);

%Create indicator for level of indebtness. 1 for higly indebted countries, 2 for lowly indebted
%countries, 0 for middle.
debt_indicator=ones(size(aggregate_nfa,1),1);

for i=1:size(aggregate_nfa,1)
    if aggregate_nfa(i,1)<=threshold_high
        debt_indicator(i,1)=2;
    elseif aggregate_nfa(i,1)>=threshold_low
        debt_indicator(i,1)=0;
    else
        debt_indicator(i,1)=1;
    end
end

%Calculate averages, and confidence interval for output gap and detrended
%index divided by higly indebted countries and not highly indebted
%countries: OVERALL INDEX
[num_debt_boom, num_debt_bust, boom_bust_indicator, xt_average_matrix_boom_d, xt_average_matrix_bust_d, overall_average_matrix_boom_d, overall_average_matrix_bust_d, overall_interval_matrix_boom_d, overall_interval_matrix_bust_d] = debt_exercise_v1_Mar_01_2015(num_boom, num_bust, boom_matrix, bust_matrix, aggregate_xt, debt_indicator, aggregate_overall_indext);   

%Calculate averages, and confidence interval for output gap and detrended
%index divided by higly indebted countries and not highly indebted
%countries: INFLOWS INDEX
[~, ~, ~, ~, ~, inflow_average_matrix_boom_d, inflow_average_matrix_bust_d, inflow_interval_matrix_boom_d, inflow_interval_matrix_bust_d] = debt_exercise_v1_Mar_01_2015(num_boom, num_bust, boom_matrix, bust_matrix, aggregate_xt, debt_indicator, aggregate_inflow_indext);   

%Calculate averages, and confidence interval for output gap and detrended
%index divided by higly indebted countries and not highly indebted
%countries: OUTFLOWS INDEX
[~, ~, ~, ~, ~, outflow_average_matrix_boom_d, outflow_average_matrix_bust_d, outflow_interval_matrix_boom_d, outflow_interval_matrix_bust_d] = debt_exercise_v1_Mar_01_2015(num_boom, num_bust, boom_matrix, bust_matrix, aggregate_xt, debt_indicator, aggregate_outflow_indext);   

save case_GDP21 num_debt_boom num_debt_bust boom_bust_indicator xt_average_matrix_boom_d xt_average_matrix_bust_d overall_average_matrix_boom_d overall_average_matrix_bust_d overall_interval_matrix_boom_d overall_interval_matrix_bust_d inflow_average_matrix_boom_d inflow_average_matrix_bust_d inflow_interval_matrix_boom_d inflow_interval_matrix_bust_d outflow_average_matrix_boom_d outflow_average_matrix_bust_d outflow_interval_matrix_boom_d outflow_interval_matrix_bust_d

    case 13
%% Figure 5
load case_GDP21
load case_GDP1
load case_GDP2
load case_GDP3
load case_GDP4
load case_GDP7
load case_GDP8
load case_GDP12

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

% ROW 1: BOOM EPISODES AND CAPITAL CONTROLS BY LEVEL OF DEVELOPMENT
figure(1)
% Inflow-Advanced countries
subplot(3,3,1)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom_adv(1,:,2), 'b', 100*average_boom_values_adv ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
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_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('Advanced countries','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
% Inflow-Middle Income countries
subplot(3,3,2)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom_mid(1,:,2), 'b', 100*average_boom_values_mid ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
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_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('Emerging markets','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
% Inflow-Low Income Countries
subplot(3,3,3)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom_poor(1,:,2), 'b', 100*average_boom_values_poor ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
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_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('Low income countries','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

% ROW 2: BOOM EPISODES AND CAPITAL CONTROLS ACROSS EXCHANGE RATE REGIMES

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

%REGIME 2
% Inflows
subplot(3,3,5)
[haxes,hline1,hline2] = plotyy(inflow_average_matrix_boom(1,:,2), 'b', 100*xt_average_matrix_boom(1,:,2) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','')
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(inflow_interval_matrix_boom(1,:,2)),2) roundTo(1.75*max(inflow_interval_matrix_boom(2,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(inflow_interval_matrix_boom(1,:,2)),2) 0 roundTo(1.75*max(inflow_interval_matrix_boom(2,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(inflow_interval_matrix_boom(1,:,2)),2) 0 roundTo(1.75*max(inflow_interval_matrix_boom(2,:,2)),2)],'FontSize',10);
t=title('de facto Crawling Peg','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(inflow_interval_matrix_boom(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(inflow_interval_matrix_boom(2,:,2),'k-.');


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

% ROW 3: BOOM EPISODES AND CAPITAL CONTROLS BY LEVEL OF EXTERNAL INDEBTDENESS

% LOW EXTERNAL DEBT COUNTRIES
% Inflows
subplot(3,3,7)
[haxes,hline1,hline2] = plotyy(inflow_average_matrix_boom_d(1,:,1), 'b', 100*xt_average_matrix_boom_d(1,:,1) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','')
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(inflow_interval_matrix_boom_d(1,:,1)),2) roundTo(1.75*max(inflow_interval_matrix_boom_d(2,:,1)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(inflow_interval_matrix_boom_d(1,:,1)),2) 0 roundTo(1.75*max(inflow_interval_matrix_boom_d(2,:,1)),2)], 'YTickLabel', [roundTo(1.75*min(inflow_interval_matrix_boom_d(1,:,1)),2) 0 roundTo(1.75*max(inflow_interval_matrix_boom_d(2,:,1)),2)],'FontSize',10);
t=title('Low External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(inflow_interval_matrix_boom_d(1,:,1),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(inflow_interval_matrix_boom_d(2,:,1),'k-.');
box off
hold off

% MEDIUM EXTERNAL DEBT COUNTRIES
% Inflows
subplot(3,3,8)
[haxes,hline1,hline2] = plotyy(inflow_average_matrix_boom_d(1,:,2), 'b', 100*xt_average_matrix_boom_d(1,:,2) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','')
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(inflow_interval_matrix_boom_d(1,:,2)),2) roundTo(1.75*max(inflow_interval_matrix_boom_d(2,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(inflow_interval_matrix_boom_d(1,:,2)),2) 0 roundTo(1.75*max(inflow_interval_matrix_boom_d(2,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(inflow_interval_matrix_boom_d(1,:,2)),2) 0 roundTo(1.75*max(inflow_interval_matrix_boom_d(2,:,2)),2)],'FontSize',10);
t=title('Medium External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(inflow_interval_matrix_boom_d(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(inflow_interval_matrix_boom_d(2,:,2),'k-.');
box off
hold off


% HIGH EXTERNAL DEBT COUNTRIES
% Inflows
subplot(3,3,9)
[haxes,hline1,hline2] = plotyy(inflow_average_matrix_boom_d(1,:,3), 'b', 100*xt_average_matrix_boom_d(1,:,3) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','')
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(inflow_interval_matrix_boom_d(1,:,3)),2) roundTo(1.75*max(inflow_interval_matrix_boom_d(2,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(inflow_interval_matrix_boom_d(1,:,3)),2) 0 roundTo(1.75*max(inflow_interval_matrix_boom_d(2,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(inflow_interval_matrix_boom_d(1,:,3)),2) 0 roundTo(1.75*max(inflow_interval_matrix_boom_d(2,:,3)),2)],'FontSize',10);
t=title('High External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(inflow_interval_matrix_boom_d(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(inflow_interval_matrix_boom_d(2,:,3),'k-.');
box off
hold off

h=legend([hline1 hline2 hline3],'Average Inflow Index (lhs)','Average Output Gap (rhs)','Two Standard Deviation for the Index (lhs)');
set(h,'Orientation','horizontal','Position',[0.254947916666667 0.0154279490155329 0.548697916666667 0.0338504936530324],'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 5. Capital-Inflow Controls During Booms By Income Level, Exchange-Rate Regime, and Level of External Indebtedness','Position',[0.508203125 0.978604294478528 9.16025403784439],'HorizontalAlignment','center')

    case 15

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%ONLY LARGE BOOM/BUST AND DETRENDED INDEX%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load case_GDP1
load case_GDP2
load case_GDP3
load case_GDP4
load case_GDP7

%% Identify only the large boom/bustes (more than 1.3 standard deviation)

%Calculate standard deviation for each country
for i=1:num_c
    country_std(i,1)=nanstd(xt(i,:),0,2);
end

%Calculate country mean
for i=1:num_c
   country_mean(i,1)=nanmean(xt(i,:),2); 
end

%Calculate the interval for small boom/bust
for i=1:num_c
    country_int(i,:)=[country_mean(i,1)-1.3*country_std(i,1), country_mean(i,1)+1.3*country_std(i,1)];
end

%Select only the boom/busts outside the interval (large boom/bust)
%Boom
count_boom=1;
for j=1:num_boom
    if boom_matrix(j,4)>country_int(boom_matrix(j,1),2)
       large_boom_matrix(count_boom,:)=boom_matrix(j,:);
       count_boom=count_boom+1;
    end
end

num_large_boom=size(large_boom_matrix,1);

%Bust
count_bust=1;
for j=1:num_bust
    if bust_matrix(j,4)<country_int(bust_matrix(j,1),1)
        large_bust_matrix(count_bust,:)=bust_matrix(j,:);
        count_bust=count_bust+1;
    end
end

num_large_bust=size(large_bust_matrix,1);

%% 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
[large_average_boom_values,~,~,~, large_boom_values] = average_boom_bust(num_gdp, num_large_boom, large_boom_matrix, xt);

%BUST
%Calculate average, standard deviation and confidence interval
[large_average_bust_values,~,~,~, large_bust_values] = average_boom_bust(num_gdp, num_large_bust, large_bust_matrix, xt);

%% 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
[large_average_indext_values_boom, large_std_indext_values_boom, large_upper_bound_boom, large_lower_bound_boom, large_indext_values_boom] = average_boom_bust(num_index, num_large_boom, large_boom_matrix, indext);

%BUST
%Calculate average, standard deviation and confidence interval
[large_average_indext_values_bust, large_std_indext_values_bust, large_upper_bound_bust, large_lower_bound_bust, large_indext_values_bust] = average_boom_bust(num_index, num_large_bust, large_bust_matrix, indext);

%% Make plots

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

figure(1)
% Inflow-Boom
subplot(2,2,1)
[haxes,hline1,hline2] = plotyy(large_average_indext_values_boom(1,:,2), 'b', 100*large_average_boom_values ,'r-*','plot');
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
set(hline1,'LineWidth',2.0)
set(hline2,'LineWidth',2.0)
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(large_lower_bound_boom(1,:,2)),2) roundTo(1.75*max(large_upper_bound_boom(1,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(large_lower_bound_boom(1,:,2)),2) 0 roundTo(1.75*max(large_upper_bound_boom(1,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(large_lower_bound_boom(1,:,2)),2) 0 roundTo(1.75*max(large_upper_bound_boom(1,:,2)),2)],'FontSize',10);
t=title('Inflows','FontWeight','bold');
set(t,'FontSize',10);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(large_lower_bound_boom(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(large_upper_bound_boom(1,:,2),'k-.');
box off
hold off
% Inflow_Bust
subplot(2,2,3)
[haxes,hline1,hline2] = plotyy(large_average_indext_values_bust(1,:,2), 'b', 100*large_average_bust_values ,'r-*','plot');
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
set(hline1,'LineWidth',2.0)
set(hline2,'LineWidth',2.0)
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(large_lower_bound_bust(1,:,2)),2) roundTo(1.75*max(large_upper_bound_bust(1,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(large_lower_bound_bust(1,:,2)),2) 0 roundTo(1.75*max(large_upper_bound_bust(1,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(large_lower_bound_bust(1,:,2)),2) 0 roundTo(1.75*max(large_upper_bound_bust(1,:,2)),2)],'FontSize',10);
t=title('Inflows','FontWeight','bold');
set(t,'FontSize',10);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(large_lower_bound_bust(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(large_upper_bound_bust(1,:,2),'k-.');
box off
hold off
% Outflow-Boom
subplot(2,2,2)
[haxes,hline1,hline2] = plotyy(large_average_indext_values_boom(1,:,3), 'b', 100*large_average_boom_values ,'r-*','plot');
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
set(hline1,'LineWidth',2.0)
set(hline2,'LineWidth',2.0)
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(large_lower_bound_boom(1,:,3)),2) roundTo(1.75*max(large_upper_bound_boom(1,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(large_lower_bound_boom(1,:,3)),2) 0 roundTo(1.75*max(large_upper_bound_boom(1,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(large_lower_bound_boom(1,:,3)),2) 0 roundTo(1.75*max(large_upper_bound_boom(1,:,3)),2)],'FontSize',10);
t=title('Outflows','FontWeight','bold');
set(t,'FontSize',10);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(large_lower_bound_boom(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(large_upper_bound_boom(1,:,3),'k-.');
box off
hold off
% Outflow-Bust
subplot(2,2,4)
[haxes,hline1,hline2] = plotyy(large_average_indext_values_bust(1,:,3), 'b', 100*large_average_bust_values ,'r-*','plot');
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
set(hline1,'LineWidth',2.0)
set(hline2,'LineWidth',2.0)
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(large_lower_bound_bust(1,:,3)),2) roundTo(1.75*max(large_upper_bound_bust(1,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(large_lower_bound_bust(1,:,3)),2) 0 roundTo(1.75*max(large_upper_bound_bust(1,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(large_lower_bound_bust(1,:,3)),2) 0 roundTo(1.75*max(large_upper_bound_bust(1,:,3)),2)],'FontSize',10);
t=title('Outflows','FontWeight','bold');
set(t,'FontSize',10);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(large_lower_bound_bust(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(large_upper_bound_bust(1,:,3),'k-.');
box off
hold off
h=legend([hline1 hline2 hline3],'Average Index (lhs)','Average Output Gap (rhs)','Two Standard Deviation for the Index (lhs)');
set(h,'FontSize',8,'Position',[0.324088541666667 0.0112994350282485 0.430729166666667 0.0286831812255541], 'Orientation', 'horizontal', '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.2 Large Boom-Bust Episodes in Output and Capital Controls','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) Boom','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) Bust','Position',[0.516796875 0.526069518716578 9.16025403784439],'HorizontalAlignment','center');


    case 16

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%CRISIS EXERCISE%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

load case_GDP1
load case_GDP2
load case_GDP3
load case_GDP4

%% Make plot for all countries overall index

average_xt_crisis=nanmean(xt(:,end-6:end),1);
for i=1:num_index
    average_indext_crisis(:,:,i)=nanmean(indext(:,end-6:end,i),1);
    std_indext_crisis(:,:,i)=nanstd(indext(:,end-6:end,i));
    interval_indext_crisis(:,:,i)=[average_indext_crisis(:,:,i)-2*std_indext_crisis(:,:,i); average_indext_crisis(:,:,i)+2*std_indext_crisis(:,:,i)];
end

%% Create averages and interval for the income subgroups

%Advanced Countries
average_xt_crisis_adv=nanmean(xt_adv(:,end-6:end),1);
for i=1:num_index
    average_indext_crisis_adv(:,:,i)=nanmean(indext_adv(:,end-6:end,i),1);
    std_indext_crisis_adv(:,:,i)=nanstd(indext_adv(:,end-6:end,i));
    interval_indext_crisis_adv(:,:,i)=[average_indext_crisis_adv(:,:,i)-2*std_indext_crisis_adv(:,:,i); average_indext_crisis_adv(:,:,i)+2*std_indext_crisis_adv(:,:,i)];
end

%Middle Income Countries
average_xt_crisis_mid=nanmean(xt_mid(:,end-6:end),1);
for i=1:num_index
    average_indext_crisis_mid(:,:,i)=nanmean(indext_mid(:,end-6:end,i),1);
    std_indext_crisis_mid(:,:,i)=nanstd(indext_mid(:,end-6:end,i));
    interval_indext_crisis_mid(:,:,i)=[average_indext_crisis_mid(:,:,i)-2*std_indext_crisis_mid(:,:,i); average_indext_crisis_mid(:,:,i)+2*std_indext_crisis_mid(:,:,i)];
end

%Poor Countries
average_xt_crisis_poor=nanmean(xt_poor(:,end-6:end),1);
for i=1:num_index
    average_indext_crisis_poor(:,:,i)=nanmean(indext_poor(:,end-6:end,i),1);
    std_indext_crisis_poor(:,:,i)=nanstd(indext_poor(:,end-6:end,i));
    interval_indext_crisis_poor(:,:,i)=[average_indext_crisis_poor(:,:,i)-2*std_indext_crisis_poor(:,:,i); average_indext_crisis_poor(:,:,i)+2*std_indext_crisis_poor(:,:,i)];
end

%% Make plots for income groups

x=[-4 -3 -2 -1 0 1 2];
label_x=[2005 2006 2007 2008 2009 2010 2011];

%% Calculate highly impacted countries

%Pick up 2009 observation and order for magnitude
count=(1:1:num_c);
xt_2009_aux=[count' xt(:,15)];
xt_2009_sort=sortrows(xt_2009_aux,2);

%Find the threshold at the 33% and 66% levels
threshold_h=prctile(xt_2009_sort(:,2),33);
threshold_m=prctile(xt_2009_sort(:,2),67);

%Create vector for impact groups
count=ones(1,3);
for i=1:num_c
    if xt_2009_sort(i,2)<=threshold_h
        xt_2009_h(count(1,1),:)=xt_2009_sort(i,:);
        count(1,1)=count(1,1)+1;
    elseif and(xt_2009_sort(i,2)>threshold_h, xt_2009_sort(i,2)<=threshold_m)
        xt_2009_m(count(1,2),:)=xt_2009_sort(i,:);
        count(1,2)=count(1,2)+1;
    else 
        xt_2009_l(count(1,3),:)=xt_2009_sort(i,:);
        count(1,3)=count(1,3)+1;
    end
end

%Create full matrixes with output gap and index between 2005-2011
for k=1:num_index
    for i=1:num_c
        for j=1:size(xt_2009_h,1)
            if i==xt_2009_h(j,1)
                xt_crisis_h(j,:)=[xt(i,11) xt(i,12) xt(i,13) xt(i,14) xt(i,15) xt(i,16) xt(i,17)];
                indext_crisis_h(j,:,k)=[indext(i,11,k) indext(i,12,k) indext(i,13,k) indext(i,14,k) indext(i,15,k) indext(i,16,k) indext(i,17,k)];
            end
        end
        for j=1:size(xt_2009_m,1)
            if i==xt_2009_m(j,1)
                xt_crisis_m(j,:)=[xt(i,11) xt(i,12) xt(i,13) xt(i,14) xt(i,15) xt(i,16) xt(i,17)];
                indext_crisis_m(j,:,k)=[indext(i,11,k) indext(i,12,k) indext(i,13,k) indext(i,14,k) indext(i,15,k) indext(i,16,k) indext(i,17,k)];
            end
        end
        for j=1:size(xt_2009_l,1)
            if i==xt_2009_l(j,1)
                xt_crisis_l(j,:)=[xt(i,11) xt(i,12) xt(i,13) xt(i,14) xt(i,15) xt(i,16) xt(i,17)];
                indext_crisis_l(j,:,k)=[indext(i,11,k) indext(i,12,k) indext(i,13,k) indext(i,14,k) indext(i,15,k) indext(i,16,k) indext(i,17,k)];
            end
        end
    end
end

%Create averages of output gap and indexes for the impact groups
average_xt_crisis_h=nanmean(xt_crisis_h,1);
average_xt_crisis_m=nanmean(xt_crisis_m,1);
average_xt_crisis_l=nanmean(xt_crisis_l,1);

for k=1:num_index
    average_indext_crisis_h(:,:,k)=nanmean(indext_crisis_h(:,:,k),1);
    average_indext_crisis_m(:,:,k)=nanmean(indext_crisis_m(:,:,k),1);
    average_indext_crisis_l(:,:,k)=nanmean(indext_crisis_l(:,:,k),1);
end

%Create standard deviation of the index for the impact groups
for k=1:num_index
    std_indext_crisis_h(:,:,k)=nanstd(indext_crisis_h(:,:,k));
    std_indext_crisis_m(:,:,k)=nanstd(indext_crisis_m(:,:,k));
    std_indext_crisis_l(:,:,k)=nanstd(indext_crisis_l(:,:,k));
end

%Create interval of the index for the impact groups
for k=1:num_index
    interval_indext_crisis_h(:,:,k)=[average_indext_crisis_h(:,:,k)-2*std_indext_crisis_h(:,:,k); average_indext_crisis_h(:,:,k)+2*std_indext_crisis_h(:,:,k)];
    interval_indext_crisis_m(:,:,k)=[average_indext_crisis_m(:,:,k)-2*std_indext_crisis_m(:,:,k); average_indext_crisis_m(:,:,k)+2*std_indext_crisis_m(:,:,k)];
    interval_indext_crisis_l(:,:,k)=[average_indext_crisis_l(:,:,k)-2*std_indext_crisis_l(:,:,k); average_indext_crisis_l(:,:,k)+2*std_indext_crisis_l(:,:,k)];
end

%% Make plots for income groups

figure(1)
%Inflow-High Impact
subplot(3,2,1)
[haxes,hline1,hline2] = plotyy(average_indext_crisis_h(:,:,2), 'b', 100*average_xt_crisis_h ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 7])
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:7, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(interval_indext_crisis_h(1,:,2)),2) roundTo(1.75*max(interval_indext_crisis_h(2,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(interval_indext_crisis_h(1,:,2)),2) 0 roundTo(1.75*max(interval_indext_crisis_h(2,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(interval_indext_crisis_h(1,:,2)),2) 0 roundTo(1.75*max(interval_indext_crisis_h(2,:,2)),2)],'FontSize',10);
t=title('Inflows','FontWeight','bold');
set(t,'FontSize',10);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(interval_indext_crisis_h(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(interval_indext_crisis_h(2,:,2),'k-.');
box off
hold off
% Outflow-High Impact
subplot(3,2,2)
[haxes,hline1,hline2] = plotyy(average_indext_crisis_h(:,:,3), 'b', 100*average_xt_crisis_h ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 7])
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:7, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(interval_indext_crisis_h(1,:,3)),2) roundTo(1.75*max(interval_indext_crisis_h(2,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(interval_indext_crisis_h(1,:,3)),2) 0 roundTo(1.75*max(interval_indext_crisis_h(2,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(interval_indext_crisis_h(1,:,3)),2) 0 roundTo(1.75*max(interval_indext_crisis_h(2,:,3)),2)],'FontSize',10);
t=title('Outflows','FontWeight','bold');
set(t,'FontSize',10);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(interval_indext_crisis_h(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(interval_indext_crisis_h(2,:,3),'k-.');
box off
hold off
% Inflows-Medium Impact
subplot(3,2,3)
[haxes,hline1,hline2] = plotyy(average_indext_crisis_m(:,:,2), 'b', 100*average_xt_crisis_m ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 7])
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:7, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(interval_indext_crisis_m(1,:,2)),2) roundTo(1.75*max(interval_indext_crisis_m(2,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(interval_indext_crisis_m(1,:,2)),2) 0 roundTo(1.75*max(interval_indext_crisis_m(2,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(interval_indext_crisis_m(1,:,2)),2) 0 roundTo(1.75*max(interval_indext_crisis_m(2,:,2)),2)],'FontSize',10);
t=title('Inflows','FontWeight','bold');
set(t,'FontSize',10);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(interval_indext_crisis_m(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(interval_indext_crisis_m(2,:,2),'k-.');
box off
hold off
% Outflows-Medium Impact
subplot(3,2,4)
[haxes,hline1,hline2] = plotyy(average_indext_crisis_m(:,:,3), 'b', 100*average_xt_crisis_m,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 7])
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:7, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(interval_indext_crisis_m(1,:,3)),2) roundTo(1.75*max(interval_indext_crisis_m(2,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(interval_indext_crisis_m(1,:,3)),2) 0 roundTo(1.75*max(interval_indext_crisis_m(2,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(interval_indext_crisis_m(1,:,3)),2) 0 roundTo(1.75*max(interval_indext_crisis_m(2,:,3)),2)],'FontSize',10);
t=title('Outflows','FontWeight','bold');
set(t,'FontSize',10);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(interval_indext_crisis_m(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(interval_indext_crisis_m(2,:,3),'k-.');
box off
hold off
% Inflows-Low Impact
subplot(3,2,5)
[haxes,hline1,hline2] = plotyy(average_indext_crisis_l(1,:,2), 'b', 100*average_xt_crisis_l,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 7])
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:7, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(interval_indext_crisis_l(1,:,2)),2) roundTo(1.75*max(interval_indext_crisis_l(2,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(interval_indext_crisis_l(1,:,2)),2) 0 roundTo(1.75*max(interval_indext_crisis_l(2,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(interval_indext_crisis_l(1,:,2)),2) 0 roundTo(1.75*max(interval_indext_crisis_l(2,:,2)),2)],'FontSize',10);
t=title('Inflows','FontWeight','bold');
set(t,'FontSize',10);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(interval_indext_crisis_l(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(interval_indext_crisis_l(2,:,2),'k-.');
box off
hold off
% Outflows-Low Impact
subplot(3,2,6)
[haxes,hline1,hline2] = plotyy(average_indext_crisis_l(1,:,3), 'b', 100*average_xt_crisis_l,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 7])
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:7, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(interval_indext_crisis_l(1,:,3)),2) roundTo(1.75*max(interval_indext_crisis_l(2,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(interval_indext_crisis_l(1,:,3)),2) 0 roundTo(1.75*max(interval_indext_crisis_l(2,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(interval_indext_crisis_l(1,:,3)),2) 0 roundTo(1.75*max(interval_indext_crisis_l(2,:,3)),2)],'FontSize',10);
t=title('Outflows','FontWeight','bold');
set(t,'FontSize',10);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(interval_indext_crisis_l(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(interval_indext_crisis_l(2,:,3),'k-.');
box off
hold off
legend1 = legend([hline1 hline2 hline3],'Average Index (lhs)','Average Output Gap (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 7. Capital Controls around the Great Contraction by Impact Level','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) High Impact','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) Medium Impact','Position',[0.517578125 0.667763157894737 9.16025403784439],'HorizontalAlignment','center');
sub_ha3 = axes('Visible','off','Position',[0 0 1 1]);
text('Parent',sub_ha3,'String','\bf (c) Low Impact','Position',[0.517578125 0.366447368421053 9.16025403784439],'HorizontalAlignment','center');

    case 17

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%KLEIN EXERCISE%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load case_GDP1
load case_GDP2
load case_GDP3
load case_GDP4
load case_GDP7

%Define countries in Klein sample
%Argentina, Australia, Brazil, Chile, France, Germany, Iceland, Ireland, Israel, Korea, Mexico, Peru, Portugal, Sweden, Turkey
%Three countries of the Klein sample excluded because we don't have the
%data: Czech Rep, Hungary and Poland
klein_countries=[2 3 8 12 23 24 29 32 33 38 44 54 56 64 70]';

%Count # of countries in Klein sample
num_klein=size(klein_countries,1);

%Construct boom matrix for Klein countries
count=1;
for i=1:num_boom
    for j=1:num_klein
        if boom_matrix(i,1)==klein_countries(j,1)
            boom_klein(count,:)=boom_matrix(i,:);
            count=count+1;
        end
    end
end
num_boom_klein=size(boom_klein,1);

%Construct bust matrix for Klein countries
count=1;
for i=1:num_bust
    for j=1:num_klein
        if bust_matrix(i,1)==klein_countries(j,1)
            bust_klein(count,:)=bust_matrix(i,:);
            count=count+1;
        end
    end
end
num_bust_klein=size(bust_klein,1);

%% 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_klein,~,~,~, boom_values_klein] = average_boom_bust(num_gdp, num_boom_klein, boom_klein, xt);

%BUST
%Calculate average, standard deviation and confidence interval
[average_bust_klein,~,~,~, bust_values_klein] = average_boom_bust(num_gdp, num_bust_klein, bust_klein, xt);

%% 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_klein_boom, std_indext_klein_boom, upper_bound_boom_klein, lower_bound_boom_klein, indext_boom_klein] = average_boom_bust(num_index, num_boom_klein, boom_klein, indext);

%BUST
%Calculate average, standard deviation and confidence interval
[average_indext_klein_bust, std_indext_klein_bust, upper_bound_bust_klein, lower_bound_bust_klein, indext_bust_klein] = average_boom_bust(num_index, num_bust_klein, bust_klein, indext);

save case_KLEIN average_boom_klein boom_values_klein average_bust_klein bust_values_klein average_indext_klein_boom std_indext_klein_boom upper_bound_boom_klein lower_bound_boom_klein indext_boom_klein average_indext_klein_bust std_indext_klein_bust upper_bound_bust_klein lower_bound_bust_klein indext_bust_klein



    case 18

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%BRAZIL CASE%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

load case_GDP1
load case_GDP2
load case_GDP3
load case_GDP4

%Brazil Inflows
brazil_inflow=[0.8 0.8 0.833333333 1 1 0.916666667 0.916666667 0.166666667...
        0.166666667 0.166666667 0.333333333 0.25 0.25 0.416666667 1 0.75...
        0.75 0.666666667];

%Average Inflows all countries without Brazil
index_matrix_aux=[index_matrix(1:7,:,2);index_matrix(9:end,:,2)];
inflow_all_noBra= nanmean(index_matrix_aux);

%Average Inflows emeriging countries without Brazil
index_mid_aux=[index_mid(1,:,2);index_mid(3:end,:,2)];
inflow_mid_noBra= nanmean(index_mid_aux);

%Define years for the Schindler index
schindler_years_list=(1995:3:2012);

%Plot the time series for inflows considerign the Brazilian case
hfig=figure(1);
set(hfig,'Position',[250 250 700 500])
subplot(1,2,2)
plot(brazil_inflow,'-r','LineWidth',2);
set(gca, 'XTick', 1:3:schindler_years+1, 'XTickLabel', schindler_years_list,'FontSize',8);
set(gca,'xLim',[1 18])
xlabel('Years');
set(gca,'YLim',[0 1])
hold on
plot(inflow_all_noBra,'-ob','LineWidth',2);
set(gca,'xLim',[1 18])
hold on
plot(inflow_mid_noBra,'-*g','LineWidth',2);
set(gca,'xLim',[1 18])
h=legend('Brazil', 'Average - All countries (except Brazil)', 'Average - Emerging Countries (except Brazil)');
set(h,'FontSize',7,'Location','SouthOutside', 'Orientation', 'vertical', 'Box','off');
text(0.5, 1,'\bf  Figure 8. Capital-Inflow Controls in Brazil: The Schindler Index And Actual Tax Rates','Position',[0.508203125 1.05 9.16025403784439],'HorizontalAlignment','center')
box off
hold off

%Brazil IOF-NFI
iof_nfi=[0 0 0.02 0.06 0.06];
iof_nfi=iof_nfi*100;

%Brazil IOF_Equity
iof_equ=[0 0 0.02 0.02 0];
iof_equ=iof_equ*100;

%Brazil GDP Cyclical Component
brazil_xt=xt(8,:);

%Brazil Inflows Cyclical Component
brazil_inflowt=indext(8,:,2);

five_years_list=(2007:1:2011);
%Plot cyclical components for GDp and Inlofws for Brazil
subplot(2,2,1)
[haxes,hline4,hline5] = plotyy(iof_nfi ,'b-*',brazil_inflow(:,13:end-1),'-r','plot');
set(hline4,'LineWidth',2)
set(hline5,'LineWidth',2)
set(gca,'YLim',[-1 7])
set(gca, 'YTick',0:7);
set(haxes, 'xTickLabel','')
set(haxes,'XLim',[1 5])
set(gca,'xLim',[1 5])
set(gca, 'XTick', 1:5, 'XTickLabel',five_years_list,'FontSize',8);
xlabel('Years');
ylabel('Percent');
hold on
set(haxes(1),'Nextplot','add');
hline6=plot(iof_equ,'g-o');
set(hline6,'LineWidth',2)
set(gca,'xLim',[1 5])
h=legend([hline4 hline5 hline6],'IOF Tax Rate on Fixed Income (Pereira da Silva and Harris, 2012) (left)','Inflows Index (right)','IOF Tax Rate on Equity (Pereira da Silva and Harris, 2012) (left)', 'Brazil', 'Average - All countries (except Brazil)', 'Average - Emerging Countries (except Brazil)');
set(h,'FontSize',7,'Location','SouthOutside', 'Orientation', 'vertical', 'Box','off');
box off
hold off
 
    case 21
%% ONLINE APPENDIX FIGURES A.5.1, A.5.2, A.5.3

load case_GDP1
load case_GDP2
load case_GDP3
load case_GDP4
load case_GDP7
load case_GDP8
load case_GDP21
load case_GDP12

%% This is for Figure A.5.1: Outflows booms

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

% ROW 1: BOOM EPISODES AND CAPITAL CONTROLS BY LEVEL OF DEVELOPMENT
figure(1)
% Out-Advanced countries
subplot(3,3,1)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom_adv(1,:,3), 'b', 100*average_boom_values_adv ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_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',7.5);
t=title('Advanced countries','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
% Out-Middle Income countries
subplot(3,3,2)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom_mid(1,:,3), 'b', 100*average_boom_values_mid ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_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',7.5);
t=title('Emerging markets','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
% Out-Low Income Countries
subplot(3,3,3)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom_poor(1,:,3), 'b', 100*average_boom_values_poor ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_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',7.5);
t=title('Low income countries','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

% ROW 2: BOOM EPISODES AND CAPITAL CONTROLS ACROSS EXCHANGE RATE REGIMES

%REGIME 1
%Outflows
subplot(3,3,4)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_boom(1,:,1), 'b', 100*xt_average_matrix_boom(1,:,1) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_boom(1,:,1)),2) roundTo(1.75*max(outflow_interval_matrix_boom(2,:,1)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_boom(1,:,1)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom(2,:,1)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_boom(1,:,1)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom(2,:,1)),2)],'FontSize',7.5);
t=title('de facto Peg','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_boom(1,:,1),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_boom(2,:,1),'k-.');
box off
hold off

%REGIME 2
% Outflows
subplot(3,3,5)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_boom(1,:,2), 'b', 100*xt_average_matrix_boom(1,:,2) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_boom(1,:,2)),2) roundTo(1.75*max(outflow_interval_matrix_boom(2,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_boom(1,:,2)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom(2,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_boom(1,:,2)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom(2,:,2)),2)],'FontSize',7.5);
t=title('de facto Crawling Peg','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_boom(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_boom(2,:,2),'k-.');


% REGIME 3
% Outflows
subplot(3,3,6)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_boom(1,:,3), 'b', 100*xt_average_matrix_boom(1,:,3) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_boom(1,:,3)),2) roundTo(1.75*max(outflow_interval_matrix_boom(2,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_boom(1,:,3)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom(2,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_boom(1,:,3)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom(2,:,3)),2)],'FontSize',7.5);
t=title('Managed Floating','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_boom(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_boom(2,:,3),'k-.');
box off
hold off

% ROW 3: BOOM EPISODES AND CAPITAL CONTROLS BY LEVEL OF EXTERNAL INDEBTDENESS

% LOW EXTERNAL DEBT COUNTRIES
% Outflows
subplot(3,3,7)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_boom_d(1,:,1), 'b', 100*xt_average_matrix_boom_d(1,:,1) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_boom_d(1,:,1)),2) roundTo(1.75*max(outflow_interval_matrix_boom_d(2,:,1)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_boom_d(1,:,1)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom_d(2,:,1)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_boom_d(1,:,1)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom_d(2,:,1)),2)],'FontSize',7.5);
t=title('Low External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_boom_d(1,:,1),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_boom_d(2,:,1),'k-.');
box off
hold off

% MEDIUM EXTERNAL DEBT COUNTRIES
% Outflows
subplot(3,3,8)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_boom_d(1,:,2), 'b', 100*xt_average_matrix_boom_d(1,:,2) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_boom_d(1,:,2)),2) roundTo(1.75*max(outflow_interval_matrix_boom_d(2,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_boom_d(1,:,2)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom_d(2,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_boom_d(1,:,2)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom_d(2,:,2)),2)],'FontSize',7.5);
t=title('Medium External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_boom_d(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_boom_d(2,:,2),'k-.');
box off
hold off


% HIGH EXTERNAL DEBT COUNTRIES
% Outflows
subplot(3,3,9)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_boom_d(1,:,3), 'b', 100*xt_average_matrix_boom_d(1,:,3) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_boom_d(1,:,3)),2) roundTo(1.75*max(outflow_interval_matrix_boom_d(2,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_boom_d(1,:,3)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom_d(2,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_boom_d(1,:,3)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom_d(2,:,3)),2)],'FontSize',7.5);
t=title('High External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_boom_d(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_boom_d(2,:,3),'k-.');
box off
hold off

h=legend([hline1 hline2 hline3],'Average Outflow Index (lhs)','Average Output Gap (rhs)','Two Standard Deviation for the Index (lhs)');
set(h,'Orientation','horizontal','Position',[0.254947916666667 0.0154279490155329 0.548697916666667 0.0338504936530324],'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.5.1 Capital-Outflow Controls in Boom Episodes By Level of Development, Exchange-Rate Regime and Level of External Indebtedness','Position',[0.508203125 0.978604294478528 9.16025403784439],'HorizontalAlignment','center')

%% This for Figure A.5.3: Outflows busts

% ROW 1: BUST EPISODES AND CAPITAL CONTROLS BY LEVEL OF DEVELOPMENT
figure(3)
% Out-Advanced countries
subplot(3,3,1)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust_adv(1,:,3), 'b', 100*average_bust_values_adv ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_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',7.5);
t=title('Advanced countries','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
% Out-Middle Income countries
subplot(3,3,2)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust_mid(1,:,3), 'b', 100*average_bust_values_mid ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_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',7.5);
t=title('Emerging markets','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
% Out-Low Income Countries
subplot(3,3,3)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust_poor(1,:,3), 'b', 100*average_bust_values_poor ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_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',7.5);
t=title('Low income countries','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

% ROW 2: BUST EPISODES AND CAPITAL CONTROLS ACROSS EXCHANGE RATE REGIMES

%REGIME 1
%Outflows
subplot(3,3,4)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_bust(1,:,1), 'b', 100*xt_average_matrix_bust(1,:,1) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_bust(1,:,1)),2) roundTo(1.75*max(outflow_interval_matrix_bust(2,:,1)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_bust(1,:,1)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust(2,:,1)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_bust(1,:,1)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust(2,:,1)),2)],'FontSize',7.5);
t=title('de facto Peg','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_bust(1,:,1),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_bust(2,:,1),'k-.');
box off
hold off

%REGIME 2
% Outflows
subplot(3,3,5)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_bust(1,:,2), 'b', 100*xt_average_matrix_bust(1,:,2) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_bust(1,:,2)),2) roundTo(1.75*max(outflow_interval_matrix_bust(2,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_bust(1,:,2)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust(2,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_bust(1,:,2)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust(2,:,2)),2)],'FontSize',7.5);
t=title('de facto Crawling Peg','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_bust(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_bust(2,:,2),'k-.');


% REGIME 3
% Outflows
subplot(3,3,6)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_bust(1,:,3), 'b', 100*xt_average_matrix_bust(1,:,3) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_bust(1,:,3)),2) roundTo(1.75*max(outflow_interval_matrix_bust(2,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_bust(1,:,3)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust(2,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_bust(1,:,3)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust(2,:,3)),2)],'FontSize',7.5);
t=title('Managed Floating','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_bust(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_bust(2,:,3),'k-.');
box off
hold off

% ROW 3: BUST EPISODES AND CAPITAL CONTROLS BY LEVEL OF EXTERNAL INDEBTDENESS

% LOW EXTERNAL DEBT COUNTRIES
% Outflows
subplot(3,3,7)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_bust_d(1,:,1), 'b', 100*xt_average_matrix_bust_d(1,:,1) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_bust_d(1,:,1)),2) roundTo(1.75*max(outflow_interval_matrix_bust_d(2,:,1)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_bust_d(1,:,1)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust_d(2,:,1)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_bust_d(1,:,1)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust_d(2,:,1)),2)],'FontSize',7.5);
t=title('Low External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_bust_d(1,:,1),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_bust_d(2,:,1),'k-.');
box off
hold off

% MEDIUM EXTERNAL DEBT COUNTRIES
% Outflows
subplot(3,3,8)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_bust_d(1,:,2), 'b', 100*xt_average_matrix_bust_d(1,:,2) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_bust_d(1,:,2)),2) roundTo(1.75*max(outflow_interval_matrix_bust_d(2,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_bust_d(1,:,2)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust_d(2,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_bust_d(1,:,2)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust_d(2,:,2)),2)],'FontSize',7.5);
t=title('Medium External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_bust_d(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_bust_d(2,:,2),'k-.');
box off
hold off


% HIGH EXTERNAL DEBT COUNTRIES
% Outflows
subplot(3,3,9)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_bust_d(1,:,3), 'b', 100*xt_average_matrix_bust_d(1,:,3) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_bust_d(1,:,3)),2) roundTo(1.75*max(outflow_interval_matrix_bust_d(2,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_bust_d(1,:,3)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust_d(2,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_bust_d(1,:,3)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust_d(2,:,3)),2)],'FontSize',7.5);
t=title('High External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_bust_d(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_bust_d(2,:,3),'k-.');
box off
hold off

h=legend([hline1 hline2 hline3],'Average Outflow Index (lhs)','Average Output Gap (rhs)','Two Standard Deviation for the Index (lhs)');
set(h,'Orientation','horizontal','Position',[0.254947916666667 0.0154279490155329 0.548697916666667 0.0338504936530324],'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.5.3 Capital-Outflow Controls in Bust Episodes By Level of Development, Exchange-Rate Regime and Level of External Indebtedness','Position',[0.508203125 0.978604294478528 9.16025403784439],'HorizontalAlignment','center')

%% This is for Figure A.5.2: Inflows busts

% ROW 1: BUST EPISODES AND CAPITAL CONTROLS BY LEVEL OF DEVELOPMENT
figure(2)
% Inflow-Advanced countries
subplot(3,3,1)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust_adv(1,:,2), 'b', 100*average_bust_values_adv ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_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',7.5);
t=title('Advanced countries','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,3,2)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust_mid(1,:,2), 'b', 100*average_bust_values_mid ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_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',7.5);
t=title('Emerging markets','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
% Inflow-Low Income Countries
subplot(3,3,3)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust_poor(1,:,2), 'b', 100*average_bust_values_poor ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_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',7.5);
t=title('Low income countries','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

% ROW 2: BUST EPISODES AND CAPITAL CONTROLS ACROSS EXCHANGE RATE REGIMES

%REGIME 1
%Inflows
subplot(3,3,4)
[haxes,hline1,hline2] = plotyy(inflow_average_matrix_bust(1,:,1), 'b', 100*xt_average_matrix_bust(1,:,1) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(inflow_interval_matrix_bust(1,:,1)),2) roundTo(1.75*max(inflow_interval_matrix_bust(2,:,1)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(inflow_interval_matrix_bust(1,:,1)),2) 0 roundTo(1.75*max(inflow_interval_matrix_bust(2,:,1)),2)], 'YTickLabel', [roundTo(1.75*min(inflow_interval_matrix_bust(1,:,1)),2) 0 roundTo(1.75*max(inflow_interval_matrix_bust(2,:,1)),2)],'FontSize',7.5);
t=title('de facto Peg','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(inflow_interval_matrix_bust(1,:,1),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(inflow_interval_matrix_bust(2,:,1),'k-.');
box off
hold off

%REGIME 2
% Inflows
subplot(3,3,5)
[haxes,hline1,hline2] = plotyy(inflow_average_matrix_bust(1,:,2), 'b', 100*xt_average_matrix_bust(1,:,2) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(inflow_interval_matrix_bust(1,:,2)),2) roundTo(1.75*max(inflow_interval_matrix_bust(2,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(inflow_interval_matrix_bust(1,:,2)),2) 0 roundTo(1.75*max(inflow_interval_matrix_bust(2,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(inflow_interval_matrix_bust(1,:,2)),2) 0 roundTo(1.75*max(inflow_interval_matrix_bust(2,:,2)),2)],'FontSize',7.5);
t=title('de facto Crawling Peg','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(inflow_interval_matrix_bust(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(inflow_interval_matrix_bust(2,:,2),'k-.');


% REGIME 3
% Inflows
subplot(3,3,6)
[haxes,hline1,hline2] = plotyy(inflow_average_matrix_bust(1,:,3), 'b', 100*xt_average_matrix_bust(1,:,3) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(inflow_interval_matrix_bust(1,:,3)),2) roundTo(1.75*max(inflow_interval_matrix_bust(2,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(inflow_interval_matrix_bust(1,:,3)),2) 0 roundTo(1.75*max(inflow_interval_matrix_bust(2,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(inflow_interval_matrix_bust(1,:,3)),2) 0 roundTo(1.75*max(inflow_interval_matrix_bust(2,:,3)),2)],'FontSize',7.5);
t=title('Managed Floating','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(inflow_interval_matrix_bust(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(inflow_interval_matrix_bust(2,:,3),'k-.');
box off
hold off

% ROW 3: BUST EPISODES AND CAPITAL CONTROLS BY LEVEL OF EXTERNAL INDEBTDENESS

% LOW EXTERNAL DEBT COUNTRIES
% Inflows
subplot(3,3,7)
[haxes,hline1,hline2] = plotyy(inflow_average_matrix_bust_d(1,:,1), 'b', 100*xt_average_matrix_bust_d(1,:,1) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(inflow_interval_matrix_bust_d(1,:,1)),2) roundTo(1.75*max(inflow_interval_matrix_bust_d(2,:,1)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(inflow_interval_matrix_bust_d(1,:,1)),2) 0 roundTo(1.75*max(inflow_interval_matrix_bust_d(2,:,1)),2)], 'YTickLabel', [roundTo(1.75*min(inflow_interval_matrix_bust_d(1,:,1)),2) 0 roundTo(1.75*max(inflow_interval_matrix_bust_d(2,:,1)),2)],'FontSize',7.5);
t=title('Low External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(inflow_interval_matrix_bust_d(1,:,1),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(inflow_interval_matrix_bust_d(2,:,1),'k-.');
box off
hold off

% MEDIUM EXTERNAL DEBT COUNTRIES
% Inflows
subplot(3,3,8)
[haxes,hline1,hline2] = plotyy(inflow_average_matrix_bust_d(1,:,2), 'b', 100*xt_average_matrix_bust_d(1,:,2) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(inflow_interval_matrix_bust_d(1,:,2)),2) roundTo(1.75*max(inflow_interval_matrix_bust_d(2,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(inflow_interval_matrix_bust_d(1,:,2)),2) 0 roundTo(1.75*max(inflow_interval_matrix_bust_d(2,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(inflow_interval_matrix_bust_d(1,:,2)),2) 0 roundTo(1.75*max(inflow_interval_matrix_bust_d(2,:,2)),2)],'FontSize',7.5);
t=title('Medium External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(inflow_interval_matrix_bust_d(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(inflow_interval_matrix_bust_d(2,:,2),'k-.');
box off
hold off


% HIGH EXTERNAL DEBT COUNTRIES
% Inflows
subplot(3,3,9)
[haxes,hline1,hline2] = plotyy(inflow_average_matrix_bust_d(1,:,3), 'b', 100*xt_average_matrix_bust_d(1,:,3) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(inflow_interval_matrix_bust_d(1,:,3)),2) roundTo(1.75*max(inflow_interval_matrix_bust_d(2,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(inflow_interval_matrix_bust_d(1,:,3)),2) 0 roundTo(1.75*max(inflow_interval_matrix_bust_d(2,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(inflow_interval_matrix_bust_d(1,:,3)),2) 0 roundTo(1.75*max(inflow_interval_matrix_bust_d(2,:,3)),2)],'FontSize',7.5);
t=title('High External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(inflow_interval_matrix_bust_d(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(inflow_interval_matrix_bust_d(2,:,3),'k-.');
box off
hold off

h=legend([hline1 hline2 hline3],'Average Inflow Index (lhs)','Average Output Gap (rhs)','Two Standard Deviation for the Index (lhs)');
set(h,'Orientation','horizontal','Position',[0.254947916666667 0.0154279490155329 0.548697916666667 0.0338504936530324],'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.5.2 Capital-Inflow Controls in Bust Episodes By Level of Development, Exchange-Rate Regime and Level of External Indebtedness','Position',[0.508203125 0.978604294478528 9.16025403784439],'HorizontalAlignment','center')

    case 22      
%% ONLINE APPENDIX FIGURES 4.A.1, 4.A.2, 4.A.3

load case_GDP8
load case_GDP22

%% ONLINE APPENDIX FIGURE 4.A.1

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

% ROW 1: AGGREGATES OUTFLOWS (EQO, BOO, DIO)

figure(1)

%Equity

subplot(3,3,1)
[haxes,hline1,hline2] = plotyy(average_subindext_outflow_values_boom(1,:,1), 'b', 100*average_boom_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
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_boom_subindext_outflow(1,:,1)),2) roundTo(1.75*max(upper_bound_boom_subindext_outflow(1,:,1)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_subindext_outflow(1,:,1)),2)  0 roundTo(1.75*max(upper_bound_boom_subindext_outflow(1,:,1)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_subindext_outflow(1,:,1)),2) 0 roundTo(1.75*max(upper_bound_boom_subindext_outflow(1,:,1)),2)],'FontSize',10);
t=title(' eqo','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_subindext_outflow(1,:,1),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_subindext_outflow(1,:,1),'k-.');
box off
hold off

% Bonds

subplot(3,3,2)
[haxes,hline1,hline2] = plotyy(average_subindext_outflow_values_boom(1,:,2), 'b', 100*average_boom_values ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
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_boom_subindext_outflow(1,:,2)),2) roundTo(1.75*max(upper_bound_boom_subindext_outflow(1,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_subindext_outflow(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_boom_subindext_outflow(1,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_subindext_outflow(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_boom_subindext_outflow(1,:,2)),2)],'FontSize',10);
t=title('boo','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_subindext_outflow(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_subindext_outflow(1,:,2),'k-.');
box off
hold off

% Direct Investment

subplot(3,3,3)
[haxes,hline1,hline2] = plotyy(average_subindext_outflow_values_boom(1,:,6), 'b', 100*average_boom_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
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_boom_subindext_outflow(1,:,6)),2) roundTo(1.75*max(upper_bound_boom_subindext_outflow(1,:,6)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_subindext_outflow(1,:,6)),2) 0 roundTo(1.75*max(upper_bound_boom_subindext_outflow(1,:,6)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_subindext_outflow(1,:,6)),2) 0 roundTo(1.75*max(upper_bound_boom_subindext_outflow(1,:,6)),2)],'FontSize',10);
t=title('dio','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_subindext_outflow(1,:,6),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_subindext_outflow(1,:,6),'k-.');
box off
hold off

% ROW 2: SILN

%eq_siln

subplot(3,3,4)
[haxes,hline1,hline2] = plotyy(average_subindexes_outflow_values_boom(1,:,2), 'b', 100*average_boom_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
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_boom_subindexes_outflow(1,:,2)),2) roundTo(1.75*max(upper_bound_boom_subindexes_outflow(1,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_subindexes_outflow(1,:,2)),2)  0 roundTo(1.75*max(upper_bound_boom_subindexes_outflow(1,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_subindexes_outflow(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_boom_subindexes_outflow(1,:,2)),2)],'FontSize',10);
t=title(' eq-siln','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_subindexes_outflow(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_subindexes_outflow(1,:,2),'k-.');
box off
hold off

%bo_siln

subplot(3,3,5)
[haxes,hline1,hline2] = plotyy(average_subindexes_outflow_values_boom(1,:,5), 'b', 100*average_boom_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
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_boom_subindexes_outflow(1,:,5)),2) roundTo(1.75*max(upper_bound_boom_subindexes_outflow(1,:,5)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_subindexes_outflow(1,:,5)),2)  0 roundTo(1.75*max(upper_bound_boom_subindexes_outflow(1,:,5)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_subindexes_outflow(1,:,5)),2) 0 roundTo(1.75*max(upper_bound_boom_subindexes_outflow(1,:,5)),2)],'FontSize',10);
t=title(' bo-plbn','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_subindexes_outflow(1,:,5),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_subindexes_outflow(1,:,5),'k-.');
box off
hold off

% ROW 3: PABR

%eq_pabr

subplot(3,3,7)
[haxes,hline1,hline2] = plotyy(average_subindexes_outflow_values_boom(1,:,3), 'b', 100*average_boom_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
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_boom_subindexes_outflow(1,:,3)),2) roundTo(1.75*max(upper_bound_boom_subindexes_outflow(1,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_subindexes_outflow(1,:,3)),2)  0 roundTo(1.75*max(upper_bound_boom_subindexes_outflow(1,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_subindexes_outflow(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_boom_subindexes_outflow(1,:,3)),2)],'FontSize',10);
t=title(' eq-pabr','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_subindexes_outflow(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_subindexes_outflow(1,:,3),'k-.');
box off
hold off

%bo_pabr

subplot(3,3,8)
[haxes,hline1,hline2] = plotyy(average_subindexes_outflow_values_boom(1,:,6), 'b', 100*average_boom_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
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_boom_subindexes_outflow(1,:,6)),2) roundTo(1.75*max(upper_bound_boom_subindexes_outflow(1,:,6)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_subindexes_outflow(1,:,6)),2)  0 roundTo(1.75*max(upper_bound_boom_subindexes_outflow(1,:,6)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_subindexes_outflow(1,:,6)),2) 0 roundTo(1.75*max(upper_bound_boom_subindexes_outflow(1,:,6)),2)],'FontSize',10);
t=title(' bo-pabr','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_subindexes_outflow(1,:,6),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_subindexes_outflow(1,:,6),'k-.');
box off
hold off

legend1 = legend([hline1 hline2 hline3],'Average Outflow Index (lhs)','Average Output Gap(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 4.A.1 Acyclicality of Granular Measures of Capital-Outflow Controls in Booms','Position',[0.508203125 0.978604294478528 9.16025403784439],'HorizontalAlignment','center')

%% FIGURE 4.A.2: Bust-Inflows

% ROW 1: AGGREGATES INFLOWS (EQI, BOI, DII_LDI)

figure(2)
%Equity

subplot(3,3,1)
[haxes,hline1,hline2] = plotyy(average_subindext_inflow_values_bust(1,:,1), 'b', 100*average_bust_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_subindext_inflow(1,:,1)),2) roundTo(1.75*max(upper_bound_bust_subindext_inflow(1,:,1)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust_subindext_inflow(1,:,1)),2)  0 roundTo(1.75*max(upper_bound_bust_subindext_inflow(1,:,1)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust_subindext_inflow(1,:,1)),2) 0 roundTo(1.75*max(upper_bound_bust_subindext_inflow(1,:,1)),2)],'FontSize',7.5);
t=title(' eqi','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_bust_subindext_inflow(1,:,1),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust_subindext_inflow(1,:,1),'k-.');
box off
hold off

% Bonds

subplot(3,3,2)
[haxes,hline1,hline2] = plotyy(average_subindext_inflow_values_bust(1,:,2), 'b', 100*average_bust_values ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_subindext_inflow(1,:,2)),2) roundTo(1.75*max(upper_bound_bust_subindext_inflow(1,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust_subindext_inflow(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_bust_subindext_inflow(1,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust_subindext_inflow(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_bust_subindext_inflow(1,:,2)),2)],'FontSize',7.5);
t=title('boi','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_bust_subindext_inflow(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust_subindext_inflow(1,:,2),'k-.');
box off
hold off

% Direct Investment

subplot(3,3,3)
[haxes,hline1,hline2] = plotyy(average_subindext_inflow_values_bust(1,:,6), 'b', 100*average_bust_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_subindext_inflow(1,:,6)),2) roundTo(1.75*max(upper_bound_bust_subindext_inflow(1,:,6)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust_subindext_inflow(1,:,6)),2) 0 roundTo(1.75*max(upper_bound_bust_subindext_inflow(1,:,6)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust_subindext_inflow(1,:,6)),2) 0 roundTo(1.75*max(upper_bound_bust_subindext_inflow(1,:,6)),2)],'FontSize',7.5);
t=title('dii-ldi','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_bust_subindext_inflow(1,:,6),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust_subindext_inflow(1,:,6),'k-.');
box off
hold off

% ROW 2: PLBN + DII

%eq_plbn

subplot(3,3,4)
[haxes,hline1,hline2] = plotyy(average_subindexes_inflow_values_bust(1,:,2), 'b', 100*average_bust_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_subindexes_inflow(1,:,2)),2) roundTo(1.75*max(upper_bound_bust_subindexes_inflow(1,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust_subindexes_inflow(1,:,2)),2)  0 roundTo(1.75*max(upper_bound_bust_subindexes_inflow(1,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust_subindexes_inflow(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_bust_subindexes_inflow(1,:,2)),2)],'FontSize',7.5);
t=title(' eq-plbn','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_bust_subindexes_inflow(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust_subindexes_inflow(1,:,2),'k-.');
box off
hold off

%bo_plbn

subplot(3,3,5)
[haxes,hline1,hline2] = plotyy(average_subindexes_inflow_values_bust(1,:,5), 'b', 100*average_bust_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_subindexes_inflow(1,:,5)),2) roundTo(1.75*max(upper_bound_bust_subindexes_inflow(1,:,5)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust_subindexes_inflow(1,:,5)),2)  0 roundTo(1.75*max(upper_bound_bust_subindexes_inflow(1,:,5)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust_subindexes_inflow(1,:,5)),2) 0 roundTo(1.75*max(upper_bound_bust_subindexes_inflow(1,:,5)),2)],'FontSize',7.5);
t=title(' bo-plbn','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_bust_subindexes_inflow(1,:,5),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust_subindexes_inflow(1,:,5),'k-.');
box off
hold off

%dii

subplot(3,3,6)
[haxes,hline1,hline2] = plotyy(average_subindexes_inflow_values_bust(1,:,8), 'b', 100*average_bust_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_subindexes_inflow(1,:,8)),2) roundTo(1.75*max(upper_bound_bust_subindexes_inflow(1,:,8)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust_subindexes_inflow(1,:,8)),2)  0 roundTo(1.75*max(upper_bound_bust_subindexes_inflow(1,:,8)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust_subindexes_inflow(1,:,8)),2) 0 roundTo(1.75*max(upper_bound_bust_subindexes_inflow(1,:,8)),2)],'FontSize',7.5);
t=title(' dii','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_bust_subindexes_inflow(1,:,8),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust_subindexes_inflow(1,:,8),'k-.');
box off
hold off

% ROW 3: SIAR + LDI

%eq_siar

subplot(3,3,7)
[haxes,hline1,hline2] = plotyy(average_subindexes_inflow_values_bust(1,:,3), 'b', 100*average_bust_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_subindexes_inflow(1,:,3)),2) roundTo(1.75*max(upper_bound_bust_subindexes_inflow(1,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust_subindexes_inflow(1,:,3)),2)  0 roundTo(1.75*max(upper_bound_bust_subindexes_inflow(1,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust_subindexes_inflow(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_bust_subindexes_inflow(1,:,3)),2)],'FontSize',7.5);
t=title(' eq-siar','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_bust_subindexes_inflow(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust_subindexes_inflow(1,:,3),'k-.');
box off
hold off

%bo_siar

subplot(3,3,8)
[haxes,hline1,hline2] = plotyy(average_subindexes_inflow_values_bust(1,:,6), 'b', 100*average_bust_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_subindexes_inflow(1,:,6)),2) roundTo(1.75*max(upper_bound_bust_subindexes_inflow(1,:,6)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust_subindexes_inflow(1,:,6)),2)  0 roundTo(1.75*max(upper_bound_bust_subindexes_inflow(1,:,6)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust_subindexes_inflow(1,:,6)),2) 0 roundTo(1.75*max(upper_bound_bust_subindexes_inflow(1,:,6)),2)],'FontSize',7.5);
t=title('bo-siar','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_bust_subindexes_inflow(1,:,6),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust_subindexes_inflow(1,:,6),'k-.');
box off
hold off

%ldi

subplot(3,3,9)
[haxes,hline1,hline2] = plotyy(average_subindexes_inflow_values_bust(1,:,9), 'b', 100*average_bust_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_subindexes_inflow(1,:,9)),2) roundTo(1.75*max(upper_bound_bust_subindexes_inflow(1,:,9)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust_subindexes_inflow(1,:,9)),2)  0 roundTo(1.75*max(upper_bound_bust_subindexes_inflow(1,:,9)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust_subindexes_inflow(1,:,9)),2) 0 roundTo(1.75*max(upper_bound_bust_subindexes_inflow(1,:,9)),2)],'FontSize',7.5);
t=title('ldi','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_bust_subindexes_inflow(1,:,9),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust_subindexes_inflow(1,:,9),'k-.');
box off
hold off

legend1 = legend([hline1 hline2 hline3],'Average Inflow Index (lhs)','Average Output Gap(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 4.A.2 Acyclicality of Granular Measures of Capital-Inflow Controls in Busts','Position',[0.508203125 0.978604294478528 9.16025403784439],'HorizontalAlignment','center')


%% 4.A.3: Bust Outflows

% ROW 1: AGGREGATES OUTFLOWS (EQO, BOO, DIO)

figure(3)

%Equity

subplot(3,3,1)
[haxes,hline1,hline2] = plotyy(average_subindext_outflow_values_bust(1,:,1), 'b', 100*average_bust_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_subindext_outflow(1,:,1)),2) roundTo(1.75*max(upper_bound_bust_subindext_outflow(1,:,1)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust_subindext_outflow(1,:,1)),2)  0 roundTo(1.75*max(upper_bound_bust_subindext_outflow(1,:,1)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust_subindext_outflow(1,:,1)),2) 0 roundTo(1.75*max(upper_bound_bust_subindext_outflow(1,:,1)),2)],'FontSize',7.5);
t=title(' eqo','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_bust_subindext_outflow(1,:,1),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust_subindext_outflow(1,:,1),'k-.');
box off
hold off

% Bonds

subplot(3,3,2)
[haxes,hline1,hline2] = plotyy(average_subindext_outflow_values_bust(1,:,2), 'b', 100*average_bust_values ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_subindext_outflow(1,:,2)),2) roundTo(1.75*max(upper_bound_bust_subindext_outflow(1,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust_subindext_outflow(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_bust_subindext_outflow(1,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust_subindext_outflow(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_bust_subindext_outflow(1,:,2)),2)],'FontSize',7.5);
t=title('boo','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_bust_subindext_outflow(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust_subindext_outflow(1,:,2),'k-.');
box off
hold off

% Direct Investment

subplot(3,3,3)
[haxes,hline1,hline2] = plotyy(average_subindext_outflow_values_bust(1,:,6), 'b', 100*average_bust_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_subindext_outflow(1,:,6)),2) roundTo(1.75*max(upper_bound_bust_subindext_outflow(1,:,6)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust_subindext_outflow(1,:,6)),2) 0 roundTo(1.75*max(upper_bound_bust_subindext_outflow(1,:,6)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust_subindext_outflow(1,:,6)),2) 0 roundTo(1.75*max(upper_bound_bust_subindext_outflow(1,:,6)),2)],'FontSize',7.5);
t=title('dio','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_bust_subindext_outflow(1,:,6),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust_subindext_outflow(1,:,6),'k-.');
box off
hold off

% ROW 2: SILN

%eq_siln

subplot(3,3,4)
[haxes,hline1,hline2] = plotyy(average_subindexes_outflow_values_bust(1,:,2), 'b', 100*average_bust_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_subindexes_outflow(1,:,2)),2) roundTo(1.75*max(upper_bound_bust_subindexes_outflow(1,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust_subindexes_outflow(1,:,2)),2)  0 roundTo(1.75*max(upper_bound_bust_subindexes_outflow(1,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust_subindexes_outflow(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_bust_subindexes_outflow(1,:,2)),2)],'FontSize',7.5);
t=title(' eq-siln','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_bust_subindexes_outflow(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust_subindexes_outflow(1,:,2),'k-.');
box off
hold off

%bo_siln

subplot(3,3,5)
[haxes,hline1,hline2] = plotyy(average_subindexes_outflow_values_bust(1,:,5), 'b', 100*average_bust_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_subindexes_outflow(1,:,5)),2) roundTo(1.75*max(upper_bound_bust_subindexes_outflow(1,:,5)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust_subindexes_outflow(1,:,5)),2)  0 roundTo(1.75*max(upper_bound_bust_subindexes_outflow(1,:,5)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust_subindexes_outflow(1,:,5)),2) 0 roundTo(1.75*max(upper_bound_bust_subindexes_outflow(1,:,5)),2)],'FontSize',7.5);
t=title(' bo-plbn','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_bust_subindexes_outflow(1,:,5),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust_subindexes_outflow(1,:,5),'k-.');
box off
hold off

% ROW 3: PABR

%eq_pabr

subplot(3,3,7)
[haxes,hline1,hline2] = plotyy(average_subindexes_outflow_values_bust(1,:,3), 'b', 100*average_bust_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_subindexes_outflow(1,:,3)),2) roundTo(1.75*max(upper_bound_bust_subindexes_outflow(1,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust_subindexes_outflow(1,:,3)),2)  0 roundTo(1.75*max(upper_bound_bust_subindexes_outflow(1,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust_subindexes_outflow(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_bust_subindexes_outflow(1,:,3)),2)],'FontSize',7.5);
t=title(' eq-pabr','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_bust_subindexes_outflow(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust_subindexes_outflow(1,:,3),'k-.');
box off
hold off

%bo_pabr

subplot(3,3,8)
[haxes,hline1,hline2] = plotyy(average_subindexes_outflow_values_bust(1,:,6), 'b', 100*average_bust_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_subindexes_outflow(1,:,6)),2) roundTo(1.75*max(upper_bound_bust_subindexes_outflow(1,:,6)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust_subindexes_outflow(1,:,6)),2)  0 roundTo(1.75*max(upper_bound_bust_subindexes_outflow(1,:,6)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust_subindexes_outflow(1,:,6)),2) 0 roundTo(1.75*max(upper_bound_bust_subindexes_outflow(1,:,6)),2)],'FontSize',7.5);
t=title(' bo-pabr','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_bust_subindexes_outflow(1,:,6),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_bust_subindexes_outflow(1,:,6),'k-.');
box off
hold off

legend1 = legend([hline1 hline2 hline3],'Average Outflow Index (lhs)','Average Output Gap(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 4.A.3 Acyclicality of Granular Measures of Capital-Outflows Controls in Busts','Position',[0.508203125 0.978604294478528 9.16025403784439],'HorizontalAlignment','center')


 case 24

% This task produces figures 1.A.1, 1.A.2, 2.A.1, 2.A.2
     
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%LINEAR DETREND FOR SCHINDLER INDEX AND BAR CHART%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load case_GDP1
load case_GDP2
load case_GDP3
load case_GDP22

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

%Replacing data with only bonds and equity
num_index=6;

eqi=reshape(matrix_inflow_detrended(:,:,1),[num_c*schindler_years,1]);
eqo=reshape(matrix_outflow_detrended(:,:,1),[num_c*schindler_years,1]);
boi=reshape(matrix_inflow_detrended(:,:,4),[num_c*schindler_years,1]);
boo=reshape(matrix_outflow_detrended(:,:,4),[num_c*schindler_years,1]);

eq = [eqi eqo];
bo = [boi boo];

clear index_matrix

index_matrix=[mean(eq,2) mean(bo,2) eqi eqo boi boo];

indext=reshape(index_matrix,[num_c,schindler_years,num_index]);

save case_GDP23 index_matrix indext
%% Calculate correlations between output gap and index for t=0 and make plots

%Calculate correlation and pvalues when output gap t=0 and index t=0

r=zeros(num_c,num_index);
p=zeros(num_c,num_index);

for k=1:num_index
    if k==2 || k==5 || k==6
    [r(:,k), p(:,k)] = country_correlation(1, xt(:,3:end), indext(:,3:end,k));
    else
    [r(:,k), p(:,k)] = country_correlation(1, xt(:,:), indext(:,:,k));
    end
end

%Sort them and associate stars to the country name
[corr_matrix_sort, legend_matrix_star, star_matrix, legend_matrix] = star_zero(num_index, r, p);

%Create vector for inflow Index
corr_inflow_sort=corr_matrix_sort(:,:,3);
legend_inflow_star= legend_matrix_star(:,:,3);

%Create vector for outflow Index
corr_outflow_sort=corr_matrix_sort(:,:,4);
legend_outflow_star= legend_matrix_star(:,:,4);

%Calculate correlation between Inflows and outflows (cyclical components)
[r_in_out, p_in_out] = country_correlation(1, indext(:,:,3), indext(:,:,4));

%Sort correlation between inflow and outflow cyclical components
[corr_in_out_sort, legend_in_out_star, star_n_out, legend_in_out] = star_zero(1, r_in_out, p_in_out);

figure(1)
%Create a bar chart for the INFLOW CORRELATIONS - EQUITY
subplot(1,2,1)
b_in=barh(corr_inflow_sort(:,2));
set(gca, 'YDir', 'reverse')
set(gca, 'YTick', 1:num_c, 'YTickLabel', legend_inflow_star,'FontSize',5,'FontWeight','bold');
set(gca,'XLim',[-1 1])
t=title('(a) Inflows','FontWeight','bold');
set(t, 'Visible', 'on','Fontsize', 8);
bar_child_in=get(b_in,'Children');
set(bar_child_in,'CData', corr_inflow_sort(:,2));
%Vector of colors for bars: ***blue, **green, white
mycolor=[0 0 1; 0 1 0; 1 1 1];
for iCount_in=1:num_c
    if (corr_inflow_sort(iCount_in,3)<.01)
        index_in(iCount_in)=1;
    elseif and(corr_inflow_sort(iCount_in,3)>.01, corr_inflow_sort(iCount_in,3)<.05)
        index_in(iCount_in)=2;
    else
        index_in(iCount_in)=3;
    end
end
set(bar_child_in, 'CData',index_in);
colormap(mycolor);
box off
%Create a bar chart for the OUTFLOW CORRELATIONS - EQUITY
subplot(1,2,2)
b_out=barh(corr_outflow_sort(:,2));
set(gca, 'YDir', 'reverse')
set(gca, 'YTick', 1:num_c, 'YTickLabel', legend_outflow_star,'FontSize',5,'FontWeight','bold');
set(gca,'XLim',[-1 1])
t=title('(b) Outflows','FontWeight','bold');
set(t, 'Visible', 'on','Fontsize', 8);
bar_child_out=get(b_out,'Children');
set(bar_child_out,'CData', corr_outflow_sort(:,2));
%Vector of colors for bars: ***blue, **green, white
mycolor=[0 0 1; 0 1 0; 1 1 1];
for iCount_out=1:num_c
    if (corr_outflow_sort(iCount_out,3)<.01)
        index_out(iCount_out)=1;
    elseif and(corr_outflow_sort(iCount_out,3)>.01, corr_outflow_sort(iCount_out,3)<.05)
        index_out(iCount_out)=2;
    else
        index_out(iCount_out)=3;
    end
end
set(bar_child_out, 'CData',index_out);
colormap(mycolor);
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 1.A.2 Country-By-Country Correlations Between Capital Controls on Equity and Output','Position',[0.508203125 0.978604294478528 9.16025403784439],'HorizontalAlignment','center')

%Sort them and associate stars to the country name
[corr_matrix_sort, legend_matrix_star, star_matrix, legend_matrix] = star_zero(num_index, r, p);

%Create vector for inflow Index
corr_inflow_sort=corr_matrix_sort(:,:,5);
legend_inflow_star= legend_matrix_star(:,:,5);

%Create vector for outflow Index
corr_outflow_sort=corr_matrix_sort(:,:,6);
legend_outflow_star= legend_matrix_star(:,:,6);

%Calculate correlation between Inflows and outflows (cyclical components)
[r_in_out, p_in_out] = country_correlation(1, indext(:,:,5), indext(:,:,6));

%Sort correlation between inflow and outflow cyclical components
[corr_in_out_sort, legend_in_out_star, star_n_out, legend_in_out] = star_zero(1, r_in_out, p_in_out);

figure(2)
%Create a bar chart for the INFLOW CORRELATIONS - BONDS
subplot(1,2,1)
b_in=barh(corr_inflow_sort(:,2));
set(gca, 'YDir', 'reverse')
set(gca, 'YTick', 1:num_c, 'YTickLabel', legend_inflow_star,'FontSize',5,'FontWeight','bold');
set(gca,'XLim',[-1 1])
t=title('(a) Inflows','FontWeight','bold');
set(t, 'Visible', 'on','Fontsize', 8);
bar_child_in=get(b_in,'Children');
set(bar_child_in,'CData', corr_inflow_sort(:,2));
%Vector of colors for bars: ***blue, **green, white
mycolor=[0 0 1; 0 1 0; 1 1 1];
for iCount_in=1:num_c
    if (corr_inflow_sort(iCount_in,3)<.01)
        index_in(iCount_in)=1;
    elseif and(corr_inflow_sort(iCount_in,3)>.01, corr_inflow_sort(iCount_in,3)<.05)
        index_in(iCount_in)=2;
    else
        index_in(iCount_in)=3;
    end
end
set(bar_child_in, 'CData',index_in);
colormap(mycolor);
box off
%Create a bar chart for the OUTFLOW CORRELATIONS - BONDS
subplot(1,2,2)
b_out=barh(corr_outflow_sort(:,2));
set(gca, 'YDir', 'reverse')
set(gca, 'YTick', 1:num_c, 'YTickLabel', legend_outflow_star,'FontSize',5,'FontWeight','bold');
set(gca,'XLim',[-1 1])
t=title('(b) Outflows','FontWeight','bold');
set(t, 'Visible', 'on','Fontsize', 8);
bar_child_out=get(b_out,'Children');
set(bar_child_out,'CData', corr_outflow_sort(:,1));
%Vector of colors for bars: ***blue, **green, white
mycolor=[0 0 1; 0 1 0; 1 1 1];
for iCount_out=1:num_c
    if (corr_outflow_sort(iCount_out,3)<.01)
        index_out(iCount_out)=1;
    elseif and(corr_outflow_sort(iCount_out,3)>.01, corr_outflow_sort(iCount_out,3)<.05)
        index_out(iCount_out)=2;
    else
        index_out(iCount_out)=3;
    end
end
set(bar_child_out, 'CData',index_out);
colormap(mycolor);
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 1.A.1 Country-By-Country Correlations Between Capital Controls on Bonds and Output','Position',[0.508203125 0.978604294478528 9.16025403784439],'HorizontalAlignment','center')

%Calculate correlation between Inflows and outflows (cyclical components)
[r_in_out, p_in_out] = country_correlation(1, xt(:,:), indext(:,:,1));

%Sort correlation between inflow and outflow cyclical components
[corr_in_out_sort, legend_in_out_star, star_n_out, legend_in_out] = star_zero(1, r_in_out, p_in_out);

%Create a bar chart for the INFLOW-OUTFLOW CORRELATIONS - EQUITY
figure(3)
b_in_out=barh(corr_in_out_sort(:,2));
set(gca, 'YDir', 'reverse')
set(gca, 'YTick', 1:num_c, 'YTickLabel', legend_in_out_star,'FontSize',5,'FontWeight','bold');
set(gca,'XLim',[-1 1])
t=title('Figure 2.A.2 Country-By-Country Correlations Between Controls on Capital Inflows and Outflows in Equity','FontWeight','bold');
set(t, 'Visible', 'on','Fontsize', 10);
bar_child_in_out=get(b_in_out,'Children');
set(bar_child_in_out,'CData', corr_in_out_sort(:,1));
%Vector of colors for bars: ***blue, **green, white
mycolor=[0 0 1; 0 1 0; 1 1 1];
for iCount_in_out=1:num_c
    if (corr_in_out_sort(iCount_in_out,3)<.01)
        index_in_out(iCount_in_out)=1;
    elseif and(corr_in_out_sort(iCount_in_out,3)>.01, corr_in_out_sort(iCount_in_out,3)<.05)
        index_in_out(iCount_in_out)=2;
    else
        index_in_out(iCount_in_out)=3;
    end
end
set(bar_child_in_out, 'CData',index_in_out);
colormap(mycolor);
box off


%Calculate correlation between Inflows and outflows (cyclical components)
[r_in_out, p_in_out] = country_correlation(1, xt(:,3:end), indext(:,3:end,2));

%Sort correlation between inflow and outflow cyclical components
[corr_in_out_sort, legend_in_out_star, star_n_out, legend_in_out] = star_zero(1, r_in_out, p_in_out);

%Create a bar chart for the INFLOW-OUTFLOW CORRELATIONS - BONDS
figure(4)
b_in_out=barh(corr_in_out_sort(:,2));
set(gca, 'YDir', 'reverse')
set(gca, 'YTick', 1:num_c, 'YTickLabel', legend_in_out_star,'FontSize',5,'FontWeight','bold');
set(gca,'XLim',[-1 1])
t=title('Figure 2.A.1 Country-By-Country Correlations Between Controls on Capital Inflows and Outflows in Bonds','FontWeight','bold');
set(t, 'Visible', 'on','Fontsize', 10);
bar_child_in_out=get(b_in_out,'Children');
set(bar_child_in_out,'CData', corr_in_out_sort(:,1));
%Vector of colors for bars: ***blue, **green, white
mycolor=[0 0 1; 0 1 0; 1 1 1];
for iCount_in_out=1:num_c
    if (corr_in_out_sort(iCount_in_out,3)<.01)
        index_in_out(iCount_in_out)=1;
    elseif and(corr_in_out_sort(iCount_in_out,3)>.01, corr_in_out_sort(iCount_in_out,3)<.05)
        index_in_out(iCount_in_out)=2;
    else
        index_in_out(iCount_in_out)=3;
    end
end
set(bar_child_in_out, 'CData',index_in_out);
colormap(mycolor);
box off

  case 25

% This task produces figure 3.A.1  
      
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%BOOM/BUST AND DETRENDED INDEX%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load case_GDP1
load case_GDP2
load case_GDP3
load case_GDP4
load case_GDP7

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

%BOOM
[average_boom_values,~,~,~,boom_values] = average_boom_bust(num_gdp, num_boom, boom_matrix, xt);

%BUST
[average_bust_values,~,~,~,bust_values] = average_boom_bust(num_gdp, num_bust, bust_matrix, xt);

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

%BOOM
[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
[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 output gap (reference point boom/bust)

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

ka_avg2sd  = [0.12 0.12 0.12 0.12 0.12];
kai_avg2sd = [0.13 0.13 0.13 0.13 0.13];
kao_avg2sd = [0.13 0.13 0.13 0.13 0.13];

figure(1)
% OVERALL INDEX
% Boom
subplot(3,2,1)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom(1,:,1), 'b', 100*average_boom_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_boom(1,:,1)),2) roundTo(1.75*max(upper_bound_boom(1,:,1)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom(1,:,1)),2) 0 roundTo(1.75*max(upper_bound_boom(1,:,1)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom(1,:,1)),2) 0 roundTo(1.75*max(upper_bound_boom(1,:,1)),2)],'FontSize',10);
t=title('Boom','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(-1*ka_avg2sd,'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(ka_avg2sd,'k-.');
box off
hold off
%Bust
subplot(3,2,2)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust(1,:,1), '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,:,1)),2) roundTo(1.75*max(upper_bound_bust(1,:,1)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_bust(1,:,1)),2) 0 roundTo(1.75*max(upper_bound_bust(1,:,1)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_bust(1,:,1)),2) 0 roundTo(1.75*max(upper_bound_bust(1,:,1)),2)],'FontSize',10);
t=title('Bust','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(-1*ka_avg2sd,'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(ka_avg2sd,'k-.');
box off
hold off
% INFLOW
%Boom
subplot(3,2,3)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom(1,:,2), 'b', 100*average_boom_values ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
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(-1*kai_avg2sd,'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(kai_avg2sd,'k-.');
box off
hold off
% Bust
subplot(3,2,4)
[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',8);
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(-1*kai_avg2sd,'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(kai_avg2sd,'k-.');
box off
hold off
%OUTFLOW
% Boom
subplot(3,2,5)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom(1,:,3), 'b', 100*average_boom_values ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
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(-1*kao_avg2sd,'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(kao_avg2sd,'k-.');
box off
hold off
% Bust
subplot(3,2,6)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust(1,:,3), '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',8);
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(-1*kao_avg2sd,'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(kao_avg2sd,'k-.');
box off
hold off
legend1 = legend([hline1 hline2 hline3],'Average Index (lhs)','Average Output Gap (rhs)','Two Standard Deviation for the Index (lhs)');
set(legend1,'Orientation','horizontal','Position',[0.271744791666667 0.0203429912151471 0.515104166666667 0.0322147651006711],'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 3.A.1 Boom-Bust Episodes and Capital Controls (Alternative Error Bands)','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) Overall Index','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) Inflows','Position',[0.514453125 0.660651920838184 9.16025403784439],'HorizontalAlignment','center');
sub_ha3 = axes('Visible','off','Position',[0 0 1 1]);
text('Parent',sub_ha3,'String','\bf (c) Outflows','Position',[0.517578125 0.364959254947614 9.16025403784439],'HorizontalAlignment','center');
save case_GDP20 boom_matrix indext_values_boom bust_matrix indext_values_bust num_boom num_bust

    case 26
    % This task produces online appendix figures 5.A.5, 5.A.6, 5.A.7, 5.A.8    

load case_GDP1
load case_GDP2
load case_GDP3
load case_GDP4
load case_GDP7
load case_GDP8
load case_GDP12
load case_GDP21
load case_GDP22
clear indext
load case_GDP23

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

num_c=78;
num_index=3;

indext_eq=nan(num_c,schindler_years,num_index);

for j=1:num_c
    for t=1:schindler_years
        for k=1:num_index
        if k==1
            indext_eq(j,t,k)=indext(j,t,1);
        elseif k==2
            indext_eq(j,t,k)=indext(j,t,3);
        else
            indext_eq(j,t,k)=indext(j,t,4);
        end
        end
    end
end

save case_GDP24 indext_eq

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%% EQUITY %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%BOOM
[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
[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);

%BOOM
%Divide ouput gap in country groups
[boom_values_adv, boom_values_mid, boom_values_poor] = country_groups_boom_bust(num_gdp, 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_gdp, 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
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    

%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);

%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);

%LOW-INCOME 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
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    

%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);
 
%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);
 
%LOW-INCOME 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);

if mac_user==1
[vardat,~, ~]=xlsread(strcat(BasePath, '\exchange_rate_regime.xls') ,char('data')); 
else
[vardat,~, ~]=xlsread(strcat(BasePath, '/exchange_rate_regime.xls') ,char('data')); 
end    
    
ex_regime=vardat(2:end,:);    
    

%ELIMINATE 12 COUNTRIES WITH LESS THAN 25 YEAR OBSERVATIONS FOR GDP 
ex_regime=[ex_regime(1:4,:,:); ex_regime(6:10,:,:); ex_regime(12:18,:,:); ex_regime(20:26,:,:);ex_regime(28:32,:,:);ex_regime(34:41,:,:);ex_regime(43:45,:,:);
     ex_regime(48:52,:,:);ex_regime(54:66,:,:);ex_regime(69:70,:,:);ex_regime(72:89,:,:);ex_regime(91,:,:)];

%Aggregate exchange regime data
for n=1:num_c
    for j=1:schindler_years
        aggregate_ex_regime(j+schindler_years*(n-1),1)=ex_regime(n,j);
        aggregate_xt(j+schindler_years*(n-1),1)=xt(n,j);
        aggregate_overall_indext(j+schindler_years*(n-1),1)=indext_eq(n,j,1);
        aggregate_inflow_indext(j+schindler_years*(n-1),1)=indext_eq(n,j,2);
        aggregate_outflow_indext(j+schindler_years*(n-1),1)=indext_eq(n,j,3);
    end
end

%Calculate the averages and confidence intervals for output gap and OVERALL
%index divided by different exchange rate regime
[num_regime_boom, num_regime_bust, ~, xt_average_matrix_boom, xt_average_matrix_bust, overall_average_matrix_boom, overall_average_matrix_bust, overall_interval_matrix_boom, overall_interval_matrix_bust] = exchange_regime_exercise(num_boom, num_bust, boom_matrix, bust_matrix, aggregate_xt, aggregate_ex_regime, aggregate_overall_indext);

%Calculate the averages and confidence intervals for output gap and INFLOW
%index divided by different exchange rate regime
[~, ~, ~, ~, ~, inflow_average_matrix_boom, inflow_average_matrix_bust, inflow_interval_matrix_boom, inflow_interval_matrix_bust] = exchange_regime_exercise(num_boom, num_bust, boom_matrix, bust_matrix, aggregate_xt, aggregate_ex_regime, aggregate_inflow_indext);

%Calculate the averages and confidence intervals for output gap and OUTFLOW
%index divided by different exchange rate regime
[~, ~, ~, ~, ~, outflow_average_matrix_boom, outflow_average_matrix_bust, outflow_interval_matrix_boom, outflow_interval_matrix_bust] = exchange_regime_exercise(num_boom, num_bust, boom_matrix, bust_matrix, aggregate_xt, aggregate_ex_regime, aggregate_outflow_indext);



%% Import data on nfa and manipulate main variables

if mac_user==1
[vardat,~, ~]=xlsread(strcat(BasePath, '\NFA.xls') ,char('data')); 
else
[vardat,~, ~]=xlsread(strcat(BasePath, '/NFA.xls') ,char('data')); 
    
end

%Define level of indebtness
nfa=vardat(2:end,:);
    

%ELIMINATE 12 COUNTRIES WITH LESS THAN 25 YEAR OBSERVATIONS FOR GDP AND THE BAHRAIN THAT HAS LESS THAN 17 OBSERVATION FOR INDEX
nfa=[nfa(1:4,:,:); nfa(6:10,:,:); nfa(12:18,:,:); nfa(20:26,:,:);nfa(28:32,:,:);nfa(34:41,:,:);nfa(43:45,:,:);
     nfa(48:52,:,:);nfa(54:66,:,:);nfa(69:70,:,:);nfa(72:89,:,:);nfa(91,:,:)];

%Create aggregated vector
for i=1:num_c
    for j=1:schindler_years
        aggregate_nfa(j+schindler_years*(i-1),1)=nfa(i,j);
    end
end

%Sort the vector in ascendign order
aggregate_nfa_sort=sort(aggregate_nfa);

% Find the two threshold levels (20% and 80%)
threshold_high = prctile(aggregate_nfa_sort,level_threshold_high);
threshold_low  = prctile(aggregate_nfa_sort,level_threshold_low);

%Create indicator for level of indebtness. 1 for higly indebted countries, 2 for lowly indebted
%countries, 0 for middle.
debt_indicator=ones(size(aggregate_nfa,1),1);

for i=1:size(aggregate_nfa,1)
    if aggregate_nfa(i,1)<=threshold_high
        debt_indicator(i,1)=2;
    elseif aggregate_nfa(i,1)>=threshold_low
        debt_indicator(i,1)=0;
    else
        debt_indicator(i,1)=1;
    end
end

if mac_user==1
[vardat,textdata, raw]=xlsread(strcat(BasePath, '\NFA.xls') ,char('data')); 
else
[vardat,textdata, raw]=xlsread(strcat(BasePath, '/NFA.xls') ,char('data')); 
    
end

%Define level of indebtness
nfa=vardat(2:end,:);
    

%ELIMINATE 12 COUNTRIES WITH LESS THAN 25 YEAR OBSERVATIONS FOR GDP 
nfa=[nfa(1:4,:,:); nfa(6:10,:,:); nfa(12:18,:,:); nfa(20:26,:,:);nfa(28:32,:,:);nfa(34:41,:,:);nfa(43:45,:,:);
     nfa(48:52,:,:);nfa(54:66,:,:);nfa(69:70,:,:);nfa(72:89,:,:);nfa(91,:,:)];

%Create aggregated vector
for i=1:num_c
    for j=1:schindler_years
        aggregate_nfa(j+schindler_years*(i-1),1)=nfa(i,j);
    end
end

%Sort the vector in ascendign order
aggregate_nfa_sort=sort(aggregate_nfa);

% Find the two threshold levels (20% and 80%)
threshold_high = prctile(aggregate_nfa_sort,level_threshold_high);
threshold_low  = prctile(aggregate_nfa_sort,level_threshold_low);

%Create indicator for level of indebtness. 1 for higly indebted countries, 2 for lowly indebted
%countries, 0 for middle.
debt_indicator=ones(size(aggregate_nfa,1),1);

for i=1:size(aggregate_nfa,1)
    if aggregate_nfa(i,1)<=threshold_high
        debt_indicator(i,1)=2;
    elseif aggregate_nfa(i,1)>=threshold_low
        debt_indicator(i,1)=0;
    else
        debt_indicator(i,1)=1;
    end
end

%Calculate averages, and confidence interval for output gap and detrended
%index divided by higly indebted countries and not highly indebted
%countries: OVERALL INDEX
[num_debt_boom, num_debt_bust, boom_bust_indicator, xt_average_matrix_boom_d, xt_average_matrix_bust_d, overall_average_matrix_boom_d, overall_average_matrix_bust_d, overall_interval_matrix_boom_d, overall_interval_matrix_bust_d] = debt_exercise_v1_Mar_01_2015(num_boom, num_bust, boom_matrix, bust_matrix, aggregate_xt, debt_indicator, aggregate_overall_indext);   

%Calculate averages, and confidence interval for output gap and detrended
%index divided by higly indebted countries and not highly indebted
%countries: INFLOWS INDEX
[~, ~, ~, ~, ~, inflow_average_matrix_boom_d, inflow_average_matrix_bust_d, inflow_interval_matrix_boom_d, inflow_interval_matrix_bust_d] = debt_exercise_v1_Mar_01_2015(num_boom, num_bust, boom_matrix, bust_matrix, aggregate_xt, debt_indicator, aggregate_inflow_indext);   

%Calculate averages, and confidence interval for output gap and detrended
%index divided by higly indebted countries and not highly indebted
%countries: OUTFLOWS INDEX
[~, ~, ~, ~, ~, outflow_average_matrix_boom_d, outflow_average_matrix_bust_d, outflow_interval_matrix_boom_d, outflow_interval_matrix_bust_d] = debt_exercise_v1_Mar_01_2015(num_boom, num_bust, boom_matrix, bust_matrix, aggregate_xt, debt_indicator, aggregate_outflow_indext);

% ROW 1: BOOM EPISODES AND CAPITAL CONTROLS BY LEVEL OF DEVELOPMENT
figure(1)
% Inflow-Advanced countries
subplot(3,3,1)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom_adv(1,:,2), 'b', 100*average_boom_values_adv ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
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_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('Advanced countries','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
% Inflow-Middle Income countries
subplot(3,3,2)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom_mid(1,:,2), 'b', 100*average_boom_values_mid ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
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_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('Emerging markets','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
% Inflow-Low Income Countries
subplot(3,3,3)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom_poor(1,:,2), 'b', 100*average_boom_values_poor ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
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_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('Low income countries','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

% ROW 2: BOOM EPISODES AND CAPITAL CONTROLS ACROSS EXCHANGE RATE REGIMES

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

%REGIME 2
% Inflows
subplot(3,3,5)
[haxes,hline1,hline2] = plotyy(inflow_average_matrix_boom(1,:,2), 'b', 100*xt_average_matrix_boom(1,:,2) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','')
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(inflow_interval_matrix_boom(1,:,2)),2) roundTo(1.75*max(inflow_interval_matrix_boom(2,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(inflow_interval_matrix_boom(1,:,2)),2) 0 roundTo(1.75*max(inflow_interval_matrix_boom(2,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(inflow_interval_matrix_boom(1,:,2)),2) 0 roundTo(1.75*max(inflow_interval_matrix_boom(2,:,2)),2)],'FontSize',10);
t=title('de facto Crawling Peg','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(inflow_interval_matrix_boom(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(inflow_interval_matrix_boom(2,:,2),'k-.');


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

% ROW 3: BOOM EPISODES AND CAPITAL CONTROLS BY LEVEL OF EXTERNAL INDEBTDENESS

% LOW EXTERNAL DEBT COUNTRIES
% Inflows
subplot(3,3,7)
[haxes,hline1,hline2] = plotyy(inflow_average_matrix_boom_d(1,:,1), 'b', 100*xt_average_matrix_boom_d(1,:,1) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','')
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(inflow_interval_matrix_boom_d(1,:,1)),2) roundTo(1.75*max(inflow_interval_matrix_boom_d(2,:,1)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(inflow_interval_matrix_boom_d(1,:,1)),2) 0 roundTo(1.75*max(inflow_interval_matrix_boom_d(2,:,1)),2)], 'YTickLabel', [roundTo(1.75*min(inflow_interval_matrix_boom_d(1,:,1)),2) 0 roundTo(1.75*max(inflow_interval_matrix_boom_d(2,:,1)),2)],'FontSize',10);
t=title('Low External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(inflow_interval_matrix_boom_d(1,:,1),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(inflow_interval_matrix_boom_d(2,:,1),'k-.');
box off
hold off

% MEDIUM EXTERNAL DEBT COUNTRIES
% Inflows
subplot(3,3,8)
[haxes,hline1,hline2] = plotyy(inflow_average_matrix_boom_d(1,:,2), 'b', 100*xt_average_matrix_boom_d(1,:,2) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','')
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(inflow_interval_matrix_boom_d(1,:,2)),2) roundTo(1.75*max(inflow_interval_matrix_boom_d(2,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(inflow_interval_matrix_boom_d(1,:,2)),2) 0 roundTo(1.75*max(inflow_interval_matrix_boom_d(2,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(inflow_interval_matrix_boom_d(1,:,2)),2) 0 roundTo(1.75*max(inflow_interval_matrix_boom_d(2,:,2)),2)],'FontSize',10);
t=title('Medium External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(inflow_interval_matrix_boom_d(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(inflow_interval_matrix_boom_d(2,:,2),'k-.');
box off
hold off


% HIGH EXTERNAL DEBT COUNTRIES
% Inflows
subplot(3,3,9)
[haxes,hline1,hline2] = plotyy(inflow_average_matrix_boom_d(1,:,3), 'b', 100*xt_average_matrix_boom_d(1,:,3) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','')
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(inflow_interval_matrix_boom_d(1,:,3)),2) roundTo(1.75*max(inflow_interval_matrix_boom_d(2,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(inflow_interval_matrix_boom_d(1,:,3)),2) 0 roundTo(1.75*max(inflow_interval_matrix_boom_d(2,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(inflow_interval_matrix_boom_d(1,:,3)),2) 0 roundTo(1.75*max(inflow_interval_matrix_boom_d(2,:,3)),2)],'FontSize',10);
t=title('High External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(inflow_interval_matrix_boom_d(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(inflow_interval_matrix_boom_d(2,:,3),'k-.');
box off
hold off

h=legend([hline1 hline2 hline3],'Average Inflow Index (lhs)','Average Output Gap (rhs)','Two Standard Deviation for the Index (lhs)');
set(h,'Orientation','horizontal','Position',[0.254947916666667 0.0154279490155329 0.548697916666667 0.0338504936530324],'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 5.A.5 Capital-Inflow Controls in Equity During Booms By Income Level, Exchange-Rate Regime, and Level of External Indebtedness','Position',[0.508203125 0.978604294478528 9.16025403784439],'HorizontalAlignment','center')

% ROW 1: BUST EPISODES AND CAPITAL CONTROLS BY LEVEL OF DEVELOPMENT
figure(2)

% Inflow-Advanced countries
subplot(3,3,1)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust_adv(1,:,2), 'b', 100*average_bust_values_adv ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
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_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('Advanced countries','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,3,2)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust_mid(1,:,2), 'b', 100*average_bust_values_mid ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
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_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('Emerging markets','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
% Inflow-Low Income Countries
subplot(3,3,3)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust_poor(1,:,2), 'b', 100*average_bust_values_poor ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
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_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('Low income countries','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

% ROW 2: BUST EPISODES AND CAPITAL CONTROLS ACROSS EXCHANGE RATE REGIMES

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

%REGIME 2
% Inflows
subplot(3,3,5)
[haxes,hline1,hline2] = plotyy(inflow_average_matrix_bust(1,:,2), 'b', 100*xt_average_matrix_bust(1,:,2) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','')
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(inflow_interval_matrix_bust(1,:,2)),2) roundTo(1.75*max(inflow_interval_matrix_bust(2,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(inflow_interval_matrix_bust(1,:,2)),2) 0 roundTo(1.75*max(inflow_interval_matrix_bust(2,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(inflow_interval_matrix_bust(1,:,2)),2) 0 roundTo(1.75*max(inflow_interval_matrix_bust(2,:,2)),2)],'FontSize',10);
t=title('de facto Crawling Peg','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(inflow_interval_matrix_bust(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(inflow_interval_matrix_bust(2,:,2),'k-.');


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

% ROW 3: BUST EPISODES AND CAPITAL CONTROLS BY LEVEL OF EXTERNAL INDEBTDENESS

% LOW EXTERNAL DEBT COUNTRIES
% Inflows
subplot(3,3,7)
[haxes,hline1,hline2] = plotyy(inflow_average_matrix_bust_d(1,:,1), 'b', 100*xt_average_matrix_bust_d(1,:,1) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','')
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(inflow_interval_matrix_bust_d(1,:,1)),2) roundTo(1.75*max(inflow_interval_matrix_bust_d(2,:,1)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(inflow_interval_matrix_bust_d(1,:,1)),2) 0 roundTo(1.75*max(inflow_interval_matrix_bust_d(2,:,1)),2)], 'YTickLabel', [roundTo(1.75*min(inflow_interval_matrix_bust_d(1,:,1)),2) 0 roundTo(1.75*max(inflow_interval_matrix_bust_d(2,:,1)),2)],'FontSize',10);
t=title('Low External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(inflow_interval_matrix_bust_d(1,:,1),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(inflow_interval_matrix_bust_d(2,:,1),'k-.');
box off
hold off

% MEDIUM EXTERNAL DEBT COUNTRIES
% Inflows
subplot(3,3,8)
[haxes,hline1,hline2] = plotyy(inflow_average_matrix_bust_d(1,:,2), 'b', 100*xt_average_matrix_bust_d(1,:,2) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','')
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(inflow_interval_matrix_bust_d(1,:,2)),2) roundTo(1.75*max(inflow_interval_matrix_bust_d(2,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(inflow_interval_matrix_bust_d(1,:,2)),2) 0 roundTo(1.75*max(inflow_interval_matrix_bust_d(2,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(inflow_interval_matrix_bust_d(1,:,2)),2) 0 roundTo(1.75*max(inflow_interval_matrix_bust_d(2,:,2)),2)],'FontSize',10);
t=title('Medium External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(inflow_interval_matrix_bust_d(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(inflow_interval_matrix_bust_d(2,:,2),'k-.');
box off
hold off


% HIGH EXTERNAL DEBT COUNTRIES
% Inflows
subplot(3,3,9)
[haxes,hline1,hline2] = plotyy(inflow_average_matrix_bust_d(1,:,3), 'b', 100*xt_average_matrix_bust_d(1,:,3) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','')
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(inflow_interval_matrix_bust_d(1,:,3)),2) roundTo(1.75*max(inflow_interval_matrix_bust_d(2,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(inflow_interval_matrix_bust_d(1,:,3)),2) 0 roundTo(1.75*max(inflow_interval_matrix_bust_d(2,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(inflow_interval_matrix_bust_d(1,:,3)),2) 0 roundTo(1.75*max(inflow_interval_matrix_bust_d(2,:,3)),2)],'FontSize',10);
t=title('High External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(inflow_interval_matrix_bust_d(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(inflow_interval_matrix_bust_d(2,:,3),'k-.');
box off
hold off

h=legend([hline1 hline2 hline3],'Average Inflow Index (lhs)','Average Output Gap (rhs)','Two Standard Deviation for the Index (lhs)');
set(h,'Orientation','horizontal','Position',[0.254947916666667 0.0154279490155329 0.548697916666667 0.0338504936530324],'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 5.A.6 Capital-Inflow Controls in Equity During Busts By Income Level, Exchange-Rate Regime, and Level of External Indebtedness','Position',[0.508203125 0.978604294478528 9.16025403784439],'HorizontalAlignment','center')

% ROW 1: BOOM EPISODES AND CAPITAL CONTROLS BY LEVEL OF DEVELOPMENT
figure(3)
% Out-Advanced countries
subplot(3,3,1)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom_adv(1,:,3), 'b', 100*average_boom_values_adv ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_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',7.5);
t=title('Advanced countries','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
% Out-Middle Income countries
subplot(3,3,2)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom_mid(1,:,3), 'b', 100*average_boom_values_mid ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_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',7.5);
t=title('Emerging markets','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
% Out-Low Income Countries
subplot(3,3,3)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom_poor(1,:,3), 'b', 100*average_boom_values_poor ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_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',7.5);
t=title('Low income countries','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

% ROW 2: BOOM EPISODES AND CAPITAL CONTROLS ACROSS EXCHANGE RATE REGIMES

%REGIME 1
%Outflows
subplot(3,3,4)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_boom(1,:,1), 'b', 100*xt_average_matrix_boom(1,:,1) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_boom(1,:,1)),2) roundTo(1.75*max(outflow_interval_matrix_boom(2,:,1)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_boom(1,:,1)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom(2,:,1)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_boom(1,:,1)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom(2,:,1)),2)],'FontSize',7.5);
t=title('de facto Peg','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_boom(1,:,1),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_boom(2,:,1),'k-.');
box off
hold off

%REGIME 2
% Outflows
subplot(3,3,5)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_boom(1,:,2), 'b', 100*xt_average_matrix_boom(1,:,2) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_boom(1,:,2)),2) roundTo(1.75*max(outflow_interval_matrix_boom(2,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_boom(1,:,2)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom(2,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_boom(1,:,2)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom(2,:,2)),2)],'FontSize',7.5);
t=title('de facto Crawling Peg','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_boom(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_boom(2,:,2),'k-.');


% REGIME 3
% Outflows
subplot(3,3,6)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_boom(1,:,3), 'b', 100*xt_average_matrix_boom(1,:,3) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_boom(1,:,3)),2) roundTo(1.75*max(outflow_interval_matrix_boom(2,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_boom(1,:,3)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom(2,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_boom(1,:,3)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom(2,:,3)),2)],'FontSize',7.5);
t=title('Managed Floating','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_boom(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_boom(2,:,3),'k-.');
box off
hold off

% ROW 3: BOOM EPISODES AND CAPITAL CONTROLS BY LEVEL OF EXTERNAL INDEBTDENESS

% LOW EXTERNAL DEBT COUNTRIES
% Outflows
subplot(3,3,7)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_boom_d(1,:,1), 'b', 100*xt_average_matrix_boom_d(1,:,1) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_boom_d(1,:,1)),2) roundTo(1.75*max(outflow_interval_matrix_boom_d(2,:,1)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_boom_d(1,:,1)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom_d(2,:,1)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_boom_d(1,:,1)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom_d(2,:,1)),2)],'FontSize',7.5);
t=title('Low External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_boom_d(1,:,1),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_boom_d(2,:,1),'k-.');
box off
hold off

% MEDIUM EXTERNAL DEBT COUNTRIES
% Outflows
subplot(3,3,8)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_boom_d(1,:,2), 'b', 100*xt_average_matrix_boom_d(1,:,2) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_boom_d(1,:,2)),2) roundTo(1.75*max(outflow_interval_matrix_boom_d(2,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_boom_d(1,:,2)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom_d(2,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_boom_d(1,:,2)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom_d(2,:,2)),2)],'FontSize',7.5);
t=title('Medium External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_boom_d(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_boom_d(2,:,2),'k-.');
box off
hold off


% HIGH EXTERNAL DEBT COUNTRIES
% Outflows
subplot(3,3,9)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_boom_d(1,:,3), 'b', 100*xt_average_matrix_boom_d(1,:,3) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_boom_d(1,:,3)),2) roundTo(1.75*max(outflow_interval_matrix_boom_d(2,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_boom_d(1,:,3)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom_d(2,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_boom_d(1,:,3)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom_d(2,:,3)),2)],'FontSize',7.5);
t=title('High External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_boom_d(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_boom_d(2,:,3),'k-.');
box off
hold off

h=legend([hline1 hline2 hline3],'Average Outflow Index (lhs)','Average Output Gap (rhs)','Two Standard Deviation for the Index (lhs)');
set(h,'Orientation','horizontal','Position',[0.254947916666667 0.0154279490155329 0.548697916666667 0.0338504936530324],'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 5.A.7 Capital-Outflow Controls in Equity During Booms By Income Level, Exchange-Rate Regime, and Level of External Indebtedness','Position',[0.508203125 0.978604294478528 9.16025403784439],'HorizontalAlignment','center')

% ROW 1: BUST EPISODES AND CAPITAL CONTROLS BY LEVEL OF DEVELOPMENT
figure(4)
% Out-Advanced countries
subplot(3,3,1)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust_adv(1,:,3), 'b', 100*average_bust_values_adv ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_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',7.5);
t=title('Advanced countries','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
% Out-Middle Income countries
subplot(3,3,2)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust_mid(1,:,3), 'b', 100*average_bust_values_mid ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_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',7.5);
t=title('Emerging markets','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
% Out-Low Income Countries
subplot(3,3,3)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust_poor(1,:,3), 'b', 100*average_bust_values_poor ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_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',7.5);
t=title('Low income countries','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

% ROW 2: BUST EPISODES AND CAPITAL CONTROLS ACROSS EXCHANGE RATE REGIMES

%REGIME 1
%Outflows
subplot(3,3,4)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_bust(1,:,1), 'b', 100*xt_average_matrix_bust(1,:,1) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_bust(1,:,1)),2) roundTo(1.75*max(outflow_interval_matrix_bust(2,:,1)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_bust(1,:,1)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust(2,:,1)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_bust(1,:,1)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust(2,:,1)),2)],'FontSize',7.5);
t=title('de facto Peg','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_bust(1,:,1),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_bust(2,:,1),'k-.');
box off
hold off

%REGIME 2
% Outflows
subplot(3,3,5)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_bust(1,:,2), 'b', 100*xt_average_matrix_bust(1,:,2) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_bust(1,:,2)),2) roundTo(1.75*max(outflow_interval_matrix_bust(2,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_bust(1,:,2)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust(2,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_bust(1,:,2)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust(2,:,2)),2)],'FontSize',7.5);
t=title('de facto Crawling Peg','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_bust(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_bust(2,:,2),'k-.');


% REGIME 3
% Outflows
subplot(3,3,6)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_bust(1,:,3), 'b', 100*xt_average_matrix_bust(1,:,3) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_bust(1,:,3)),2) roundTo(1.75*max(outflow_interval_matrix_bust(2,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_bust(1,:,3)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust(2,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_bust(1,:,3)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust(2,:,3)),2)],'FontSize',7.5);
t=title('Managed Floating','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_bust(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_bust(2,:,3),'k-.');
box off
hold off

% ROW 3: BUST EPISODES AND CAPITAL CONTROLS BY LEVEL OF EXTERNAL INDEBTDENESS

% LOW EXTERNAL DEBT COUNTRIES
% Outflows
subplot(3,3,7)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_bust_d(1,:,1), 'b', 100*xt_average_matrix_bust_d(1,:,1) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_bust_d(1,:,1)),2) roundTo(1.75*max(outflow_interval_matrix_bust_d(2,:,1)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_bust_d(1,:,1)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust_d(2,:,1)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_bust_d(1,:,1)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust_d(2,:,1)),2)],'FontSize',7.5);
t=title('Low External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_bust_d(1,:,1),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_bust_d(2,:,1),'k-.');
box off
hold off

% MEDIUM EXTERNAL DEBT COUNTRIES
% Outflows
subplot(3,3,8)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_bust_d(1,:,2), 'b', 100*xt_average_matrix_bust_d(1,:,2) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_bust_d(1,:,2)),2) roundTo(1.75*max(outflow_interval_matrix_bust_d(2,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_bust_d(1,:,2)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust_d(2,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_bust_d(1,:,2)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust_d(2,:,2)),2)],'FontSize',7.5);
t=title('Medium External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_bust_d(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_bust_d(2,:,2),'k-.');
box off
hold off


% HIGH EXTERNAL DEBT COUNTRIES
% Outflows
subplot(3,3,9)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_bust_d(1,:,3), 'b', 100*xt_average_matrix_bust_d(1,:,3) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_bust_d(1,:,3)),2) roundTo(1.75*max(outflow_interval_matrix_bust_d(2,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_bust_d(1,:,3)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust_d(2,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_bust_d(1,:,3)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust_d(2,:,3)),2)],'FontSize',7.5);
t=title('High External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_bust_d(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_bust_d(2,:,3),'k-.');
box off
hold off

h=legend([hline1 hline2 hline3],'Average Outflow Index (lhs)','Average Output Gap (rhs)','Two Standard Deviation for the Index (lhs)');
set(h,'Orientation','horizontal','Position',[0.254947916666667 0.0154279490155329 0.548697916666667 0.0338504936530324],'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 5.A.8 Capital-Outflow Controls in Equity During Busts By Income Level, Exchange-Rate Regime, and Level of External Indebtedness','Position',[0.508203125 0.978604294478528 9.16025403784439],'HorizontalAlignment','center')

 case 27
    % This task produces online appendix figures 5.A.1, 5.A.2, 5.A.3, 5.A.4    

load case_GDP1
load case_GDP2
load case_GDP3
load case_GDP4
load case_GDP7
load case_GDP8
load case_GDP12
load case_GDP21
load case_GDP22
clear indext
load case_GDP23

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

num_c=78;
num_index=3;


indext_bo=nan(num_c,schindler_years,num_index);


for j=1:num_c
    for tau=1:schindler_years
        for k=1:num_index
        if k==1
            indext_bo(j,tau,k)=indext(j,tau,2);
        elseif k==2
            indext_bo(j,tau,k)=indext(j,tau,5);
        else
            indext_bo(j,tau,k)=indext(j,tau,6);
        end
        end
    end
end

save case_GDP25 indext_bo

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%% BONDS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%BOOM
[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
[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);

%BOOM
%Divide ouput gap in country groups
[boom_values_adv, boom_values_mid, boom_values_poor] = country_groups_boom_bust(num_gdp, 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_gdp, 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
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    

%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);

%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);

%LOW-INCOME 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
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    

%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);
 
%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);
 
%LOW-INCOME 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);

if mac_user==1
[vardat,~, ~]=xlsread(strcat(BasePath, '\exchange_rate_regime.xls') ,char('data')); 
else
[vardat,~, ~]=xlsread(strcat(BasePath, '/exchange_rate_regime.xls') ,char('data'));     
end
    
ex_regime=vardat(2:end,:);    
    

%ELIMINATE 12 COUNTRIES WITH LESS THAN 25 YEAR OBSERVATIONS FOR GDP 
ex_regime=[ex_regime(1:4,:,:); ex_regime(6:10,:,:); ex_regime(12:18,:,:); ex_regime(20:26,:,:);ex_regime(28:32,:,:);ex_regime(34:41,:,:);ex_regime(43:45,:,:);
     ex_regime(48:52,:,:);ex_regime(54:66,:,:);ex_regime(69:70,:,:);ex_regime(72:89,:,:);ex_regime(91,:,:)];

%Aggregate exchange regime data
for n=1:num_c
    for j=1:schindler_years
        aggregate_ex_regime(j+schindler_years*(n-1),1)=ex_regime(n,j);
        aggregate_xt(j+schindler_years*(n-1),1)=xt(n,j);
        aggregate_overall_indext(j+schindler_years*(n-1),1)=indext_bo(n,j,1);
        aggregate_inflow_indext(j+schindler_years*(n-1),1)=indext_bo(n,j,2);
        aggregate_outflow_indext(j+schindler_years*(n-1),1)=indext_bo(n,j,3);
    end
end

%Calculate the averages and confidence intervals for output gap and OVERALL
%index divided by different exchange rate regime
[num_regime_boom, num_regime_bust, boom_bust_indicator, xt_average_matrix_boom, xt_average_matrix_bust, overall_average_matrix_boom, overall_average_matrix_bust, overall_interval_matrix_boom, overall_interval_matrix_bust] = exchange_regime_exercise(num_boom, num_bust, boom_matrix, bust_matrix, aggregate_xt, aggregate_ex_regime, aggregate_overall_indext);

%Calculate the averages and confidence intervals for output gap and INFLOW
%index divided by different exchange rate regime
[~, ~, ~, ~, ~, inflow_average_matrix_boom, inflow_average_matrix_bust, inflow_interval_matrix_boom, inflow_interval_matrix_bust] = exchange_regime_exercise(num_boom, num_bust, boom_matrix, bust_matrix, aggregate_xt, aggregate_ex_regime, aggregate_inflow_indext);

%Calculate the averages and confidence intervals for output gap and OUTFLOW
%index divided by different exchange rate regime
[~, ~, ~, ~, ~, outflow_average_matrix_boom, outflow_average_matrix_bust, outflow_interval_matrix_boom, outflow_interval_matrix_bust] = exchange_regime_exercise(num_boom, num_bust, boom_matrix, bust_matrix, aggregate_xt, aggregate_ex_regime, aggregate_outflow_indext);



%% Import data on nfa and manipulate main variables

if mac_user==1
[vardat,~, ~]=xlsread(strcat(BasePath, '\NFA.xls') ,char('data')); 
else
[vardat,~, ~]=xlsread(strcat(BasePath, '/NFA.xls') ,char('data')); 
end

%Define level of indebtness
nfa=vardat(2:end,:);
    

%ELIMINATE 12 COUNTRIES WITH LESS THAN 25 YEAR OBSERVATIONS FOR GDP
nfa=[nfa(1:4,:,:); nfa(6:10,:,:); nfa(12:18,:,:); nfa(20:26,:,:);nfa(28:32,:,:);nfa(34:41,:,:);nfa(43:45,:,:);
     nfa(48:52,:,:);nfa(54:66,:,:);nfa(69:70,:,:);nfa(72:89,:,:);nfa(91,:,:)];

%Create aggregated vector
for i=1:num_c
    for j=1:schindler_years
        aggregate_nfa(j+schindler_years*(i-1),1)=nfa(i,j);
    end
end

%Sort the vector in ascendign order
aggregate_nfa_sort=sort(aggregate_nfa);

% Find the two threshold levels (20% and 80%)
threshold_high = prctile(aggregate_nfa_sort,level_threshold_high);
threshold_low  = prctile(aggregate_nfa_sort,level_threshold_low);

%Create indicator for level of indebtness. 1 for higly indebted countries, 2 for lowly indebted
%countries, 0 for middle.
debt_indicator=ones(size(aggregate_nfa,1),1);

for i=1:size(aggregate_nfa,1)
    if aggregate_nfa(i,1)<=threshold_high
        debt_indicator(i,1)=2;
    elseif aggregate_nfa(i,1)>=threshold_low
        debt_indicator(i,1)=0;
    else
        debt_indicator(i,1)=1;
    end
end

if mac_user==1
[vardat,~, ~]=xlsread(strcat(BasePath, '\NFA.xls') ,char('data')); 
else
[vardat,~, ~]=xlsread(strcat(BasePath, '/NFA.xls') ,char('data')); 
end
    
%Define level of indebtness
nfa=vardat(2:end,:);
    

%ELIMINATE 12 COUNTRIES WITH LESS THAN 25 YEAR OBSERVATIONS FOR GDP 
nfa=[nfa(1:4,:,:); nfa(6:10,:,:); nfa(12:18,:,:); nfa(20:26,:,:);nfa(28:32,:,:);nfa(34:41,:,:);nfa(43:45,:,:);
     nfa(48:52,:,:);nfa(54:66,:,:);nfa(69:70,:,:);nfa(72:89,:,:);nfa(91,:,:)];

%Create aggregated vector
for i=1:num_c
    for j=1:schindler_years
        aggregate_nfa(j+schindler_years*(i-1),1)=nfa(i,j);
    end
end

%Sort the vector in ascendign order
aggregate_nfa_sort=sort(aggregate_nfa);

% Find the two threshold levels (20% and 80%)
threshold_high = prctile(aggregate_nfa_sort,level_threshold_high);
threshold_low  = prctile(aggregate_nfa_sort,level_threshold_low);

%Create indicator for level of indebtness. 1 for higly indebted countries, 2 for lowly indebted
%countries, 0 for middle.
debt_indicator=ones(size(aggregate_nfa,1),1);

for i=1:size(aggregate_nfa,1)
    if aggregate_nfa(i,1)<=threshold_high
        debt_indicator(i,1)=2;
    elseif aggregate_nfa(i,1)>=threshold_low
        debt_indicator(i,1)=0;
    else
        debt_indicator(i,1)=1;
    end
end

%Calculate averages, and confidence interval for output gap and detrended
%index divided by higly indebted countries and not highly indebted
%countries: OVERALL INDEX
[num_debt_boom, num_debt_bust, boom_bust_indicator, xt_average_matrix_boom_d, xt_average_matrix_bust_d, overall_average_matrix_boom_d, overall_average_matrix_bust_d, overall_interval_matrix_boom_d, overall_interval_matrix_bust_d] = debt_exercise_v1_Mar_01_2015(num_boom, num_bust, boom_matrix, bust_matrix, aggregate_xt, debt_indicator, aggregate_overall_indext);   

%Calculate averages, and confidence interval for output gap and detrended
%index divided by higly indebted countries and not highly indebted
%countries: INFLOWS INDEX
[~, ~, ~, ~, ~, inflow_average_matrix_boom_d, inflow_average_matrix_bust_d, inflow_interval_matrix_boom_d, inflow_interval_matrix_bust_d] = debt_exercise_v1_Mar_01_2015(num_boom, num_bust, boom_matrix, bust_matrix, aggregate_xt, debt_indicator, aggregate_inflow_indext);   

%Calculate averages, and confidence interval for output gap and detrended
%index divided by higly indebted countries and not highly indebted
%countries: OUTFLOWS INDEX
[~, ~, ~, ~, ~, outflow_average_matrix_boom_d, outflow_average_matrix_bust_d, outflow_interval_matrix_boom_d, outflow_interval_matrix_bust_d] = debt_exercise_v1_Mar_01_2015(num_boom, num_bust, boom_matrix, bust_matrix, aggregate_xt, debt_indicator, aggregate_outflow_indext);

% ROW 1: BOOM EPISODES AND CAPITAL CONTROLS BY LEVEL OF DEVELOPMENT
figure(1)
% Inflow-Advanced countries
subplot(3,3,1)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom_adv(1,:,2), 'b', 100*average_boom_values_adv ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
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_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('Advanced countries','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
% Inflow-Middle Income countries
subplot(3,3,2)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom_mid(1,:,2), 'b', 100*average_boom_values_mid ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
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_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('Emerging markets','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
% Inflow-Low Income Countries
subplot(3,3,3)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom_poor(1,:,2), 'b', 100*average_boom_values_poor ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
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_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('Low income countries','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

% ROW 2: BOOM EPISODES AND CAPITAL CONTROLS ACROSS EXCHANGE RATE REGIMES

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

%REGIME 2
% Inflows
subplot(3,3,5)
[haxes,hline1,hline2] = plotyy(inflow_average_matrix_boom(1,:,2), 'b', 100*xt_average_matrix_boom(1,:,2) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','')
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(inflow_interval_matrix_boom(1,:,2)),2) roundTo(1.75*max(inflow_interval_matrix_boom(2,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(inflow_interval_matrix_boom(1,:,2)),2) 0 roundTo(1.75*max(inflow_interval_matrix_boom(2,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(inflow_interval_matrix_boom(1,:,2)),2) 0 roundTo(1.75*max(inflow_interval_matrix_boom(2,:,2)),2)],'FontSize',10);
t=title('de facto Crawling Peg','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(inflow_interval_matrix_boom(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(inflow_interval_matrix_boom(2,:,2),'k-.');


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

% ROW 3: BOOM EPISODES AND CAPITAL CONTROLS BY LEVEL OF EXTERNAL INDEBTDENESS

% LOW EXTERNAL DEBT COUNTRIES
% Inflows
subplot(3,3,7)
[haxes,hline1,hline2] = plotyy(inflow_average_matrix_boom_d(1,:,1), 'b', 100*xt_average_matrix_boom_d(1,:,1) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','')
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(inflow_interval_matrix_boom_d(1,:,1)),2) roundTo(1.75*max(inflow_interval_matrix_boom_d(2,:,1)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(inflow_interval_matrix_boom_d(1,:,1)),2) 0 roundTo(1.75*max(inflow_interval_matrix_boom_d(2,:,1)),2)], 'YTickLabel', [roundTo(1.75*min(inflow_interval_matrix_boom_d(1,:,1)),2) 0 roundTo(1.75*max(inflow_interval_matrix_boom_d(2,:,1)),2)],'FontSize',10);
t=title('Low External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(inflow_interval_matrix_boom_d(1,:,1),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(inflow_interval_matrix_boom_d(2,:,1),'k-.');
box off
hold off

% MEDIUM EXTERNAL DEBT COUNTRIES
% Inflows
subplot(3,3,8)
[haxes,hline1,hline2] = plotyy(inflow_average_matrix_boom_d(1,:,2), 'b', 100*xt_average_matrix_boom_d(1,:,2) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','')
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(inflow_interval_matrix_boom_d(1,:,2)),2) roundTo(1.75*max(inflow_interval_matrix_boom_d(2,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(inflow_interval_matrix_boom_d(1,:,2)),2) 0 roundTo(1.75*max(inflow_interval_matrix_boom_d(2,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(inflow_interval_matrix_boom_d(1,:,2)),2) 0 roundTo(1.75*max(inflow_interval_matrix_boom_d(2,:,2)),2)],'FontSize',10);
t=title('Medium External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(inflow_interval_matrix_boom_d(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(inflow_interval_matrix_boom_d(2,:,2),'k-.');
box off
hold off


% HIGH EXTERNAL DEBT COUNTRIES
% Inflows
subplot(3,3,9)
[haxes,hline1,hline2] = plotyy(inflow_average_matrix_boom_d(1,:,3), 'b', 100*xt_average_matrix_boom_d(1,:,3) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','')
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(inflow_interval_matrix_boom_d(1,:,3)),2) roundTo(1.75*max(inflow_interval_matrix_boom_d(2,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(inflow_interval_matrix_boom_d(1,:,3)),2) 0 roundTo(1.75*max(inflow_interval_matrix_boom_d(2,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(inflow_interval_matrix_boom_d(1,:,3)),2) 0 roundTo(1.75*max(inflow_interval_matrix_boom_d(2,:,3)),2)],'FontSize',10);
t=title('High External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(inflow_interval_matrix_boom_d(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(inflow_interval_matrix_boom_d(2,:,3),'k-.');
box off
hold off

h=legend([hline1 hline2 hline3],'Average Inflow Index (lhs)','Average Output Gap (rhs)','Two Standard Deviation for the Index (lhs)');
set(h,'Orientation','horizontal','Position',[0.254947916666667 0.0154279490155329 0.548697916666667 0.0338504936530324],'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 5.A.1 Capital-Inflow Controls in Bonds During Booms By Income Level, Exchange-Rate Regime, and Level of External Indebtedness','Position',[0.508203125 0.978604294478528 9.16025403784439],'HorizontalAlignment','center')

% ROW 1: BUST EPISODES AND CAPITAL CONTROLS BY LEVEL OF DEVELOPMENT
figure(2)

% Inflow-Advanced countries
subplot(3,3,1)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust_adv(1,:,2), 'b', 100*average_bust_values_adv ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
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_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('Advanced countries','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,3,2)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust_mid(1,:,2), 'b', 100*average_bust_values_mid ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
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_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('Emerging markets','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
% Inflow-Low Income Countries
subplot(3,3,3)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust_poor(1,:,2), 'b', 100*average_bust_values_poor ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
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_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('Low income countries','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

% ROW 2: BUST EPISODES AND CAPITAL CONTROLS ACROSS EXCHANGE RATE REGIMES

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

%REGIME 2
% Inflows
subplot(3,3,5)
[haxes,hline1,hline2] = plotyy(inflow_average_matrix_bust(1,:,2), 'b', 100*xt_average_matrix_bust(1,:,2) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','')
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(inflow_interval_matrix_bust(1,:,2)),2) roundTo(1.75*max(inflow_interval_matrix_bust(2,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(inflow_interval_matrix_bust(1,:,2)),2) 0 roundTo(1.75*max(inflow_interval_matrix_bust(2,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(inflow_interval_matrix_bust(1,:,2)),2) 0 roundTo(1.75*max(inflow_interval_matrix_bust(2,:,2)),2)],'FontSize',10);
t=title('de facto Crawling Peg','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(inflow_interval_matrix_bust(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(inflow_interval_matrix_bust(2,:,2),'k-.');


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

% ROW 3: BUST EPISODES AND CAPITAL CONTROLS BY LEVEL OF EXTERNAL INDEBTDENESS

% LOW EXTERNAL DEBT COUNTRIES
% Inflows
subplot(3,3,7)
[haxes,hline1,hline2] = plotyy(inflow_average_matrix_bust_d(1,:,1), 'b', 100*xt_average_matrix_bust_d(1,:,1) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','')
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(inflow_interval_matrix_bust_d(1,:,1)),2) roundTo(1.75*max(inflow_interval_matrix_bust_d(2,:,1)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(inflow_interval_matrix_bust_d(1,:,1)),2) 0 roundTo(1.75*max(inflow_interval_matrix_bust_d(2,:,1)),2)], 'YTickLabel', [roundTo(1.75*min(inflow_interval_matrix_bust_d(1,:,1)),2) 0 roundTo(1.75*max(inflow_interval_matrix_bust_d(2,:,1)),2)],'FontSize',10);
t=title('Low External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(inflow_interval_matrix_bust_d(1,:,1),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(inflow_interval_matrix_bust_d(2,:,1),'k-.');
box off
hold off

% MEDIUM EXTERNAL DEBT COUNTRIES
% Inflows
subplot(3,3,8)
[haxes,hline1,hline2] = plotyy(inflow_average_matrix_bust_d(1,:,2), 'b', 100*xt_average_matrix_bust_d(1,:,2) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','')
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(inflow_interval_matrix_bust_d(1,:,2)),2) roundTo(1.75*max(inflow_interval_matrix_bust_d(2,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(inflow_interval_matrix_bust_d(1,:,2)),2) 0 roundTo(1.75*max(inflow_interval_matrix_bust_d(2,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(inflow_interval_matrix_bust_d(1,:,2)),2) 0 roundTo(1.75*max(inflow_interval_matrix_bust_d(2,:,2)),2)],'FontSize',10);
t=title('Medium External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(inflow_interval_matrix_bust_d(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(inflow_interval_matrix_bust_d(2,:,2),'k-.');
box off
hold off


% HIGH EXTERNAL DEBT COUNTRIES
% Inflows
subplot(3,3,9)
[haxes,hline1,hline2] = plotyy(inflow_average_matrix_bust_d(1,:,3), 'b', 100*xt_average_matrix_bust_d(1,:,3) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','')
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(inflow_interval_matrix_bust_d(1,:,3)),2) roundTo(1.75*max(inflow_interval_matrix_bust_d(2,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(inflow_interval_matrix_bust_d(1,:,3)),2) 0 roundTo(1.75*max(inflow_interval_matrix_bust_d(2,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(inflow_interval_matrix_bust_d(1,:,3)),2) 0 roundTo(1.75*max(inflow_interval_matrix_bust_d(2,:,3)),2)],'FontSize',10);
t=title('High External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(inflow_interval_matrix_bust_d(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(inflow_interval_matrix_bust_d(2,:,3),'k-.');
box off
hold off

h=legend([hline1 hline2 hline3],'Average Inflow Index (lhs)','Average Output Gap (rhs)','Two Standard Deviation for the Index (lhs)');
set(h,'Orientation','horizontal','Position',[0.254947916666667 0.0154279490155329 0.548697916666667 0.0338504936530324],'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 5.A.2 Capital-Inflow Controls in Bonds During Busts By Income Level, Exchange-Rate Regime, and Level of External Indebtedness','Position',[0.508203125 0.978604294478528 9.16025403784439],'HorizontalAlignment','center')

% ROW 1: BOOM EPISODES AND CAPITAL CONTROLS BY LEVEL OF DEVELOPMENT
figure(3)
% Out-Advanced countries
subplot(3,3,1)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom_adv(1,:,3), 'b', 100*average_boom_values_adv ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_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',7.5);
t=title('Advanced countries','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
% Out-Middle Income countries
subplot(3,3,2)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom_mid(1,:,3), 'b', 100*average_boom_values_mid ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_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',7.5);
t=title('Emerging markets','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
% Out-Low Income Countries
subplot(3,3,3)
[haxes,hline1,hline2] = plotyy(average_indext_values_boom_poor(1,:,3), 'b', 100*average_boom_values_poor ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_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',7.5);
t=title('Low income countries','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

% ROW 2: BOOM EPISODES AND CAPITAL CONTROLS ACROSS EXCHANGE RATE REGIMES

%REGIME 1
%Outflows
subplot(3,3,4)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_boom(1,:,1), 'b', 100*xt_average_matrix_boom(1,:,1) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_boom(1,:,1)),2) roundTo(1.75*max(outflow_interval_matrix_boom(2,:,1)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_boom(1,:,1)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom(2,:,1)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_boom(1,:,1)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom(2,:,1)),2)],'FontSize',7.5);
t=title('de facto Peg','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_boom(1,:,1),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_boom(2,:,1),'k-.');
box off
hold off

%REGIME 2
% Outflows
subplot(3,3,5)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_boom(1,:,2), 'b', 100*xt_average_matrix_boom(1,:,2) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_boom(1,:,2)),2) roundTo(1.75*max(outflow_interval_matrix_boom(2,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_boom(1,:,2)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom(2,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_boom(1,:,2)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom(2,:,2)),2)],'FontSize',7.5);
t=title('de facto Crawling Peg','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_boom(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_boom(2,:,2),'k-.');


% REGIME 3
% Outflows
subplot(3,3,6)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_boom(1,:,3), 'b', 100*xt_average_matrix_boom(1,:,3) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_boom(1,:,3)),2) roundTo(1.75*max(outflow_interval_matrix_boom(2,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_boom(1,:,3)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom(2,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_boom(1,:,3)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom(2,:,3)),2)],'FontSize',7.5);
t=title('Managed Floating','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_boom(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_boom(2,:,3),'k-.');
box off
hold off

% ROW 3: BOOM EPISODES AND CAPITAL CONTROLS BY LEVEL OF EXTERNAL INDEBTDENESS

% LOW EXTERNAL DEBT COUNTRIES
% Outflows
subplot(3,3,7)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_boom_d(1,:,1), 'b', 100*xt_average_matrix_boom_d(1,:,1) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_boom_d(1,:,1)),2) roundTo(1.75*max(outflow_interval_matrix_boom_d(2,:,1)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_boom_d(1,:,1)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom_d(2,:,1)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_boom_d(1,:,1)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom_d(2,:,1)),2)],'FontSize',7.5);
t=title('Low External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_boom_d(1,:,1),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_boom_d(2,:,1),'k-.');
box off
hold off

% MEDIUM EXTERNAL DEBT COUNTRIES
% Outflows
subplot(3,3,8)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_boom_d(1,:,2), 'b', 100*xt_average_matrix_boom_d(1,:,2) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_boom_d(1,:,2)),2) roundTo(1.75*max(outflow_interval_matrix_boom_d(2,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_boom_d(1,:,2)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom_d(2,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_boom_d(1,:,2)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom_d(2,:,2)),2)],'FontSize',7.5);
t=title('Medium External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_boom_d(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_boom_d(2,:,2),'k-.');
box off
hold off


% HIGH EXTERNAL DEBT COUNTRIES
% Outflows
subplot(3,3,9)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_boom_d(1,:,3), 'b', 100*xt_average_matrix_boom_d(1,:,3) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_boom_d(1,:,3)),2) roundTo(1.75*max(outflow_interval_matrix_boom_d(2,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_boom_d(1,:,3)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom_d(2,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_boom_d(1,:,3)),2) 0 roundTo(1.75*max(outflow_interval_matrix_boom_d(2,:,3)),2)],'FontSize',7.5);
t=title('High External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_boom_d(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_boom_d(2,:,3),'k-.');
box off
hold off

h=legend([hline1 hline2 hline3],'Average Outflow Index (lhs)','Average Output Gap (rhs)','Two Standard Deviation for the Index (lhs)');
set(h,'Orientation','horizontal','Position',[0.254947916666667 0.0154279490155329 0.548697916666667 0.0338504936530324],'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 5.A.3 Capital-Outflow Controls in Bonds During Booms By Income Level, Exchange-Rate Regime, and Level of External Indebtedness','Position',[0.508203125 0.978604294478528 9.16025403784439],'HorizontalAlignment','center')

% ROW 1: BUST EPISODES AND CAPITAL CONTROLS BY LEVEL OF DEVELOPMENT
figure(4)
% Out-Advanced countries
subplot(3,3,1)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust_adv(1,:,3), 'b', 100*average_bust_values_adv ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_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',7.5);
t=title('Advanced countries','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
% Out-Middle Income countries
subplot(3,3,2)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust_mid(1,:,3), 'b', 100*average_bust_values_mid ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_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',7.5);
t=title('Emerging markets','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
% Out-Low Income Countries
subplot(3,3,3)
[haxes,hline1,hline2] = plotyy(average_indext_values_bust_poor(1,:,3), 'b', 100*average_bust_values_poor ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xLim', [1 5])
set(haxes, 'xTickLabel','','FontSize',7.5)
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_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',7.5);
t=title('Low income countries','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

% ROW 2: BUST EPISODES AND CAPITAL CONTROLS ACROSS EXCHANGE RATE REGIMES

%REGIME 1
%Outflows
subplot(3,3,4)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_bust(1,:,1), 'b', 100*xt_average_matrix_bust(1,:,1) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_bust(1,:,1)),2) roundTo(1.75*max(outflow_interval_matrix_bust(2,:,1)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_bust(1,:,1)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust(2,:,1)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_bust(1,:,1)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust(2,:,1)),2)],'FontSize',7.5);
t=title('de facto Peg','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_bust(1,:,1),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_bust(2,:,1),'k-.');
box off
hold off

%REGIME 2
% Outflows
subplot(3,3,5)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_bust(1,:,2), 'b', 100*xt_average_matrix_bust(1,:,2) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_bust(1,:,2)),2) roundTo(1.75*max(outflow_interval_matrix_bust(2,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_bust(1,:,2)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust(2,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_bust(1,:,2)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust(2,:,2)),2)],'FontSize',7.5);
t=title('de facto Crawling Peg','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_bust(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_bust(2,:,2),'k-.');


% REGIME 3
% Outflows
subplot(3,3,6)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_bust(1,:,3), 'b', 100*xt_average_matrix_bust(1,:,3) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_bust(1,:,3)),2) roundTo(1.75*max(outflow_interval_matrix_bust(2,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_bust(1,:,3)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust(2,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_bust(1,:,3)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust(2,:,3)),2)],'FontSize',7.5);
t=title('Managed Floating','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_bust(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_bust(2,:,3),'k-.');
box off
hold off

% ROW 3: BUST EPISODES AND CAPITAL CONTROLS BY LEVEL OF EXTERNAL INDEBTDENESS

% LOW EXTERNAL DEBT COUNTRIES
% Outflows
subplot(3,3,7)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_bust_d(1,:,1), 'b', 100*xt_average_matrix_bust_d(1,:,1) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_bust_d(1,:,1)),2) roundTo(1.75*max(outflow_interval_matrix_bust_d(2,:,1)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_bust_d(1,:,1)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust_d(2,:,1)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_bust_d(1,:,1)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust_d(2,:,1)),2)],'FontSize',7.5);
t=title('Low External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_bust_d(1,:,1),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_bust_d(2,:,1),'k-.');
box off
hold off

% MEDIUM EXTERNAL DEBT COUNTRIES
% Outflows
subplot(3,3,8)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_bust_d(1,:,2), 'b', 100*xt_average_matrix_bust_d(1,:,2) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_bust_d(1,:,2)),2) roundTo(1.75*max(outflow_interval_matrix_bust_d(2,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_bust_d(1,:,2)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust_d(2,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_bust_d(1,:,2)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust_d(2,:,2)),2)],'FontSize',7.5);
t=title('Medium External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_bust_d(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_bust_d(2,:,2),'k-.');
box off
hold off


% HIGH EXTERNAL DEBT COUNTRIES
% Outflows
subplot(3,3,9)
[haxes,hline1,hline2] = plotyy(outflow_average_matrix_bust_d(1,:,3), 'b', 100*xt_average_matrix_bust_d(1,:,3) ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes, 'xTickLabel','','FontSize',7.5)
set(haxes, 'xLim', [1 5])
set(gca, 'XTick', 1:5, 'XTickLabel', label_x,'FontSize',8);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(outflow_interval_matrix_bust_d(1,:,3)),2) roundTo(1.75*max(outflow_interval_matrix_bust_d(2,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(outflow_interval_matrix_bust_d(1,:,3)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust_d(2,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(outflow_interval_matrix_bust_d(1,:,3)),2) 0 roundTo(1.75*max(outflow_interval_matrix_bust_d(2,:,3)),2)],'FontSize',7.5);
t=title('High External Debt Countries','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(outflow_interval_matrix_bust_d(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(outflow_interval_matrix_bust_d(2,:,3),'k-.');
box off
hold off

h=legend([hline1 hline2 hline3],'Average Outflow Index (lhs)','Average Output Gap (rhs)','Two Standard Deviation for the Index (lhs)');
set(h,'Orientation','horizontal','Position',[0.254947916666667 0.0154279490155329 0.548697916666667 0.0338504936530324],'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 5.A.4 Capital-Outflow Controls in Bonds During Busts By Income Level, Exchange-Rate Regime, and Level of External Indebtedness','Position',[0.508203125 0.978604294478528 9.16025403784439],'HorizontalAlignment','center')

    case 28
        
load case_GDP1
load case_GDP2
load case_GDP3
load case_GDP4
load case_GDP24

%% Make plot for all countries overall index

average_xt_crisis=nanmean(xt(:,end-6:end),1);
for i=1:num_index
    average_indext_crisis(:,:,i)=nanmean(indext_eq(:,end-6:end,i),1);
    std_indext_crisis(:,:,i)=nanstd(indext_eq(:,end-6:end,i));
    interval_indext_crisis(:,:,i)=[average_indext_crisis(:,:,i)-2*std_indext_crisis(:,:,i); average_indext_crisis(:,:,i)+2*std_indext_crisis(:,:,i)];
end

%% Create averages and interval for the income subgroups

%Advanced Countries
average_xt_crisis_adv=nanmean(xt_adv(:,end-6:end),1);
for i=1:num_index
    average_indext_crisis_adv(:,:,i)=nanmean(indext_adv(:,end-6:end,i),1);
    std_indext_crisis_adv(:,:,i)=nanstd(indext_adv(:,end-6:end,i));
    interval_indext_crisis_adv(:,:,i)=[average_indext_crisis_adv(:,:,i)-2*std_indext_crisis_adv(:,:,i); average_indext_crisis_adv(:,:,i)+2*std_indext_crisis_adv(:,:,i)];
end

%Middle Income Countries
average_xt_crisis_mid=nanmean(xt_mid(:,end-6:end),1);
for i=1:num_index
    average_indext_crisis_mid(:,:,i)=nanmean(indext_mid(:,end-6:end,i),1);
    std_indext_crisis_mid(:,:,i)=nanstd(indext_mid(:,end-6:end,i));
    interval_indext_crisis_mid(:,:,i)=[average_indext_crisis_mid(:,:,i)-2*std_indext_crisis_mid(:,:,i); average_indext_crisis_mid(:,:,i)+2*std_indext_crisis_mid(:,:,i)];
end

%Poor Countries
average_xt_crisis_poor=nanmean(xt_poor(:,end-6:end),1);
for i=1:num_index
    average_indext_crisis_poor(:,:,i)=nanmean(indext_poor(:,end-6:end,i),1);
    std_indext_crisis_poor(:,:,i)=nanstd(indext_poor(:,end-6:end,i));
    interval_indext_crisis_poor(:,:,i)=[average_indext_crisis_poor(:,:,i)-2*std_indext_crisis_poor(:,:,i); average_indext_crisis_poor(:,:,i)+2*std_indext_crisis_poor(:,:,i)];
end

%% Make plots for income groups

x=[-4 -3 -2 -1 0 1 2];
label_x=[2005 2006 2007 2008 2009 2010 2011];

%% Calculate highly impacted countries

%Pick up 2009 observation and order for magnitude
count=(1:1:num_c);
xt_2009_aux=[count' xt(:,15)];
xt_2009_sort=sortrows(xt_2009_aux,2);

%Find the threshold at the 33% and 66% levels
threshold_h=prctile(xt_2009_sort(:,2),33);
threshold_m=prctile(xt_2009_sort(:,2),67);

%Create vector for impact groups
count=ones(1,3);
for i=1:num_c
    if xt_2009_sort(i,2)<=threshold_h
        xt_2009_h(count(1,1),:)=xt_2009_sort(i,:);
        count(1,1)=count(1,1)+1;
    elseif and(xt_2009_sort(i,2)>threshold_h, xt_2009_sort(i,2)<=threshold_m)
        xt_2009_m(count(1,2),:)=xt_2009_sort(i,:);
        count(1,2)=count(1,2)+1;
    else 
        xt_2009_l(count(1,3),:)=xt_2009_sort(i,:);
        count(1,3)=count(1,3)+1;
    end
end

%Create full matrixes with output gap and index between 2005-2011
for k=1:num_index
    for i=1:num_c
        for j=1:size(xt_2009_h,1)
            if i==xt_2009_h(j,1)
                xt_crisis_h(j,:)=[xt(i,11) xt(i,12) xt(i,13) xt(i,14) xt(i,15) xt(i,16) xt(i,17)];
                indext_crisis_h(j,:,k)=[indext_eq(i,11,k) indext_eq(i,12,k) indext_eq(i,13,k) indext_eq(i,14,k) indext_eq(i,15,k) indext_eq(i,16,k) indext_eq(i,17,k)];
            end
        end
        for j=1:size(xt_2009_m,1)
            if i==xt_2009_m(j,1)
                xt_crisis_m(j,:)=[xt(i,11) xt(i,12) xt(i,13) xt(i,14) xt(i,15) xt(i,16) xt(i,17)];
                indext_crisis_m(j,:,k)=[indext_eq(i,11,k) indext_eq(i,12,k) indext_eq(i,13,k) indext_eq(i,14,k) indext_eq(i,15,k) indext_eq(i,16,k) indext_eq(i,17,k)];
            end
        end
        for j=1:size(xt_2009_l,1)
            if i==xt_2009_l(j,1)
                xt_crisis_l(j,:)=[xt(i,11) xt(i,12) xt(i,13) xt(i,14) xt(i,15) xt(i,16) xt(i,17)];
                indext_crisis_l(j,:,k)=[indext_eq(i,11,k) indext_eq(i,12,k) indext_eq(i,13,k) indext_eq(i,14,k) indext_eq(i,15,k) indext_eq(i,16,k) indext_eq(i,17,k)];
            end
        end
    end
end

%Create averages of output gap and indexes for the impact groups
average_xt_crisis_h=nanmean(xt_crisis_h,1);
average_xt_crisis_m=nanmean(xt_crisis_m,1);
average_xt_crisis_l=nanmean(xt_crisis_l,1);

for k=1:num_index
    average_indext_crisis_h(:,:,k)=nanmean(indext_crisis_h(:,:,k),1);
    average_indext_crisis_m(:,:,k)=nanmean(indext_crisis_m(:,:,k),1);
    average_indext_crisis_l(:,:,k)=nanmean(indext_crisis_l(:,:,k),1);
end

%Create standard deviation of the index for the impact groups
for k=1:num_index
    std_indext_crisis_h(:,:,k)=nanstd(indext_crisis_h(:,:,k));
    std_indext_crisis_m(:,:,k)=nanstd(indext_crisis_m(:,:,k));
    std_indext_crisis_l(:,:,k)=nanstd(indext_crisis_l(:,:,k));
end

%Create interval of the index for the impact groups
for k=1:num_index
    interval_indext_crisis_h(:,:,k)=[average_indext_crisis_h(:,:,k)-2*std_indext_crisis_h(:,:,k); average_indext_crisis_h(:,:,k)+2*std_indext_crisis_h(:,:,k)];
    interval_indext_crisis_m(:,:,k)=[average_indext_crisis_m(:,:,k)-2*std_indext_crisis_m(:,:,k); average_indext_crisis_m(:,:,k)+2*std_indext_crisis_m(:,:,k)];
    interval_indext_crisis_l(:,:,k)=[average_indext_crisis_l(:,:,k)-2*std_indext_crisis_l(:,:,k); average_indext_crisis_l(:,:,k)+2*std_indext_crisis_l(:,:,k)];
end

%% Make plots for income groups

figure(2)
%Inflow-High Impact
subplot(3,2,1)
[haxes,hline1,hline2] = plotyy(average_indext_crisis_h(:,:,2), 'b', 100*average_xt_crisis_h ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 7])
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:7, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(interval_indext_crisis_h(1,:,2)),2) roundTo(1.75*max(interval_indext_crisis_h(2,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(interval_indext_crisis_h(1,:,2)),2) 0 roundTo(1.75*max(interval_indext_crisis_h(2,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(interval_indext_crisis_h(1,:,2)),2) 0 roundTo(1.75*max(interval_indext_crisis_h(2,:,2)),2)],'FontSize',10);
t=title('Inflows','FontWeight','bold');
set(t,'FontSize',10);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(interval_indext_crisis_h(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(interval_indext_crisis_h(2,:,2),'k-.');
box off
hold off
% Outflow-High Impact
subplot(3,2,2)
[haxes,hline1,hline2] = plotyy(average_indext_crisis_h(:,:,3), 'b', 100*average_xt_crisis_h ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 7])
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:7, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(interval_indext_crisis_h(1,:,3)),2) roundTo(1.75*max(interval_indext_crisis_h(2,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(interval_indext_crisis_h(1,:,3)),2) 0 roundTo(1.75*max(interval_indext_crisis_h(2,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(interval_indext_crisis_h(1,:,3)),2) 0 roundTo(1.75*max(interval_indext_crisis_h(2,:,3)),2)],'FontSize',10);
t=title('Outflows','FontWeight','bold');
set(t,'FontSize',10);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(interval_indext_crisis_h(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(interval_indext_crisis_h(2,:,3),'k-.');
box off
hold off
% Inflows-Medium Impact
subplot(3,2,3)
[haxes,hline1,hline2] = plotyy(average_indext_crisis_m(:,:,2), 'b', 100*average_xt_crisis_m ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 7])
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:7, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(interval_indext_crisis_m(1,:,2)),2) roundTo(1.75*max(interval_indext_crisis_m(2,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(interval_indext_crisis_m(1,:,2)),2) 0 roundTo(1.75*max(interval_indext_crisis_m(2,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(interval_indext_crisis_m(1,:,2)),2) 0 roundTo(1.75*max(interval_indext_crisis_m(2,:,2)),2)],'FontSize',10);
t=title('Inflows','FontWeight','bold');
set(t,'FontSize',10);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(interval_indext_crisis_m(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(interval_indext_crisis_m(2,:,2),'k-.');
box off
hold off
% Outflows-Medium Impact
subplot(3,2,4)
[haxes,hline1,hline2] = plotyy(average_indext_crisis_m(:,:,3), 'b', 100*average_xt_crisis_m,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 7])
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:7, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(interval_indext_crisis_m(1,:,3)),2) roundTo(1.75*max(interval_indext_crisis_m(2,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(interval_indext_crisis_m(1,:,3)),2) 0 roundTo(1.75*max(interval_indext_crisis_m(2,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(interval_indext_crisis_m(1,:,3)),2) 0 roundTo(1.75*max(interval_indext_crisis_m(2,:,3)),2)],'FontSize',10);
t=title('Outflows','FontWeight','bold');
set(t,'FontSize',10);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(interval_indext_crisis_m(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(interval_indext_crisis_m(2,:,3),'k-.');
box off
hold off
% Inflows-Low Impact
subplot(3,2,5)
[haxes,hline1,hline2] = plotyy(average_indext_crisis_l(1,:,2), 'b', 100*average_xt_crisis_l,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 7])
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:7, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(interval_indext_crisis_l(1,:,2)),2) roundTo(1.75*max(interval_indext_crisis_l(2,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(interval_indext_crisis_l(1,:,2)),2) 0 roundTo(1.75*max(interval_indext_crisis_l(2,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(interval_indext_crisis_l(1,:,2)),2) 0 roundTo(1.75*max(interval_indext_crisis_l(2,:,2)),2)],'FontSize',10);
t=title('Inflows','FontWeight','bold');
set(t,'FontSize',10);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(interval_indext_crisis_l(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(interval_indext_crisis_l(2,:,2),'k-.');
box off
hold off
% Outflows-Low Impact
subplot(3,2,6)
[haxes,hline1,hline2] = plotyy(average_indext_crisis_l(1,:,3), 'b', 100*average_xt_crisis_l,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 7])
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:7, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(interval_indext_crisis_l(1,:,3)),2) roundTo(1.75*max(interval_indext_crisis_l(2,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(interval_indext_crisis_l(1,:,3)),2) 0 roundTo(1.75*max(interval_indext_crisis_l(2,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(interval_indext_crisis_l(1,:,3)),2) 0 roundTo(1.75*max(interval_indext_crisis_l(2,:,3)),2)],'FontSize',10);
t=title('Outflows','FontWeight','bold');
set(t,'FontSize',10);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(interval_indext_crisis_l(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(interval_indext_crisis_l(2,:,3),'k-.');
box off
hold off
legend1 = legend([hline1 hline2 hline3],'Average Index (lhs)','Average Output Gap (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 7.A.2 Capital Controls on Equity Around the Great Contraction By Impact Level','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) High Impact','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) Medium Impact','Position',[0.517578125 0.667763157894737 9.16025403784439],'HorizontalAlignment','center');
sub_ha3 = axes('Visible','off','Position',[0 0 1 1]);
text('Parent',sub_ha3,'String','\bf (c) Low Impact','Position',[0.517578125 0.366447368421053 9.16025403784439],'HorizontalAlignment','center');

    case 29
        
load case_GDP1
load case_GDP2
load case_GDP3
load case_GDP4
load case_GDP25

%% Make plot for all countries overall index

average_xt_crisis=nanmean(xt(:,end-6:end),1);
for i=1:num_index
    average_indext_crisis(:,:,i)=nanmean(indext_bo(:,end-6:end,i),1);
    std_indext_crisis(:,:,i)=nanstd(indext_bo(:,end-6:end,i));
    interval_indext_crisis(:,:,i)=[average_indext_crisis(:,:,i)-2*std_indext_crisis(:,:,i); average_indext_crisis(:,:,i)+2*std_indext_crisis(:,:,i)];
end

%% Create averages and interval for the income subgroups

%Advanced Countries
average_xt_crisis_adv=nanmean(xt_adv(:,end-6:end),1);
for i=1:num_index
    average_indext_crisis_adv(:,:,i)=nanmean(indext_adv(:,end-6:end,i),1);
    std_indext_crisis_adv(:,:,i)=nanstd(indext_adv(:,end-6:end,i));
    interval_indext_crisis_adv(:,:,i)=[average_indext_crisis_adv(:,:,i)-2*std_indext_crisis_adv(:,:,i); average_indext_crisis_adv(:,:,i)+2*std_indext_crisis_adv(:,:,i)];
end

%Middle Income Countries
average_xt_crisis_mid=nanmean(xt_mid(:,end-6:end),1);
for i=1:num_index
    average_indext_crisis_mid(:,:,i)=nanmean(indext_mid(:,end-6:end,i),1);
    std_indext_crisis_mid(:,:,i)=nanstd(indext_mid(:,end-6:end,i));
    interval_indext_crisis_mid(:,:,i)=[average_indext_crisis_mid(:,:,i)-2*std_indext_crisis_mid(:,:,i); average_indext_crisis_mid(:,:,i)+2*std_indext_crisis_mid(:,:,i)];
end

%Poor Countries
average_xt_crisis_poor=nanmean(xt_poor(:,end-6:end),1);
for i=1:num_index
    average_indext_crisis_poor(:,:,i)=nanmean(indext_poor(:,end-6:end,i),1);
    std_indext_crisis_poor(:,:,i)=nanstd(indext_poor(:,end-6:end,i));
    interval_indext_crisis_poor(:,:,i)=[average_indext_crisis_poor(:,:,i)-2*std_indext_crisis_poor(:,:,i); average_indext_crisis_poor(:,:,i)+2*std_indext_crisis_poor(:,:,i)];
end

%% Make plots for income groups

x=[-4 -3 -2 -1 0 1 2];
label_x=[2005 2006 2007 2008 2009 2010 2011];

%% Calculate highly impacted countries

%Pick up 2009 observation and order for magnitude
count=(1:1:num_c);
xt_2009_aux=[count' xt(:,15)];
xt_2009_sort=sortrows(xt_2009_aux,2);

%Find the threshold at the 33% and 66% levels
threshold_h=prctile(xt_2009_sort(:,2),33);
threshold_m=prctile(xt_2009_sort(:,2),67);

%Create vector for impact groups
count=ones(1,3);
for i=1:num_c
    if xt_2009_sort(i,2)<=threshold_h
        xt_2009_h(count(1,1),:)=xt_2009_sort(i,:);
        count(1,1)=count(1,1)+1;
    elseif and(xt_2009_sort(i,2)>threshold_h, xt_2009_sort(i,2)<=threshold_m)
        xt_2009_m(count(1,2),:)=xt_2009_sort(i,:);
        count(1,2)=count(1,2)+1;
    else 
        xt_2009_l(count(1,3),:)=xt_2009_sort(i,:);
        count(1,3)=count(1,3)+1;
    end
end

%Create full matrixes with output gap and index between 2005-2011
for k=1:num_index
    for i=1:num_c
        for j=1:size(xt_2009_h,1)
            if i==xt_2009_h(j,1)
                xt_crisis_h(j,:)=[xt(i,11) xt(i,12) xt(i,13) xt(i,14) xt(i,15) xt(i,16) xt(i,17)];
                indext_crisis_h(j,:,k)=[indext_bo(i,11,k) indext_bo(i,12,k) indext_bo(i,13,k) indext_bo(i,14,k) indext_bo(i,15,k) indext_bo(i,16,k) indext_bo(i,17,k)];
            end
        end
        for j=1:size(xt_2009_m,1)
            if i==xt_2009_m(j,1)
                xt_crisis_m(j,:)=[xt(i,11) xt(i,12) xt(i,13) xt(i,14) xt(i,15) xt(i,16) xt(i,17)];
                indext_crisis_m(j,:,k)=[indext_bo(i,11,k) indext_bo(i,12,k) indext_bo(i,13,k) indext_bo(i,14,k) indext_bo(i,15,k) indext_bo(i,16,k) indext_bo(i,17,k)];
            end
        end
        for j=1:size(xt_2009_l,1)
            if i==xt_2009_l(j,1)
                xt_crisis_l(j,:)=[xt(i,11) xt(i,12) xt(i,13) xt(i,14) xt(i,15) xt(i,16) xt(i,17)];
                indext_crisis_l(j,:,k)=[indext_bo(i,11,k) indext_bo(i,12,k) indext_bo(i,13,k) indext_bo(i,14,k) indext_bo(i,15,k) indext_bo(i,16,k) indext_bo(i,17,k)];
            end
        end
    end
end
%Create averages of output gap and indexes for the impact groups
average_xt_crisis_h=nanmean(xt_crisis_h,1);
average_xt_crisis_m=nanmean(xt_crisis_m,1);
average_xt_crisis_l=nanmean(xt_crisis_l,1);

for k=1:num_index
    average_indext_crisis_h(:,:,k)=nanmean(indext_crisis_h(:,:,k),1);
    average_indext_crisis_m(:,:,k)=nanmean(indext_crisis_m(:,:,k),1);
    average_indext_crisis_l(:,:,k)=nanmean(indext_crisis_l(:,:,k),1);
end

%Create standard deviation of the index for the impact groups
for k=1:num_index
    std_indext_crisis_h(:,:,k)=nanstd(indext_crisis_h(:,:,k));
    std_indext_crisis_m(:,:,k)=nanstd(indext_crisis_m(:,:,k));
    std_indext_crisis_l(:,:,k)=nanstd(indext_crisis_l(:,:,k));
end

%Create interval of the index for the impact groups
for k=1:num_index
    interval_indext_crisis_h(:,:,k)=[average_indext_crisis_h(:,:,k)-2*std_indext_crisis_h(:,:,k); average_indext_crisis_h(:,:,k)+2*std_indext_crisis_h(:,:,k)];
    interval_indext_crisis_m(:,:,k)=[average_indext_crisis_m(:,:,k)-2*std_indext_crisis_m(:,:,k); average_indext_crisis_m(:,:,k)+2*std_indext_crisis_m(:,:,k)];
    interval_indext_crisis_l(:,:,k)=[average_indext_crisis_l(:,:,k)-2*std_indext_crisis_l(:,:,k); average_indext_crisis_l(:,:,k)+2*std_indext_crisis_l(:,:,k)];
end

%% Make plots for income groups

figure(2)
%Inflow-High Impact
subplot(3,2,1)
[haxes,hline1,hline2] = plotyy(average_indext_crisis_h(:,:,2), 'b', 100*average_xt_crisis_h ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 7])
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:7, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(interval_indext_crisis_h(1,:,2)),2) roundTo(1.75*max(interval_indext_crisis_h(2,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(interval_indext_crisis_h(1,:,2)),2) 0 roundTo(1.75*max(interval_indext_crisis_h(2,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(interval_indext_crisis_h(1,:,2)),2) 0 roundTo(1.75*max(interval_indext_crisis_h(2,:,2)),2)],'FontSize',10);
t=title('Inflows','FontWeight','bold');
set(t,'FontSize',10);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(interval_indext_crisis_h(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(interval_indext_crisis_h(2,:,2),'k-.');
box off
hold off
% Outflow-High Impact
subplot(3,2,2)
[haxes,hline1,hline2] = plotyy(average_indext_crisis_h(:,:,3), 'b', 100*average_xt_crisis_h ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 7])
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:7, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(interval_indext_crisis_h(1,:,3)),2) roundTo(1.75*max(interval_indext_crisis_h(2,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(interval_indext_crisis_h(1,:,3)),2) 0 roundTo(1.75*max(interval_indext_crisis_h(2,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(interval_indext_crisis_h(1,:,3)),2) 0 roundTo(1.75*max(interval_indext_crisis_h(2,:,3)),2)],'FontSize',10);
t=title('Outflows','FontWeight','bold');
set(t,'FontSize',10);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(interval_indext_crisis_h(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(interval_indext_crisis_h(2,:,3),'k-.');
box off
hold off
% Inflows-Medium Impact
subplot(3,2,3)
[haxes,hline1,hline2] = plotyy(average_indext_crisis_m(:,:,2), 'b', 100*average_xt_crisis_m ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 7])
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:7, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(interval_indext_crisis_m(1,:,2)),2) roundTo(1.75*max(interval_indext_crisis_m(2,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(interval_indext_crisis_m(1,:,2)),2) 0 roundTo(1.75*max(interval_indext_crisis_m(2,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(interval_indext_crisis_m(1,:,2)),2) 0 roundTo(1.75*max(interval_indext_crisis_m(2,:,2)),2)],'FontSize',10);
t=title('Inflows','FontWeight','bold');
set(t,'FontSize',10);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(interval_indext_crisis_m(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(interval_indext_crisis_m(2,:,2),'k-.');
box off
hold off
% Outflows-Medium Impact
subplot(3,2,4)
[haxes,hline1,hline2] = plotyy(average_indext_crisis_m(:,:,3), 'b', 100*average_xt_crisis_m,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 7])
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:7, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(interval_indext_crisis_m(1,:,3)),2) roundTo(1.75*max(interval_indext_crisis_m(2,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(interval_indext_crisis_m(1,:,3)),2) 0 roundTo(1.75*max(interval_indext_crisis_m(2,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(interval_indext_crisis_m(1,:,3)),2) 0 roundTo(1.75*max(interval_indext_crisis_m(2,:,3)),2)],'FontSize',10);
t=title('Outflows','FontWeight','bold');
set(t,'FontSize',10);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(interval_indext_crisis_m(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(interval_indext_crisis_m(2,:,3),'k-.');
box off
hold off
% Inflows-Low Impact
subplot(3,2,5)
[haxes,hline1,hline2] = plotyy(average_indext_crisis_l(1,:,2), 'b', 100*average_xt_crisis_l,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 7])
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:7, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(interval_indext_crisis_l(1,:,2)),2) roundTo(1.75*max(interval_indext_crisis_l(2,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(interval_indext_crisis_l(1,:,2)),2) 0 roundTo(1.75*max(interval_indext_crisis_l(2,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(interval_indext_crisis_l(1,:,2)),2) 0 roundTo(1.75*max(interval_indext_crisis_l(2,:,2)),2)],'FontSize',10);
t=title('Inflows','FontWeight','bold');
set(t,'FontSize',10);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(interval_indext_crisis_l(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(interval_indext_crisis_l(2,:,2),'k-.');
box off
hold off
% Outflows-Low Impact
subplot(3,2,6)
[haxes,hline1,hline2] = plotyy(average_indext_crisis_l(1,:,3), 'b', 100*average_xt_crisis_l,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 7])
set(haxes, 'xTickLabel','')
set(gca, 'XTick', 1:7, 'XTickLabel', label_x,'FontSize',10);
xlabel('Years');
set(haxes(1), 'yTickLabel','')
set(haxes(1), 'yLim',[roundTo(1.75*min(interval_indext_crisis_l(1,:,3)),2) roundTo(1.75*max(interval_indext_crisis_l(2,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(interval_indext_crisis_l(1,:,3)),2) 0 roundTo(1.75*max(interval_indext_crisis_l(2,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(interval_indext_crisis_l(1,:,3)),2) 0 roundTo(1.75*max(interval_indext_crisis_l(2,:,3)),2)],'FontSize',10);
t=title('Outflows','FontWeight','bold');
set(t,'FontSize',10);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(interval_indext_crisis_l(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(interval_indext_crisis_l(2,:,3),'k-.');
box off
hold off
legend1 = legend([hline1 hline2 hline3],'Average Index (lhs)','Average Output Gap (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 7.A.1 Capital Controls on Bonds Around the Great Contraction By Impact Level','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) High Impact','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) Medium Impact','Position',[0.517578125 0.667763157894737 9.16025403784439],'HorizontalAlignment','center');
sub_ha3 = axes('Visible','off','Position',[0 0 1 1]);
text('Parent',sub_ha3,'String','\bf (c) Low Impact','Position',[0.517578125 0.366447368421053 9.16025403784439],'HorizontalAlignment','center');

    case 31
        
%% This is the figure 4
        
load case_GDP22

%Define labels
x=[-2 -1 0 1 2];
label_x=[-2 -1 0 +1 +2];

% ROW 1: AGGREGATES INFLOWS (EQI, BOI, DII_LDI)

figure(1)

%Equity

subplot(3,3,1)
[haxes,hline1,hline2] = plotyy(average_subindext_inflow_values_boom(1,:,1), 'b', 100*average_boom_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
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_boom_subindext_inflow(1,:,1)),2) roundTo(1.75*max(upper_bound_boom_subindext_inflow(1,:,1)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_subindext_inflow(1,:,1)),2)  0 roundTo(1.75*max(upper_bound_boom_subindext_inflow(1,:,1)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_subindext_inflow(1,:,1)),2) 0 roundTo(1.75*max(upper_bound_boom_subindext_inflow(1,:,1)),2)],'FontSize',10);
t=title(' eq','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_subindext_inflow(1,:,1),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_subindext_inflow(1,:,1),'k-.');
box off
hold off

% Bonds

subplot(3,3,2)
[haxes,hline1,hline2] = plotyy(average_subindext_inflow_values_boom(1,:,2), 'b', 100*average_boom_values ,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
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_boom_subindext_inflow(1,:,2)),2) roundTo(1.75*max(upper_bound_boom_subindext_inflow(1,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_subindext_inflow(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_boom_subindext_inflow(1,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_subindext_inflow(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_boom_subindext_inflow(1,:,2)),2)],'FontSize',10);
t=title('bo','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_subindext_inflow(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_subindext_inflow(1,:,2),'k-.');
box off
hold off

% Direct Investment

subplot(3,3,3)
[haxes,hline1,hline2] = plotyy(average_subindext_inflow_values_boom(1,:,6), 'b', 100*average_boom_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
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_boom_subindext_inflow(1,:,6)),2) roundTo(1.75*max(upper_bound_boom_subindext_inflow(1,:,6)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_subindext_inflow(1,:,6)),2) 0 roundTo(1.75*max(upper_bound_boom_subindext_inflow(1,:,6)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_subindext_inflow(1,:,6)),2) 0 roundTo(1.75*max(upper_bound_boom_subindext_inflow(1,:,6)),2)],'FontSize',10);
t=title('di','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_subindext_inflow(1,:,6),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_subindext_inflow(1,:,6),'k-.');
box off
hold off

% ROW 2: PLBN + DII

%eq_plbn

subplot(3,3,4)
[haxes,hline1,hline2] = plotyy(average_subindexes_inflow_values_boom(1,:,2), 'b', 100*average_boom_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
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_boom_subindexes_inflow(1,:,2)),2) roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,2)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_subindexes_inflow(1,:,2)),2)  0 roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,2)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_subindexes_inflow(1,:,2)),2) 0 roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,2)),2)],'FontSize',10);
t=title(' eq-plbn','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_subindexes_inflow(1,:,2),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_subindexes_inflow(1,:,2),'k-.');
box off
hold off

%bo_plbn

subplot(3,3,5)
[haxes,hline1,hline2] = plotyy(average_subindexes_inflow_values_boom(1,:,5), 'b', 100*average_boom_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
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_boom_subindexes_inflow(1,:,5)),2) roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,5)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_subindexes_inflow(1,:,5)),2)  0 roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,5)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_subindexes_inflow(1,:,5)),2) 0 roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,5)),2)],'FontSize',10);
t=title(' bo-plbn','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_subindexes_inflow(1,:,5),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_subindexes_inflow(1,:,5),'k-.');
box off
hold off

%dii

subplot(3,3,6)
[haxes,hline1,hline2] = plotyy(average_subindexes_inflow_values_boom(1,:,8), 'b', 100*average_boom_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
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_boom_subindexes_inflow(1,:,8)),2) roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,8)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_subindexes_inflow(1,:,8)),2)  0 roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,8)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_subindexes_inflow(1,:,8)),2) 0 roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,8)),2)],'FontSize',10);
t=title(' dii','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_subindexes_inflow(1,:,8),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_subindexes_inflow(1,:,8),'k-.');
box off
hold off

% ROW 3: SIAR + LDI

%eq_siar

subplot(3,3,7)
[haxes,hline1,hline2] = plotyy(average_subindexes_inflow_values_boom(1,:,3), 'b', 100*average_boom_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
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_boom_subindexes_inflow(1,:,3)),2) roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,3)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_subindexes_inflow(1,:,3)),2)  0 roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,3)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_subindexes_inflow(1,:,3)),2) 0 roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,3)),2)],'FontSize',10);
t=title(' eq-siar','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_subindexes_inflow(1,:,3),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_subindexes_inflow(1,:,3),'k-.');
box off
hold off

%bo_siar

subplot(3,3,8)
[haxes,hline1,hline2] = plotyy(average_subindexes_inflow_values_boom(1,:,6), 'b', 100*average_boom_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
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_boom_subindexes_inflow(1,:,6)),2) roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,6)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_subindexes_inflow(1,:,6)),2)  0 roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,6)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_subindexes_inflow(1,:,6)),2) 0 roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,6)),2)],'FontSize',10);
t=title('bo-siar','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_subindexes_inflow(1,:,6),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_subindexes_inflow(1,:,6),'k-.');
box off
hold off

%ldi

subplot(3,3,9)
[haxes,hline1,hline2] = plotyy(average_subindexes_inflow_values_boom(1,:,9), 'b', 100*average_boom_values,'r-*','plot');
set(hline1,'LineWidth',2)
set(hline2,'LineWidth',2)
set(haxes,'xLim',[1 5])
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_boom_subindexes_inflow(1,:,9)),2) roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,9)),2)])
set(haxes(1),'YTick',[roundTo(1.75*min(lower_bound_boom_subindexes_inflow(1,:,9)),2)  0 roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,9)),2)], 'YTickLabel', [roundTo(1.75*min(lower_bound_boom_subindexes_inflow(1,:,9)),2) 0 roundTo(1.75*max(upper_bound_boom_subindexes_inflow(1,:,9)),2)],'FontSize',10);
t=title('ldi','FontWeight','bold');
set(t,'FontSize',8);
hold on
set(haxes(1),'Nextplot','add');
hline3=plot(lower_bound_boom_subindexes_inflow(1,:,9),'k-.');
hold on
set(haxes(1),'Nextplot','add');
plot(upper_bound_boom_subindexes_inflow(1,:,9),'k-.');
box off
hold off

legend1 = legend([hline1 hline2 hline3],'Average Inflow Index (lhs)','Average Output Gap(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 4. Acyclicality of Granular Measures of Capital-Inflow Controls','Position',[0.508203125 0.978604294478528 9.16025403784439],'HorizontalAlignment','center')


end