function [ average_corr, std_corr, confidence_int ] = average_country_correlation( num_var, r, corr_adv, corr_mid, corr_poor)

% 
% PURPOSE: CALCULATE THE AVERAGES, STANDARD DEVIATION AND CONFIDENCE INTERVAL
% FOR THE CORRELATIONS BETWEEN INDEX AND OUTPUT GAP AT DIFFERENT TIME HORIZONS 
% IN EACH COUNTRY GROUP
% 
% INPUT:
%     -num_var: NUMBER OF VARIABLES
%     -r: CORRELATION FOR ALL COUNTRIES
%     -corr_adv: CORRELATION FOR ADVANCED ECONOMIES
%     -corr_mid: CORRELATION FOR EMERGING MARKETS
%     -corr_poor: CORRELATION FOR DEVELOPING ECONOMIES
% 
% OUTPUT:
%     -average_corr: AVERAGE CORRELATION MATRIX
%     -std_corr: STANDARD DEVIATION MATRIX
%     -confidence_int: CONFIDENCE INTERVAL MATRIX
    
    
    for i=1:num_var
        %Average over all countries of the correlations between index and output gap
        average_corr(:,:,i)=[mean(r(:,i),1); mean(corr_adv(:,i),1); mean(corr_mid(:,i),1); mean(corr_poor(:,i),1)];

        %Standard deviation over all countries of the correlations between index and output gap
        std_corr(:,:,i)=[nanstd(r(:,i),1); nanstd(corr_adv(:,i),1); nanstd(corr_mid(:,i),1); nanstd(corr_poor(:,i),1)];

        %Confidence interval over all countries of the correlations between index and output gap
        confidence_int(:,:,i)=[average_corr(1,1,i)-2*std_corr(1,1,i), average_corr(1,1,i)+2*std_corr(1,1,i)
            average_corr(2,1,i)-2*std_corr(2,1,i), average_corr(2,1,i)+2*std_corr(2,1,i)
            average_corr(3,1,i)-2*std_corr(3,1,i), average_corr(3,1,i)+2*std_corr(3,1,i)
            average_corr(4,1,i)-2*std_corr(4,1,i), average_corr(4,1,i)+2*std_corr(4,1,i)];
    end
    
    average_corr=roundTo(average_corr,2);
    confidence_int=roundTo(confidence_int,2);
    
end

