%Sign Concordance Index T-MDS (standard version)

%Select threshold
threshold=0.75;

m=size(myset,1); 
n=size(myset,2);

%If output is above the cycle table z=1 otherwise z=-1
z=zeros(m,n);

for i=1:m
    for j=1:n 
        if myset(i,j)>=0 
            z(i,j)=1;
        elseif myset(i,j)<0
            z(i,j)=-1;
        end
    end
end

%Compare the stationary components. If the ratio of equal signs to the
%total number of observations is equal or bigger than the given threshold
%create an edge between the agents (Adjacency matrix cell=1)
Adjacency=zeros(n);
total=zeros(n);

for i=1:n 
    for j=1:n 
        count=0;
        for k=1:m 
            if z(k,i)==z(k,j) 
                count=count+1;
            end
        end
        crit=count/m;
        total(i,j)=crit;
        if crit>=threshold 
            Adjacency(i,j)=1;
        elseif crit<threshold 
            Adjacency(i,j)=0;
        end
    end
end


%Identify the T-MDS---------------------------------
A=Adjacency;
n=length(A);
f = ones(1,n); 
b=ones(n,1); 
intcon=1:n;
lb=zeros(n,1);
ub=ones(n,1);
[x,fval,exitflag,output]= intlinprog(f,intcon,-A,-b,[],[],lb,ub);
S = find(x);
%-------------------------------------------


%Number of isolated nodes and which ones
s=sum(A);
NoofIsol=sum(s(:) == 1); 
Isolated=find(s==1);
Nodedegree=(s-1)'; 

%Calculate average node gegree 
cors2=total;
cors2(cors2 <= threshold) = 0;
cors2=cors2-eye(n);
NoofEdges=(nnz(cors2))/2;
avgdegree=(2*NoofEdges)/n; %n is the number of countries 
CompleteEdges=(n*(n-1))/2;
Density=NoofEdges/CompleteEdges;

%calculate average correlation coefficient
cors3=total;
cors3(cors3<0)=0;
count=0;
sumcors=0;
for i=1:n^2
    if cors3(i)>0 
        count=count+1;
        sumcors=sumcors+cors3(i);
    end
end
avgcor=sumcors/count;

minmaxcors=total-eye(n);
minmaxvalues(1)=min(total(:));
minmaxvalues(2)=max(minmaxcors(:));

