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
|
import matplotlib.pyplot as plt
import numpy as np
import cascade_creation
import convex_optimization
import algorithms
import timeout
import rip_condition
def theoreticalGuarantees(graph_name, n_cascades, min_proba, max_proba,
sparse_edges, p_init, passed_function, *args, **kwargs):
#creating original graph
G = cascade_creation.InfluenceGraph(max_proba = max_proba,
min_proba = min_proba,
sparse_edges = sparse_edges
)
G.import_from_file(graph_name)
#creating dummy graph
G_hat = cascade_creation.InfluenceGraph(max_proba=None)
G_hat.add_nodes_from(G.nodes())
#generating cascades
A = cascade_creation.generate_cascades(G, p_init=p_init,
n_cascades = n_cascades)
#Value of restrictedEigenvalueList
restrictedEigenvalueList = []
for node in G.nodes():
print(node)
try:
M, _ = cascade_creation.icc_matrixvector_for_node(A, node)
M_val = np.delete(M, node, axis=1)
parents = np.nonzero(G.mat[:,node])[0]
restrictedEigenvalueList.append(
convex_optimization.restrictedEigenvalue(M, S=parents,
gramMatrix=True, node=node))
print(restrictedEigenvalueList[-1])
except timeout.TimeoutError:
print("TimeoutError, skipping to next node")
print(restrictedEigenvalueList)
alpha = 1
theoreticalGuarantee = sum(
np.sqrt(len(parents)*np.log(G.number_of_nodes())/len(M))/(
alpha*gamma) for gamma in restrictedEigenvalueList if gamma >=
1000)
print(theoreticalGuarantee)
def cascades_vs_measurements(graph_name, n_cascades, min_proba, max_proba,
sparse_edges=False, p_init=0.05):
"""
Compares the number of measurements within a cascade for different types of
graphs
"""
G = cascade_creation.InfluenceGraph(max_proba = max_proba,
min_proba = min_proba,
sparse_edges = sparse_edges
)
G.import_from_file(graph_name)
A = cascade_creation.generate_cascades(G, p_init=p_init,
n_cascades = n_cascades)
L = [len(cascade) for cascade in A]
print(L)
print("mean: {}\nstandard deviation: {}".format(np.mean(L), np.std(L)))
def compare_greedy_and_lagrange_cs284r():
"""
Compares the performance of the greedy algorithm on the
lagrangian sparse recovery objective on the Facebook dataset
for the CS284r project
"""
G = cascade_creation.InfluenceGraph(max_proba = .8)
G.import_from_file("../datasets/subset_facebook_SNAPnormalize.txt")
A = cascade_creation.generate_cascades(G, p_init=.05, n_cascades=100)
#Greedy
G_hat = algorithms.greedy_prediction(G, A)
algorithms.correctness_measure(G, G_hat, print_values=True)
#Lagrange Objective
G_hat = algorithms.recovery_l1obj_l2constraint(G, A,
passed_function=convex_optimization.type_lasso,
floor_cstt=.05, lbda=10)
algorithms.correctness_measure(G, G_hat, print_values=True)
def compute_graph(graph_name, n_cascades, lbda, passed_function, min_proba,
max_proba, sparse_edges=False, p_init=.05):
"""
Test running time on different algorithms
"""
G = cascade_creation.InfluenceGraph(max_proba=max_proba,
min_proba=min_proba,
sparse_edges=sparse_edges)
G.import_from_file(graph_name)
A = cascade_creation.generate_cascades(G, p_init=p_init,
n_cascades=n_cascades)
if passed_function==algorithms.greedy_prediction:
G_hat = algorithms.greedy_prediction(G, A)
else:
G_hat = algorithms.recovery_passed_function(G, A,
passed_function=passed_function,
floor_cstt=.1, lbda=lbda, n_cascades=n_cascades)
algorithms.correctness_measure(G, G_hat, print_values=True)
def plot_watts_strogatz_graph():
"""
plot information in a pretty way
"""
plt.clf()
fig = plt.figure(1)
labels = [50, 100, 500, 1000, 2000, 5000]
x = [np.log(50), np.log(100), np.log(500),
np.log(1000), np.log(2000), np.log(5000)]
sparse_recov = [.25, .32, .7, .82, .89, .92]
max_likel = [.21, .29, .67, .8, .87, .9]
lasso = [.07, .30, .46, .65, .86, .89]
greedy = [.09, .15, .4, .63, .82, .92]
fig, ax = plt.subplots()
plt.axis((np.log(45), np.log(5500), 0, 1))
plt.xlabel("Number of Cascades")
plt.ylabel("F1 score")
plt.grid(color="lightgrey")
ax.plot(x, greedy, 'ko-', color="lightseagreen", label='Greedy')
ax.plot(x, lasso, 'ko-', color="orange", label="Lasso")
ax.plot(x, max_likel, 'ko-', color="cornflowerblue", label="MLE")
ax.plot(x, sparse_recov, 'ko-', color="k", label="Our Method")
plt.legend(loc="lower right", fontsize=18)
ax.set_xticks(x)
ax.set_xticklabels(tuple(labels))
plt.savefig("../paper/figures/"+"watts_strogatz.pdf")
def plot_barabasi_albert_graph():
"""
plot information in a pretty way
"""
plt.clf()
fig = plt.figure(1)
labels = [50, 100, 500, 1000, 2000, 5000]
x = [np.log(50), np.log(100), np.log(500),
np.log(1000), np.log(2000), np.log(5000)]
sparse_recov = [.35, .38, .58, .69, .79, .86]
max_likel = [.35, .38, .56, .68, .78, .85]
lasso = [.25, .3, .55, .67, .76, .83]
greedy = [.1, .13, .33, .47, .6, .75]
fig, ax = plt.subplots()
plt.axis((np.log(45), np.log(5500), 0, 1))
plt.xlabel("Number of Cascades")
plt.ylabel("F1 score")
plt.grid(color="lightgrey")
ax.plot(x, greedy, 'ko-', color="lightseagreen", label='Greedy')
ax.plot(x, lasso, 'ko-', color="orange", label="Lasso")
ax.plot(x, max_likel, 'ko-', color="cornflowerblue", label="MLE")
ax.plot(x, sparse_recov, 'ko-', color="k", label="Our Method")
plt.legend(loc="lower right", fontsize=18)
ax.set_xticks(x)
ax.set_xticklabels(tuple(labels))
plt.savefig("../paper/figures/"+"barabasi_albert.pdf")
def plot_kronecker_l2norm():
plt.clf()
fig = plt.figure(1)
x = [50, 150, 500, 1000, 2000]
sparse_recov = [62, 60, 36, 28, 21]
max_likel = [139, 101, 42, 31, 25]
lasso = [50, 48, 33, 29, 23]
fig, ax = plt.subplots()
plt.subplots_adjust(bottom=.2, top=.85)
plt.xticks(ha="right", rotation=45)
plt.axis((50, 2000, 0, 145))
plt.xlabel("Number of Cascades")
plt.ylabel("l2-norm")
plt.grid(color="lightgrey")
ax.plot(x, lasso, 'ko-', color="orange", label="Lasso")
ax.plot(x, max_likel, 'ko-', color="cornflowerblue", label="MLE")
ax.plot(x, sparse_recov, 'ko-', color="k", label="Our Method")
plt.legend(loc="upper right")
ax.set_xticks(x)
ax.set_xticklabels(tuple([50, 100, 500, 1000, 2000]))
ax.set_yticklabels(tuple(['', 20, 40, 60, 80, 100, 120, 140]))
plt.savefig("../paper/figures/"+"kronecker_l2_norm.pdf")
def plot_kronecker_l2norm_nonsparse():
plt.clf()
fig = plt.figure(1)
x = [50, 150, 500, 1000, 2000]
sparse_recov = [56, 55, 28, 21, 15]
max_likel = [125, 80, 35, 25, 20]
lasso = [47, 47, 27, 22, 17]
fig, ax = plt.subplots()
plt.subplots_adjust(bottom=.2, top=.85)
plt.xticks(ha="right", rotation=45)
plt.axis((50, 2000, 0, 145))
plt.xlabel("Number of Cascades")
plt.ylabel("l2-norm")
plt.grid(color="lightgrey")
ax.plot(x, lasso, 'ko-', color="orange", label="Lasso")
ax.plot(x, max_likel, 'ko-', color="cornflowerblue", label="MLE")
ax.plot(x, sparse_recov, 'ko-', color="k", label="Our Method")
plt.legend(loc="upper right")
ax.set_xticks(x)
ax.set_xticklabels(tuple([50, 100, 500, 1000, 2000]))
ax.set_yticklabels(tuple(['', 20, 40, 60, 80, 100, 120, 140]))
plt.savefig("../paper/figures/"+"kronecker_l2_norm_nonsparse.pdf")
def plot_ROC_curve(figure_name):
"""
plot information in a pretty way
"""
plt.clf()
fig = plt.figure(2)
fig, ax = plt.subplots()
recall_sparse_200 = [.03, .16, .37, .4, .49]
precision_sparse_200 = [.9, .76, .61, .6, .63]
recall_lasso_200 = [.02, .11, .25, .43, .5, .54]
precision_lasso_200 = [.77, .77, .66, .56, .55, .51]
recall_sparse_50 = [.07, .13, .16, .58]
precision_sparse_50 = [.56, .53, .49, .37]
recall_lasso_50 = [.03, .18, .27, .82]
precision_lasso_50 = [.6, .47, .44, .24]
plt.xlabel("Recall")
plt.ylabel("Precision")
plt.grid(color="lightgrey")
ax.plot(recall_lasso_200, precision_lasso_200, 'ko-',
color="lightseagreen", label="Lasso-200 cascades")
ax.plot(recall_sparse_200, precision_sparse_200, 'ko-',
color="k", label="Our Method-200 cascades")
ax.plot(recall_lasso_50, precision_lasso_50, 'ko-',
color="orange", label="Lasso-50 cascades")
ax.plot(recall_sparse_50, precision_sparse_50, 'ko-',
color="cornflowerblue", label="Our Method-50 cascades")
plt.legend(loc="upper right", fontsize=14)
plt.savefig("../paper/figures/"+"ROC_curve.pdf")
def plot_p_init_watts_strogatz():
plt.clf()
fig = plt.figure(1)
x = [.01, .05, .1, .15, .2]
greedy = [.43, .29, .18, .1, .08]
sparse_recov = [.7, .58, .48, .39, .31]
max_likel = [.69, .56, .45, .37, .3]
lasso = [.66, .55, .46, .38, .3]
fig, ax = plt.subplots()
plt.subplots_adjust(bottom=.2, top=.85)
plt.xticks(ha="right", rotation=45)
plt.axis((0, .21, .05, .8))
plt.xlabel("Number of Cascades", fontsize=20)
plt.ylabel("F1-score", fontsize=20)
plt.grid(color="lightgrey")
ax.plot(x, greedy, 'ko-', color="lightseagreen", label='Greedy')
ax.plot(x, lasso, 'ko-', color="orange", label="Lasso")
ax.plot(x, max_likel, 'ko-', color="cornflowerblue", label="MLE")
ax.plot(x, sparse_recov, 'ko-', color="k", label="Our Method")
plt.legend(loc="upper right", fontsize=16)
ax.set_xticks(x)
ax.set_yticklabels(tuple([.1, .2, .3, .4, .5, .6, .7, .8]), fontsize=20)
ax.set_xticklabels(tuple(x), fontsize=20)
plt.savefig("../paper/figures/"+"watts_strogatz_p_init.pdf")
if __name__=="__main__":
if 1:
theoreticalGuarantees(graph_name =
"../datasets/powerlaw_200_30_point3.txt",
n_cascades=100, min_proba=.2, max_proba=.7,
p_init=.05, sparse_edges=False,
passed_function=convex_optimization.sparse_recovery,
lbda=.001)
if 0:
cascades_vs_measurements("../datasets/watts_strogatz_p_init.txt",
n_cascades=1000, min_proba=.2, max_proba=.7)
if 0:
compute_graph("../datasets/watts_strogatz_300_30_point3.txt",
n_cascades=300, lbda=.013382, min_proba=.2, max_proba=.7,
passed_function=
#convex_optimization.sparse_recovery)
algorithms.greedy_prediction, p_init=.2)
#convex_optimization.sparse_recovery, p_init=.15)
if 0:
compute_graph("../datasets/powerlaw_200_30_point3.txt",
n_cascades=200, lbda=.01, min_proba=.2, max_proba=.7,
passed_function=
#convex_optimization.sparse_recovery)
#algorithms.greedy_prediction)
convex_optimization.type_lasso)
if 0:
compute_graph("../datasets/barabasi_albert_300_30.txt",
n_cascades=100, lbda=.002, min_proba=.2,
max_proba=.7, passed_function=
convex_optimization.sparse_recovery)
#algorithms.greedy_prediction)
#convex_optimization.type_lasso)
if 0:
compute_graph("../datasets/kronecker_graph_256_cross.txt",
n_cascades=50, lbda=0., min_proba=.2, max_proba=.7,
passed_function=
convex_optimization.sparse_recovery,
#convex_optimization.type_lasso,
sparse_edges=True)
|