From 1c08943bed693d3110634fc3e5bd0f4631066db0 Mon Sep 17 00:00:00 2001 From: jeanpouget-abadie Date: Sun, 23 Nov 2014 23:22:35 -0500 Subject: messy cascade_creation.py --- jpa_test/cascade_creation.jl | 8 ++++++++ jpa_test/cascade_creation.py | 38 ++++++++++++++++++++++++++++++++++++++ notes/formalisation.pdf | Bin 203767 -> 203767 bytes 3 files changed, 46 insertions(+) create mode 100644 jpa_test/cascade_creation.py diff --git a/jpa_test/cascade_creation.jl b/jpa_test/cascade_creation.jl index 2305e58..7ce5245 100644 --- a/jpa_test/cascade_creation.jl +++ b/jpa_test/cascade_creation.jl @@ -3,3 +3,11 @@ using Graphs n = 100 p = .1 G = Graphs.erdos_renyi_graph(n, p) +rounds = 10 + +function indep_cascade(G, rounds, model, neg_num) +#neg_num represents negative values in matrix M: 0 or -1 + +end + +cascade(G, rounds, "voter", 0) diff --git a/jpa_test/cascade_creation.py b/jpa_test/cascade_creation.py new file mode 100644 index 0000000..88798cd --- /dev/null +++ b/jpa_test/cascade_creation.py @@ -0,0 +1,38 @@ +import networkx as nx +import numpy as np + +def icc_cascade(G, p_init): + """ + input: graph with prob as edge attr + returns: 2D boolean matrix with indep. casc. + where True means node was active at that time step + p_init: proba that node in seed set + """ + susceptible = np.ones(G.number_of_nodes(), dtype=bool) + active = np.random.rand(G.number_of_nodes()) < p_init + susceptible = susceptible - active + cascade = [] + while sum(active) and sum(susceptible): + cascade.append(active) + tmp = np.zeros(G.number_of_nodes(), dtype=bool) + for node in np.where(active)[0]: + #rand_p = np.random.rand(G.degree(2)) + for edge in G.edges(node, data=True): + tmp[edge[1]] += np.random.rand() < edge[2]["weight"] \ + and susceptible[edge[1]] + active = tmp + susceptible = susceptible - active + return cascade + +def test(): + """ + unit test + """ + G = nx.erdos_renyi_graph(500, .15, directed=True) + for edge in G.edges(data=True): + edge[2]['weight'] = .1*np.random.rand() + + print len(icc_cascade(G, p_init=.1)) + +if __name__ == "__main__": + test() diff --git a/notes/formalisation.pdf b/notes/formalisation.pdf index ae0c87f..c262cb1 100644 Binary files a/notes/formalisation.pdf and b/notes/formalisation.pdf differ -- cgit v1.2.3-70-g09d2