summaryrefslogtreecommitdiffstats
path: root/experiments/ml3.pyx
diff options
context:
space:
mode:
Diffstat (limited to 'experiments/ml3.pyx')
-rw-r--r--experiments/ml3.pyx27
1 files changed, 13 insertions, 14 deletions
diff --git a/experiments/ml3.pyx b/experiments/ml3.pyx
index da993bc..6e031ef 100644
--- a/experiments/ml3.pyx
+++ b/experiments/ml3.pyx
@@ -6,7 +6,7 @@ from libc.math cimport log, exp
DTYPE = np.float64
ctypedef np.float_t DTYPE_t
-cdef DTYPE_t weight_success(int dist, DTYPE_t dt, DTYPE_t alpha, DTYPE_t delta,
+cdef DTYPE_t weight_success(int dist, int dt, DTYPE_t alpha, DTYPE_t delta,
DTYPE_t w1, DTYPE_t w2, DTYPE_t w3):
"""weight for successful infection, exponential time model"""
cdef DTYPE_t structural, temporal, result
@@ -15,7 +15,7 @@ cdef DTYPE_t weight_success(int dist, DTYPE_t dt, DTYPE_t alpha, DTYPE_t delta,
result = log(structural) + temporal
return result
-cdef DTYPE_t weight_failure(int dist, DTYPE_t dt, DTYPE_t alpha, DTYPE_t delta,
+cdef DTYPE_t weight_failure(int dist, int dt, DTYPE_t alpha, DTYPE_t delta,
DTYPE_t w1, DTYPE_t w2, DTYPE_t w3):
"""weight for failed infection, exponential time model"""
cdef DTYPE_t structural, temporal, result
@@ -36,20 +36,19 @@ def ml3(dict root_victims, dict victims, dict non_victims, DTYPE_t age,
np.ndarray[DTYPE_t] probs_fail = np.zeros(n_victims, dtype=DTYPE)
np.ndarray[DTYPE_t] parent_dists = np.zeros(n_victims, dtype=DTYPE)
np.ndarray[DTYPE_t] parent_dts = np.zeros(n_victims, dtype=DTYPE)
- np.ndarray[DTYPE_t] isSeed = np.ones(n_victims, dtype=DTYPE)
# loop through victims
for i, parents in enumerate(victims.itervalues()):
# for each victim node i, compute the probability that all its parents
# fail to infect it, also computes the probability that its most
# likely parent infects it
- failures = [weight_failure(dist, dt/100., alpha, delta, w1, w2, w3)
- for (dist, dt, w1, w2, w3) in parents]
+ failures = [weight_failure(dist, dt, alpha, delta, w1, w2, w3)
+ for (prnt, dist, dt, w1, w2, w3) in parents]
probs_fail[i] = sum(failures)
- successes = [weight_success(dist, dt/100., alpha, delta, w1, w2, w3)
- for (dist, dt, w1, w2, w3) in parents]
- dists = [dist for (dist, dt, w1, w2, w3) in parents]
- dts = [dt for (dist, dt, w1, w2, w3) in parents]
+ successes = [weight_success(dist, dt, alpha, delta, w1, w2, w3)
+ for (prnt, dist, dt, w1, w2, w3) in parents]
+ dists = [dist for (prnt, dist, dt, w1, w2, w3) in parents]
+ dts = [dt for (prnt, dist, dt, w1, w2, w3) in parents]
# find parent that maximizes log(p) - log(\tilde{p})
# probs[i] = max(s - failures[l] for l, s in enumerate(successes))
probs[i] = float("-inf")
@@ -62,15 +61,15 @@ def ml3(dict root_victims, dict victims, dict non_victims, DTYPE_t age,
# probs_fail[i] = failures[l]
# calculate log likelihood
- ll = probs_fail.sum() # add probability that all edges to all victims fail
+ ll = probs_fail.sum() # add probability that all edges to all victims fail
# print 'probs', probs
max_beta_add = float('-inf')
# iterate over all victim nodes to find the optimal threshold
- for beta in np.arange(0.00001, 1., 1.):
+ for beta in np.arange(0.01, 1, .01):
thresh = log(beta/(3012.*(1.-beta)))
- seeds = isSeed==1
- non_seeds = isSeed==0
+ seeds = probs<thresh
+ non_seeds = probs>=thresh
roots = n_roots + sum(seeds)
beta_add = 0.
@@ -89,4 +88,4 @@ def ml3(dict root_victims, dict victims, dict non_victims, DTYPE_t age,
roots = max_roots
beta = max_beta
# print n_nodes, n_roots, n_victims, max_i, roots
- return (beta, roots, ll)
+ return (beta, roots, ll) \ No newline at end of file