diff --git a/pymc3/distributions/multivariate.py b/pymc3/distributions/multivariate.py index 5caf1c8896b..c56336c5239 100644 --- a/pymc3/distributions/multivariate.py +++ b/pymc3/distributions/multivariate.py @@ -24,6 +24,7 @@ import numpy as np import scipy +from aesara.assert_op import Assert from aesara.graph.basic import Apply from aesara.graph.op import Op from aesara.sparse.basic import sp_sum @@ -1939,12 +1940,12 @@ def make_node(self, rng, size, dtype, mu, W, alpha, tau): raise ValueError("W must be a matrix (ndim=2).") sparse = isinstance(W, aesara.sparse.SparseVariable) + msg = "W must be a symmetric adjacency matrix." if sparse: - if not at.isclose(aesara.sparse.basic.sp_sum(W - W.T), 0): - raise ValueError("W must be a symmetric adjacency matrix.") + abs_diff = aesara.sparse.basic.mul(aesara.sparse.basic.sgn(W - W.T), W - W.T) + W = Assert(msg)(W, at.isclose(aesara.sparse.basic.sp_sum(abs_diff), 0)) else: - if not at.allclose(W, W.T): - raise ValueError("W must be a symmetric adjacency matrix.") + W = Assert(msg)(W, at.allclose(W, W.T)) tau = at.as_tensor_variable(floatX(tau)) alpha = at.as_tensor_variable(floatX(alpha)) diff --git a/pymc3/tests/test_distributions.py b/pymc3/tests/test_distributions.py index 3289de3312f..7578defec1c 100644 --- a/pymc3/tests/test_distributions.py +++ b/pymc3/tests/test_distributions.py @@ -3093,6 +3093,7 @@ def test_car_symmetry_check(sparse): tau = 2 alpha = 0.5 mu = np.zeros(4) + xs = np.random.randn(*mu.shape) # non-symmetric matrix W = np.array( @@ -3102,8 +3103,9 @@ def test_car_symmetry_check(sparse): if sparse: W = aesara.sparse.csr_from_dense(W) - with pytest.raises(ValueError): - car_dist = CAR.dist(mu, W, alpha, tau) + car_dist = CAR.dist(mu, W, alpha, tau) + with pytest.raises(AssertionError): + logp(car_dist, xs).eval() class TestBugfixes: