Skip to content
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

Add JAX implementation for LogNormalRV #1346

Merged
merged 1 commit into from
Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions aesara/link/jax/dispatch/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,18 @@ def sample_fn(rng, size, dtype, *parameters):
return (rng, sample)

return sample_fn


@jax_sample_fn.register(aer.LogNormalRV)
def jax_sample_fn_lognormal(op):
"""JAX implementation of `LogNormalRV`."""

def sample_fn(rng, size, dtype, *parameters):
rng_key = rng["jax_state"]
loc, scale = parameters
sample = loc + jax.random.normal(rng_key, size, dtype) * scale
sample_exp = jax.numpy.exp(sample)
rng["jax_state"] = jax.random.split(rng_key, num=1)[0]
return (rng, sample_exp)

return sample_fn
16 changes: 16 additions & 0 deletions tests/link/jax/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,22 @@ def test_random_updates(rng_ctor):
"logistic",
lambda *args: args,
),
(
aer.lognormal,
[
set_test_value(
at.lvector(),
np.array([0, 0], dtype=np.int64),
Copy link
Contributor Author

@juanitorduz juanitorduz Dec 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seem scipy's implementation does not accept a loc param ? https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.lognorm.html

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to work with something like scipy.stats.lognorm(1, loc=10).rvs().

),
set_test_value(
at.dscalar(),
np.array(1.0, dtype=np.float64),
),
],
(2,),
"lognorm",
lambda *args: args,
),
(
aer.normal,
[
Expand Down