Skip to content

NotImplementedError for icdf of non-injective MeasurableTransforms #6793

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

Merged
merged 1 commit into from
Jun 29, 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
8 changes: 7 additions & 1 deletion pymc/logprob/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,8 @@ def measurable_transform_logcdf(op: MeasurableTransform, value, *inputs, **kwarg

backward_value = op.transform_elemwise.backward(value, *other_inputs)

# Some transformations, like squaring may produce multiple backward values
# Fail if transformation is not injective
# A TensorVariable is returned in 1-to-1 inversions, and a tuple in 1-to-many
if isinstance(backward_value, tuple):
raise NotImplementedError

Expand All @@ -469,6 +470,11 @@ def measurable_transform_icdf(op: MeasurableTransform, value, *inputs, **kwargs)
input_icdf = _icdf_helper(measurable_input, value)
icdf = op.transform_elemwise.forward(input_icdf, *other_inputs)

# Fail if transformation is not injective
# A TensorVariable is returned in 1-to-1 inversions, and a tuple in 1-to-many
if isinstance(op.transform_elemwise.backward(icdf, *other_inputs), tuple):
raise NotImplementedError

return icdf


Expand Down
36 changes: 35 additions & 1 deletion tests/logprob/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

from pymc.distributions.transforms import _default_transform, log, logodds
from pymc.logprob.abstract import MeasurableVariable, _logprob
from pymc.logprob.basic import conditional_logp, logp
from pymc.logprob.basic import conditional_logp, icdf, logcdf, logp
from pymc.logprob.transforms import (
ArccoshTransform,
ArcsinhTransform,
Expand Down Expand Up @@ -1080,3 +1080,37 @@ def test_check_jac_det(transform):
elemwise=True,
rv_var=pt.random.normal(0.5, 1, name="base_rv"),
)


def test_logcdf_measurable_transform():
x = pt.exp(pt.random.uniform(0, 1))
value = x.type()
logcdf_fn = pytensor.function([value], logcdf(x, value))

assert logcdf_fn(0) == -np.inf
np.testing.assert_almost_equal(logcdf_fn(np.exp(0.5)), np.log(0.5))
np.testing.assert_almost_equal(logcdf_fn(5), 0)


def test_logcdf_measurable_non_injective_fails():
x = pt.abs(pt.random.uniform(0, 1))
value = x.type()
with pytest.raises(NotImplementedError):
logcdf(x, value)


def test_icdf_measurable_transform():
x = pt.exp(pt.random.uniform(0, 1))
value = x.type()
icdf_fn = pytensor.function([value], icdf(x, value))

np.testing.assert_almost_equal(icdf_fn(1e-16), 1)
np.testing.assert_almost_equal(icdf_fn(0.5), np.exp(0.5))
np.testing.assert_almost_equal(icdf_fn(1 - 1e-16), np.e)


def test_icdf_measurable_non_injective_fails():
x = pt.abs(pt.random.uniform(0, 1))
value = x.type()
with pytest.raises(NotImplementedError):
icdf(x, value)