Repository: scipy/scipy
File: scipy/stats/tests/test_multivariate.py (Python)

Code before:

        X = multivariate_normal(mean, cov)

        msg = r"Dimensions \[3\] are invalid .*"
        with pytest.raises(ValueError, match=msg):
            X.marginal(3)

        with pytest.raises(ValueError, match=msg):
            X.marginal([0, 1, 2, 3])

        msg = r"All elements of `dimensions` must be unique."
        with pytest.raises(ValueError, match=msg):
            X.marginal([2, -1])

        with pytest.raises(ValueError, match=msg):
            X.marginal([[0, 1]])
        
        msg = r"Elements of `dimensions` must be integers."
        with pytest.raises(ValueError, match=msg):
            X.marginal([1.1, 2.0])
    
    def test_special_cases(self):
        rng = np.random.default_rng(413911473)
        mean = rng.standard_normal(3)
        A = rng.standard_normal((3, 3))
        cov = np.dot(A, A.transpose())        

        X = multivariate_normal(mean, cov)
        
        msg = r"Cannot marginalize all dimensions."
        with pytest.raises(ValueError, match=msg):
            X.marginal([])

class TestMatrixNormal:

    def test_bad_input(self):
        # Check that bad inputs raise errors
        num_rows = 4
        num_cols = 3
        M = np.full((num_rows,num_cols), 0.3)
        U = 0.5 * np.identity(num_rows) + np.full((num_rows, num_rows), 0.5)
        V = 0.7 * np.identity(num_cols) + np.full((num_cols, num_cols), 0.3)

        # Incorrect dimensions
        assert_raises(ValueError, matrix_normal, np.zeros((5,4,3)))
        assert_raises(ValueError, matrix_normal, M, np.zeros(10), V)
        assert_raises(ValueError, matrix_normal, M, U, np.zeros(10))
        assert_raises(ValueError, matrix_normal, M, U, U)
        assert_raises(ValueError, matrix_normal, M, V, V)
        assert_raises(ValueError, matrix_normal, M.T, U, V)


Review comment (suggestion):
nitpick:
```suggestion
        cov = A @ A.T
```

Code after:
        mean = rng.standard_normal(3)
        A = rng.standard_normal((3, 3))
        cov = A @ A.T

        X = multivariate_normal(mean, cov)

        msg = r"Dimensions \[3\] are invalid .*"
        with pytest.raises(ValueError, match=msg):
            X.marginal(3)

        with pytest.raises(ValueError, match=msg):
            X.marginal([0, 1, 2, 3])

        msg = r"All elements of `dimensions` must be unique."
        with pytest.raises(ValueError, match=msg):
            X.marginal([2, -1])

        with pytest.raises(ValueError, match=msg):
            X.marginal([[0, 1]])
        
        msg = r"Elements of `dimensions` must be integers."
        with pytest.raises(ValueError, match=msg):
            X.marginal([1.1, 2.0])
    
    def test_marginal_special_cases(self):
        rng = np.random.default_rng(413911473)
        mean = rng.standard_normal(3)
        A = rng.standard_normal((3, 3))
        cov = A @ A.T

        X = multivariate_normal(mean, cov)
        
        msg = r"Cannot marginalize all dimensions."
        with pytest.raises(ValueError, match=msg):
            X.marginal([])

class TestMatrixNormal:

    def test_bad_input(self):
        # Check that bad inputs raise errors
        num_rows = 4
        num_cols = 3
        M = np.full((num_rows,num_cols), 0.3)
        U = 0.5 * np.identity(num_rows) + np.full((num_rows, num_rows), 0.5)
        V = 0.7 * np.identity(num_cols) + np.full((num_cols, num_cols), 0.3)

        # Incorrect dimensions
        assert_raises(ValueError, matrix_normal, np.zeros((5,4,3)))
        assert_raises(ValueError, matrix_normal, M, np.zeros(10), V)
        assert_raises(ValueError, matrix_normal, M, U, np.zeros(10))
        assert_raises(ValueError, matrix_normal, M, U, U)