diff --git a/pymc/distributions/truncated.py b/pymc/distributions/truncated.py index 45f44e01f0..45f77574d6 100644 --- a/pymc/distributions/truncated.py +++ b/pymc/distributions/truncated.py @@ -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, diff --git a/tests/distributions/test_truncated.py b/tests/distributions/test_truncated.py index a451051210..4f748f0fbc 100644 --- a/tests/distributions/test_truncated.py +++ b/tests/distributions/test_truncated.py @@ -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 @@ -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