function [ num_debt_boom, num_debt_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 ] = debt_exercise_v1_Mar_01_2015( num_boom, num_bust, boom_matrix, bust_matrix, aggregate_xt, debt_indicator, aggregate_overall_indext)

% PURPOSE: IDENTIFY BOOM/BUST EPISODES IN CASE OF HIGHLY AND NOT HIGHLY INDEBTEB COUNTRIES, 
% CREATE THE MATRIX CONTAINING ALL EPISODES AND CALCULATE THE AVERAGES AND THE CONFIDENCE INTERVALS
% 
% INPUT:
%     -num_boom:NUMBER OF BOOM EPISODES
%     -num_bust:NUMBER OF BUST EPISODES
%     -boom_matrix: BOOM EPISODES MATRIX
%     -bust_matrix: BUST EPISODES MATRIX
%     -aggregate_xt: AGGREGATED OUTPUT GAP
%     -debt_indicator: INDICATOR FOR HIGHLY INDEBTED COUNTRIES
%     -aggregate_overall_indext: AGGREGATED INDEX
% 
% OUTPUT
%     -num_debt_boom:NUMBER OF BOOM EPISODES IN HIGHLY INDEBTED COUNTRIES
%     -num_debt_bust:NUMBER OF BUST EPISODES IN HIGHLY INDEBTED COUNTRIES
%     -boom_bust_indicator: MATRIX WITH ALL BOOM/BUST EPISODES
%     -xt_average_matrix_boom: AVERAGE OUTPUT GAP IN BOOM EPISODES
%     -xt_average_matrix_bust: AVERAGE OUTPUT GAP IN BUST EPISODES
%     -overall_average_matrix_boom: AVERAGE OVERALL INDEX IN BOOM EPISODES
%     -overall_average_matrix_bust: AVERAGE OVERALL INDEX IN BUST EPISODES
%     -overall_interval_matrix_boom: OVERALL INDEX CONFIDENCE INTERVAL IN BOOM EPISODES
%     -overall_interval_matrix_bust: OVERALL INDEX CONFIDENCE INTERVAL IN BUST EPISODES

    
    
%Recall variable defiend in the main script
global num_c schindler_years

    num_column=5;
     
    %Counter for the years of the index
    schindler_years_list=[1:1:17]';

    %Create vector of years for all countries
    vector_years=repmat(schindler_years_list, [num_c 1]);

    %Create vector for country number identifier
    for i=1:num_c
        for j=1:schindler_years
            vector_country(j+schindler_years*(i-1),1)=i;
        end
    end

    %Create zeros vector
    vector_zeros=zeros(num_c*schindler_years,1);

    %Create the boom and bust indicator
    boom_bust_indicator=[vector_country vector_years vector_zeros];

    %Create matrix for boom and bust indicator. 1 when there is a boom, 2 when
    %there is a bust, 0 when there is neither a boom nor a bust
    for k=1:num_boom
        for i=1:size(boom_bust_indicator,1)
            for j=1:size(boom_bust_indicator,1)
                if and(boom_matrix(k,1)==boom_bust_indicator(i,1),boom_matrix(k,2)==boom_bust_indicator(j,2))
                   boom_bust_indicator(boom_bust_indicator(j,2)+schindler_years*(boom_bust_indicator(i,1)-1),3)=1;
                end
            end
        end
    end

    for k=1:num_bust
        for i=1:size(boom_bust_indicator,1)
            for j=1:size(boom_bust_indicator,1)
                if and(bust_matrix(k,1)==boom_bust_indicator(i,1),bust_matrix(k,2)==boom_bust_indicator(j,2))
                   boom_bust_indicator(boom_bust_indicator(j,2)+schindler_years*(boom_bust_indicator(i,1)-1),3)=2;
                end
            end
        end
    end
    
    %Set equal to zero the years and country where we don't have observations
    %for the indebteness either in that year or in the year
    %before or after
    for j=1:size(boom_bust_indicator,1)
          if boom_bust_indicator(j,2)==17
             boom_bust_indicator(j,3)=0;
          end
    end
   
    %Create matrices for output gap and detrended overall index for the two
    %indebtness groups
    count_boom=ones(1,3);
    count_bust=ones(1,3);
    h_debt_boom=zeros(1,7,3);
    m_debt_boom=zeros(1,7,3);
    l_debt_boom=zeros(1,7,3);
    h_debt_bust=zeros(1,7,3);
    m_debt_bust=zeros(1,7,3);
    l_debt_bust=zeros(1,7,3);

    %BOOM
    for i=1:size(boom_bust_indicator,1)
        if boom_bust_indicator(i,3)==1 & boom_bust_indicator(i,2)==1
            if debt_indicator(i,1)==0 & debt_indicator(i+1,1)==0
                l_debt_boom(count_boom(1,1),:,1)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), nan, nan, aggregate_xt(i,1), aggregate_xt(i+1,1), aggregate_xt(i+2,1)];
                l_debt_boom(count_boom(1,1),:,2)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), nan, nan, aggregate_overall_indext(i,1), aggregate_overall_indext(i+1,1), aggregate_overall_indext(i+2,1)];
                count_boom(1,1)=count_boom(1,1)+1;
            elseif debt_indicator(i,1)==1  & debt_indicator(i+1,1)==1
                m_debt_boom(count_boom(1,2),:,1)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), nan, nan, aggregate_xt(i,1), aggregate_xt(i+1,1), aggregate_xt(i+2,1)];
                m_debt_boom(count_boom(1,2),:,2)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), nan, nan, aggregate_overall_indext(i,1), aggregate_overall_indext(i+1,1), aggregate_overall_indext(i+2,1)];
                count_boom(1,2)=count_boom(1,2)+1;
            elseif debt_indicator(i,1)==2  & debt_indicator(i+1,1)==2
                h_debt_boom(count_boom(1,3),:,1)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), nan, nan, aggregate_xt(i,1), aggregate_xt(i+1,1), aggregate_xt(i+2,1)];
                h_debt_boom(count_boom(1,3),:,2)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), nan, nan, aggregate_overall_indext(i,1), aggregate_overall_indext(i+1,1), aggregate_overall_indext(i+2,1)];
                count_boom(1,3)=count_boom(1,3)+1;
            end
                
        elseif boom_bust_indicator(i,3)==1 & boom_bust_indicator(i,2)==2
            if debt_indicator(i,1)==0 & debt_indicator(i-1,1)==0 & debt_indicator(i+1,1)==0
                l_debt_boom(count_boom(1,1),:,1)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), nan, aggregate_xt(i-1,1), aggregate_xt(i,1), aggregate_xt(i+1,1), aggregate_xt(i+2,1)];
                l_debt_boom(count_boom(1,1),:,2)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), nan, aggregate_overall_indext(i-1,1), aggregate_overall_indext(i,1), aggregate_overall_indext(i+1,1), aggregate_overall_indext(i+2,1)];
                count_boom(1,1)=count_boom(1,1)+1;
            elseif debt_indicator(i,1)==1 & debt_indicator(i-1,1)==1 & debt_indicator(i+1,1)==1
                m_debt_boom(count_boom(1,2),:,1)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), nan, aggregate_xt(i-1,1), aggregate_xt(i,1), aggregate_xt(i+1,1), aggregate_xt(i+2,1)];
                m_debt_boom(count_boom(1,2),:,2)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), nan, aggregate_overall_indext(i-1,1), aggregate_overall_indext(i,1), aggregate_overall_indext(i+1,1), aggregate_overall_indext(i+2,1)];
                count_boom(1,2)=count_boom(1,2)+1;
            elseif debt_indicator(i,1)==1 & debt_indicator(i-1,1)==1 & debt_indicator(i+1,1)==1
                h_debt_boom(count_boom(1,3),:,1)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), nan, aggregate_xt(i-1,1), aggregate_xt(i,1), aggregate_xt(i+1,1), aggregate_xt(i+2,1)];
                h_debt_boom(count_boom(1,3),:,2)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), nan, aggregate_overall_indext(i-1,1), aggregate_overall_indext(i,1), aggregate_overall_indext(i+1,1), aggregate_overall_indext(i+2,1)];
                count_boom(1,3)=count_boom(1,3)+1;
            end

         elseif boom_bust_indicator(i,3)==1 & boom_bust_indicator(i,2)==16
            if debt_indicator(i,1)==0 & debt_indicator(i-1,1)==0 & debt_indicator(i+1,1)==0
                l_debt_boom(count_boom(1,1),:,1)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), aggregate_xt(i-2,1), aggregate_xt(i-1,1), aggregate_xt(i,1), aggregate_xt(i+1,1), nan];
                l_debt_boom(count_boom(1,1),:,2)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), aggregate_overall_indext(i-2,1), aggregate_overall_indext(i-1,1), aggregate_overall_indext(i,1), aggregate_overall_indext(i+1,1), nan];
                count_boom(1,1)=count_boom(1,1)+1;
            elseif debt_indicator(i,1)==1 & debt_indicator(i-1,1)==1 & debt_indicator(i+1,1)==1
                m_debt_boom(count_boom(1,2),:,1)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), aggregate_xt(i-2,1), aggregate_xt(i-1,1), aggregate_xt(i,1), aggregate_xt(i+1,1), nan];
                m_debt_boom(count_boom(1,2),:,2)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), aggregate_overall_indext(i-2,1), aggregate_overall_indext(i-1,1), aggregate_overall_indext(i,1), aggregate_overall_indext(i+1,1), nan];
                count_boom(1,2)=count_boom(1,2)+1;
            elseif debt_indicator(i,1)==2 & debt_indicator(i-1,1)==2 & debt_indicator(i+1,1)==2
                h_debt_boom(count_boom(1,3),:,1)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), aggregate_xt(i-2,1), aggregate_xt(i-1,1), aggregate_xt(i,1), aggregate_xt(i+1,1), nan];
                h_debt_boom(count_boom(1,3),:,2)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), aggregate_overall_indext(i-2,1), aggregate_overall_indext(i-1,1), aggregate_overall_indext(i,1), aggregate_overall_indext(i+1,1), nan];
                count_boom(1,3)=count_boom(1,3)+1;
            end
   
         elseif boom_bust_indicator(i,3)==1 & boom_bust_indicator(i,2)~=1 & boom_bust_indicator(i,2)~=2 & boom_bust_indicator(i,2)~=16
            if debt_indicator(i,1)==0 & debt_indicator(i-1,1)==0 & debt_indicator(i+1,1)==0
                l_debt_boom(count_boom(1,1),:,1)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), aggregate_xt(i-2,1), aggregate_xt(i-1,1), aggregate_xt(i,1), aggregate_xt(i+1,1), aggregate_xt(i+2,1)];
                l_debt_boom(count_boom(1,1),:,2)=[boom_bust_indicator(i,1) boom_bust_indicator(i,2), aggregate_overall_indext(i-2,1), aggregate_overall_indext(i-1,1), aggregate_overall_indext(i,1), aggregate_overall_indext(i+1,1), aggregate_overall_indext(i+2,1)];
                count_boom(1,1)=count_boom(1,1)+1;
            elseif debt_indicator(i,1)==1 & debt_indicator(i-1,1)==1 & debt_indicator(i+1,1)==1
                m_debt_boom(count_boom(1,2),:,1)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), aggregate_xt(i-2,1), aggregate_xt(i-1,1), aggregate_xt(i,1), aggregate_xt(i+1,1), aggregate_xt(i+2,1)];
                m_debt_boom(count_boom(1,2),:,2)=[boom_bust_indicator(i,1) boom_bust_indicator(i,2), aggregate_overall_indext(i-2,1), aggregate_overall_indext(i-1,1), aggregate_overall_indext(i,1), aggregate_overall_indext(i+1,1), aggregate_overall_indext(i+2,1)];
                count_boom(1,2)=count_boom(1,2)+1;
            elseif debt_indicator(i,1)==2 & debt_indicator(i-1,1)==2 & debt_indicator(i+1,1)==2
                h_debt_boom(count_boom(1,3),:,1)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), aggregate_xt(i-2,1), aggregate_xt(i-1,1), aggregate_xt(i,1), aggregate_xt(i+1,1), aggregate_xt(i+2,1)];
                h_debt_boom(count_boom(1,3),:,2)=[boom_bust_indicator(i,1) boom_bust_indicator(i,2), aggregate_overall_indext(i-2,1), aggregate_overall_indext(i-1,1), aggregate_overall_indext(i,1), aggregate_overall_indext(i+1,1), aggregate_overall_indext(i+2,1)];
                count_boom(1,3)=count_boom(1,3)+1;
            end
        end
    end
   
    %BUST
    for i=1:size(boom_bust_indicator,1)
        if boom_bust_indicator(i,3)==2 & boom_bust_indicator(i,2)==1
            if debt_indicator(i,1)==0 & debt_indicator(i+1,1)==0
                l_debt_bust(count_bust(1,1),:,1)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), nan, nan, aggregate_xt(i,1), aggregate_xt(i+1,1), aggregate_xt(i+2,1)];
                l_debt_bust(count_bust(1,1),:,2)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), nan, nan, aggregate_overall_indext(i,1), aggregate_overall_indext(i+1,1), aggregate_overall_indext(i+2,1)];
                count_bust(1,1)=count_bust(1,1)+1;
            elseif debt_indicator(i,1)==1  & debt_indicator(i+1,1)==1
                m_debt_bust(count_bust(1,2),:,1)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), nan, nan, aggregate_xt(i,1), aggregate_xt(i+1,1), aggregate_xt(i+2,1)];
                m_debt_bust(count_bust(1,2),:,2)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), nan, nan, aggregate_overall_indext(i,1), aggregate_overall_indext(i+1,1), aggregate_overall_indext(i+2,1)];
                count_bust(1,2)=count_bust(1,2)+1;
            elseif debt_indicator(i,1)==2  & debt_indicator(i+1,1)==2
                h_debt_bust(count_bust(1,3),:,1)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), nan, nan, aggregate_xt(i,1), aggregate_xt(i+1,1), aggregate_xt(i+2,1)];
                h_debt_bust(count_bust(1,3),:,2)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), nan, nan, aggregate_overall_indext(i,1), aggregate_overall_indext(i+1,1), aggregate_overall_indext(i+2,1)];
                count_bust(1,3)=count_bust(1,3)+1;
            end
                
        elseif boom_bust_indicator(i,3)==2 & boom_bust_indicator(i,2)==2
            if debt_indicator(i,1)==0 & debt_indicator(i-1,1)==0 & debt_indicator(i+1,1)==0
                l_debt_bust(count_bust(1,1),:,1)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), nan, aggregate_xt(i-1,1), aggregate_xt(i,1), aggregate_xt(i+1,1), aggregate_xt(i+2,1)];
                l_debt_bust(count_bust(1,1),:,2)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), nan, aggregate_overall_indext(i-1,1), aggregate_overall_indext(i,1), aggregate_overall_indext(i+1,1), aggregate_overall_indext(i+2,1)];
                count_bust(1,1)=count_bust(1,1)+1;
            elseif debt_indicator(i,1)==1 & debt_indicator(i-1,1)==1 & debt_indicator(i+1,1)==1
                m_debt_bust(count_bust(1,2),:,1)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), nan, aggregate_xt(i-1,1), aggregate_xt(i,1), aggregate_xt(i+1,1), aggregate_xt(i+2,1)];
                m_debt_bust(count_bust(1,2),:,2)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), nan, aggregate_overall_indext(i-1,1), aggregate_overall_indext(i,1), aggregate_overall_indext(i+1,1), aggregate_overall_indext(i+2,1)];
                count_bust(1,2)=count_bust(1,2)+1;
            elseif debt_indicator(i,1)==2 & debt_indicator(i-1,1)==2 & debt_indicator(i+1,1)==2
                h_debt_bust(count_bust(1,3),:,1)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), nan, aggregate_xt(i-1,1), aggregate_xt(i,1), aggregate_xt(i+1,1), aggregate_xt(i+2,1)];
                h_debt_bust(count_bust(1,3),:,2)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), nan, aggregate_overall_indext(i-1,1), aggregate_overall_indext(i,1), aggregate_overall_indext(i+1,1), aggregate_overall_indext(i+2,1)];
                count_bust(1,3)=count_bust(1,3)+1;
            end
 
         elseif boom_bust_indicator(i,3)==2 & boom_bust_indicator(i,2)==16
            if debt_indicator(i,1)==0 & debt_indicator(i-1,1)==0 & debt_indicator(i+1,1)==0
                l_debt_bust(count_bust(1,1),:,1)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), aggregate_xt(i-2,1), aggregate_xt(i-1,1), aggregate_xt(i,1), aggregate_xt(i+1,1), nan];
                l_debt_bust(count_bust(1,1),:,2)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), aggregate_overall_indext(i-2,1), aggregate_overall_indext(i-1,1), aggregate_overall_indext(i,1), aggregate_overall_indext(i+1,1), nan];
                count_bust(1,1)=count_bust(1,1)+1;
            elseif debt_indicator(i,1)==1 & debt_indicator(i-1,1)==1 & debt_indicator(i+1,1)==1
                m_debt_bust(count_bust(1,2),:,1)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), aggregate_xt(i-2,1), aggregate_xt(i-1,1), aggregate_xt(i,1), aggregate_xt(i+1,1), nan];
                m_debt_bust(count_bust(1,2),:,2)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), aggregate_overall_indext(i-2,1), aggregate_overall_indext(i-1,1), aggregate_overall_indext(i,1), aggregate_overall_indext(i+1,1), nan];
                count_bust(1,2)=count_bust(1,2)+1;
            elseif debt_indicator(i,1)==2 & debt_indicator(i-1,1)==2 & debt_indicator(i+1,1)==2
                h_debt_bust(count_bust(1,3),:,1)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), aggregate_xt(i-2,1), aggregate_xt(i-1,1), aggregate_xt(i,1), aggregate_xt(i+1,1), nan];
                h_debt_bust(count_bust(1,3),:,2)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), aggregate_overall_indext(i-2,1), aggregate_overall_indext(i-1,1), aggregate_overall_indext(i,1), aggregate_overall_indext(i+1,1), nan];
                count_bust(1,3)=count_bust(1,3)+1;
            end
   
         elseif boom_bust_indicator(i,3)==2 & boom_bust_indicator(i,2)~=1 & boom_bust_indicator(i,2)~=2 & boom_bust_indicator(i,2)~=16
            if debt_indicator(i,1)==0 & debt_indicator(i-1,1)==0 & debt_indicator(i+1,1)==0
                l_debt_bust(count_bust(1,1),:,1)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), aggregate_xt(i-2,1), aggregate_xt(i-1,1), aggregate_xt(i,1), aggregate_xt(i+1,1), aggregate_xt(i+2,1)];
                l_debt_bust(count_bust(1,1),:,2)=[boom_bust_indicator(i,1) boom_bust_indicator(i,2), aggregate_overall_indext(i-2,1), aggregate_overall_indext(i-1,1), aggregate_overall_indext(i,1), aggregate_overall_indext(i+1,1), aggregate_overall_indext(i+2,1)];
                count_bust(1,1)=count_bust(1,1)+1;
            elseif debt_indicator(i,1)==1 & debt_indicator(i-1,1)==1 & debt_indicator(i+1,1)==1
                m_debt_bust(count_bust(1,2),:,1)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), aggregate_xt(i-2,1), aggregate_xt(i-1,1), aggregate_xt(i,1), aggregate_xt(i+1,1), aggregate_xt(i+2,1)];
                m_debt_bust(count_bust(1,2),:,2)=[boom_bust_indicator(i,1) boom_bust_indicator(i,2), aggregate_overall_indext(i-2,1), aggregate_overall_indext(i-1,1), aggregate_overall_indext(i,1), aggregate_overall_indext(i+1,1), aggregate_overall_indext(i+2,1)];
                count_bust(1,2)=count_bust(1,2)+1;
            elseif debt_indicator(i,1)==2 & debt_indicator(i-1,1)==2 & debt_indicator(i+1,1)==2
                h_debt_bust(count_bust(1,3),:,1)=[boom_bust_indicator(i,1), boom_bust_indicator(i,2), aggregate_xt(i-2,1), aggregate_xt(i-1,1), aggregate_xt(i,1), aggregate_xt(i+1,1), aggregate_xt(i+2,1)];
                h_debt_bust(count_bust(1,3),:,2)=[boom_bust_indicator(i,1) boom_bust_indicator(i,2), aggregate_overall_indext(i-2,1), aggregate_overall_indext(i-1,1), aggregate_overall_indext(i,1), aggregate_overall_indext(i+1,1), aggregate_overall_indext(i+2,1)];
                count_bust(1,3)=count_bust(1,3)+1;
            end
        end
    end
    
    %Calculate average of output gap and overall index for each group
    %BOOM
    l_xt_average_boom(1,:)=[nanmean(l_debt_boom(:,3,1),1) nanmean(l_debt_boom(:,4,1),1) nanmean(l_debt_boom(:,5,1),1) nanmean(l_debt_boom(:,6,1),1) nanmean(l_debt_boom(:,7,1),1)];
    m_xt_average_boom(1,:)=[nanmean(m_debt_boom(:,3,1),1) nanmean(m_debt_boom(:,4,1),1) nanmean(m_debt_boom(:,5,1),1) nanmean(m_debt_boom(:,6,1),1) nanmean(m_debt_boom(:,7,1),1)];
    h_xt_average_boom(1,:)=[nanmean(h_debt_boom(:,3,1),1) nanmean(h_debt_boom(:,4,1),1) nanmean(h_debt_boom(:,5,1),1) nanmean(h_debt_boom(:,6,1),1) nanmean(h_debt_boom(:,7,1),1)];
    
    l_overall_average_boom(1,:)=[nanmean(l_debt_boom(:,3,2),1) nanmean(l_debt_boom(:,4,2),1) nanmean(l_debt_boom(:,5,2),1) nanmean(l_debt_boom(:,6,2),1) nanmean(l_debt_boom(:,7,2),1)];
    m_overall_average_boom(1,:)=[nanmean(m_debt_boom(:,3,2),1) nanmean(m_debt_boom(:,4,2),1) nanmean(m_debt_boom(:,5,2),1) nanmean(m_debt_boom(:,6,2),1) nanmean(m_debt_boom(:,7,2),1)];
    h_overall_average_boom(1,:)=[nanmean(h_debt_boom(:,3,2),1) nanmean(h_debt_boom(:,4,2),1) nanmean(h_debt_boom(:,5,2),1) nanmean(h_debt_boom(:,6,2),1) nanmean(h_debt_boom(:,7,2),1)];

    %BUST
    l_xt_average_bust(1,:)=[nanmean(l_debt_bust(:,3,1),1) nanmean(l_debt_bust(:,4,1),1) nanmean(l_debt_bust(:,5,1),1) nanmean(l_debt_bust(:,6,1),1) nanmean(l_debt_bust(:,7,1),1)];
    m_xt_average_bust(1,:)=[nanmean(m_debt_bust(:,3,1),1) nanmean(m_debt_bust(:,4,1),1) nanmean(m_debt_bust(:,5,1),1) nanmean(m_debt_bust(:,6,1),1) nanmean(m_debt_bust(:,7,1),1)];
    h_xt_average_bust(1,:)=[nanmean(h_debt_bust(:,3,1),1) nanmean(h_debt_bust(:,4,1),1) nanmean(h_debt_bust(:,5,1),1) nanmean(h_debt_bust(:,6,1),1) nanmean(h_debt_bust(:,7,1),1)];
 
    l_overall_average_bust(1,:)=[nanmean(l_debt_bust(:,3,2),1) nanmean(l_debt_bust(:,4,2),1) nanmean(l_debt_bust(:,5,2),1) nanmean(l_debt_bust(:,6,2),1) nanmean(l_debt_bust(:,7,2),1)];
    m_overall_average_bust(1,:)=[nanmean(m_debt_bust(:,3,2),1) nanmean(m_debt_bust(:,4,2),1) nanmean(m_debt_bust(:,5,2),1) nanmean(m_debt_bust(:,6,2),1) nanmean(m_debt_bust(:,7,2),1)];
    h_overall_average_bust(1,:)=[nanmean(h_debt_bust(:,3,2),1) nanmean(h_debt_bust(:,4,2),1) nanmean(h_debt_bust(:,5,2),1) nanmean(h_debt_bust(:,6,2),1) nanmean(h_debt_bust(:,7,2),1)];

    %Calculate standard deviations for index in each group
    %BOOM
    l_overall_std_boom(1,:)=[nanstd(l_debt_boom(:,3,2),0,1) nanstd(l_debt_boom(:,4,2),0,1) nanstd(l_debt_boom(:,5,2),0,1) nanstd(l_debt_boom(:,6,2),0,1) nanstd(l_debt_boom(:,7,2),0,1)];
    m_overall_std_boom(1,:)=[nanstd(m_debt_boom(:,3,2),0,1) nanstd(m_debt_boom(:,4,2),0,1) nanstd(m_debt_boom(:,5,2),0,1) nanstd(m_debt_boom(:,6,2),0,1) nanstd(m_debt_boom(:,7,2),0,1)];
    h_overall_std_boom(1,:)=[nanstd(h_debt_boom(:,3,2),0,1) nanstd(h_debt_boom(:,4,2),0,1) nanstd(h_debt_boom(:,5,2),0,1) nanstd(h_debt_boom(:,6,2),0,1) nanstd(h_debt_boom(:,7,2),0,1)];

    %BUST
    l_overall_std_bust(1,:)=[nanstd(l_debt_bust(:,3,2),0,1) nanstd(l_debt_bust(:,4,2),0,1) nanstd(l_debt_bust(:,5,2),0,1) nanstd(l_debt_bust(:,6,2),0,1) nanstd(l_debt_bust(:,7,2),0,1)];
    m_overall_std_bust(1,:)=[nanstd(m_debt_bust(:,3,2),0,1) nanstd(m_debt_bust(:,4,2),0,1) nanstd(m_debt_bust(:,5,2),0,1) nanstd(m_debt_bust(:,6,2),0,1) nanstd(m_debt_bust(:,7,2),0,1)];
    h_overall_std_bust(1,:)=[nanstd(h_debt_bust(:,3,2),0,1) nanstd(h_debt_bust(:,4,2),0,1) nanstd(h_debt_bust(:,5,2),0,1) nanstd(h_debt_bust(:,6,2),0,1) nanstd(h_debt_bust(:,7,2),0,1)];

    %Create interval confidence
    %BOOM
    for i=1:num_column
        l_upper_bound_boom(1,i)=l_overall_average_boom(1,i)+2*l_overall_std_boom(1,i);
        l_lower_bound_boom(1,i)=l_overall_average_boom(1,i)-2*l_overall_std_boom(1,i);
    end
    
    for i=1:num_column
        m_upper_bound_boom(1,i)=m_overall_average_boom(1,i)+2*m_overall_std_boom(1,i);
        m_lower_bound_boom(1,i)=m_overall_average_boom(1,i)-2*m_overall_std_boom(1,i);
    end

    for i=1:num_column
        h_upper_bound_boom(1,i)=h_overall_average_boom(1,i)+2*h_overall_std_boom(1,i);
        h_lower_bound_boom(1,i)=h_overall_average_boom(1,i)-2*h_overall_std_boom(1,i);
    end

    %BUST
    for i=1:num_column
        l_upper_bound_bust(1,i)=l_overall_average_bust(1,i)+2*l_overall_std_bust(1,i);
        l_lower_bound_bust(1,i)=l_overall_average_bust(1,i)-2*l_overall_std_bust(1,i);
    end
    
    for i=1:num_column
        m_upper_bound_bust(1,i)=m_overall_average_bust(1,i)+2*m_overall_std_bust(1,i);
        m_lower_bound_bust(1,i)=m_overall_average_bust(1,i)-2*m_overall_std_bust(1,i);
    end
 
    for i=1:num_column
        h_upper_bound_bust(1,i)=h_overall_average_bust(1,i)+2*h_overall_std_bust(1,i);
        h_lower_bound_bust(1,i)=h_overall_average_bust(1,i)-2*h_overall_std_bust(1,i);
    end

    %Create matrix for all output gap averages for booms
    xt_average_matrix_boom(:,:,1)=l_xt_average_boom;
    xt_average_matrix_boom(:,:,2)=m_xt_average_boom;
    xt_average_matrix_boom(:,:,3)=h_xt_average_boom;

    %Create matrix for all output gap averages for bust
    xt_average_matrix_bust(:,:,1)=l_xt_average_bust;
    xt_average_matrix_bust(:,:,2)=m_xt_average_bust;
    xt_average_matrix_bust(:,:,3)=h_xt_average_bust;

    %Create matrix for all index averages for booms
    overall_average_matrix_boom(:,:,1)=l_overall_average_boom;
    overall_average_matrix_boom(:,:,2)=m_overall_average_boom;
    overall_average_matrix_boom(:,:,3)=h_overall_average_boom;

    %Create matrix for all index averages for bust
    overall_average_matrix_bust(:,:,1)=l_overall_average_bust;
    overall_average_matrix_bust(:,:,2)=m_overall_average_bust;
    overall_average_matrix_bust(:,:,3)=h_overall_average_bust;

    %Create matrix for all index confidence interval for boom
    overall_interval_matrix_boom(:,:,1)=[l_lower_bound_boom;l_upper_bound_boom];
    overall_interval_matrix_boom(:,:,2)=[m_lower_bound_boom;m_upper_bound_boom];
    overall_interval_matrix_boom(:,:,3)=[h_lower_bound_boom;h_upper_bound_boom];

    %Create matrix for all index confidence interval for bust
    overall_interval_matrix_bust(:,:,1)=[l_lower_bound_bust;l_upper_bound_bust];
    overall_interval_matrix_bust(:,:,2)=[m_lower_bound_bust;m_upper_bound_bust];
    overall_interval_matrix_bust(:,:,3)=[h_lower_bound_bust;h_upper_bound_bust];

    %Calculate # of booms in both debt group
    num_l_boom=size(l_debt_boom(:,1,1),1);
    num_m_boom=size(m_debt_boom(:,1,1),1);
    num_h_boom=size(h_debt_boom(:,1,1),1);

    num_debt_boom=[num_l_boom num_m_boom num_h_boom];
    
    %Calculate # of booms in both debt group
    num_l_bust=size(l_debt_bust(:,1,1),1);
    num_m_bust=size(m_debt_bust(:,1,1),1);
    num_h_bust=size(h_debt_bust(:,1,1),1);
    
    num_debt_bust=[num_l_bust num_m_bust num_h_bust];


end

