diff options
| author | Ben Green <ben@SEASITs-MacBook-Pro.local> | 2015-06-15 17:15:33 -0400 |
|---|---|---|
| committer | Ben Green <ben@SEASITs-MacBook-Pro.local> | 2015-06-15 17:15:33 -0400 |
| commit | f7997e0c67ea85b42da94894b6a6f9bef7382170 (patch) | |
| tree | 57ac35463e4d45a84c0e1f215793519023a9ccbc /experiments/ml.pyx | |
| parent | 1143cf60812c37957c81d39d6180921460e62901 (diff) | |
| download | criminal_cascades-f7997e0c67ea85b42da94894b6a6f9bef7382170.tar.gz | |
minor changes for testing
Diffstat (limited to 'experiments/ml.pyx')
| -rw-r--r-- | experiments/ml.pyx | 32 |
1 files changed, 18 insertions, 14 deletions
diff --git a/experiments/ml.pyx b/experiments/ml.pyx index 9c570e9..9a786db 100644 --- a/experiments/ml.pyx +++ b/experiments/ml.pyx @@ -7,50 +7,54 @@ DTYPE = np.float64 ctypedef np.float_t DTYPE_t cdef DTYPE_t plogis(DTYPE_t weight, DTYPE_t delta): - return 1/(1 + exp(-weight/delta)) + return 1./(1. + exp(-weight/delta)) -cdef DTYPE_t weight_success(int dist, int dt, DTYPE_t alpha, - DTYPE_t delta, DTYPE_t gamma): +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) + # structural = plogis(w1,delta) * plogis(w2,delta) * plogis(w3,delta) temporal = exp(-alpha * dt) * (1 - exp(-alpha)) result = log(structural * temporal) return result -cdef DTYPE_t weight_success_power(int dist, int dt, DTYPE_t alpha, - DTYPE_t delta, DTYPE_t gamma): +cdef DTYPE_t weight_success_power(int dist, int dt, DTYPE_t alpha, DTYPE_t delta, + DTYPE_t w1, DTYPE_t w2, DTYPE_t w3): """weight for successful infection, power-law time model""" cdef DTYPE_t structural, temporal, result structural = delta ** (dist) + # structural = plogis(w1,delta) * plogis(w2,delta) * plogis(w3,delta) temporal = 1. / (1. + (dt - 1.)/alpha)**0.01 - 1. / (1. + dt/alpha)**0.01 result = log(structural * temporal) return result -cdef DTYPE_t weight_failure(int dist, int dt, DTYPE_t alpha, - DTYPE_t delta, DTYPE_t gamma): +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) + # structural = plogis(w1,delta) * plogis(w2,delta) * plogis(w3,delta) temporal = 1. - exp(-alpha * dt) #result = log(1. - structural) result = log(1. - structural * temporal) return result -cdef DTYPE_t weight_failure_power(int dist, int dt, DTYPE_t alpha, - DTYPE_t delta, DTYPE_t gamma): +cdef DTYPE_t weight_failure_power(int dist, int dt, DTYPE_t alpha, DTYPE_t delta, + DTYPE_t w1, DTYPE_t w2, DTYPE_t w3): """weight for failed infection, power-law time model""" cdef DTYPE_t structural, temporal, result structural = delta ** (dist) + # structural = plogis(w1,delta) * plogis(w2,delta) * plogis(w3,delta) temporal = 1. - 1. / (1. + dt/alpha)**0.01 result = log(1. - structural * temporal) return result def ml(dict root_victims, dict victims, dict non_victims, DTYPE_t age, - DTYPE_t alpha, DTYPE_t delta, DTYPE_t gamma=10): + DTYPE_t alpha, DTYPE_t delta): cdef: int n_roots, n_victims, n_nodes, roots, i, dist, dt, t, l DTYPE_t beta, ll, beta2 @@ -67,19 +71,19 @@ def ml(dict root_victims, dict victims, dict non_victims, DTYPE_t age, # 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, gamma) + failures = [weight_failure(dist, dt, alpha, delta, w1, w2, w3) for (dist, dt, w1, w2, w3) in parents] probs_fail[i] = sum(failures) - successes = [weight_success(dist, dt, alpha, delta, gamma) + successes = [weight_success(dist, dt, alpha, delta, w1, w2, w3) for (dist, dt, w1, w2, w3) in parents] - # find parent that maximizes p/\tilde{p} + # find parent that maximizes log(p) - log(\tilde{p}) probs[i] = max(s - failures[l] for l, s in enumerate(successes)) # loop through non-victims for i, parents in enumerate(non_victims.itervalues()): # for each non victim node, compute the probability that all its # parents fail to infect it - failures = [weight_failure(dist, dt, alpha, delta, gamma) + failures = [weight_failure(dist, dt, alpha, delta, w1, w2, w3) for (dist, dt, w1, w2, w3) in parents] probs_nv[i] = sum(failures) |
