-
Notifications
You must be signed in to change notification settings - Fork 16
/
significance.m
executable file
·50 lines (45 loc) · 1.29 KB
/
significance.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
function S = significance(A, ci)
%SIGNIFICANCE Compute significance of a vertex partition on a binary network.
%
%
% Inputs A, undirected binary network. If weighted, weights are
% ignored.
% ci, membership vector
%
% Outputs: S, value of Significance.
%
% Carlo Nicolini, Istituto Italiano di Tecnologia (2015).
%
if length(unique(A(:))) ~= 2
warning on;
warning('Input matrix is not binary {0,1}. Ignoring edge weights to compute Surprise.');
end
groups = membership2groups(ci); % convert membership vector to groups
ncomms = length(groups); % number of communities
% Significance value
S = 0;
% Density of the graph
d = density_und(A);
for c=1:ncomms
nodes = groups{c}; % nodes in the community c
nc = length(nodes); % number of nodes in community c
if nc>1
%mc = sum(sum(triu(A(nodes,nodes))));
dc = density_und(A(nodes,nodes)); % Density of community c
pc = nc*(nc-1)/2; % number of nodes pairs in community c
S = S + pc*KL(dc,d); % increment significance
end
end
S = 2*S; % to be compliant with the Traag implementation
function D = KL(q,p)
if (q==p)
D=0;
return;
end
D = 0.0;
if (q > 0.0 && p > 0.0)
D = D + q*log(q/p);
end
if (q < 1.0 && p < 1.0)
D = D + (1.0-q)*log((1.0-q)/(1.0-p));
end