summaryrefslogtreecommitdiffstats
path: root/experiments/ml.pyx
diff options
context:
space:
mode:
Diffstat (limited to 'experiments/ml.pyx')
-rw-r--r--experiments/ml.pyx26
1 files changed, 15 insertions, 11 deletions
diff --git a/experiments/ml.pyx b/experiments/ml.pyx
index 67b561c..e1ce4cf 100644
--- a/experiments/ml.pyx
+++ b/experiments/ml.pyx
@@ -15,8 +15,8 @@ cdef DTYPE_t weight_success(int dist, int dt, DTYPE_t alpha, DTYPE_t delta,
cdef DTYPE_t structural, temporal, result
structural = delta ** dist
# structural = plogis(w1,delta) * plogis(w2,delta) * plogis(w3,delta)
- # temporal = exp(-alpha*dt) * (exp(alpha)-1)
- temporal = 1 - exp(-alpha*dt)
+ temporal = exp(-alpha*dt) * (exp(alpha)-1.)
+ # 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)
@@ -42,7 +42,7 @@ def ml(dict root_victims, dict victims, dict non_victims, DTYPE_t age,
DTYPE_t beta, ll
list parents, failures, successes
n_roots, n_victims = len(root_victims), len(victims)
- n_nodes = 11270
+ n_nodes = 100
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 +62,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,7 +83,7 @@ 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):
+ for beta in [0.13]:#np.arange(0.001, .2, .002):
thresh = log(beta/(1.-beta))
# print 'beta:', beta, 'thresh:', thresh, 'infected:', len(probs[probs>=thresh])
roots = n_roots + len(probs[probs<thresh])
@@ -87,7 +91,7 @@ def ml(dict root_victims, dict victims, dict non_victims, DTYPE_t age,
# add probability for realized edges and subtract probability these edges fail
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) + (n_nodes-roots) * log(1. - beta)
if beta_add > max_beta_add:
max_beta = beta