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

Fix bug in Truncated with Deterministic inputs #7315

Merged
merged 1 commit into from
May 16, 2024
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
4 changes: 3 additions & 1 deletion pymc/distributions/truncated.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ def rv_op(cls, dist, lower, upper, max_n_steps, *, size=None):
rng=uniform_rng,
size=rv.shape,
).owner.outputs
truncated_rv = icdf(rv, uniform, warn_rvs=False)
# So icdf does not see the random graph of uniform
uniform_type = uniform.type()
truncated_rv = graph_replace(icdf(rv, uniform_type), {uniform_type: uniform})
return TruncatedRV(
base_rv_op=dist.owner.op,
inputs=graph_inputs,
Expand Down
12 changes: 12 additions & 0 deletions tests/distributions/test_truncated.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import pytest
import scipy

from pytensor.scalar import Identity
from pytensor.tensor.random.basic import GeometricRV, NormalRV
from pytensor.tensor.random.type import RandomType

Expand Down Expand Up @@ -573,3 +574,14 @@ def maxwell_dist(scale, size):
logp(trunc_x, test_value).eval(),
expected_logp,
)


@pytest.mark.parametrize("dist_op", [icdf_normal, rejection_normal])
def test_truncated_identity_input(dist_op):
# Regression test for https://github.com/pymc-devs/pymc/issues/7312
mu = Exponential.dist(scale=0.5)
mu_identity = mu.copy()
assert isinstance(mu_identity.owner.op.scalar_op, Identity)

rv_out = Truncated.dist(dist=dist_op(mu_identity, 5), lower=0, upper=1)
assert np.ptp(draw(rv_out, draws=500)) < 1
Loading