summaryrefslogtreecommitdiffstats
path: root/experiments/ml.pyx
diff options
context:
space:
mode:
Diffstat (limited to 'experiments/ml.pyx')
-rw-r--r--experiments/ml.pyx42
1 files changed, 22 insertions, 20 deletions
diff --git a/experiments/ml.pyx b/experiments/ml.pyx
index 67b561c..c8c99cc 100644
--- a/experiments/ml.pyx
+++ b/experiments/ml.pyx
@@ -6,20 +6,17 @@ from libc.math cimport log, exp
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))
-
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 = dist * log(delta)
# structural = plogis(w1,delta) * plogis(w2,delta) * plogis(w3,delta)
- # temporal = exp(-alpha*dt) * (exp(alpha)-1)
- temporal = 1 - exp(-alpha*dt)
- if exp(-alpha*dt)==0.: print 'UNDERFLOW ERROR'
+ temporal = log(exp(alpha)-1.) - alpha*dt
+ # temporal = 1 - exp(-alpha*dt)
+ # if exp(-alpha*dt)==0.: print 'UNDERFLOW ERROR'
# temporal = 1. / (1. + (dt - 1.)/alpha)**0.01 - 1. / (1. + dt/alpha)**0.01
- result = log(structural * temporal)
+ result = structural + temporal
# print 'st', structural, temporal
return result
@@ -39,10 +36,10 @@ def ml(dict root_victims, dict victims, dict non_victims, DTYPE_t age,
DTYPE_t alpha, DTYPE_t delta):
cdef:
int n_roots, n_victims, n_nodes, roots, i, dist, dt, t, l
- DTYPE_t beta, ll
+ DTYPE_t beta, ll, beta_add, max_beta, max_beta_add
list parents, failures, successes
n_roots, n_victims = len(root_victims), len(victims)
- n_nodes = 11270
+ n_nodes = 148152
cdef:
np.ndarray[DTYPE_t] probs = np.zeros(n_victims, dtype=DTYPE)
np.ndarray[DTYPE_t] probs_fail = np.zeros(n_victims, dtype=DTYPE)
@@ -62,12 +59,16 @@ def ml(dict root_victims, dict victims, dict non_victims, DTYPE_t age,
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, w1, w2, w3)
- # for (dist, dt, w1, w2, w3) in parents]
- # probs_nv[i] = sum(failures)
+ 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, w1, w2, w3)
+ for (dist, dt, w1, w2, w3) in parents]
+ probs_nv[i] = sum(failures)
+
+ # print successes
+ # print failures
+ # print probs
# calculate log likelihood
# probs.sort(); probs = probs[::-1] # sort probs in descending order
@@ -79,15 +80,16 @@ def ml(dict root_victims, dict victims, dict non_victims, DTYPE_t age,
# print 'probs', probs
max_beta_add = float('-inf')
# iterate over all victim nodes to find the optimal threshold
- for beta in np.arange(0.001, .1, .001):
- thresh = log(beta/(1.-beta))
+ for beta in np.arange(0.001, .2, .002):
+ thresh = log(beta/(3012.*(1.-beta)))
# print 'beta:', beta, 'thresh:', thresh, 'infected:', len(probs[probs>=thresh])
roots = n_roots + len(probs[probs<thresh])
+ beta_add = 0.
# add probability for realized edges and subtract probability these edges fail
- beta_add = (probs[probs>=thresh]).sum()
+ beta_add += (probs[probs>=thresh]).sum()
# add probability for the seeds and non-seeds
- beta_add += roots * log(beta) + (n_nodes-roots) * log(1 - beta)
+ beta_add += roots * log(beta/3012.) + (n_nodes-roots) * log(1. - beta)
if beta_add > max_beta_add:
max_beta = beta