function [boom_matrix,bust_matrix,jump] = identify_boom_bust( x )


% PURPOSE: IDENTIFY BOOM/BUST EPISODES
% 
% INPUT:
%   -x= VARIABLE OF INTEREST WHERE TO IDENTIFY BOOM/BUST EPISODES
%
% OUTPUT
%   -boom_matrix: MATRIX WITH ALL BOOM EPISODES
%   -bust_matrix: MATRIX WITH ALL BUST EPISODES
%   -jump: MATRIX WITH ALL JUMPS, INTREVALS AND DURATIONS


%Recall variables defined in the main script
global num_c schindler_years

    %Counter that assigns 1 when value positive, zero otherwise
    for i=1:num_c
        counter(x(i,:)>0, i)=1;
        counter(x(i,:)<0, i)=0;
    end
    counter_tran=counter';

    %Identify position where there is the change of sign in the output gap and
    %report the first and last position in the schindler index
    for i=1:num_c
        jump(i).pos = [1 find(diff(counter_tran(i,:))~=0) schindler_years];
    end

    %Find and reports the position of the jump when the positions between jumps are less than three
    %Identify change in sign in output gap that lasts more than three periods.
    %The number reported in the structure todel is the corresponding position
    for i=1:num_c
        jump(i).todel = find(diff(jump(i).pos())<3);
    end

    %Delete the positions where the change in sign happens after less than
    %three periods. jump.clean is the structure with the remanng positions of 
    %jumps that last more than three periods 
    for i=1:num_c
        jump(i).clean = jump(i).pos;
        jump(i).clean(jump(i).todel) = []; 
    end

    %Identify # of remaning jumps fpr each countries after having eliminated
    %the nonpersistent jumps
    for i=1:num_c
        num_jump(i,1) = size(jump(i).clean,2);
    end

    %Divide the sample in subsamples using as reference the jumps and
    %create intervals between the jumps and identify the initial and final
    %periods of each interval
    for i=1:num_c
        if size(jump(i).clean,2) == 3
            jump(i).firstinterval = [jump(i).clean(1)  jump(i).clean(2)];  
            jump(i).secondinterval = [jump(i).clean(2)+1  jump(i).clean(3)];
        end
        if size(jump(i).clean,2) == 4
            jump(i).firstinterval = [jump(i).clean(1)  jump(i).clean(2)];  
            jump(i).secondinterval = [jump(i).clean(2)+1  jump(i).clean(3)];
            jump(i).thirdinterval = [jump(i).clean(3)+1  jump(i).clean(4)];
        end
        if size(jump(i).clean,2) == 5
            jump(i).firstinterval = [jump(i).clean(1)  jump(i).clean(2)]; 
            jump(i).secondinterval = [jump(i).clean(2)+1  jump(i).clean(3)];
            jump(i).thirdinterval = [jump(i).clean(3)+1  jump(i).clean(4)];
            jump(i).fourthinterval = [jump(i).clean(4)+1  jump(i).clean(5)];
        end 
    end

    num_intervals=18;
    for i=1:num_intervals
        eval(['idx_' num2str(i) '=zeros(num_c,2)']);
    end
    clc
    
    %Calculate the maximum and minimum in each interval and report the length of
    %the subinterval before and after each jump for all countries in the sample
    %and considering the difference among them in the number of jumps. Finally
    %identify the country that shows the jump
    for i=1:num_c
        %In case of countries with only three jumps (two intervals)
        if size(jump(i).clean,2) == 3
            %First Interval: If the first value of the output gap after the jump is negative
            if  x(i,jump(i).firstinterval(1,1)+1)<0;
                %Identify the minimum in the interval and the position of this value
                [min_1(i,1) idx_1b(i,2)] = min(x(i,jump(i).firstinterval(1,1):jump(i).firstinterval(1,2)));
                %Calculate the length of the interval (# periods) from
                %beginning interval to minimum
                idx_1(i,2) = idx_1b(i,2) + jump(i).firstinterval(1,1) -1;
                %Calculate the length of the interval (# periods) from the jump
                %to the end (duration jump episode)
                idx_1(i,3) = jump(i).firstinterval(1,2) - jump(i).firstinterval(1,1) +1;
                %Identify the country that shows the jump
                idx_1(i,1) = i; 
                %Report value for the miminum value
                idx_1(i,4)=min_1(i,1);
            %First Interval: If the first value of the output gap after the jump is positive
            elseif x(i,jump(i).firstinterval(1,1)+1)>0;
                %Identify the maximum in the interval and the position of this value
                [max_1(i,1) idx_10b(i,2)] = max(x(i,jump(i).firstinterval(1,1):jump(i).firstinterval(1,2)));
                %Calculate the length of the interval (# periods) from
                %beginning interval to maximum
                idx_10(i,2) = idx_10b(i,2) + jump(i).firstinterval(1,1) -1;
                %Calculate the length of the interval (# periods) from the jump
                %to the end (duration of the jump episode)
                idx_10(i,3) = jump(i).firstinterval(1,2) - jump(i).firstinterval(1,1) +1;
                %Identify the country that shows the jump
                idx_10(i,1) = i;
                %Report value for the maximum
                idx_10(i,4)=max_1(i,1);
            end
            %Second Interval: If the first value of the output gap after the jump is negative
            if  x(i,jump(i).secondinterval(1,1))<0
                %Identify the minimum in the interval and the position of this value
                [min_2(i,1) idx_2b(i,2)] = min(x(i,jump(i).secondinterval(1,1):jump(i).secondinterval(1,2)));
                %Calculate the length of the interval (# periods) from beginning interval to jump
                idx_2(i,2) = idx_2b(i,2) + jump(i).secondinterval(1,1) -1;
                %Calculate the length of the interval (# periods) from the jump
                %to the end (duration of the jump episode)
                idx_2(i,3) = jump(i).secondinterval(1,2) - jump(i).secondinterval(1,1) +1;
                %Identify the country that shows the jump
                idx_2(i,1) = i; 
                %Report value for the miminum value
                idx_2(i,4)=min_2(i,1);
            %Second Interval: If the first value of the output gap after the jump is positive
            elseif x(i,jump(i).secondinterval(1,1))>0
                %Identify the minimum in the interval and the position of this value
                [max_2(i,1) idx_11b(i,2)] = max(x(i,jump(i).secondinterval(1,1):jump(i).secondinterval(1,2)));
                %Calculate the length of the interval (# periods) from beginning interval to jump
                idx_11(i,2) = idx_11b(i,2) + jump(i).secondinterval(1,1) -1;
                %Calculate the length of the interval (# periods) from the jump
                %to the end (duration of the jump episode)
                idx_11(i,3) = jump(i).secondinterval(1,2) - jump(i).secondinterval(1,1) +1;
                %Identify the country that shows the jump
                idx_11(i,1) = i;            
                %Report value for the miminum value
                idx_11(i,4)=max_2(i,1);
            end
        end

        %In case of countries with only four jumps (three intervals)
        if size(jump(i).clean,2) == 4
            %First Interval: If the first value of the output gap after the jump is negative
            if   x(i,jump(i).firstinterval(1,1)+1)<0;
                [min_3(i,1) idx_3b(i,2)] = min(x(i,jump(i).firstinterval(1,1):jump(i).firstinterval(1,2)));
                idx_3(i,2) = idx_3b(i,2) + jump(i).firstinterval(1,1) -1;
                idx_3(i,3) = jump(i).firstinterval(1,2) - jump(i).firstinterval(1,1) +1;
                idx_3(i,1) = i;           
                idx_3(i,4)=min_3(i,1);
            %First Interval: If the first value of the output gap after the jump is positive
            elseif x(i,jump(i).firstinterval(1,1)+1)>0;
                [max_3(i,1) idx_12b(i,2)] = max(x(i,jump(i).firstinterval(1,1):jump(i).firstinterval(1,2)));
                idx_12(i,2) = idx_12b(i,2) + jump(i).firstinterval(1,1) -1;
                idx_12(i,3) = jump(i).firstinterval(1,2) - jump(i).firstinterval(1,1) +1;
                idx_12(i,1) = i; 
                idx_12(i,4)=max_3(i,1);
            end
            %Second Interval: If the first value of the output gap after the jump is negative
            if   x(i,jump(i).secondinterval(1,1))<0
                [min_4(i,1) idx_4b(i,2)] = min(x(i,jump(i).secondinterval(1,1):jump(i).secondinterval(1,2)));
                idx_4(i,2) = idx_4b(i,2) + jump(i).secondinterval(1,1) -1;
                idx_4(i,3) = jump(i).secondinterval(1,2) - jump(i).secondinterval(1,1) +1;
                %Identify the country that shows the jump
                idx_4(i,1) = i;
                idx_4(i,4)=min_4(i,1);
            %Second Interval: If the first value of the output gap after the jump is positive
            elseif x(i,jump(i).secondinterval(1,1))>0
                [max_4(i,1) idx_13b(i,2)] = max(x(i,jump(i).secondinterval(1,1):jump(i).secondinterval(1,2)));
                idx_13(i,2) = idx_13b(i,2) + jump(i).secondinterval(1,1) -1;
                idx_13(i,3) = jump(i).secondinterval(1,2) - jump(i).secondinterval(1,1) +1;
                idx_13(i,1) = i;
                idx_13(i,4)=max_4(i,1);
            end
            %Third Interval: If the first value of the output gap after the jump is negative
            if   x(i,jump(i).thirdinterval(1,1))<0
                [min_5(i,1) idx_5b(i,2)] = min(x(i,jump(i).thirdinterval(1,1):jump(i).thirdinterval(1,2)));
                idx_5(i,2) = idx_5b(i,2) + jump(i).thirdinterval(1,1) -1;
                idx_5(i,3) = jump(i).thirdinterval(1,2) - jump(i).thirdinterval(1,1) +1;
                idx_5(i,1) = i; 
                idx_5(i,4)=min_5(i,1);
            %Third Interval: If the first value of the output gap after the jump is positive
            elseif x(i,jump(i).thirdinterval(1,1))>0
                [max_5(i,1) idx_14b(i,2)] = max(x(i,jump(i).thirdinterval(1,1):jump(i).thirdinterval(1,2)));
                idx_14(i,2) = idx_14b(i,2) + jump(i).thirdinterval(1,1) -1;
                idx_14(i,3) = jump(i).thirdinterval(1,2) - jump(i).thirdinterval(1,1) +1;
                idx_14(i,1) = i; 
                idx_14(i,4)=max_5(i,1);
            end
        end

        %In case of countries with only five jumps (four intervals)
        if size(jump(i).clean,2) == 5
            %First Interval: If the first value of the output gap after the jump is negative
            if   x(i,jump(i).firstinterval(1,1)+1)<0;
                [min_6(i,1) idx_6b(i,2)] = min(x(i,jump(i).firstinterval(1,1):jump(i).firstinterval(1,2)));
                idx_6(i,2) = idx_6b(i,2) + jump(i).firstinterval(1,1) -1;
                idx_6(i,3) = jump(i).firstinterval(1,2) - jump(i).firstinterval(1,1) +1;
                idx_6(i,1) = i; 
                idx_6(i,4)=min_6(i,1);
            %First Interval: If the first value of the output gap after the jump is positive
            elseif x(i,jump(i).firstinterval(1,1)+1)>0;
                [max_6(i,1) idx_15b(i,2)] = max(x(i,jump(i).firstinterval(1,1):jump(i).firstinterval(1,2)));
                idx_15(i,2) = idx_15b(i,2) + jump(i).firstinterval(1,1) -1;
                idx_15(i,3) = jump(i).firstinterval(1,2) - jump(i).firstinterval(1,1) +1;
                idx_15(i,1) = i;             
                idx_15(i,4)=max_6(i,1);
            end
            %Second Interval: If the first value of the output gap after the jump is negative
            if   x(i,jump(i).secondinterval(1,1))<0
                [min_7(i,1) idx_7b(i,2)] = min(x(i,jump(i).secondinterval(1,1):jump(i).secondinterval(1,2)));
                idx_7(i,2) = idx_7b(i,2) + jump(i).secondinterval(1,1) -1;
                idx_7(i,3) = jump(i).secondinterval(1,2) - jump(i).secondinterval(1,1) +1;
                idx_7(i,1) = i; 
                idx_7(i,4)=min_7(i,1);
            %Second Interval: If the first value of the output gap after the jump is positive
            elseif x(i,jump(i).secondinterval(1,1))>0
                [max_7(i,1) idx_16b(i,2)] = max(x(i,jump(i).secondinterval(1,1):jump(i).secondinterval(1,2)));
                idx_16(i,2) = idx_16b(i,2) + jump(i).secondinterval(1,1) -1;
                idx_16(i,3) = jump(i).secondinterval(1,2) - jump(i).secondinterval(1,1) +1;
                idx_16(i,1) = i;
                idx_16(i,4)=max_7(i,1);
            end
           %Third Interval: If the first value of the output gap after the jump is negative
           if   x(i,jump(i).thirdinterval(1,1))<0
                [min_8(i,1) idx_8b(i,2)] = min(x(i,jump(i).thirdinterval(1,1):jump(i).thirdinterval(1,2)));
                idx_8(i,2) = idx_8b(i,2) + jump(i).thirdinterval(1,1) -1;
                idx_8(i,3) = jump(i).thirdinterval(1,2) - jump(i).thirdinterval(1,1) +1;
                idx_8(i,1) = i;             
                idx_8(i,4)=min_8(i,1);
           %Third Interval: If the first value of the output gap after the jump is positive
           elseif x(i,jump(i).thirdinterval(1,1))>0
                [max_8(i,1) idx_17b(i,2)] = max(x(i,jump(i).thirdinterval(1,1):jump(i).thirdinterval(1,2)));
                idx_17(i,2) = idx_17b(i,2) + jump(i).thirdinterval(1,1) -1;
                idx_17(i,3) = jump(i).thirdinterval(1,2) - jump(i).thirdinterval(1,1) +1;
                idx_17(i,1) = i;             
                idx_17(i,4)=max_8(i,1);
           end
           %Fourth Interval: If the first value of the output gap after the jump is negative
           if   x(i,jump(i).fourthinterval(1,1))<0
                [min_9(i,1) idx_9b(i,2)] = min(x(i,jump(i).fourthinterval(1,1):jump(i).fourthinterval(1,2)));
                idx_9(i,2) = idx_9b(i,2) + jump(i).fourthinterval(1,1) -1;
                idx_9(i,3) = jump(i).fourthinterval(1,2) - jump(i).fourthinterval(1,1) +1;
                idx_9(i,1) = i;           
                idx_9(i,4)=min_9(i,1);
           %Fourth Interval: If the first value of the output gap after the jump is positive     
           elseif x(i,jump(i).fourthinterval(1,1))>0
                [max_9(i,1) idx_18b(i,2)] = max(x(i,jump(i).fourthinterval(1,1):jump(i).fourthinterval(1,2)));
                idx_18(i,2) = idx_18b(i,2) + jump(i).fourthinterval(1,1) -1;
                idx_18(i,3) = jump(i).fourthinterval(1,2) - jump(i).fourthinterval(1,1) +1;
                idx_18(i,1) = i;            
                idx_18(i,4)=max_9(i,1);
           end
        end
    end

    %Delete all rows were there are not observations
    %Final version of idx vectors contains:
        % First column: respective number of the country that has the jump
        % Second Column: Periods before the jump (period when the jump happens, perido of maximum/minimum)
        % Third Column: Periods after the jump in the same interval (duration of the episode)
        % Fourth Column: Report the values of the maximum/minimum
    %Reference number for the vectors idx indicates the number of intervals and the interval
    for i=1:num_c
        idx_1( ~any(idx_1,2), : ) = [];
        idx_2( ~any(idx_2,2), : ) = [];
        idx_3( ~any(idx_3,2), : ) = [];
        idx_4( ~any(idx_4,2), : ) = [];
        idx_5( ~any(idx_5,2), : ) = [];
        idx_6( ~any(idx_6,2), : ) = [];
        idx_7( ~any(idx_7,2), : ) = [];
        idx_8( ~any(idx_8,2), : ) = [];
        idx_9( ~any(idx_9,2), : ) = [];
        idx_10( ~any(idx_10,2), : ) = [];
        idx_11( ~any(idx_11,2), : ) = [];
        idx_12( ~any(idx_12,2), : ) = [];
        idx_13( ~any(idx_13,2), : ) = [];
        idx_14( ~any(idx_14,2), : ) = [];
        idx_15( ~any(idx_15,2), : ) = [];
        idx_16( ~any(idx_16,2), : ) = [];
        idx_17( ~any(idx_17,2), : ) = [];
        idx_18( ~any(idx_18,2), : ) = [];
    end

    %Create a matrxi for all indexes for minimum
    minimum_mat = [idx_1 ; idx_2 ; idx_3; idx_4; idx_5; idx_6; idx_7; idx_8; idx_9];

    %Create a matrix for all indexes for maximum
    maximum_mat = [idx_10; idx_11; idx_12; idx_13; idx_14; idx_15; idx_16; idx_17; idx_18];

    %Create a matrix for all bustes sorted progressively by country
    bust_matrix = sortrows(minimum_mat);

    %Create a matrix for all booms sorted progressively by country
    boom_matrix = sortrows(maximum_mat);

end

