diff options
| author | Ben Green <ben@SEASITs-MacBook-Pro.local> | 2015-07-02 00:41:49 -0400 |
|---|---|---|
| committer | Ben Green <ben@SEASITs-MacBook-Pro.local> | 2015-07-02 00:41:49 -0400 |
| commit | 375f29ab4306821c888fd9ef0637f9ab2879e375 (patch) | |
| tree | 30d1c40e429abda8c9cbae953b719543f9cd9cc8 /experiments/ml3.pyx | |
| parent | 8e09ca6ca68c71bdab65525b529e2adfa281823c (diff) | |
| download | criminal_cascades-375f29ab4306821c888fd9ef0637f9ab2879e375.tar.gz | |
in which the model works!!
Diffstat (limited to 'experiments/ml3.pyx')
| -rw-r--r-- | experiments/ml3.pyx | 27 |
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 |
