%Sign Concordance Index T-MDS (weighted 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);

for i=1:n 
    for j=1:n 
        yo=zeros(m,1);
        for k=1:m 
            if z(k,i)==z(k,j) 
                yo(k,1)=1;
            elseif z(k,i)~=z(k,j)
                yo(k,1)=0;
            end
            wyo = bsxfun(@times,yo,w);
            sumyo=sum(wyo);
        end
        total(i,j)=sumyo;
        if sumyo>=threshold 
            Adjacency(i,j)=1;
        elseif sumyo<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)';

%find dominant nodes
x2=x;
x2(Isolated)=0;
domnumbers=find(x2==1);
domstates=labels(domnumbers)';

%Calculate average node gegree 
cors2=total;
cors2(cors2 <= threshold) = 0;
cors2=cors2-eye(n);
NoofEdges=(nnz(cors2))/2;
CompleteEdges=(n*(n-1))/2;
avgdegree=(2*NoofEdges)/n; %n is the number of countries
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(:));

