function [group_sub_corr ] = subindext_corr_group( num_subindex, aggregate_xt_group, aggregate_subindext_matrix_group, aggregate_xt_bond_group, aggregate_bond_group,aggregate_xt_aux1_group, aggregate_subindext_aux1_group,aggregate_xt_aux1_group_bond, aggregate_subindext_aux1_group_bond, aggregate_xt_aux2_group, aggregate_subindext_aux2_group, aggregate_xt_aux2_group_bond, aggregate_subindext_aux2_group_bond)


% PURPOSE: CALCULATE THE CORRELATION FOR THE SUBINDEX IN THE DIFFERENT INCOME GROUPS
% 
% INPUT:
%     -num_subindex: NUMBER OF SUBINDEXES
%     -aggregate_xt_group: AGGREGATED VECTOR FOR OUTPUT GAP
%     -aggregate_subindext_matrix_group: AGGREGATED MATRIX FOR SUBINDEXES
%     -aggregate_xt_bond_group: AGGREGATED VECTOR FOR OUTPUT GAP IN CASE OF BOND
%     -aggregate_bond_group: AGGREGATED MATRIX FOR BOND SUBINDEX
%     -aggregate_xt_aux1_group: FIRST AUXILIARY VECTOR FOR OUTPUT GAP
%     -aggregate_subindext_aux1_group: FIRST AUXILIARY MATRIX FOR SUBINDEXES
%     -aggregate_xt_aux1_group_bond: FIRST AUXILIARY VECTOR FOR OUTPUT GAP FOR BOND INDEX
%     -aggregate_subindext_aux1_group_bond: FIRST AUXILIARY MATRIX FOR BOND INDEX
%     -aggregate_xt_aux2_group: SECOND AUXILIARY VECTOR FOR OUTPUT GAP
%     -aggregate_subindext_aux2_group: SECOND AUXILIARY MATRIX FOR SUBINDEXES
%     -aggregate_xt_aux2_group_bond: SECOND AUXILIARY VECTOR FOR OUTPUT GAP FOR BOND INDEX
%     -aggregate_subindext_aux2_group_bond: SECOND AUXILIARY MATRIX FOR BOND SUBINDEX
% 
% OUTPUT:
%     -group_sub_corr: CORRELATION MATRIX FOR THE COUNTRY GROUP
% 

    %GROUP COUNTRIES AT TIME T=0
    %Calculate correlation for aggregated matrices
    [r_agg_sub_group, p_agg_sub_group, star_agg_sub_group] = aggregate_correlation(num_subindex, aggregate_xt_group, aggregate_subindext_matrix_group);
 
    %Calculate correlation for the bond case
    [r_agg_bond_group, p_agg_bond_group, star_agg_bond_group] = aggregate_correlation(1, aggregate_xt_bond_group, aggregate_bond_group);
 
    %Concatenate values and stars
    r_agg_equity_group=num2str(r_agg_sub_group(:,1));
    r_agg_equity_group=strcat(r_agg_equity_group,star_agg_sub_group(:,1));
 
    r_agg_bond_group=num2str(r_agg_bond_group(:,1));
    r_agg_bond_group=strcat(r_agg_bond_group,star_agg_bond_group(:,1));
 
    r_agg_money_group=num2str(r_agg_sub_group(:,3));
    r_agg_money_group=strcat(r_agg_money_group,star_agg_sub_group(:,3));
 
    r_agg_cinvestment_group=num2str(r_agg_sub_group(:,4));
    r_agg_cinvestment_group=strcat(r_agg_cinvestment_group,star_agg_sub_group(:,4));
 
    r_agg_credit_group=num2str(r_agg_sub_group(:,5));
    r_agg_credit_group=strcat(r_agg_credit_group,star_agg_sub_group(:,5));
 
    r_agg_dinvestment_group=num2str(r_agg_sub_group(:,6));
    r_agg_dinvestment_group=strcat(r_agg_dinvestment_group,star_agg_sub_group(:,6));
 
    %GROUP COUNTRIES AT TIME T=-1 LAG Index
    %Calculate correlation for aggregated matrices
    [r_agg_minus1_sub_group, p_agg_minus1_sub_group, star_agg_minus1_sub_group] = aggregate_correlation(num_subindex, aggregate_xt_aux1_group, aggregate_subindext_aux1_group);
 
    %Calculate correlation for the bond case
    [r_agg_minus1_bond_group, p_agg_minus1_bond_group, star_agg_minus1_bond_group] = aggregate_correlation(1, aggregate_xt_aux1_group_bond, aggregate_subindext_aux1_group_bond);
 
    %Concatenate values and stars
    r_agg_minus1_equity_group=num2str(r_agg_minus1_sub_group(:,1));
    r_agg_minus1_equity_group=strcat(r_agg_minus1_equity_group,star_agg_minus1_sub_group(:,1));
 
    r_agg_minus1_bond_group=num2str(r_agg_minus1_bond_group(:,1));
    r_agg_minus1_bond_group=strcat(r_agg_minus1_bond_group,star_agg_minus1_bond_group(:,1));
 
    r_agg_minus1_money_group=num2str(r_agg_minus1_sub_group(:,3));
    r_agg_minus1_money_group=strcat(r_agg_minus1_money_group,star_agg_minus1_sub_group(:,3));
 
    r_agg_minus1_cinvestment_group=num2str(r_agg_minus1_sub_group(:,4));
    r_agg_minus1_cinvestment_group=strcat(r_agg_minus1_cinvestment_group,star_agg_minus1_sub_group(:,4));
 
    r_agg_minus1_credit_group=num2str(r_agg_minus1_sub_group(:,5));
    r_agg_minus1_credit_group=strcat(r_agg_minus1_credit_group,star_agg_minus1_sub_group(:,5));
 
    r_agg_minus1_dinvestment_group=num2str(r_agg_minus1_sub_group(:,6));
    r_agg_minus1_dinvestment_group=strcat(r_agg_minus1_dinvestment_group,star_agg_minus1_sub_group(:,6));
   
    %GROUP COUNTRIES AT TIME T=+1 LEAD Index
    %Calculate correlation for aggregated matrices
    [r_agg_plus1_sub_group, p_agg_plus1_sub_group, star_agg_plus1_sub_group] = aggregate_correlation(num_subindex, aggregate_xt_aux2_group, aggregate_subindext_aux2_group);
 
    %Calculate correlation for the bond case
    [r_agg_plus1_bond_group, p_agg_plus1_bond_group, star_agg_plus1_bond_group] = aggregate_correlation(1, aggregate_xt_aux2_group_bond, aggregate_subindext_aux2_group_bond);
 
    %Concatenate values and stars
    r_agg_plus1_equity_group=num2str(r_agg_plus1_sub_group(:,1));
    r_agg_plus1_equity_group=strcat(r_agg_plus1_equity_group,star_agg_plus1_sub_group(:,1));
 
    r_agg_plus1_bond_group=num2str(r_agg_plus1_bond_group(:,1));
    r_agg_plus1_bond_group=strcat(r_agg_plus1_bond_group,star_agg_plus1_bond_group(:,1));
 
    r_agg_plus1_money_group=num2str(r_agg_plus1_sub_group(:,3));
    r_agg_plus1_money_group=strcat(r_agg_plus1_money_group,star_agg_plus1_sub_group(:,3));
 
    r_agg_plus1_cinvestment_group=num2str(r_agg_plus1_sub_group(:,4));
    r_agg_plus1_cinvestment_group=strcat(r_agg_plus1_cinvestment_group,star_agg_plus1_sub_group(:,4));
 
    r_agg_plus1_credit_group=num2str(r_agg_plus1_sub_group(:,5));
    r_agg_plus1_credit_group=strcat(r_agg_plus1_credit_group,star_agg_plus1_sub_group(:,5));
 
    r_agg_plus1_dinvestment_group=num2str(r_agg_plus1_sub_group(:,6));
    r_agg_plus1_dinvestment_group=strcat(r_agg_plus1_dinvestment_group,star_agg_plus1_sub_group(:,6));
    
    group_sub_corr(:,:,1)=[r_agg_minus1_equity_group r_agg_equity_group r_agg_plus1_equity_group];
    group_sub_corr(:,:,2)=[r_agg_minus1_bond_group r_agg_bond_group r_agg_plus1_bond_group];
    group_sub_corr(:,:,3)=[r_agg_minus1_money_group r_agg_money_group r_agg_plus1_money_group];
    group_sub_corr(:,:,4)=[r_agg_minus1_cinvestment_group r_agg_cinvestment_group r_agg_plus1_cinvestment_group];
    group_sub_corr(:,:,5)=[r_agg_minus1_credit_group r_agg_credit_group r_agg_plus1_credit_group];
    group_sub_corr(:,:,6)=[r_agg_minus1_dinvestment_group r_agg_dinvestment_group r_agg_plus1_dinvestment_group];
 
end

