Skip to content

Commit

Permalink
various other fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
erikbern committed Apr 18, 2018
1 parent 8bc36ff commit 38aa313
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
6 changes: 6 additions & 0 deletions convoys/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,18 @@ def get_arrays(groups, data, t_converter):
G, B, T = [], [], []
group2j = dict((group, j) for j, group in enumerate(groups))
for group, created_at, converted_at, now in data:
if created_at is None:
print('created at is None')
continue
if converted_at is not None and converted_at <= created_at:
print('created at', created_at, 'but converted at', converted_at)
continue
if now < created_at:
print('created at', created_at, 'but now is', now)
continue
if converted_at is not None and now < converted_at:
print('converted at', converted_at, 'but now is', now)
continue
if group in group2j:
G.append(group2j[group])
B.append(converted_at is not None)
Expand Down
7 changes: 5 additions & 2 deletions convoys/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ def _get_x(self, group):
x[group] = 1
return x

def cdf(self, group, t, *args, **kwargs):
return self.base_model.cdf(self._get_x(group), t, *args, **kwargs)
def cdf(self, group, *args, **kwargs):
return self.base_model.cdf(self._get_x(group), *args, **kwargs)

def rvs(self, group, *args, **kwargs):
return self.base_model.rvs(self._get_x(group), *args, **kwargs)


class SingleToMulti(MultiModel):
Expand Down
6 changes: 4 additions & 2 deletions convoys/regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ def rvs(self, x, n_curves=1, n_samples=1, T=None):
# T is optional and means we already observed non-conversion until T
if T is None:
T = numpy.zeros((n_curves, n_samples))
else:
assert T.shape == (n_curves, n_samples)
a = LinearCombination.sample(self.params['a'], x, 1, n_curves)
b = LinearCombination.sample(self.params['b'], x, 1, n_curves)
B = numpy.zeros((n_curves, n_samples), dtype=numpy.bool)
Expand All @@ -123,15 +125,15 @@ def rvs(self, x, n_curves=1, n_samples=1, T=None):
z = numpy.random.uniform(size=(n_samples,))
cdf_now = expit(b) * gammainc(
self.params['k'],
(T[i]*numpy.exp(a))**self.params['p'])
numpy.multiply.outer(T[i], numpy.exp(a))**self.params['p'])
cdf_final = expit(b)
adjusted_z = cdf_now + (1 - cdf_now) * z
B[i] = (adjusted_z < cdf_final)
y = adjusted_z / cdf_final
x = gammaincinv(self.params['k'], y)
# x = (t * exp(a))**p
C[i] = x**(1./self.params['p']) / numpy.exp(a)
C[~B] = 0
C[i][~B[i]] = 0

return B, C

Expand Down

0 comments on commit 38aa313

Please sign in to comment.