-
Notifications
You must be signed in to change notification settings - Fork 292
Ensure distribution moments/samples are jax arrays #2206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
ad5ddd6
92b51d0
1435475
98795d6
8f09429
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,6 +70,7 @@ | |
| import numpy as np | ||
|
|
||
| import jax | ||
| from jax import Array | ||
| import jax.numpy as jnp | ||
| from jax.tree_util import register_pytree_node | ||
| from jax.typing import ArrayLike | ||
|
|
@@ -699,8 +700,8 @@ def feasible_like(self, prototype: NumLike) -> NumLike: | |
| class _OrderedVector(_SingletonConstraint[NonScalarArray]): | ||
| event_dim = 1 | ||
|
|
||
| def __call__(self, x: NonScalarArray) -> ArrayLike: | ||
| return (x[..., 1:] > x[..., :-1]).all(axis=-1) | ||
| def __call__(self, x: NonScalarArray) -> Array: | ||
| return (x[..., 1:] > x[..., :-1]).all(axis=-1) # type: ignore | ||
|
|
||
| def feasible_like(self, prototype: NonScalarArray) -> NonScalarArray: | ||
| return jnp.broadcast_to(jnp.arange(float(prototype.shape[-1])), prototype.shape) | ||
|
|
@@ -757,7 +758,7 @@ class _PositiveOrderedVector(_SingletonConstraint[NonScalarArray]): | |
|
|
||
| event_dim = 1 | ||
|
|
||
| def __call__(self, x: NonScalarArray) -> ArrayLike: | ||
| def __call__(self, x: NonScalarArray) -> Array: | ||
| return jnp.logical_and( | ||
| ordered_vector.check(x), jnp.all(positive.check(x), axis=-1) | ||
| ) | ||
|
|
@@ -783,9 +784,9 @@ def feasible_like(self, prototype: NumLike) -> NumLike: | |
|
|
||
|
|
||
| class _Real(_SingletonConstraint[NumLike]): | ||
| def __call__(self, x: NumLike) -> ArrayLike: | ||
| def __call__(self, x: NumLike) -> Array: | ||
| # XXX: consider to relax this condition to [-inf, inf] interval | ||
| return (x == x) & (x != float("inf")) & (x != float("-inf")) | ||
| return (x == x) & (x != float("inf")) & (x != float("-inf")) # type: ignore | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe keep ArrayLike? |
||
|
|
||
| def feasible_like(self, prototype: NumLike) -> NumLike: | ||
| return jnp.zeros_like(prototype) | ||
|
|
@@ -794,9 +795,9 @@ def feasible_like(self, prototype: NumLike) -> NumLike: | |
| class _Simplex(_SingletonConstraint[NonScalarArray]): | ||
| event_dim = 1 | ||
|
|
||
| def __call__(self, x: NonScalarArray) -> ArrayLike: | ||
| def __call__(self, x: NonScalarArray) -> Array: | ||
| x_sum = x.sum(axis=-1) | ||
| return (x >= 0).all(axis=-1) & (x_sum < 1 + 1e-6) & (x_sum > 1 - 1e-6) | ||
| return (x >= 0).all(axis=-1) & (x_sum < 1 + 1e-6) & (x_sum > 1 - 1e-6) # type: ignore | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like above |
||
|
|
||
| def feasible_like(self, prototype: NonScalarArray) -> NonScalarArray: | ||
| return jnp.full_like(prototype, 1 / prototype.shape[-1]) | ||
|
|
@@ -855,6 +856,7 @@ def __call__(self, x: NonScalarArray) -> ArrayLike: | |
| zerosum_true = True | ||
| for dim in range(-self.event_dim, 0): | ||
| zerosum_true = zerosum_true & xp.allclose(x.sum(dim), 0, atol=tol) | ||
| # FIXME: shape must match batch shape of `x`, not be a literal boolean. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems to me that we need to sum over the event dimensions first, then use isclose instead of all_close. |
||
| return zerosum_true | ||
|
|
||
| def eq(self, other: object, static: bool = False) -> ArrayLike: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's return the correct type instead of ignore here?