aboutsummaryrefslogtreecommitdiffstats
path: root/jpa_test/cascade_creation.py
diff options
context:
space:
mode:
authorjeanpouget-abadie <jean.pougetabadie@gmail.com>2014-11-30 23:17:41 -0500
committerjeanpouget-abadie <jean.pougetabadie@gmail.com>2014-11-30 23:17:41 -0500
commitb49e39e300f9d87310f6cce20018427a98f34486 (patch)
tree272262ef3f19a5170dfd74dadda37dd9c3b61fb8 /jpa_test/cascade_creation.py
parent2a6010634417eac9bf2ac4682ac3675dc5074518 (diff)
downloadcascades-b49e39e300f9d87310f6cce20018427a98f34486.tar.gz
convex_optimization first draft
Diffstat (limited to 'jpa_test/cascade_creation.py')
-rw-r--r--jpa_test/cascade_creation.py29
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