-
Notifications
You must be signed in to change notification settings - Fork 1
/
spatial.m
118 lines (81 loc) · 2.54 KB
/
spatial.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%INPUT
% pp undirected adjacency matrix
% dd matrix of distances between each node i and node j of the network
% Nd number of bins Nd that we consider in the distance classification
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%OUTPUT
% S_d canonical entropy
% P link probability matrix
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [S_d, P]=spatial(pp,dd,Nd)
precision=10^(-5);
loops=100000;
n=size(pp,1);
dd=dd-diag(diag(dd));
dmax=max(squareform(dd));
dmin=min(squareform(dd));
%linear binning
l=linspace(dmin,dmax,Nd+1);
l(Nd+1)=dmax+2*eps;
class=zeros(n,n);
for ind = 2:length(l)
if ind==length(l)
class(dd>=l(ind-1) & dd<=l(ind))=ind-1;
break
end
class(dd>=l(ind-1) & dd<l(ind))=ind-1;
end
class=class-diag(diag(class));
connectivity=sum(pp,2);
avg_conn=sum(connectivity)/n;
B=zeros(Nd,1);
for d=1:Nd
B(d)=sum(sum(pp.*(class==d)))/2;
end
% compute exp(Lagrangian multipliers)
% suggested starting conditions
z=connectivity/(sqrt(n*avg_conn));
W=B/(n*avg_conn);
oldW=zeros(Nd,1);
oldz=zeros(n,1);
for kk=1:loops
bigW=zeros(n,n);
for d=1:Nd,
bigW=bigW+W(d)*(class==d);
end
U=ones(n,1)*z' .* bigW;
D=ones(n,n) + ( z*z'.* bigW);
z=connectivity ./ sum(U./D,2);
z=max(z,10^(-15));
B2=zeros(Nd,1);
for d=1:Nd
M=(class==d) .* (z*z') ./ ( ones(n,n) + ((z*z') .* bigW)) ;
B2(d)=(sum(sum(M)) )/2;
if (B2(d)*B(d))>0.0
W(d)=B(d)/(B2(d));
W(d)=max(W(d),10^(-15));
W(d)=min(W(d),10^15);
else
W(d)=0;
end
end
if (max(abs((W>0).*(1-W./(oldW+(oldW==0)))))<precision) && (max(abs((z>0).*(1-z./(oldz+(oldz==0)))))<precision)
break
end
oldW=W; oldz=z;
end
bigW=zeros(n,n);
for d=1:Nd,
bigW=bigW+W(d)*(class==d);
end
%Compute link probability
P=(z*z'.*bigW)./(ones(n,n)+z*z'.*bigW);
P=P-diag(diag(P));
%Compute Shannon entropy
P1=P.*log(P+(P==0));
P2=(ones(n,n)-P).*log(ones(n,n)-P +((ones(n,n)-P)==0));
Smatrix=-triu(P1+P2,1);
S_d=sum(sum(Smatrix));
display(kk)
return