-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathoverlapping_community_detection.m
62 lines (51 loc) · 2.21 KB
/
overlapping_community_detection.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
function [degree_correction, community_affiliation, community_detection] = overlapping_community_detection(G, p, niter)
% OVERLAPPING_COMMUNITY_DETECTION finds latent (overlapping) communities in a network
% -------------------------------------------------------------------------
% INPUTS
% - G: sparse logical adjacency matrix of size n x n
% - p: positive integer. number of features
% Optional input:
% - niter: number of MCMC iterations
%
% OUTPUTS
% - degree_correction: vector of size n x 1 of positive reals corresponding to the degree correction
% - community_affiliation: matrix of size n x p; entry (i,k) in (0,1) corresponds to
% the level of affiliation of node i to community k
% - community_detection: vector of integers of size n; index of the
% community for which the node has highest level of affiliation
% -------------------------------------------------------------------------
% EXAMPLE
% n= 5;
% G = sparse(logical([ones(n)-eye(n), zeros(n); zeros(n), ones(n)-eye(n)]));
% p = 2; niter = 2000;
% [degree_correction, community_affiliation, community_detection] = overlapping_community_detection(G, p, niter);
% Copyright (c) A. Todeschini (Inria), X. Miscouridou (University of Oxford)
% and F. Caron (University of Oxford)%
% November 2017
%--------------------------------------------------------------------------
if nargin < 3
niter=100000;
end
niterinit = 2000;
nsamples = 500;
nburn = floor(niter/2);
thin = ceil((niter-nburn)/nsamples);
verbose = true;
nchains = 1;
% CGGP graph model with p communities
objprior = graphmodel('CGGP', p);
% Create the graphMCMC object
objmcmc = graphmcmc(objprior, niter, nburn, thin, nchains);
% Run initialisation
init = graphinit(objmcmc, G, niterinit);
% Run MCMC sampler
objmcmc = graphmcmcsamples(objmcmc, G, verbose, init);
% Point estimation of the model parameters
[estimates, ~] = graphest(objmcmc);
degree_correction = sum(estimates.w,2);
community_affiliation = bsxfun(@rdivide, estimates.w, degree_correction);
[~, community_detection] = max(estimates.w, [],2); % Assign each node to the feature with highest weight
end