-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWattsModels.jl
153 lines (122 loc) · 4.57 KB
/
WattsModels.jl
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
using Distributions
using LightGraphs
using Plots
plotlyjs()
srand(20130810)
function fraction_engaged(node::Int64,
G::LightGraphs.SimpleGraphs.SimpleGraph,
node_status::Vector{Int64})
num_engaged_neighbors = 0
for nbr in neighbors(G, node)
if node_status[nbr] == 1
num_engaged_neighbors += 1
end
end
return num_engaged_neighbors/length(neighbors(G, node))
end
function update_node_status(G::LightGraphs.SimpleGraphs.SimpleGraph,
threshold::Vector{Float64},
node_status::Vector{Int64})
new_node_status = copy(node_status)
for node in vertices(G)
if node_status[node] == 0
if fraction_engaged(node, G, node_status) > threshold[node]
new_node_status[node] = 1
end
end
end
return new_node_status
end
function update_node_status!(G::LightGraphs.SimpleGraphs.SimpleGraph,
threshold::Vector{Float64},
node_status::Vector{Int64})
for node in vertices(G)
if node_status[node] == 0
if fraction_engaged(node, G, node_status) > threshold[node]
node_status[node] = 1
end
end
end
return nothing
end
function diffusion_simulation(n::Int64,
k::Int64,
threshold::Vector{Float64},
T::Int64,
n_realizations::Int64,
mode::String)
"""
This is the function we call to execute the diffusion simulation.
It takes a graph, a vector of threshold fractions for each node,
a initial seed fraction to initialize the network, and the number
of time steps for which the diffusion simulation will be run
The idea is to run the diffusion simulation a very large number
of times and see in how many of these simulations we observe a
global cascade, i.e., number of nodes engaged after the simulation
process is a certain size of the network
Hyper Parameters of the model
----------
1. Threshold (distribution or a specific value)
2. Number of nodes in the network (n)
3. Edges attached each time (k)
4. Synchronous or asynchronous updates
"""
output = Vector{Int64}(n_realizations)
if mode == "async"
for r in 1:n_realizations
G = barabasi_albert(n, k)
# Select a single random node from the network and seed it
node_status = zeros(Int64, nv(G))
node_status[sample(vertices(G))] = 1
for _ in 1:T
update_node_status!(G, threshold, node_status)
end
output[r] = sum(node_status)
end
else
for r in 1:n_realizations
G = barabasi_albert(n, k)
# Select a single random node from the network and seed it
node_status = zeros(Int64, nv(G))
node_status[sample(vertices(G))] = 1
# Easier version
for _ in 1:T
node_status = update_node_status(G, threshold, node_status)
end
output[r] = sum(node_status)
end
end
return output
end
function diffusion_simulation(n::Int64, # Number of nodes
k::Int64, # number of barabasi albert edges to rewire
threshold::Vector{Float64},
n_realizations::Int64)
output = Vector{Int64}(n_realizations)
for r in 1:n_realizations
G = watts_strogatz(n, k, 0.5)
# Select a single random node from the network and seed it
node_status = zeros(Int64, nv(G))
node_status[sample(vertices(G))] = 1
new_node_status = update_node_status(G, threshold, node_status)
t = 1
# Keep updating node status till there are more nodes to activate
while maximum(new_node_status .!= node_status) > 0
node_status = new_node_status
new_node_status = update_node_status(G, threshold, node_status)
t += 1
println("T : $t")
end
output[r] = sum(node_status)
end
return output
end
const N = 10^4
const threshold = fill(0.18, N)
@time data1 = diffusion_simulation(N, 50, threshold, 50, 100, "sync")
@time data2 = diffusion_simulation(N, 50, threshold, 50, 100, "async")
@time diffusion_simulation(N, 100, threshold, 100)
histogram(data1)
histogram(data2)
sum([i for i in data1 if i > 25])
histogram(degree(barabasi_albert(10000, 500)))