diff options
Diffstat (limited to 'jpa_test/cascade_creation.py')
| -rw-r--r-- | jpa_test/cascade_creation.py | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/jpa_test/cascade_creation.py b/jpa_test/cascade_creation.py index 80054a7..2b53963 100644 --- a/jpa_test/cascade_creation.py +++ b/jpa_test/cascade_creation.py @@ -55,6 +55,8 @@ class Cascade(list): for t, infected_set in izip(xrange(len(self)), self): if infected_set[node]: infected_times.append(t) + if not infected_times: + infected_times.append(None) return infected_times def candidate_infectors(self, node): @@ -68,6 +70,27 @@ class Cascade(list): return candidate_infectors +def icc_matrixvector_for_node(cascades, node): + """ + for the ICC model: + Returns M, w in matrix form where rows of M are i = t + k.T + Excludes all (t,k) after node infection time; w = 1_{infected} + """ + w = [] + M = [] + for cascade in cascades: + t_i = cascade.infection_time(node)[0] + if t_i > 0 or t_i is None: + indicator = np.zeros(len(cascade)) + if t_i > 0: + indicator[-1] = 1 + w.append(indicator) + M.append(np.array(cascade[:t_i])) + M = np.vstack(M) + w = np.hstack(w) + return M, w + + def icc_cascade(G, p_init): """ Returns boolean vectors for one cascade @@ -78,7 +101,7 @@ def icc_cascade(G, p_init): active = np.random.rand(G.number_of_nodes()) < p_init susceptible = susceptible - active cascade = Cascade() - while active.any() and susceptible.any(): + while active.any(): cascade.append(active) active = np.exp(np.dot(G.logmat, active)) \ < np.random.rand(G.number_of_nodes()) @@ -102,7 +125,9 @@ def test(): G.erdos_init(n = 100, p = 1) import time t0 = time.time() - print len(icc_cascade(G, p_init=.1)) + A = generate_cascades(G, .1, 10) + M, w = icc_matrixvector_for_node(A, 0) + print w t1 = time.time() print t1 - t0 |
