-
Notifications
You must be signed in to change notification settings - Fork 4
/
single_layer_null_models.m
292 lines (240 loc) · 7 KB
/
single_layer_null_models.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
clearvars
close all
clc
addpath('./fcn/')
% visualize the modularity matrix?
viz = 1 ;
%% load data
load ./data/sc_fc_data_singlesubject.mat
d = squareform(pdist(coor));
n = length(sc);
%% example 1. newman girvan
% degree-/strength-preserving null model but allows for self-loops
deg = sum(sc,2);
twom = sum(deg);
p = (deg*deg')/twom;
b = sc - p;
if viz
imagesc(b)
title('degree-/strength-preserving null modularity matrix')
waitforbuttonpress
close
end
% reference:
%
% Newman, M. E., & Girvan, M. (2004). Finding and evaluating community
% structure in networks. Physical review E, 69(2), 026113.
%% example 2. newman girvan (no loops)
% degree-/strength-preserving null model but excludes self-loops. (NOTE:
% this function estimates the expected weight by sampling individual null
% connectivity matrices. it is time consuming.
numrand = 1000;
numiter = 32;
deg = sum(sc,2);
temp = 25;
dec = 0.995;
energyfcn = 'maxpercentchange';
tolerance = 0.05;
maxiter = 1e5;
wr = zeros(n,n,numrand);
for i = 1:numrand
ar = randmio_und(sc,numiter);
wr(:,:,i) = fcn_match_strength(ar,nonzeros(triu(sc,1)),deg,temp,dec,energyfcn,tolerance,maxiter);
subplot(1,2,1);
imagesc(nanmean(wr(:,:,1:i),3));
subplot(1,2,2);
plot(deg,sum(nanmean(wr(:,:,1:i),3),2),'k.');
drawnow;
end
p = nanmean(wr,3);
b = sc - p;
if viz
imagesc(b)
title('newman girvan (no loops) null modularity matrix')
waitforbuttonpress
close
end
%% example 3. uniform
% all connections (except for self-loops) have an expected weight equal to
% that of ``gamma''
gamma = 0.01;
p = ~eye(n)*gamma;
b = sc - p;
if viz
imagesc(b)
title('uniform null modularity matrix')
waitforbuttonpress
close
end
% reference(s):
%
% Bazzi, M., Porter, M. A., Williams, S., McDonald, M., Fenn, D. J., &
% Howison, S. D. (2016). Community detection in temporal multilayer
% networks, with an application to correlation networks.
% Multiscale Modeling & Simulation, 14(1), 1-41.
%
% Traag, V. A., Van Dooren, P., & Nesterov, Y. (2011). Narrow scope for
% resolution-limit-free community detection. Physical Review E, 84(1),
% 016114.
%% example 4. geometric (exponential and probabilistic)
% binary connections are drawn stochastically from a decaying exponential
% distribution. weights are then added inversely proportional to distance.
eta = 0.01;
numiter = 100;
p = zeros(n,n,numiter);
for iter = 1:numiter
edges = find(triu(ones(n),1) > 0);
prob = exp(-eta*d(edges));
m = nnz(sc)/2;
b = zeros(n);
for i = 1:m
c = [0; cumsum(prob)];
r = sum(c(end)*rand > c);
b(edges(r)) = 1;
prob(r) = 0;
end
w = sort(sc(triu(sc > 0)),'ascend');
idx = find(b);
di = d(idx);
[~,jdx] = sort(di,'descend');
q = zeros(n);
q(idx(jdx)) = w;
q = q + q';
p(:,:,iter) = q;
end
p = nanmean(p,3);
b = sc - p;
if viz
imagesc(b)
title('geometric null modularity matrix')
waitforbuttonpress
close
end
% reference(s):
%
% Betzel, R. F., Avena-Koenigsberger, A., Goñi, J., He, Y., De Reus, M.
% A., Griffa, A., ... & Sporns, O. (2016). Generative models of the human
% connectome. Neuroimage, 124, 1054-1064.
%% example 5. minimum wiring
% binary connections are placed in the minimum wiring cost configuration.
% weights are then added inversely proportional to distance.
m = nnz(sc)/2;
edges = find(triu(ones(n),1) > 0);
di = d(edges);
[~,ddx] = sort(di,'ascend');
p = zeros(n);
w = sort(sc(triu(sc > 0)),'descend');
p(edges(ddx(1:m))) = w;
p = p + p';
b = sc - p;
if viz
imagesc(b)
title('minimum wiring null modularity matrix')
waitforbuttonpress
close
end
% reference(s):
%
% Samu, D., Seth, A. K., & Nowotny, T. (2014). Influence of wiring cost
% on the large-scale architecture of human cortical connectivity. PLoS
% Comput Biol, 10(4), e1003557.
%% example 6. degree + space
% a degree-preserving null model in which edge swaps are restricted to
% different classes based on their distances.
numiter = 100;
nbins = 31;
nswap = 1e4;
q = zeros(n,n,numiter);
for iter = 1:numiter
[~,ar] = fcn_match_length_degree_distribution(sc,d,nbins,nswap,true);
q(:,:,iter) = ar + ar';
end
p = nanmean(q,3);
b = sc - p;
if viz
imagesc(b)
title('degree + space null modularity matrix')
waitforbuttonpress
close
end
% reference(s):
%
% Betzel, R. F., & Bassett, D. S. (2018). Specificity and robustness of
% long-distance connections in weighted, interareal connectomes.
% Proceedings of the National Academy of Sciences, 115(21), E4880-E4889.
%% example 7. binary null model
% preserves the exact binary structure of the observed network but assigns
% each edge the mean weight across all edges in the observed network.
avg = mean(nonzeros(sc));
p = (sc > 0)*avg;
b = sc - p;
if viz
imagesc(b)
title('binary null modularity matrix')
waitforbuttonpress
close
end
% reference(s):
%
% Bassett, D. S., Owens, E. T., Porter, M. A., Manning, M. L., & Daniels,
% K. E. (2015). Extraction of force-chain network architecture in
% granular materials using community detection. Soft Matter, 11(14),
% 2731-2744.
%% example 8. signed and weighted (equal weighting of pos/neg contributions)
% handles positive and negative weights separately using newman-girvan null
% model. combines the positive and negative modularity matrices, weighting
% each equally.
fc = fc.*~eye(n);
fcpos = fc.*(fc > 0);
fcneg = -fc.*(fc < 0);
kpos = sum(fcpos,2);
twompos = sum(kpos);
kneg = sum(fcneg,2);
twomneg = sum(kneg);
ppos = (kpos*kpos')/twompos;
pneg = (kneg*kneg')/twomneg;
% wpos = twompos/(twompos + twomneg);
% wneg = twomneg/(twompos + twomneg);
bpos = (fcpos - ppos);
bneg = (fcneg - pneg);
b = bpos/(twompos + twomneg) - bneg/(twompos + twomneg);
if viz
imagesc(b)
title('signed and weighted null modularity matrix')
waitforbuttonpress
close
end
% reference(s):
%
% Gómez, S., Jensen, P., & Arenas, A. (2009). Analysis of community
% structure in networks of correlated data. Physical Review E, 80(1),
% 016114.
%% example 9. signed and weighted (disproportionate weighting of pos/neg contributions)
% handles positive and negative weights separately using newman-girvan null
% model. combines the positive and negative modularity matrices, but
% weights the contribution from positive weights more heavily than that of
% negative weights.
fc = fc.*~eye(n);
fcpos = fc.*(fc > 0);
fcneg = -fc.*(fc < 0);
kpos = sum(fcpos,2);
twompos = sum(kpos);
kneg = sum(fcneg,2);
twomneg = sum(kneg);
ppos = (kpos*kpos')/twompos;
pneg = (kneg*kneg')/twomneg;
wpos = twompos/(twompos + twomneg);
wneg = twomneg/(twompos + twomneg);
bpos = (fcpos - ppos);
bneg = (fcneg - pneg);
b = bpos/(twompos) - bneg/(twompos + twomneg);
if viz
imagesc(b)
title('signed and weighted (unequal wei) null modularity matrix')
waitforbuttonpress
close
end
% reference(s):
%
% Rubinov, M., & Sporns, O. (2011). Weight-conserving characterization of
% complex functional brain networks. Neuroimage, 56(4), 2068-2079.