function [ r, p ] = country_correlation(num_var, x, y )


% PURPOSE: CALCULATE THE COUNTRY SPECIFIC CORRELATION AND P-VALUES BETWEEN 
% OUTPUT GAP AND THE INDEXES
% 
% INPUT:
%    -num_var: NUMBER OF TIME SERIES FOR THE INDEX
%    -x: OUTPUT GAP
%    -y: INDEXES
% 
% OUTPUT:
%     -r: CORRELATION
%     -p: PVALUE


%Recall variable defined in the main script
global num_c

    %Calculate correlation and pvalues
    for j = 1:num_var
        for i=1:num_c
            [r(i,j) p(i,j)] = corr( x(i,:)', y(i,:,j)');
        end
    end

    %Eliminate the NaN values from the correlation vector because Matlab
    %returns this result in case of nonvarying value for the index
    for i=1:num_c
        for j=1:num_var
            if isnan(r(i,j))
                r(i,j)=0;
            end
        end
    end   
    
    r=roundTo(r,2);
end

