-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChilling-effect-of-network-externalities.cpp
204 lines (142 loc) · 5.19 KB
/
Chilling-effect-of-network-externalities.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
#define ARMA_64BIT_WORD
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::plugins(cpp11)]]
using namespace Rcpp;
using namespace arma;
using std::pow;
using std::vector;
using std::random_shuffle;
using std::normal_distribution;
using std::count;
vector<int> neighbours(const sp_mat& A, const int& n_nodes, const int& node) {
vector<int> nbrs;
for (int i = 0; i < n_nodes; ++i) {
if (A(i, node-1) == 1) nbrs.push_back(i+1);
}
return nbrs;
}
vector<bool> reset_nodes(const sp_mat& A, const int& n_nodes) {
vector<bool> node_status;
for (int i = 0; i < n_nodes; ++i) {
node_status.push_back(false);
}
return node_status;
}
vector<double> initialize_threshold(const sp_mat& A, const int& n_nodes, const double& mu, const double& sigma) {
std::random_device rd;
std::mt19937 gen(rd());
normal_distribution<double> d(mu, sigma);
vector<double> threshold;
for (int i = 0; i < n_nodes; ++i) {
threshold.push_back(d(gen));
}
return threshold;
}
vector<int> shuffled_nodes(const sp_mat& A, const int& n_nodes) {
vector<int> sn;
for (int i = 1; i <= n_nodes; ++i) {
sn.push_back(i);
}
random_shuffle(sn.begin(), sn.end());
return sn;
}
double network_externalities_effect(const sp_mat& A, const int& n_nodes, const int& node,
vector<bool>& node_status,
const vector<double>& threshold,
const double& a, const double& b) {
vector<int> nbrs = neighbours(A, n_nodes, node);
if (count(node_status.begin(), node_status.end(), true)/n_nodes > threshold[node-1]) {
int n_active_nbrs = 0;
for (auto& nbr : nbrs) {
if (node_status[nbr-1] == true) n_active_nbrs++;
}
return 1 - (1 - a) * pow(1 - b, n_active_nbrs);
}
else {
return a;
}
}
void evolve(const sp_mat& A, const int& n_nodes,
vector<bool>& node_status,
const vector<double>& threshold,
const double& a,
const double& b) {
vector<int> s_nodes = shuffled_nodes(A, n_nodes);
for (auto& node : s_nodes) {
if (R::runif(0, 1) < network_externalities_effect(A, n_nodes, node, node_status, threshold, a, b))
node_status[node-1] = true;
}
}
void evolve(const sp_mat& A, const int& n_nodes,
vector<bool>& node_status,
const vector<double>& threshold,
const double& a) {
vector<int> s_nodes = shuffled_nodes(A, n_nodes);
for (auto& node : s_nodes) {
if (R::runif(0, 1) < a)
node_status[node-1] = true;
}
}
// [[Rcpp::export]]
DataFrame simulate(const sp_mat& A,
const double& a_i,
const double& b_i,
const double& mu_i,
const double& sigma_i) {
int T = 30, n_realizations = 10;
const int n_nodes = A.n_cols;
vector<bool> node_status_e, node_status_ne;
vector<int> realizations, time_steps, n_engaged_e, n_engaged_ne;
vector<double> a, b, mu, sigma;
for (int r = 1; r <= n_realizations; ++r) {
node_status_e = reset_nodes(A, n_nodes);
node_status_ne = reset_nodes(A, n_nodes);
vector<double> threshold = initialize_threshold(A, n_nodes, mu_i, sigma_i);
for (int t = 1; t <= T; ++t) {
evolve(A, n_nodes, node_status_e, threshold, a_i, b_i);
evolve(A, n_nodes, node_status_ne, threshold, a_i);
realizations.push_back(r);
time_steps.push_back(t);
n_engaged_e.push_back(count(node_status_e.begin(), node_status_e.end(), true));
n_engaged_ne.push_back(count(node_status_ne.begin(), node_status_ne.end(), true));
a.push_back(a_i);
b.push_back(b_i);
mu.push_back(mu_i);
sigma.push_back(sigma_i);
}
}
DataFrame output = DataFrame::create(Named("realization") = realizations,
Named("time_steps") = time_steps,
Named("a") = a,
Named("b") = b,
Named("mu") = mu,
Named("sigma") = sigma,
Named("n_engaged_e") = n_engaged_e,
Named("n_engaged_ne") = n_engaged_ne);
return output;
}
/*** R
#' ---
#' title: "The chilling effect of network externalities"
#' author: Pavan Gurazada
#' output: github_document
#' ---
#' last update: Sat Mar 10 21:48:39 2018
library(igraph)
parameter_space <- expand.grid(a = seq(0.005, 0.05, length.out = 5),
b = seq(0.05, 0.25, length.out = 5),
mu = seq(0.01, 0.2, length.out = 5),
sigma = seq(0.005, 0.025, length.out = 5))
results <- data.frame()
g <- erdos.renyi.game(625, 8/625)
A <- get.adjacency(g)
cat("\n Starting simulation at ", date(), "\n")
for (row in 1:nrow(parameter_space)) {
output <- simulate(A,
parameter_space[row, 1], parameter_space[row, 2],
parameter_space[row, 3], parameter_space[row, 4])
results <- rbind(results, output)
}
cat("\n Ending simulation at ", date(), "\n")
*/