aboutsummaryrefslogtreecommitdiffstats
path: root/simulation
diff options
context:
space:
mode:
authorThibaut Horel <thibaut.horel@gmail.com>2015-11-15 19:17:32 -0500
committerThibaut Horel <thibaut.horel@gmail.com>2015-11-15 19:17:32 -0500
commit730b35ee58cd616ea626bde7171144c326d7cfd8 (patch)
treef6a2d3ec2937f6f3f5b5e2cac9445711022207cb /simulation
parentc1d8ba764f4613228e5567894920250630d72598 (diff)
downloadcascades-730b35ee58cd616ea626bde7171144c326d7cfd8.tar.gz
Variational inference [WIP]
Diffstat (limited to 'simulation')
-rw-r--r--simulation/vi.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/simulation/vi.py b/simulation/vi.py
new file mode 100644
index 0000000..9f2ed40
--- /dev/null
+++ b/simulation/vi.py
@@ -0,0 +1,29 @@
+import autograd.numpy as np
+from autograd import grad
+
+
+def g(m):
+ return np.log(1 - np.exp(-m))
+
+
+def h(m):
+ return -m
+
+
+def ll(cascades, theta):
+ res = 0
+ for x, s in cascades:
+ for t in range(1, x.shape[1] + 1):
+ w = np.dot(x[t-1], theta)
+ res += g(w)[x[t]].sum() + h(w)[~x[t] & s[t]].sum()
+
+
+def sample(params):
+ return np.random.normal(*params)
+
+
+def ll_full(params, cascades):
+ nsamples = 50
+ return np.mean([ll(cascades, sample(params)) for _ in xrange(nsamples)])
+
+grad_ll_full = grad(ll_full)