|
| 1 | +# Copyright 2024 The PyMC Developers |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +import numpy as np |
| 15 | +import pytest |
| 16 | + |
| 17 | +from pytensor.tensor.type import tensor |
| 18 | + |
| 19 | +from pymc.distributions import MatrixNormal, MvNormal, Normal |
| 20 | +from pymc.logprob.basic import logp |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.parametrize("univariate", [True, False]) |
| 24 | +@pytest.mark.parametrize("batch_shape", [(), (3,)]) |
| 25 | +def test_matrix_vector_transform(univariate, batch_shape): |
| 26 | + rng = np.random.default_rng(755) |
| 27 | + |
| 28 | + μ = rng.normal(size=(*batch_shape, 2)) |
| 29 | + if univariate: |
| 30 | + σ = np.abs(rng.normal(size=(*batch_shape, 2))) |
| 31 | + Σ = np.eye(2) * (σ**2)[..., None] |
| 32 | + x = Normal.dist(mu=μ, sigma=σ) |
| 33 | + else: |
| 34 | + A = rng.normal(size=(*batch_shape, 2, 2)) |
| 35 | + Σ = np.swapaxes(A, -1, -2) @ A |
| 36 | + x = MvNormal.dist(mu=μ, cov=Σ) |
| 37 | + |
| 38 | + c = rng.normal(size=(*batch_shape, 2)) |
| 39 | + B = rng.normal(size=(*batch_shape, 2, 2)) |
| 40 | + y = c + (B @ x[..., None]).squeeze(-1) |
| 41 | + |
| 42 | + # An affine transformed MvNormal is still a MvNormal |
| 43 | + # https://en.wikipedia.org/wiki/Multivariate_normal_distribution#Affine_transformation |
| 44 | + ref_dist = MvNormal.dist( |
| 45 | + mu=c + (B @ μ[..., None]).squeeze(-1), cov=B @ Σ @ np.swapaxes(B, -1, -2) |
| 46 | + ) |
| 47 | + test_y = rng.normal(size=(*batch_shape, 2)) |
| 48 | + np.testing.assert_allclose( |
| 49 | + logp(y, test_y).eval(), |
| 50 | + logp(ref_dist, test_y).eval(), |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +def test_matrix_matrix_transform(): |
| 55 | + rng = np.random.default_rng(46) |
| 56 | + |
| 57 | + n, p = 2, 3 |
| 58 | + M = rng.normal(size=(n, p)) |
| 59 | + A = rng.normal(size=(n, n)) * 0.1 |
| 60 | + U = A.T @ A |
| 61 | + B = rng.normal(size=(p, p)) * 0.1 |
| 62 | + V = B.T @ B |
| 63 | + X = MatrixNormal.dist(mu=M, rowcov=U, colcov=V) |
| 64 | + |
| 65 | + D = rng.normal(size=(n, n)) |
| 66 | + C = rng.normal(size=(p, p)) |
| 67 | + Y = D @ X @ C |
| 68 | + |
| 69 | + # A linearly transformed MatrixNormal is still a MatrixNormal |
| 70 | + # https://en.wikipedia.org/wiki/Matrix_normal_distribution#Transformation |
| 71 | + ref_dist = MatrixNormal.dist(mu=D @ M @ C, rowcov=D @ U @ D.T, colcov=C.T @ V @ C) |
| 72 | + test_Y = rng.normal(size=(n, p)) |
| 73 | + np.testing.assert_allclose( |
| 74 | + logp(Y, test_Y).eval(), |
| 75 | + logp(ref_dist, test_Y).eval(), |
| 76 | + rtol=1e-5, |
| 77 | + ) |
| 78 | + |
| 79 | + |
| 80 | +def test_broadcasted_matmul_fails(): |
| 81 | + x = Normal.dist(size=(3, 2)) |
| 82 | + A = tensor("A", shape=(4, 3, 3)) |
| 83 | + y = A @ x |
| 84 | + with pytest.raises(NotImplementedError): |
| 85 | + logp(y, y.type()) |
0 commit comments