summaryrefslogtreecommitdiffstats
path: root/ic_experiments/ml3.pyx
diff options
context:
space:
mode:
authorThibaut Horel <thibaut.horel@gmail.com>2015-09-14 23:08:02 -0400
committerThibaut Horel <thibaut.horel@gmail.com>2015-09-14 23:08:02 -0400
commitab0b1f3cefedb35327a19ec1b6afd560bfdf802d (patch)
treeb777f3e2c0ac0e712d8c5faab5107b1d236e2c3a /ic_experiments/ml3.pyx
parent960676226862d2d68c7a9c04c56d4f8157803025 (diff)
downloadcriminal_cascades-ab0b1f3cefedb35327a19ec1b6afd560bfdf802d.tar.gz
Import supplements and repo reorganization
Diffstat (limited to 'ic_experiments/ml3.pyx')
-rw-r--r--ic_experiments/ml3.pyx91
1 files changed, 91 insertions, 0 deletions
diff --git a/ic_experiments/ml3.pyx b/ic_experiments/ml3.pyx
new file mode 100644
index 0000000..6e031ef
--- /dev/null
+++ b/ic_experiments/ml3.pyx
@@ -0,0 +1,91 @@
+# cython: boundscheck=False, cdivision=True
+import numpy as np
+cimport numpy as np
+from libc.math cimport log, exp
+
+DTYPE = np.float64
+ctypedef np.float_t DTYPE_t
+
+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
+ structural = delta ** dist
+ temporal = log(exp(alpha)-1.) - alpha*dt
+ result = log(structural) + temporal
+ return result
+
+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
+ structural = delta ** dist
+ temporal = exp(-alpha * dt)
+ result = log(1. - structural + structural * temporal)
+ return result
+
+def ml3(dict root_victims, dict victims, dict non_victims, DTYPE_t age,
+ DTYPE_t alpha, DTYPE_t delta):
+ cdef:
+ int n_roots, n_victims, roots, i, dist, dt, t, l
+ DTYPE_t beta, ll, beta_add, max_beta, max_beta_add
+ list parents, failures, successes
+ n_roots, n_victims = len(root_victims), len(victims)
+ cdef:
+ np.ndarray[DTYPE_t] probs = np.zeros(n_victims, dtype=DTYPE)
+ 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)
+
+ # 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, alpha, delta, w1, w2, w3)
+ for (prnt, dist, dt, w1, w2, w3) in parents]
+ probs_fail[i] = sum(failures)
+ 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")
+ for l, s in enumerate(successes):
+ prob = s - failures[l]
+ if prob > probs[i]:
+ probs[i] = prob
+ parent_dists[i] = dists[l]
+ parent_dts[i] = dts[l]
+ # probs_fail[i] = failures[l]
+
+ # calculate log likelihood
+ 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.01, 1, .01):
+ thresh = log(beta/(3012.*(1.-beta)))
+ seeds = probs<thresh
+ non_seeds = probs>=thresh
+ roots = n_roots + sum(seeds)
+
+ beta_add = 0.
+ # add probability for realized edges and subtract probability these edges fail
+ beta_add += (probs[non_seeds]).sum()
+ # add probability for the seeds and non-seeds
+ beta_add += roots * log(beta/3012.) + sum(non_seeds) * log(1. - beta)
+
+ if beta_add > max_beta_add:
+ max_beta = beta
+ max_roots = roots
+ max_beta_add = beta_add
+ # print 'beta:', max_beta, 'add:', max_beta_add, 'roots:', max_roots
+
+ ll += max_beta_add
+ roots = max_roots
+ beta = max_beta
+ # print n_nodes, n_roots, n_victims, max_i, roots
+ return (beta, roots, ll) \ No newline at end of file