Skip to content

Handle MvNormal method in Op call #1252

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
Feb 27, 2025
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 pytensor/tensor/random/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ def __init__(self, *args, method: Literal["cholesky", "svd", "eigh"], **kwargs):
)
self.method = method

def __call__(self, mean, cov, size=None, **kwargs):
def __call__(self, mean, cov, size=None, method=None, **kwargs):
r""" "Draw samples from a multivariate normal distribution.

Signature
Expand All @@ -888,6 +888,12 @@ def __call__(self, mean, cov, size=None, **kwargs):
is specified, a single `N`-dimensional sample is returned.

"""
if method is not None and method != self.method:
# Recreate Op with the new method
props = self._props_dict()
props["method"] = method
new_op = type(self)(**props)
return new_op.__call__(mean, cov, size=size, method=method, **kwargs)
return super().__call__(mean, cov, size=size, **kwargs)

def rng_fn(self, rng, mean, cov, size):
Expand Down
3 changes: 1 addition & 2 deletions tests/tensor/random/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from pytensor.tensor import ones, stack
from pytensor.tensor.random.basic import (
ChoiceWithoutReplacement,
MvNormalRV,
PermutationRV,
_gamma,
bernoulli,
Expand Down Expand Up @@ -707,7 +706,7 @@ def test_mvnormal_cov_decomposition_method(method, psd):
[0, 0, 0],
]
rng = shared(np.random.default_rng(675))
draws = MvNormalRV(method=method)(mean, cov, rng=rng, size=(10_000,))
draws = multivariate_normal(mean, cov, method=method, size=(10_000,), rng=rng)
assert draws.owner.op.method == method

# JAX doesn't raise errors at runtime
Expand Down