Skip to content

Commit

Permalink
improve precision for constraint's logdet (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
fehiepsi authored and neerajprad committed May 20, 2019
1 parent d648254 commit de61773
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions numpyro/distributions/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,8 @@ def inv(self, y):
return logit(y)

def log_abs_det_jacobian(self, x, y):
return np.log(y * (1 - y))
x_abs = np.abs(x)
return -x_abs - 2 * np.log1p(np.exp(-x_abs))


class StickBreakingTransform(Transform):
Expand Down Expand Up @@ -361,8 +362,11 @@ def inv(self, y):
def log_abs_det_jacobian(self, x, y):
# Ref: https://mc-stan.org/docs/2_19/reference-manual/simplex-transform-section.html
# |det|(J) = Product(y * (1 - z))
z = _clipped_expit(x - np.log(x.shape[-1] - np.arange(x.shape[-1])))
return np.sum(np.log(y[..., :-1] * (1 - z)), axis=-1)
x = x - np.log(x.shape[-1] - np.arange(x.shape[-1]))
z = np.clip(expit(x), a_min=np.finfo(x.dtype).tiny)
# XXX we use the identity 1 - z = z * exp(-x) to not worry about
# the case z ~ 1
return np.sum(np.log(y[..., :-1] * z) - x, axis=-1)


##########################################################
Expand Down

0 comments on commit de61773

Please sign in to comment.