-
Notifications
You must be signed in to change notification settings - Fork 16
/
surprise.m
executable file
·35 lines (31 loc) · 1.03 KB
/
surprise.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
function [S] = surprise(A, ci, base10)
%SURPRISE Compute surprise of a vertex partition on a binary network.
%
%
% Inputs A, undirected binary network. If weighted, weights are
% ignored.
% ci, membership vector
% base10, if nonzero use base10 logarithms
%
% Outputs: S, value of Surprise (is log10 base, to get natural logarithm,
% multiply S by log(10)).
%
% Carlo Nicolini, Istituto Italiano di Tecnologia (2016).
%
if nargin==2
base10=true;
end
if length(unique(A(:))) ~= 2
warning on;
warning('Input matrix is not binary {0,1}. Ignoring edge weights to compute Surprise.');
end
% Get the block matrix
[B,C,~,~,L,M]=comm_mat(A,ci);
nc = sum(C,2); % number of nodes per community
Lin = sum(diag(B)); % number of intracluster edges
Min = sum(nc.*(nc-1)/2); % number of intracluster pairs
[Lin,L,Min,M]
S=compute_surprise(M, Min, L, Lin, base10);
% Compute the binomial surprise
-arrayfun(@(i)(logC(i,L)+i*log(Min/M)+(L-i)*(log(1-Min/M))),Lin)
L*KL(Lin/L, Min/M)