Skip to content
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
2 changes: 1 addition & 1 deletion pymc/distributions/multivariate.py
Original file line number Diff line number Diff line change
Expand Up @@ -1655,7 +1655,7 @@ def logp(value: TensorVariable, n, eta):
@_default_transform.register(LKJCorr)
def lkjcorr_default_transform(op, rv):
rng, shape, n, eta, *_ = rv.owner.inputs
return CholeskyCorrTransform(n=n, upper=False)
return CholeskyCorrTransform(n=n)


class MatrixNormalRV(RandomVariable):
Expand Down
36 changes: 8 additions & 28 deletions pymc/distributions/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,20 +186,17 @@ class CholeskyCorrTransform(Transform):
name = "cholesky_corr"
ndim_supp = 1

def __init__(self, n, upper: bool = False):
def __init__(self, n):
"""
Initialize the CholeskyCorr transform.
Parameters
----------
n : int
Size of the correlation matrix.
upper: bool, default False
If True, transform to an upper triangular matrix. If False, transform to a lower triangular matrix.
"""
self.n = n
self.m = (n * (n + 1)) // 2 # Number of triangular elements
self.upper = upper

super().__init__()

Expand Down Expand Up @@ -229,15 +226,8 @@ def _fill_triangular_spiral(
Notes
-----
By "spiral order", it is meant that the matrix is filled by jumping between the top and bottom rows, flipping
the fill order from left-to-right to right-to-left on each jump. For example, to fill a 4x4 matrix with
`order=True`, the matrix is filled in the following order:
- Row 0, left to right
- Row 3, right to left
- Row 1, left to right
- Row 2, right to left
When `upper` if False, everything is reversed:
the fill order from left-to-right to right-to-left on each jump. For example, to fill a 4x4 matrix, the
matrix is filled in the following order:
- Row 3, right to left
- Row 0, left to right
Expand All @@ -256,7 +246,7 @@ def _fill_triangular_spiral(
tr = CholeskyCorr(n=4)
x_unconstrained = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
tr._fill_triangular_spiral(x_unconstrained, upper=False).eval()
tr._fill_triangular_spiral(x_unconstrained, unit_diag=False).eval()
# Out:
# array([[ 5, 0, 0, 0],
Expand All @@ -267,20 +257,16 @@ def _fill_triangular_spiral(
x_raveled = pt.as_tensor(x_raveled)
*batch_shape, _ = x_raveled.shape
n, m = self.n, self.m
upper = self.upper

if unit_diag:
n = n - 1

tail = x_raveled[..., n:]

if upper:
xc = pt.concatenate([x_raveled, pt.flip(tail, -1)], axis=-1)
else:
xc = pt.concatenate([tail, pt.flip(x_raveled, -1)], axis=-1)
xc = pt.concatenate([tail, pt.flip(x_raveled, -1)], axis=-1)

y = pt.reshape(xc, (*batch_shape, n, n))
return pt.triu(y) if upper else pt.tril(y)
return pt.tril(y)

def _inverse_fill_triangular_spiral(
self, x: TensorLike, unit_diag: bool = True
Expand Down Expand Up @@ -311,14 +297,8 @@ def _inverse_fill_triangular_spiral(
m = m - n
n = n - 1

upper = self.upper

if upper:
initial_elements = x[..., 0, :]
triangular_portion = x[..., 1:, :]
else:
initial_elements = pt.flip(x[..., -1, :], axis=-1)
triangular_portion = x[..., :-1, :]
initial_elements = pt.flip(x[..., -1, :], axis=-1)
triangular_portion = x[..., :-1, :]

rotated_triangular_portion = pt.flip(triangular_portion, axis=(-1, -2)) # type: ignore[arg-type]
consolidated_matrix = triangular_portion + rotated_triangular_portion
Expand Down
38 changes: 14 additions & 24 deletions tests/distributions/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,28 +704,18 @@ def _get_test_values(self):
)
return x_unconstrained, x_constrained

@pytest.mark.parametrize("upper", [True, False], ids=["upper", "lower"])
def test_fill_triangular_spiral(self, upper):
def test_fill_triangular_spiral(self):
x_unconstrained = np.array([1, 2, 3, 4, 5, 6])

if upper:
x_constrained = np.array(
[
[1, 2, 3],
[0, 5, 6],
[0, 0, 4],
]
)
else:
x_constrained = np.array(
[
[4, 0, 0],
[6, 5, 0],
[3, 2, 1],
]
)
x_constrained = np.array(
[
[4, 0, 0],
[6, 5, 0],
[3, 2, 1],
]
)

transform = tr.CholeskyCorrTransform(n=3, upper=upper)
transform = tr.CholeskyCorrTransform(n=3)

np.testing.assert_allclose(
transform._fill_triangular_spiral(x_unconstrained, unit_diag=False).eval(),
Expand All @@ -738,7 +728,7 @@ def test_fill_triangular_spiral(self, upper):
)

def test_forward(self):
transform = tr.CholeskyCorrTransform(n=3, upper=False)
transform = tr.CholeskyCorrTransform(n=3)
x_unconstrained, x_constrained = self._get_test_values()

np.testing.assert_allclose(
Expand All @@ -748,7 +738,7 @@ def test_forward(self):
)

def test_backward(self):
transform = tr.CholeskyCorrTransform(n=3, upper=False)
transform = tr.CholeskyCorrTransform(n=3)
x_unconstrained, x_constrained = self._get_test_values()

np.testing.assert_allclose(
Expand All @@ -758,7 +748,7 @@ def test_backward(self):
)

def test_transform_round_trip(self):
transform = tr.CholeskyCorrTransform(n=3, upper=False)
transform = tr.CholeskyCorrTransform(n=3)
x_unconstrained, x_constrained = self._get_test_values()

constrained_reconstructed = transform.backward(transform.forward(x_constrained)).eval()
Expand All @@ -768,7 +758,7 @@ def test_transform_round_trip(self):
np.testing.assert_allclose(x_constrained, constrained_reconstructed, atol=1e-6)

def test_log_jac_det(self):
transform = tr.CholeskyCorrTransform(n=3, upper=False)
transform = tr.CholeskyCorrTransform(n=3)
x_unconstrained, _x_constrained = self._get_test_values()

computed_log_jac_det = transform.log_jac_det(x_unconstrained).eval()
Expand All @@ -788,7 +778,7 @@ def test_backward_produces_valid_cholesky_corr(self, n):
n_samples = 5
n_tril = n * (n - 1) // 2

transform = tr.CholeskyCorrTransform(n=n, upper=False)
transform = tr.CholeskyCorrTransform(n=n)
x_unconstrained = rng.normal(size=(n_samples, n_tril))

chol_L = transform.backward(x_unconstrained).eval()
Expand Down
Loading