Skip to content

Restrict domain on alpha in the CAR distribution #6801

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 4 commits into from
Jul 26, 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
15 changes: 10 additions & 5 deletions pymc/distributions/multivariate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2061,6 +2061,7 @@ def make_node(self, rng, size, dtype, mu, W, alpha, tau):
W = Assert(msg)(W, pt.allclose(W, W.T))

tau = pt.as_tensor_variable(floatX(tau))

alpha = pt.as_tensor_variable(floatX(alpha))

return super().make_node(rng, size, dtype, mu, W, alpha, tau)
Expand All @@ -2077,6 +2078,9 @@ def rng_fn(cls, rng: np.random.RandomState, mu, W, alpha, tau, size):
Journal of the Royal Statistical Society Series B, Royal Statistical Society,
vol. 63(2), pages 325-338. DOI: 10.1111/1467-9868.00288
"""
if np.any(alpha >= 1) or np.any(alpha <= -1):
raise ValueError("the domain of alpha is: -1 < alpha < 1")

if not scipy.sparse.issparse(W):
W = scipy.sparse.csr_matrix(W)
s = np.asarray(W.sum(axis=0))[0]
Expand Down Expand Up @@ -2143,8 +2147,9 @@ class CAR(Continuous):
:func:`~pytensor.sparse.basic.as_sparse_or_tensor_variable` is
used for this sparse or tensorvariable conversion.
alpha : tensor_like of float
Autoregression parameter taking values between -1 and 1. Values closer to 0 indicate weaker
correlation and values closer to 1 indicate higher autocorrelation. For most use cases, the
Autoregression parameter taking values greater than -1 and less than 1.
Values closer to 0 indicate weaker correlation and values closer to
1 indicate higher autocorrelation. For most use cases, the
support of alpha should be restricted to (0, 1).
tau : tensor_like of float
Positive precision variable controlling the scale of the underlying normal variates.
Expand Down Expand Up @@ -2211,10 +2216,10 @@ def logp(value, mu, W, alpha, tau):
logquad = (tau * delta * tau_dot_delta).sum(axis=-1)
return check_parameters(
0.5 * (logtau + logdet - logquad),
-1 <= alpha,
alpha <= 1,
-1 < alpha,
alpha < 1,
tau > 0,
msg="-1 <= alpha <= 1, tau > 0",
msg="-1 < alpha < 1, tau > 0",
)


Expand Down
21 changes: 21 additions & 0 deletions tests/distributions/test_multivariate.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,27 @@ def test_car_matrix_check(sparse):
car_dist = pm.CAR.dist(mu, W, alpha, tau)


@pytest.mark.parametrize("alpha", [1, -1])
def test_car_alpha_bounds(alpha):
"""
Tests the check that -1 < alpha < 1
"""

W = np.array([[0, 1, 0], [1, 0, 1], [0, 1, 0]])

tau = 1
mu = np.array([0, 0, 0])
values = np.array([-0.5, 0, 0.5])

car_dist = pm.CAR.dist(W=W, alpha=alpha, mu=mu, tau=tau)

with pytest.raises(ValueError, match="the domain of alpha is: -1 < alpha < 1"):
pm.draw(car_dist)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe worth to test the logp as well?

Copy link
Contributor Author

@daniel-saunders-phil daniel-saunders-phil Jul 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think so too. Is it okay if I write it within the same test or do we need two separate ones? I went with putting them in the same test with the latest commit but happy to reorganize it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think either is OK, it's clear what you're testing either way


with pytest.raises(ValueError, match="-1 < alpha < 1, tau > 0"):
pm.logp(car_dist, values).eval()


class TestLKJCholeskCov:
def test_dist(self):
sd_dist = pm.Exponential.dist(1, size=(10, 3))
Expand Down