-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen_model_add.m
897 lines (726 loc) · 25.6 KB
/
gen_model_add.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
function [B,b] = gen_model_add(A,PDMs,m,modeltype,modelvar,PDMsParam,TopoParam,normType,epsilon)
% gen_model_add_normmax Run generative model code for the additive
% model normalising by the max for each term
%
% Generates synthetic networks using the models described in the study by
% Oldham et al (2022; 10.1126/sciadv.abm6127)
%
% Inputs:
% A, binary network of seed connections
% PDMs, Euclidean distance/fiber length/node similarity
% matrix. Multiple can be input either as a cell,
% where each cell contains a different matrix or as a
% 3D matrix (n*n*nPD, where n is the number of nodes
% and nPD is the number of PD matrices).
% m, number of connections that should be present in
% final synthetic network
% modeltype, specifies the generative rule (see below)
% modelvar, specifies whether the generative rules are based on
% power-law or exponential relationship
% ({'powerlaw'}|{'exponential})
% PDMsParam, the parameters controlling the values in PDMs.
% PDMsParam(1,:) are the exponent values (i.e., the
% parameter in the power-law or exponential). The
% index of each column corresponds to the index in
% PDMs. PDMsParam(2,:) are the alpha values
% TopoParam, the parameters controlling the topological term.
% TopoParam(1,1) are the exponent values (i.e., the
% parameter in the power-law or exponential). The
% TopoParam(2,1) are the alpha values
% normType, either 'max' or 'sum', this will be the type of
% normalisation performed within each respective
% term. The default is 'max'
% epsilon, the baseline probability of forming a particular
% connection (should be a very small number
% {default = 0}).
%
% Output:
% B, an adjacency matrix
% b, a vector giving the index of each edge in B. Note
% that the ordering of b shows which edges formed
% first (e.g., b(1) was the fiorst edge to form, b(2)
% the second etc etc).
%
% Full list of model types:
% (each model type realizes a different generative rule)
%
% 1. 'sptl' spatial model
% 2. 'neighbors' number of common neighbors
% 3. 'matching' matching index
% 4. 'clu-avg' average clustering coeff.
% 5. 'clu-min' minimum clustering coeff.
% 6. 'clu-max' maximum clustering coeff.
% 7. 'clu-diff' difference in clustering coeff.
% 8. 'clu-prod' product of clustering coeff.
% 9. 'deg-avg' average degree
% 10. 'deg-min' minimum degree
% 11. 'deg-max' maximum degree
% 12. 'deg-diff' difference in degree
% 13. 'deg-prod' product of degree
% 14. 'com' communicability
%
% How to convert b to B:
% n = length(A); B = zeros(n); B(b(:,i)) = 1; B = B + B';
%
% Reference: Betzel et al (2016) Neuroimage 124:1054-64.
% Oldham et al (2022) Science Advances 10.1126/sciadv.abm6127
% Richard Betzel, Indiana University/University of Pennsylvania, 2015
% Edited by Stuart Oldham, Monash University 2021
if ~exist('epsilon','var')
epsilon = 0;
end
if ~exist('normType','var')
normType = 'max';
end
n = length(A);
if iscell(PDMs)
PDMs_ = PDMs;
nPDMs = length(PDMs);
PDMs = zeros(n,n,nPDMs);
for i = 1:nPDMs
PDMs(:,:,i) = PDMs_{i};
end
else
nPDMs = size(PDMs,3);
end
PDMs=bsxfun(@times, ~eye(size(PDMs(:,:,1))), PDMs);
if nPDMs == 1
modelvar{1} = {modelvar{1}};
end
if size(PDMsParam,1) == 1
PDMsParam(2,nPDMs) = 1;
end
if size(TopoParam,1) == 1
TopoParam(2,1) = 1;
end
PDMsParam(isnan(PDMsParam)) = 0;
TopoParam(isnan(TopoParam)) = 0;
switch modeltype
case 'clu-avg'
clu = clustering_coef_bu(A);
Kseed = bsxfun(@plus,clu(:,ones(1,n)),clu')/2;
b = fcn_clu(modeltype,A,Kseed,PDMs,m,PDMsParam,TopoParam,modelvar,epsilon,normType);
case 'clu-diff'
clu = clustering_coef_bu(A);
Kseed = abs(bsxfun(@minus,clu(:,ones(1,n)),clu'));
b = fcn_clu(modeltype,A,Kseed,PDMs,m,PDMsParam,TopoParam,modelvar,epsilon,normType);
case 'clu-max'
clu = clustering_coef_bu(A);
Kseed = bsxfun(@max,clu(:,ones(1,n)),clu');
b = fcn_clu(modeltype,A,Kseed,PDMs,m,PDMsParam,TopoParam,modelvar,epsilon,normType);
case 'clu-min'
clu = clustering_coef_bu(A);
Kseed = bsxfun(@min,clu(:,ones(1,n)),clu');
b = fcn_clu(modeltype,A,Kseed,PDMs,m,PDMsParam,TopoParam,modelvar,epsilon,normType);
case 'clu-prod'
clu = clustering_coef_bu(A);
Kseed = clu*clu';
b = fcn_clu(modeltype,A,Kseed,PDMs,m,PDMsParam,TopoParam,modelvar,epsilon,normType);
case 'deg-avg'
kseed = sum(A,2);
Kseed = bsxfun(@plus,kseed(:,ones(1,n)),kseed')/2;
b = fcn_deg(modeltype,A,Kseed,PDMs,m,PDMsParam,TopoParam,modelvar,epsilon,normType);
case 'deg-diff'
kseed = sum(A,2);
Kseed = abs(bsxfun(@minus,kseed(:,ones(1,n)),kseed'));
b = fcn_deg(modeltype,A,Kseed,PDMs,m,PDMsParam,TopoParam,modelvar,epsilon,normType);
case 'deg-max'
kseed = sum(A,2);
Kseed = bsxfun(@max,kseed(:,ones(1,n)),kseed');
b = fcn_deg(modeltype,A,Kseed,PDMs,m,PDMsParam,TopoParam,modelvar,epsilon,normType);
case 'deg-min'
kseed = sum(A,2);
Kseed = bsxfun(@min,kseed(:,ones(1,n)),kseed');
b = fcn_deg(modeltype,A,Kseed,PDMs,m,PDMsParam,TopoParam,modelvar,epsilon,normType);
case 'deg-prod'
kseed = sum(A,2);
Kseed = (kseed*kseed').*~eye(n);
b = fcn_deg(modeltype,A,Kseed,PDMs,m,PDMsParam,TopoParam,modelvar,epsilon,normType);
case 'neighbors'
Kseed = (A*A).*~eye(n);
b = fcn_nghbrs(A,Kseed,PDMs,m,PDMsParam,TopoParam,modelvar,epsilon,normType);
case 'matching'
Kseed = matching_ind_und(A);
b = fcn_matching(A,Kseed,PDMs,m,PDMsParam,TopoParam,modelvar,epsilon,normType);
case 'sptl'
b = fcn_sptl(A,PDMs,m,PDMsParam,modelvar,normType);
end
B = zeros(n);
B(b) = 1;
B = B + B';
function b = fcn_clu(modeltype,A,K,PDMs,m,PDMsParam,TopoParam,modelvar,epsilon,normType)
K = K + epsilon;
n = length(A);
mseed = nnz(A)/2;
A = A > 0;
mv2 = modelvar{2};
[u,v] = find(triu(ones(n),1));
indx = (v - 1)*n + u;
EdgesTriu = zeros(size(indx));
b = zeros(m,1);
b(1:mseed) = find(A(indx));
EdgesTriu(b(b~=0)) = 1;
PDMsmv = modelvar{1};
PDf = zeros(size(PDMs));
Df1 = zeros(size(PDMs));
for j = 1:length(PDMsmv)
mv1 = PDMsmv{j};
switch mv1
case 'powerlaw'
Df1(:,:,j) = PDMs(:,:,j).^PDMsParam(1,j);
case 'exponential'
Df1(:,:,j) = exp(PDMsParam(1,j)*(PDMs(:,:,j))).*~eye(n);
end
Df = Df1(:,:,j);
switch normType
case 'max'
PDf(:,:,j) = PDMsParam(2,j)*(Df./max(Df.*~A,[],'all'));
case 'sum'
PDf(:,:,j) = PDMsParam(2,j)*(Df./(sum(Df.*~A,'all')/2));
end
end
Fd = sum(PDf,3);
switch mv2
case 'powerlaw'
Kf = K.^TopoParam(1,1);
case 'exponential'
Kf = exp(TopoParam(1,1)*K);
end
Kf(isinf(Kf)) = 0;
switch normType
case 'max'
Fk = TopoParam(2,1)*(Kf./max(Kf.*~A,[],'all'));
case 'sum'
Fk = TopoParam(2,1)*(Kf./(sum(Kf.*~A,'all')/2));
end
Fk(isnan(Fk)) = 0;Fk(isinf(Fk)) = 0;
c = clustering_coef_bu(A);
k = sum(A,2);
Ff = (Fd+Fk).*~A;
P = Ff(indx);
for i = (mseed + 1):m
C = [0; cumsum(P)];
r = sum(rand*C(end) >= C);
b(i) = r;
EdgesTriu(b(i)) = 1;
uu = u(r);
vv = v(r);
A(uu,vv) = 1;
A(vv,uu) = 1;
k([uu,vv]) = k([uu,vv]) + 1;
bu = A(uu,:);
su = A(bu,bu);
bv = A(vv,:);
sv = A(bv,bv);
bth = bu & bv;
c(bth) = c(bth) + 2./(k(bth).^2 - k(bth));
c(uu) = nnz(su)/(k(uu)*(k(uu) - 1));
c(vv) = nnz(sv)/(k(vv)*(k(vv) - 1));
c(k <= 1) = 0;
bth([uu,vv]) = true;
switch modeltype
case 'clu-avg'
K(:,bth) = bsxfun(@plus,c(:,ones(1,sum(bth))),c(bth,:)')/2 + epsilon;
K(bth,:) = bsxfun(@plus,c(:,ones(1,sum(bth))),c(bth,:)')'/2 + epsilon;
case 'clu-diff'
K(:,bth) = abs(bsxfun(@minus,c(:,ones(1,sum(bth))),c(bth,:)')) + epsilon;
K(bth,:) = abs(bsxfun(@minus,c(:,ones(1,sum(bth))),c(bth,:)'))' + epsilon;
case 'clu-max'
K(:,bth) = bsxfun(@max,c(:,ones(1,sum(bth))),c(bth,:)') + epsilon;
K(bth,:) = bsxfun(@max,c(:,ones(1,sum(bth))),c(bth,:)')' + epsilon;
case 'clu-min'
K(:,bth) = bsxfun(@min,c(:,ones(1,sum(bth))),c(bth,:)') + epsilon;
K(bth,:) = bsxfun(@min,c(:,ones(1,sum(bth))),c(bth,:)')' + epsilon;
case 'clu-prod'
K(bth,:) = (c(bth,:)*c') + epsilon;
K(:,bth) = (c*c(bth,:)') + epsilon;
end
switch mv2
case 'powerlaw'
Fk = K.^TopoParam(1,1);
case 'exponential'
Fk = exp(TopoParam(1,1)*K);
end
Fk(isinf(Fk)) = 0;
switch normType
case 'max'
for j = 1:length(PDMsmv)
Df = Df1(:,:,j);
PDf(:,:,j) = PDMsParam(2,j)*(Df./max(Df.*~A,[],'all'));
end
Fd = sum(PDf,3);
MaxNorm = max(Fk.*~A,[],'all');
if MaxNorm == 0
TopoTerm = 0;
else
TopoTerm = TopoParam(2,1)*(Fk./MaxNorm).*~A;
end
case 'sum'
for j = 1:length(PDMsmv)
Df = Df1(:,:,j);
PDf(:,:,j) = PDMsParam(2,j)*(Df./(sum(Df.*~A,'all')/2));
end
SumNorm = sum(Fk.*~A,'all')/2;
if SumNorm == 0
TopoTerm = 0;
else
TopoTerm = TopoParam(2,1)*(Fk./SumNorm).*~A;
end
end
Ff = (Fd+TopoTerm).*~A;
P = Ff(indx);
end
b = indx(b);
function b = fcn_deg(modeltype,A,K,PDMs,m,PDMsParam,TopoParam,modelvar,epsilon,normType)
n = length(A);
mseed = nnz(A)/2;
k = sum(A,2);
[u,v] = find(triu(ones(n),1));
indx = (v - 1)*n + u;
EdgesTriu = zeros(size(indx));
b = zeros(m,1);
b(1:mseed) = find(A(indx));
EdgesTriu(b(b~=0)) = 1;
K = K + epsilon;
mv2 = modelvar{2};
PDMsmv = modelvar{1};
PDf = zeros(size(PDMs));
Df1 = zeros(size(PDMs));
for j = 1:length(PDMsmv)
mv1 = PDMsmv{j};
switch mv1
case 'powerlaw'
Df1(:,:,j) = PDMs(:,:,j).^PDMsParam(1,j);
case 'exponential'
Df1(:,:,j) = exp(PDMsParam(1,j)*(PDMs(:,:,j))).*~eye(n);
end
Df = Df1(:,:,j);
switch normType
case 'max'
PDf(:,:,j) = PDMsParam(2,j)*(Df./max(Df.*~A,[],'all'));
case 'sum'
PDf(:,:,j) = PDMsParam(2,j)*(Df./(sum(Df.*~A,'all')/2));
end
end
Fd = sum(PDf,3);
switch mv2
case 'powerlaw'
Kf = K.^TopoParam(1,1);
case 'exponential'
Kf = exp(TopoParam(1,1)*K);
end
Kf(isinf(Kf)) = 0;
switch normType
case 'max'
Fk = TopoParam(2,1)*(Kf./max(Kf.*~A,[],'all'));
case 'sum'
Fk = TopoParam(2,1)*(Kf./(sum(Kf.*~A,'all')/2));
end
Fk(isnan(Fk)) = 0;Fk(isinf(Fk)) = 0;
P = (Fd(indx)+Fk(indx)).*~A(indx);
for i = (mseed + 1):m
C = [0; cumsum(P)];
r = sum(rand*C(end) >= C);
w = [u(r),v(r)];
k(w) = k(w) + 1;
switch modeltype
case 'deg-avg'
K(:,w) = [((k + k(w(1)))/2) + epsilon, ((k + k(w(2)))/2) + epsilon];
K(w,:) = ([((k + k(w(1)))/2) + epsilon, ((k + k(w(2)))/2) + epsilon])';
case 'deg-diff'
K(:,w) = (abs([k - k(w(1)), k - k(w(2))]) + epsilon);
K(w,:) = (abs([k - k(w(1)), k - k(w(2))]) + epsilon)';
case 'deg-min'
K(:,w) = [min(k,k(w(1))) + epsilon, min(k,k(w(2))) + epsilon];
K(w,:) = [min(k,k(w(1))) + epsilon, min(k,k(w(2))) + epsilon]';
case 'deg-max'
K(:,w) = [max(k,k(w(1))) + epsilon, max(k,k(w(2))) + epsilon];
K(w,:) = [max(k,k(w(1))) + epsilon, max(k,k(w(2))) + epsilon]';
case 'deg-prod'
K(:,w) = [k*k(w(1)) + epsilon, k*k(w(2)) + epsilon];
K(w,:) = [k*k(w(1)) + epsilon, k*k(w(2)) + epsilon]';
end
b(i) = r;
EdgesTriu(b(i)) = 1;
switch mv2
case 'powerlaw'
Fk = K.^TopoParam(1,1);
case 'exponential'
Fk = exp(TopoParam(1,1)*K);
end
Fk(isinf(Fk)) = 0;
switch normType
case 'max'
for j = 1:length(PDMsmv)
Df = Df1(:,:,j);
PDf(:,:,j) = PDMsParam(2,j)*(Df./max(Df.*~A,[],'all'));
end
Fd = sum(PDf,3);
MaxNorm = max(Fk.*~A,[],'all');
if MaxNorm == 0
TopoTerm = 0;
else
TopoTerm = TopoParam(2,1)*(Fk./MaxNorm).*~A;
end
case 'sum'
for j = 1:length(PDMsmv)
Df = Df1(:,:,j);
PDf(:,:,j) = PDMsParam(2,j)*(Df./(sum(Df.*~A,'all')/2));
end
SumNorm = sum(Fk.*~A,'all')/2;
if SumNorm == 0
TopoTerm = 0;
else
TopoTerm = TopoParam(2,1)*(Fk./SumNorm).*~A;
end
end
Fp = Fd + TopoTerm;
P = Fp(indx);
P(b(1:i)) = 0;
end
b = indx(b);
function b = fcn_nghbrs(A,K,PDMs,m,PDMsParam,TopoParam,modelvar,epsilon,normType)
K = K + epsilon;
n = length(A);
mseed = nnz(A)/2;
A = A > 0;
mv2 = modelvar{2};
[u,v] = find(triu(ones(n),1));
indx = (v - 1)*n + u;
EdgesTriu = zeros(size(indx));
b = zeros(m,1);
b(1:mseed) = find(A(indx));
EdgesTriu(b(b~=0)) = 1;
PDMsmv = modelvar{1};
PDf = zeros(size(PDMs));
Df1 = zeros(size(PDMs));
for j = 1:length(PDMsmv)
mv1 = PDMsmv{j};
switch mv1
case 'powerlaw'
Df1(:,:,j) = PDMs(:,:,j).^PDMsParam(1,j);
case 'exponential'
Df1(:,:,j) = exp(PDMsParam(1,j)*(PDMs(:,:,j))).*~eye(n);
end
Df = Df1(:,:,j);
switch normType
case 'max'
PDf(:,:,j) = PDMsParam(2,j)*(Df./max(Df.*~A,[],'all'));
case 'sum'
PDf(:,:,j) = PDMsParam(2,j)*(Df./(sum(Df.*~A,'all')/2));
end
end
Fd = sum(PDf,3);
switch mv2
case 'powerlaw'
Kf = K.^TopoParam(1,1);
case 'exponential'
Kf = exp(TopoParam(1,1)*K);
end
Kf(isinf(Kf)) = 0;
switch normType
case 'max'
Fk = TopoParam(2,1)*(Kf./max(Kf.*~A,[],'all'));
case 'sum'
Fk = TopoParam(2,1)*(Kf./(sum(Kf.*~A,'all')/2));
end
Fk(isnan(Fk)) = 0;Fk(isinf(Fk)) = 0;
Ff = (Fd+Fk).*~A;
P = Ff(indx);
for i = (mseed + 1):m
C = [0; cumsum(P)];
r = sum(rand*C(end) >= C);
b(i) = r;
EdgesTriu(b(i)) = 1;
uu = u(r);
vv = v(r);
x = A(uu,:);
y = A(:,vv);
A(uu,vv) = 1;
A(vv,uu) = 1;
K(uu,y) = K(uu,y) + 1;
K(y,uu) = K(y,uu) + 1;
K(vv,x) = K(vv,x) + 1;
K(x,vv) = K(x,vv) + 1;
switch mv2
case 'powerlaw'
Fk = K.^TopoParam(1,1);
case 'exponential'
Fk = exp(TopoParam(1,1)*K);
end
Fk(isinf(Fk)) = 0;
switch normType
case 'max'
for j = 1:length(PDMsmv)
Df = Df1(:,:,j);
PDf(:,:,j) = PDMsParam(2,j)*(Df./max(Df.*~A,[],'all'));
end
Fd = sum(PDf,3);
MaxNorm = max(Fk.*~A,[],'all');
if MaxNorm == 0
TopoTerm = 0;
else
TopoTerm = TopoParam(2,1)*(Fk./MaxNorm).*~A;
end
case 'sum'
for j = 1:length(PDMsmv)
Df = Df1(:,:,j);
PDf(:,:,j) = PDMsParam(2,j)*(Df./(sum(Df.*~A,'all')/2));
end
SumNorm = sum(Fk.*~A,'all')/2;
if SumNorm == 0
TopoTerm = 0;
else
TopoTerm = TopoParam(2,1)*(Fk./SumNorm).*~A;
end
end
Ff = Fd + TopoTerm;
Ff(A) = 0;
P = Ff(indx);
end
b = indx(b);
function b = fcn_matching(A,K,PDMs,m,PDMsParam,TopoParam,modelvar,epsilon,normType)
% Convert input matrix A to a logical
A = A > 0;
% Add epsilon to matrix K, where K is the inital matching matrix
K = K + epsilon;
% Get the length of vector Fd. Fd is the distance matrix with the
% powerlaw/exponential decay already applied
n = length(A);
% Calculate the number of edges to add (non-zero elements in A divided by 2)
mseed = nnz(A)/2;
% Find the upper triangular indices of a ones matrix
[u,v] = find(triu(ones(n),1));
% Calculate indices for an n*n matrix
indx = (v - 1)*n + u;
% Initialize output vector b
b = zeros(m,1);
% Assign the indices of existing edges in b
b(1:mseed) = find(A(indx));
EdgesTriu = zeros(size(indx));
EdgesTriu(b(b~=0)) = 1;
PDMsmv = modelvar{1};
PDf = zeros(size(PDMs));
Df1 = zeros(size(PDMs));
for j = 1:length(PDMsmv)
mv1 = PDMsmv{j};
switch mv1
case 'powerlaw'
Df1(:,:,j) = PDMs(:,:,j).^PDMsParam(1,j);
case 'exponential'
Df1(:,:,j) = exp(PDMsParam(1,j)*(PDMs(:,:,j))).*~eye(n);
end
Df = Df1(:,:,j);
switch normType
case 'max'
PDf(:,:,j) = PDMsParam(2,j)*(Df./max(Df.*~A,[],'all'));
case 'sum'
PDf(:,:,j) = PDMsParam(2,j)*(Df./(sum(Df.*~A,'all')/2));
end
end
Fd = sum(PDf,3);
mv2 = modelvar{2};
switch mv2
case 'powerlaw'
Fk = K.^TopoParam(1,1);
case 'exponential'
Fk = exp(TopoParam(1,1)*K);
end
Fk(isinf(Fk)) = 0;
switch normType
case 'max'
TopoTerm = TopoParam(2,1)*(Fk./max(Fk(indx(~EdgesTriu))));
case 'sum'
TopoTerm = TopoParam(2,1)*(Fk./sum(Fk(indx(~EdgesTriu))));
end
TopoTerm(isnan(TopoTerm)) = 0;TopoTerm(isinf(TopoTerm)) = 0;
% Calculate the degree of each node in A
deg = sum(A);
% Create a matrix degmat by replicating deg as columns
degmat = repmat(deg,n,1);
% Transpose degmat
degmat_ = degmat';
% Calculate the sum of degmat and degmat_. This give the summed degree of
% every pair of nodes
degmat_sum = degmat + degmat_;
% The above can also be achieved via the following
% deg = sum(A);
% degmat_sum = (deg+deg').*~eye(n);
% Calculate the neighbor matrix nei by multiplying A with its transpose
nei = (A*A);
% The additive form would look like this I think
Fp = (Fd + TopoTerm).*~A;
% Get probabilities P based on Ff and indx (extracts each edge only once)
P = Fp(indx);
% Loop to find additional edges
for ii = (mseed + 1):m
% Create a cumulative probability distribution
C = [0; cumsum(P)];
% Select a random value based on the cumulative distribution
r = sum(rand*C(end) >= C);
% Assign the selected value to b(ii). In this case r represnts the
% index of the edge to add
b(ii) = r;
EdgesTriu(b(ii)) = 1;
% Get the corresponding node from vector u
uu = u(r);
% Get the corresponding node from vector v
vv = v(r);
% Extract the uu-th row of A
uu_nei = A(uu,:);
% Extract the vv-th row of A
vv_nei = A(vv,:);
% Connect nodes uu and vv in A
A(uu,vv) = 1;
A(vv,uu) = 1;
% Increment the neighbor count for uu and its neighbours
nei(uu,vv_nei) = nei(uu,vv_nei) + 1;
nei(vv_nei,uu) = nei(vv_nei,uu) + 1;
% Increment the neighbor count for vv and its neighbours
nei(vv,uu_nei) = nei(vv,uu_nei) + 1;
nei(uu_nei,vv) = nei(uu_nei,vv) + 1;
% Increment degmat_sum for uu (rows)
degmat_sum(uu,:) = degmat_sum(uu,:) + 1;
% Increment degmat_sum for vv (rows)
degmat_sum(vv,:) = degmat_sum(vv,:) + 1;
% Increment degmat_sum for uu (columns)
degmat_sum(:,uu) = degmat_sum(:,uu) + 1;
% Increment degmat_sum for vv (columns)
degmat_sum(:,vv) = degmat_sum(:,vv) + 1;
% This is an easy to follow example of the calculation for the matching
% index. Basically, double the neighbours for a pair of nodes, then
% divide this by the sum of the nodes degrees while accounting for any
% connection that may exist between that node pair
%K = ( (nei.*2) ./ (degmat_sum - (A.*2) ) ) + epsilon;
%K(isnan(K)) = epsilon;
% If two nodes have no connections, their matching index will be 0/0
% which equals nan. We can search and replace nans using 'isnan'
% however for very large networks, searching for these nans takes a
% surprising amount of time. To work around this, the section
% "(degmat_sum<=2 & nei~=1)" takes the value of 1 when two nodes have
% one connection or less each and don't have exactly one neighbor. The
% reason "degmat_sum<=2" is used is because if two nodes each have a
% degree of one but no shared neighbors, this means those two nodes are
% connected to each other (and technically share no neighbors). In this
% case the equation "degmat_sum - (A.*2)" equals zero (as the summed
% degree is cancelled by their shared connection) and could cause the
% whole equation to fail. The "& nei~=1" catches a case where two nodes
% are only connected to the same node (i.e., they share one neighbor).
% If this was not there (i.e., only "degmat_sum<=2" was used) then an
% erroneous value of one will be added to the denominator, giving an
% incorrect result.
% This first instance of the code seems to be faster when there is ~120
% nodes or less. When overm use the alternative below
%if n < 120
% K = ( (nei.*2) ./ ( (degmat_sum<=2 & nei~=1) + ( degmat_sum - (A.*2) ) ) ) + epsilon;
%
% switch mv2
% case 'powerlaw'
% Fk = K.^TopoParam(1,1);
% case 'exponential'
% Fk = exp(TopoParam(1,1)*K);
% end
%else
% Get the nodes to perform the calculation over. Simply find the nodes
% which the edge was added between (uu vv) and all their neighbours
% (uu_nei vv_nei). Note I think that uu and vv are not needed here but
% for safety I have kept them
% Due to the magic of indexing, we don't actually need to find the
% unique set of neighbours and can include duplicates
% %nodes2use = unique([uu vv find(uu_nei) find(vv_nei)]);
nodes2use = [uu vv find(uu_nei) find(vv_nei)];
% Perform the matching calculation only for the node pairs which can
% actually be updated.
switch mv2
case 'powerlaw'
Fk_update = ( (2 * nei(nodes2use,:) ./ ( (degmat_sum(nodes2use,:)<=2 & nei(nodes2use,:)~=1)+(degmat_sum(nodes2use,:) - (A(nodes2use,:) * 2)) ) ) + epsilon).^TopoParam(1,1);
case 'exponential'
Fk_update = exp(( (2 * nei(nodes2use,:) ./ ( (degmat_sum(nodes2use,:)<=2 & nei(nodes2use,:)~=1)+(degmat_sum(nodes2use,:) - (A(nodes2use,:) * 2)) ) ) + epsilon)*TopoParam(1,1));
end
%
% % Add in the updated values to the Fk matrix
Fk(nodes2use,:) = Fk_update;
Fk(:,nodes2use) = Fk_update';
%end
switch normType
case 'max'
for j = 1:length(PDMsmv)
Df = Df1(:,:,j);
%PDf(:,:,j) = PDMsParam(2,j)*(Df./max(Df(indx(~EdgesTriu))));
PDf(:,:,j) = PDMsParam(2,j)*(Df./max(Df.*~A,[],'all'));
%X{j} = PDMsParam(2,j)*(Df./max(Df.*~A,[],'all'));
end
%Y=sum(cat(3,X{:}),3);
Fd = sum(PDf,3);
MaxNorm = max(Fk.*~A,[],'all');
if MaxNorm == 0
TopoTerm = 0;
else
TopoTerm = TopoParam(2,1)*(Fk./MaxNorm).*~A;
end
case 'sum'
for j = 1:length(PDMsmv)
Df = Df1(:,:,j);
PDf(:,:,j) = PDMsParam(2,j)*(Df./(sum(Df.*~A,'all')/2));
end
SumNorm = sum(Fk.*~A,'all')/2;
if SumNorm == 0
TopoTerm = 0;
else
TopoTerm = TopoParam(2,1)*(Fk./SumNorm).*~A;
end
end
Fp = (Fd + TopoTerm).*~A;
% Update probabilities P based on Ff and indx
P = Fp(indx);
end
b = indx(b);
function b = fcn_sptl(A,PDMs,m,PDMsParam,modelvar,normType)
n = length(A);
mseed = nnz(A)/2;
[u,v] = find(triu(ones(n),1));
indx = (v - 1)*n + u;
EdgesTriu = zeros(size(indx));
b = zeros(m,1);
b(1:mseed) = find(A(indx));
EdgesTriu(b(b~=0)) = 1;
PDMsmv = modelvar{1};
PDf = zeros(size(PDMs));
Df1 = zeros(size(PDMs));
for j = 1:length(PDMsmv)
mv1 = PDMsmv{j};
switch mv1
case 'powerlaw'
Df1(:,:,j) = PDMs(:,:,j).^PDMsParam(1,j);
case 'exponential'
Df1(:,:,j) = exp(PDMsParam(1,j)*(PDMs(:,:,j))).*~eye(n);
end
Df = Df1(:,:,j);
switch normType
case 'max'
PDf(:,:,j) = PDMsParam(2,j)*(Df./max(Df.*~A,[],'all'));
case 'sum'
PDf(:,:,j) = PDMsParam(2,j)*(Df./(sum(Df.*~A,'all')/2));
end
end
Fd = sum(PDf,3);
P = Fd(indx).*~A(indx);
for i = (mseed + 1):m
C = [0; cumsum(P)];
r = sum(rand*C(end) >= C);
b(i) = r;
EdgesTriu(b(i)) = 1;
for j = 1:length(PDMsmv)
Df = Df(:,:,j);
switch normType
case 'max'
PDf(:,:,j) = PDMsParam(2,i)*(Df./max(Df.*~A,[],'all'));
case 'sum'
PDf(:,:,j) = PDMsParam(2,i)*(Df./(sum(Df.*~A,'all')/2));
end
end
Fd = sum(PDf,3);
P = Fd(indx);
P(b(1:i)) = 0;
end
b = indx(b);