Skip to content

Commit 510d3b8

Browse files
Restrict domain on alpha in the CAR distribution (#6801)
* Restricted alpha to be greater than -1 and less than 1. Adjusted parameter checks. * Ricardo's suggestions * add tests for code-cov * logp test, test rename, np.all - > np.any
1 parent cd1d354 commit 510d3b8

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

pymc/distributions/multivariate.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2064,6 +2064,7 @@ def make_node(self, rng, size, dtype, mu, W, alpha, tau):
20642064
W = Assert(msg)(W, pt.allclose(W, W.T))
20652065

20662066
tau = pt.as_tensor_variable(floatX(tau))
2067+
20672068
alpha = pt.as_tensor_variable(floatX(alpha))
20682069

20692070
return super().make_node(rng, size, dtype, mu, W, alpha, tau)
@@ -2080,6 +2081,9 @@ def rng_fn(cls, rng: np.random.RandomState, mu, W, alpha, tau, size):
20802081
Journal of the Royal Statistical Society Series B, Royal Statistical Society,
20812082
vol. 63(2), pages 325-338. DOI: 10.1111/1467-9868.00288
20822083
"""
2084+
if np.any(alpha >= 1) or np.any(alpha <= -1):
2085+
raise ValueError("the domain of alpha is: -1 < alpha < 1")
2086+
20832087
if not scipy.sparse.issparse(W):
20842088
W = scipy.sparse.csr_matrix(W)
20852089
s = np.asarray(W.sum(axis=0))[0]
@@ -2146,8 +2150,9 @@ class CAR(Continuous):
21462150
:func:`~pytensor.sparse.basic.as_sparse_or_tensor_variable` is
21472151
used for this sparse or tensorvariable conversion.
21482152
alpha : tensor_like of float
2149-
Autoregression parameter taking values between -1 and 1. Values closer to 0 indicate weaker
2150-
correlation and values closer to 1 indicate higher autocorrelation. For most use cases, the
2153+
Autoregression parameter taking values greater than -1 and less than 1.
2154+
Values closer to 0 indicate weaker correlation and values closer to
2155+
1 indicate higher autocorrelation. For most use cases, the
21512156
support of alpha should be restricted to (0, 1).
21522157
tau : tensor_like of float
21532158
Positive precision variable controlling the scale of the underlying normal variates.
@@ -2214,10 +2219,10 @@ def logp(value, mu, W, alpha, tau):
22142219
logquad = (tau * delta * tau_dot_delta).sum(axis=-1)
22152220
return check_parameters(
22162221
0.5 * (logtau + logdet - logquad),
2217-
-1 <= alpha,
2218-
alpha <= 1,
2222+
-1 < alpha,
2223+
alpha < 1,
22192224
tau > 0,
2220-
msg="-1 <= alpha <= 1, tau > 0",
2225+
msg="-1 < alpha < 1, tau > 0",
22212226
)
22222227

22232228

tests/distributions/test_multivariate.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,27 @@ def test_car_matrix_check(sparse):
835835
car_dist = pm.CAR.dist(mu, W, alpha, tau)
836836

837837

838+
@pytest.mark.parametrize("alpha", [1, -1])
839+
def test_car_alpha_bounds(alpha):
840+
"""
841+
Tests the check that -1 < alpha < 1
842+
"""
843+
844+
W = np.array([[0, 1, 0], [1, 0, 1], [0, 1, 0]])
845+
846+
tau = 1
847+
mu = np.array([0, 0, 0])
848+
values = np.array([-0.5, 0, 0.5])
849+
850+
car_dist = pm.CAR.dist(W=W, alpha=alpha, mu=mu, tau=tau)
851+
852+
with pytest.raises(ValueError, match="the domain of alpha is: -1 < alpha < 1"):
853+
pm.draw(car_dist)
854+
855+
with pytest.raises(ValueError, match="-1 < alpha < 1, tau > 0"):
856+
pm.logp(car_dist, values).eval()
857+
858+
838859
class TestLKJCholeskCov:
839860
def test_dist(self):
840861
sd_dist = pm.Exponential.dist(1, size=(10, 3))

0 commit comments

Comments
 (0)