summaryrefslogtreecommitdiffstats
path: root/experiments/ml2.pyx
diff options
context:
space:
mode:
Diffstat (limited to 'experiments/ml2.pyx')
-rw-r--r--experiments/ml2.pyx26
1 files changed, 12 insertions, 14 deletions
diff --git a/experiments/ml2.pyx b/experiments/ml2.pyx
index fb2ec0d..99c9784 100644
--- a/experiments/ml2.pyx
+++ b/experiments/ml2.pyx
@@ -6,35 +6,32 @@ 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,
+cdef DTYPE_t weight_success(int dist, int dt, DTYPE_t alpha, DTYPE_t delta, DTYPE_t lmbda,
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 = delta/(1. + exp(-w1*lmbda) + exp(-w2*lmbda) + exp(-w3*lmbda))
+ structural = delta/(1. + 1./(w1*lmbda) + 1./(w2*lmbda) + 1./(w3*lmbda))
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
# print 'st', structural, temporal
return result
-cdef DTYPE_t weight_failure(int dist, int dt, DTYPE_t alpha, DTYPE_t delta,
+cdef DTYPE_t weight_failure(int dist, int dt, DTYPE_t alpha, DTYPE_t delta, DTYPE_t lmbda,
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 = delta/(1. + exp(-w1/lmbda) + exp(-w2/lmbda) + exp(-w3/lmbda))
+ structural = delta/(1. + 1./(w1*lmbda) + 1./(w2*lmbda) + 1./(w3*lmbda))
temporal = exp(-alpha * dt)
# temporal = 1. - 1. / (1. + dt/alpha)**0.01
result = log(1. - structural + structural * temporal)
# print 'stnv', structural, temporal
return result
-def ml2(dict root_victims, dict victims, dict non_victims, DTYPE_t age,
- DTYPE_t alpha, DTYPE_t delta):
- print 'ML2'
+def ml2(dict root_victims, dict victims, dict non_victims,
+ DTYPE_t alpha, DTYPE_t delta, DTYPE_t lmbda):
cdef:
int n_roots, n_victims, roots, i, dist, dt, t, l
DTYPE_t ll
@@ -52,10 +49,10 @@ def ml2(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, w1, w2, w3)
+ failures = [weight_failure(dist, dt, alpha, delta, lmbda, w1, w2, w3)
for (dist, dt, w1, w2, w3) in parents]
probs_fail[i] = sum(failures)
- successes = [weight_success(dist, dt, alpha, delta, w1, w2, w3)
+ successes = [weight_success(dist, dt, alpha, delta, lmbda, 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]
@@ -73,7 +70,7 @@ def ml2(dict root_victims, dict victims, dict non_victims, DTYPE_t age,
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)
+ failures = [weight_failure(dist, dt, alpha, delta, lmbda, w1, w2, w3)
for (dist, dt, w1, w2, w3) in parents]
probs_nv[i] = sum(failures)
@@ -83,8 +80,9 @@ def ml2(dict root_victims, dict victims, dict non_victims, DTYPE_t age,
ll += probs.sum() # add probability for realized edges and subtract probability these edges fail
roots = n_roots
- beta = float('nan')
# print n_nodes, n_roots, n_victims, max_i, roots
print parent_dists[1:100]
print parent_dts[1:100]
- return (beta, roots, ll)
+ print np.mean(parent_dists)
+ print np.mean(parent_dts)
+ return (lmbda, roots, ll)