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 LogNormal #120

Merged
merged 2 commits into from
Dec 1, 2023
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
31 changes: 30 additions & 1 deletion flowjax/distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from jax.scipy import stats as jstats

from flowjax._custom_types import ArrayLike
from flowjax.bijections import AbstractBijection, Affine, Chain, TriangularAffine
from flowjax.bijections import AbstractBijection, Affine, Chain, Exp, TriangularAffine
from flowjax.utils import _get_ufunc_signature, arraylike_to_array, merge_cond_shapes


Expand Down Expand Up @@ -401,6 +401,35 @@ def scale(self):
return self.bijection.scale


class LogNormal(AbstractTransformed):
"""Log normal distribution.

``loc`` and ``scale`` here refers to the underlying normal distribution.

Args:
loc: Location paramter. Defaults to 0.
scale: Scale parameter. Defaults to 1.0.
"""

base_dist: StandardNormal
bijection: Chain

def __init__(self, loc: ArrayLike = 0, scale: ArrayLike = 1):
shape = jnp.broadcast_shapes(jnp.shape(loc), jnp.shape(scale))
self.base_dist = StandardNormal(shape)
self.bijection = Chain([Affine(loc, scale), Exp(shape)])

@property
def loc(self):
"""Location of the distribution."""
return self.bijection[0].loc

@property
def scale(self):
"""Scale of the distribution."""
return self.bijection[0].scale


class MultivariateNormal(AbstractTransformed):
"""Multivariate normal distribution.

Expand Down
2 changes: 2 additions & 0 deletions tests/test_distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
AbstractTransformed,
Cauchy,
Gumbel,
LogNormal,
MultivariateNormal,
Normal,
StandardNormal,
Expand Down Expand Up @@ -42,6 +43,7 @@
"Cauchy": lambda shape: Cauchy(jnp.zeros(shape)),
"_StandardStudentT": lambda shape: _StandardStudentT(jnp.ones(shape)),
"StudentT": lambda shape: StudentT(jnp.ones(shape)),
"LogNormal": lambda shape: LogNormal(jnp.ones(shape), 2),
}


Expand Down