-
Notifications
You must be signed in to change notification settings - Fork 0
/
rewire.cpp
267 lines (264 loc) · 8.3 KB
/
rewire.cpp
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
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
//' Degree preserving rewiring process for directed networks.
//'
//' @param iteration Integer, number of iterations for rewiring attempts.
//' @param nattempts Integer, number of rewiring attempts per iteration.
//' @param tnode Vector, target node sequence - 1.
//' @param sout Vector, source nodes' out-degree.
//' @param sin Vector, source nodes' in-degree.
//' @param tout Vector, target nodes' out-degree.
//' @param tin Vector, target nodes' in-degree.
//' @param index_s Index of source nodes' out- and in-degree.
//' \code{index_s}/\code{index_t} bridges the indices of source/target nodes and the
//' target structure eta.
//' @param index_t Index of target nodes' out- and in-degree.
//' @param eta Matrix, target structure eta generated by
//' \code{wdnet::get_eta_directed()}.
//' @param rewire_history Logical, whether the rewiring history should be returned.
//' @return Returns target node sequence, four directed assortativity coefficients after each iteration, and rewire history.
//'
//' @keywords internal
//'
// [[Rcpp::export]]
Rcpp::List dprewire_directed_cpp(
int iteration,
int nattempts,
arma::uvec tnode,
arma::vec sout,
arma::vec sin,
arma::vec tout,
arma::vec tin,
arma::uvec index_s,
arma::uvec index_t,
arma::mat eta,
bool rewire_history) {
// GetRNGstate();
arma::vec outout(iteration, arma::fill::zeros);
arma::vec outin(iteration, arma::fill::zeros);
arma::vec inout(iteration, arma::fill::zeros);
arma::vec inin(iteration, arma::fill::zeros);
// arma::vec r_out_out(iteration, arma::fill::zeros);
// arma::vec r_out_in(iteration, arma::fill::zeros);
// arma::vec r_in_out(iteration, arma::fill::zeros);
// arma::vec r_in_in(iteration, arma::fill::zeros);
int nedge = tnode.size();
int e1, e2, count = 0;
int s1, s2, t1, t2, hist_row;
double u, ratio, temp;
if (rewire_history) {
hist_row = iteration * nattempts;
} else {
hist_row = 1;
}
arma::mat history(hist_row, 4, arma::fill::zeros);
for (int n = 0; n < iteration; n++) {
for (int i = 0; i < nattempts; i++) {
e1 = floor(unif_rand() * nedge);
e2 = floor(unif_rand() * nedge);
while (e1 == e2) {
e2 = floor(unif_rand() * nedge);
}
if (rewire_history) {
history(count, 0) = count;
history(count, 1) = e1;
history(count, 2) = e2;
}
s1 = index_s[e1];
s2 = index_s[e2];
t1 = index_t[e1];
t2 = index_t[e2];
if ((eta(s1, t2) * eta(s2, t1)) < (eta(s1, t1) * eta(s2, t2))) {
ratio = eta(s1, t2) * eta(s2, t1) /
(eta(s1, t1) * eta(s2, t2));
}
else {
ratio = 1;
}
u = unif_rand();
if (u <= ratio) {
temp = index_t[e1];
index_t[e1] = index_t[e2];
index_t[e2] = temp;
temp = tnode[e1];
tnode[e1] = tnode[e2];
tnode[e2] = temp;
temp = tout[e1];
tout[e1] = tout[e2];
tout[e2] = temp;
temp = tin[e1];
tin[e1] = tin[e2];
tin[e2] = temp;
// temp = r_targetOut[e1];
// r_targetOut[e1] = r_targetOut[e2];
// r_targetOut[e2] = temp;
// temp = r_targetIn[e1];
// r_targetIn[e1] = r_targetIn[e2];
// r_targetIn[e2] = temp;
if (rewire_history) {
history(count, 3) = 1;
}
}
count++;
}
outout[n] = (arma::cor(sout, tout)).eval()(0, 0);
outin[n] = (arma::cor(sout, tin)).eval()(0, 0);
inout[n] = (arma::cor(sin, tout)).eval()(0, 0);
inin[n] = (arma::cor(sin, tin)).eval()(0, 0);
// r_out_out[n] = (arma::cor(r_sourceOut, r_targetOut)).eval()(0, 0);
// r_out_in[n] = (arma::cor(r_sourceOut, r_targetIn)).eval()(0, 0);
// r_in_out[n] = (arma::cor(r_sourceIn, r_targetOut)).eval()(0, 0);
// r_in_in[n] = (arma::cor(r_sourceIn, r_targetIn)).eval()(0, 0);
}
// PutRNGstate();
Rcpp::List ret;
ret["tnode"] = tnode;
if (rewire_history) {
ret["history"] = history;
}
ret["outout"] = outout;
ret["outin"] = outin;
ret["inout"] = inout;
ret["inin"] = inin;
// ret["r_out_out"] = r_out_out;
// ret["r_out_in"] = r_out_in;
// ret["r_in_out"] = r_in_out;
// ret["r_in_in"] = r_in_in;
return ret;
}
//' Degree preserving rewiring process for undirected networks.
//'
//' @param iteration Integer, number of iterations for rewiring attempts.
//' @param nattempts Integer, number of rewiring attempts per iteration.
//' @param node1 Vector, first column of edgelist.
//' @param node2 Vector, second column of edgelist.
//' @param degree1 Vector, degree of node1 and node2.
//' @param degree2 Vector, degree of node2 and node1. degree1
//' and degree2 are used to calculate assortativity coefficient,
//' i.e., degree correlation.
//' @param index1 Index of the first column of edgelist.
//' \code{index1} and \code{index2} bridge the nodes' degree and the
//' structure \code{e}.
//' @param index2 Index of the second column of edgelist.
//' @param e Matrix, target structure (eta) generated by
//' \code{wdnet::get_eta_undirected()}.
//' @param rewire_history Logical, whether the rewiring history should be returned.
//' @return Returns node sequences, assortativity coefficient after each iteration, and rewiring history.
//'
//' @keywords internal
//'
// [[Rcpp::export]]
Rcpp::List dprewire_undirected_cpp(
int iteration,
int nattempts,
Rcpp::IntegerVector node1,
Rcpp::IntegerVector node2,
arma::vec degree1,
arma::vec degree2,
arma::vec index1,
arma::vec index2,
arma::mat e,
bool rewire_history) {
// GetRNGstate();
arma::vec rho(iteration, arma::fill::zeros);
int nedge = index1.size();
int e1, e2, temp, count = 0;
int s1, s2, t1, t2, hist_row;
double u, v, ratio;
if (rewire_history) {
hist_row = iteration * nattempts;
} else {
hist_row = 1;
}
arma::mat history(hist_row, 5, arma::fill::zeros);
for (int n = 0; n < iteration; n++) {
for (int i = 0; i < nattempts; i++) {
e1 = floor(unif_rand() * nedge);
e2 = floor(unif_rand() * nedge);
while (e1 == e2) {
e2 = floor(unif_rand() * nedge);
}
if (rewire_history) {
history(count, 0) = count;
history(count, 1) = e1;
history(count, 2) = e2;
}
s1 = index1[e1];
s2 = index1[e2];
t1 = index2[e1];
t2 = index2[e2];
v = unif_rand();
u = unif_rand();
if (v < 0.5) {
// if (rewire_history) {
// history(count, 3) = 0;
// }
if ((e(s1, t2) * e(s2, t1)) < (e(s1, t1) * e(s2, t2))) {
ratio = e(s1, t2) * e(s2, t1) /
(e(s1, t1) * e(s2, t2));
}
else {
ratio = 1;
}
if (u <= ratio) {
if (rewire_history) {
history(count, 4) = 1;
}
temp = index2[e1];
index2[e1] = index2[e2];
index2[e2] = temp;
temp = node2[e1];
node2[e1] = node2[e2];
node2[e2] = temp;
temp = degree2[e1];
degree2[e1] = degree2[e2];
degree2[e2] = temp;
temp = degree1[e1 + nedge];
degree1[e1 + nedge] = degree1[e2 + nedge];
degree1[e2 + nedge] = temp;
}
} else {
if (rewire_history) {
history(count, 3) = 1;
}
if ((e(s1, s2) * e(t1, t2)) < (e(s1, t1) * e(s2, t2))) {
ratio = e(s1, s2) * e(t1, t2) /
(e(s1, t1) * e(s2, t2));
}
else {
ratio = 1;
}
if (u <= ratio) {
if (rewire_history) {
history(count, 4) = 1;
}
temp = index2[e1];
index2[e1] = index1[e2];
index1[e2] = temp;
temp = node2[e1];
node2[e1] = node1[e2];
node1[e2] = temp;
temp = degree2[e1];
degree2[e1] = degree1[e2];
degree1[e2] = temp;
temp = degree1[e1 + nedge];
degree1[e1 + nedge] = degree2[e2 + nedge];
degree2[e2 + nedge] = temp;
}
}
count++;
}
rho[n] = (arma::cor(degree1, degree2)).eval()(0, 0);
}
// PutRNGstate();
Rcpp::List ret;
if (rewire_history) {
ret["history"] = history;
}
ret["node1"] = node1;
ret["node2"] = node2;
ret["rho"] = rho;
// ret["degree1"] = degree1;
// ret["degree2"] = degree2;
return ret;
}