-
Notifications
You must be signed in to change notification settings - Fork 1
/
TwitterCascades.jl
executable file
·369 lines (282 loc) · 8.92 KB
/
TwitterCascades.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
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
module TwitterCascades
export CASCADE_ALGO
using JLD2
using DataStructures
using Distributions
using Distributed
using SparseArrays
using SharedArrays
@enum CASCADE_ALGO single multitry multi
function cascade_single(
adj::SparseMatrixCSC{E,V},
start::Integer,
time_distribution::UnivariateDistribution{Support},
alpha::Real,
lambda::Real,
forward::Bool) where {V<:Integer,E,Support}
N, M = size(adj)
@assert N == M
@assert start > 0
@assert start <= N
@assert alpha <= 1
@assert alpha >= 0
@assert lambda >= 0
@assert !forward
if forward
adj = copy(adj')
end
adj::SparseMatrixCSC{E,V}
tried = falses(N)
retweeted = falses(N)
is_initiator = true
queue = [ Pair{Float32,V}(0.0, start) ]
while !isempty(queue)
recieved_time, vertex = heappop!(queue)
if tried[vertex]
continue
end
tried[vertex] = true
# compute retweeting probability
retweet_time = recieved_time + rand(time_distribution)
retweet_prob = alpha * exp(-lambda * retweet_time)
# sample whether vertex retweets (initiator always do)
is_retweeting = is_initiator || (retweet_prob > rand())
is_initiator = false
if !is_retweeting
continue
end
retweeted[vertex] = true
@inbounds firstedge = adj.colptr[vertex]
@inbounds lastedge = adj.colptr[vertex+1] - 1
for edge in firstedge : lastedge
@inbounds neighbor = adj.rowval[edge]
# if the node has already retweeted we do not allow it to tweet anymore
@inbounds if tried[neighbor]
continue
end
heappush!(queue, Pair{Float32,V}(retweet_time, neighbor))
end
end
return retweeted
end
function cascade_multitry(
adj::SparseMatrixCSC{E,V},
start::Integer,
time_distribution::UnivariateDistribution{Support},
alpha::Real,
lambda::Real,
forward::Bool) where {V<:Integer,E,Support}#::BitArray{1}
N, M = size(adj)
@assert N == M
@assert start > 0
@assert start <= N
@assert alpha <= 1
@assert alpha >= 0
@assert lambda >= 0
@assert !forward
if forward
adj = copy(adj')
end
adj::SparseMatrixCSC{E,V}
retweeted = falses(N)
is_initiator = true
queue = [ Pair{Float32,V}(0.0, start) ]
while !isempty(queue)
recieved_time, vertex = heappop!(queue)
if retweeted[vertex]
continue
end
# compute retweeting probability
retweet_time = recieved_time + rand(time_distribution)
retweet_prob = alpha * exp(-lambda * retweet_time)
# sample whether vertex retweets (initiator always do)
is_retweeting = is_initiator || (retweet_prob > rand())
is_initiator = false
if !is_retweeting
continue
end
retweeted[vertex] = true
@inbounds firstedge = adj.colptr[vertex]
@inbounds lastedge = adj.colptr[vertex+1] - 1
for edge in firstedge : lastedge
@inbounds neighbor = adj.rowval[edge]
# if the node has already retweeted we do not allow it to tweet anymore
@inbounds if retweeted[neighbor]
continue
end
#
heappush!(queue, Pair{Float32,V}(retweet_time, neighbor))
end
end
return retweeted
end
function cascade(
algo::CASCADE_ALGO,
adj::SparseMatrixCSC{E,V},
start::Integer,
time_distribution::UnivariateDistribution{Support},
alpha::Real,
lambda::Real,
forward::Bool) where {V<:Integer,E,Support}
if algo == single
return cascade_single(adj, start, time_distribution, alpha, lambda, forward)
elseif algo == multitry
return cascade_multitry(adj, start, time_distribution, alpha, lambda, forward)
else
error("unknown algorithm $algo")
end
end
function cascadesize(
algo::CASCADE_ALGO,
adj::SparseMatrixCSC{E,V},
start::Integer,
time_distribution::UnivariateDistribution{S1},
alpha_distribution::UnivariateDistribution{S2},
lambda_distribution::UnivariateDistribution{S3},
;
forward::Bool=true)::V where {V<:Integer,E,S1,S2,S3}
sum(cascade(algo, adj, start, time_distribution, rand(alpha_distribution), rand(lambda_distribution), forward))
end
function cascadesizes(
repeats::Integer,
adj::SparseMatrixCSC{E,V},
time_distribution::UnivariateDistribution{S1},
alpha_distribution::UnivariateDistribution{S2},
lambda_distribution::UnivariateDistribution{S3},
;
forward::Bool=true)::Vector{V} where {V<:Integer,E,S1,S2,S3}
if forward
adj = copy(adj')
end
adj::SparseMatrixCSC{E,V}
N::V = size(adj,1)
start_distribution::DiscreteUniform = DiscreteUniform(V(1),N)
local sizes = Vector{V}(repeats)
local start::V
local sz::V
for i in 1:repeats
start = V(rand( start_distribution ))
sz = cascadesize(adj,start,time_distribution, alpha_distribution, lambda_distribution, false)
sizes[i] = V(sz)
end
return sizes
end
function cascadesizes_parallel(
algo::CASCADE_ALGO,
repeats::Integer,
adj::SparseMatrixCSC{E,V},
time_distribution::UnivariateDistribution{S1},
alpha_distribution::UnivariateDistribution{S2},
lambda_distribution::UnivariateDistribution{S3},
;
forward::Bool=true) where {V<:Integer,E,S1,S2,S3}
if forward
adj = copy(adj')
end
adj::SparseMatrixCSC{E,V}
N::V = size(adj,1)
start_distribution::DiscreteUniform = DiscreteUniform(V(1),N)
sizes = SharedArray{V}(repeats)
#sizes = Array{V}(undef, repeats)
starts = rand(start_distribution, repeats)
@sync @distributed for (idx, start) in collect(enumerate(starts))
#Threads.@threads for (idx, start) in enumerate(starts)
local sz::V = cascadesize(algo, adj, start, time_distribution, alpha_distribution, lambda_distribution, forward=false)
sizes[idx] = V(sz)
end
return Array(sizes)
end
function tree_cascade_size(
branching_dist::UnivariateDistribution{Discrete},
time_dist::UnivariateDistribution,
alpha::Real,
lambda::Real,
stack::Vector{ Pair{Float64,Int64} } = Pair{Float64,Int64}[]
)
StackValue = Pair{Float64,Int64}
#stack = StackValue[]
empty!(stack)
push!(stack, StackValue(0.0, 1) )
result = 1
while !isempty(stack)
received_time, children_count = pop!(stack)
if 0 == children_count
continue
end
# put the same thing on the stack with children node decreased
push!(stack, StackValue(received_time, children_count-1) )
# compute retweeting probability
retweet_time = received_time + rand(time_dist)
retweet_prob = alpha * exp(-lambda * retweet_time)
# sample whether vertex retweets (initiator always do)
is_retweeting = retweet_prob > rand()
if result >= 10^6
@warn "reached $result retweets, the stack length is $(length(stack))"
return result
end
# if not retweeting close this branch
if !is_retweeting
continue
end
result += 1
offspring_count = rand(branching_dist)
push!(stack, StackValue(retweet_time, offspring_count))
end
return result
end
function tree_cascade_sizes(
branching_dist::UnivariateDistribution{Discrete},
time_dist::UnivariateDistribution,
alpha_dist::UnivariateDistribution,
lambda_dist::UnivariateDistribution,
N::Integer
)
stack = Pair{Float64,Int64}[]
sizes = Vector{Int}(undef, N)
for i in 1:N
alpha = rand(alpha_dist)
lambda = rand(lambda_dist)
sizes[i] = tree_cascade_size(branching_dist, time_dist, alpha, lambda)
end
return sizes
end
function computecascades!(
dict,
adj::SparseMatrixCSC{E,V},
time_dist,
alpha_dist,
niters::Integer,
beta_dist = Beta(2,1),
lognormal_dist = LogNormal(0,1.5)
) where {V<:Integer, E}
dict["time_dist"] = time_dist
dict["alpha_dist"] = alpha_dist
dict["niters"] = niters
dict["beta_dist"] = beta_dist
dict["lognormal_dist"] = lognormal_dist
@time dict["cascade_single_beta"] = TwitterCascades.cascadesizes_parallel(
TwitterCascades.single, niters, adj, time_dist, alpha_dist, beta_dist, forward = true);
@time dict["cascade_single_lognormal"] = TwitterCascades.cascadesizes_parallel(
TwitterCascades.single, niters, adj, time_dist, alpha_dist, lognormal_dist, forward = true);
@time dict["cascade_multitry_beta"] = TwitterCascades.cascadesizes_parallel(
TwitterCascades.multitry, niters, adj, time_dist, alpha_dist, beta_dist, forward = true);
@time dict["cascade_multitry_lognormal"] = TwitterCascades.cascadesizes_parallel(
TwitterCascades.multitry, niters, adj, time_dist, alpha_dist, lognormal_dist, forward = true);
end
function computecascades(
datasetname::AbstractString,
filename::AbstractString,
adj::SparseMatrixCSC{E,V},
time_dist::UnivariateDistribution,
alpha_dist::UnivariateDistribution,
niters::Integer,
beta_dist::UnivariateDistribution,
lognormal_dist::UnivariateDistribution
) where {V<:Integer,E}
jldopen(filename,"w") do file
computecascades!(file, adj, time_dist, alpha_dist, niters, beta_dist, lognormal_dist)
file["datasetname"] = datasetname
end
nothing
end
end #module TwitterCascades