Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Bernhardsson committed Mar 19, 2018
1 parent ff64d0e commit b74c32f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 31 deletions.
33 changes: 20 additions & 13 deletions convoys/regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ def _get_constants(args):
return (tf.constant(arg.astype(numpy.float32)) for arg in args)


def _get_params(sess, params):
return {key: sess.run(param) for key, param in params.items()}


def _fix_t(t):
# TODO: this is stupid, should at least have tests for it
t = numpy.array(t)
Expand Down Expand Up @@ -71,9 +67,12 @@ def fit(self, X, B, T):

with tf.Session() as sess:
tf_utils.optimize(sess, LL_penalized, (alpha, beta))
self.params = _get_params(sess, {'beta': beta, 'alpha': alpha})
self.params['alpha_hessian'] = tf_utils.get_hessian(sess, LL_penalized, alpha)
self.params['beta_hessian'] = tf_utils.get_hessian(sess, LL_penalized, beta)
self.params = {
'beta': sess.run(beta),
'alpha': sess.run(alpha),
'alpha_hessian': tf_utils.get_hessian(sess, LL_penalized, alpha),
'beta_hessian': tf_utils.get_hessian(sess, LL_penalized, beta),
}

def predict(self, x, t, ci=None, n=1000):
t = _fix_t(t)
Expand Down Expand Up @@ -117,9 +116,13 @@ def fit(self, X, B, T):

with tf.Session() as sess:
tf_utils.optimize(sess, LL_penalized, (alpha, beta, log_k_var))
self.params = _get_params(sess, {'beta': beta, 'alpha': alpha, 'k': k})
self.params['alpha_hessian'] = tf_utils.get_hessian(sess, LL_penalized, alpha)
self.params['beta_hessian'] = tf_utils.get_hessian(sess, LL_penalized, beta)
self.params = {
'beta': sess.run(beta),
'alpha': sess.run(alpha),
'k': sess.run(k),
'alpha_hessian': tf_utils.get_hessian(sess, LL_penalized, alpha),
'beta_hessian': tf_utils.get_hessian(sess, LL_penalized, beta),
}

def predict(self, x, t, ci=None, n=1000):
t = _fix_t(t)
Expand Down Expand Up @@ -163,9 +166,13 @@ def fit(self, X, B, T):

with tf.Session() as sess:
tf_utils.optimize(sess, LL_penalized, (alpha, beta, log_k_var))
self.params = _get_params(sess, {'beta': beta, 'alpha': alpha, 'k': k})
self.params['alpha_hessian'] = tf_utils.get_hessian(sess, LL_penalized, alpha)
self.params['beta_hessian'] = tf_utils.get_hessian(sess, LL_penalized, beta)
self.params = {
'beta': sess.run(beta),
'alpha': sess.run(alpha),
'k': sess.run(k),
'alpha_hessian': tf_utils.get_hessian(sess, LL_penalized, alpha),
'beta_hessian': tf_utils.get_hessian(sess, LL_penalized, beta),
}

def predict(self, x, t, ci=None, n=1000):
t = _fix_t(t)
Expand Down
25 changes: 8 additions & 17 deletions convoys/single.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def median(ps):


class Nonparametric(SingleModel):
def fit(self, B, T, n=3):
def fit(self, B, T, n=100):
# We're going to fit c and p_0, p_1, ...
# so that the probability of conversion at time i is c * (1 - p_0) * ... p_i
# What's the total likelihood
Expand All @@ -57,12 +57,9 @@ def fit(self, B, T, n=3):
# Need to sum up the log of that
# We also replace the p_i's with sigmoids just to make the problem unconstrained (and note that 1-s(z) = s(-z))
all_ts = list(sorted(t for b, t in zip(B, T) if b))
print('len(all_ts):', len(all_ts))
n = min(n, len(all_ts))
js = [int(round(1.0 * len(all_ts) * (z + 1) / n - 1)) for z in range(n)]
print('js:', js)
ts = [all_ts[j] for j in js]
print('len(ts):', len(ts))
count_observed = numpy.zeros((n,), dtype=numpy.float32)
count_unobserved = numpy.zeros((n,), dtype=numpy.float32)
for i, (b, t) in enumerate(zip(B, T)):
Expand All @@ -88,16 +85,10 @@ def fit(self, B, T, n=3):

with tf.Session() as sess:
tf_utils.optimize(sess, LL, (z, beta))

print('sum of probabilities of conversion:', sess.run(tf.exp(log_survived_until + log_observed)))

print('survival rates', sess.run(tf.log(tf.sigmoid(-z))))
print('log survived', sess.run(log_survived_until))
print('log survived + endpoint', sess.run(log_survived_after))
print('LL_observed', sess.run(LL_observed))
print('LL_unobserved', sess.run(LL_unobserved))
print('c:', sess.run(c))
w = sess.run(c * (1 - tf.exp(log_survived_until)))
from matplotlib import pyplot
pyplot.plot([0] + ts, w)
pyplot.savefig('nonparametric.png')
self.params = {
'beta': sess.run(beta),
'z': sess.run(z),
'beta_hessian': tf_utils.get_hessian(sess, LL, beta),
'z_hessian': tf_utils.get_hessian(sess, LL, z),
}
print(self.params)
2 changes: 1 addition & 1 deletion test_convoys.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def test_plot_cohorts(cs=[0.3, 0.5, 0.7], k=0.5, lambd=0.1, n=10000):
assert 0.95*c < y < 1.05 * c


def test_nonparametric_model(c=0.7, lambd=0.1, k=0.5, n=100000):
def test_nonparametric_model(c=0.3, lambd=0.1, k=0.5, n=100000):
C = scipy.stats.bernoulli.rvs(c, size=(n,))
N = scipy.stats.uniform.rvs(scale=5./lambd, size=(n,))
E = numpy.array([sample_weibull(k, lambd) for r in range(n)])
Expand Down

0 comments on commit b74c32f

Please sign in to comment.