Beispiel #1
0
    def test__make_positive_definite_iterate(self):
        """Make positive when check_matrix returns True with iterations"""

        # Setup
        check_matrix_mock = Mock()
        check_matrix_mock.side_effect = [False, False, True]

        # Run
        sampler_mock = Mock()
        sampler_mock._check_matrix_symmetric_positive_definite = check_matrix_mock

        matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

        Sampler._make_positive_definite(sampler_mock, matrix)

        # Asserts
        assert check_matrix_mock.call_count == 3
Beispiel #2
0
    def test__make_positive_definite_no_iterate(self, check_mock):
        """Make positive when check_matrix returns True without iterate"""

        # Setup
        check_matrix_mock = Mock()
        check_matrix_mock.return_value = True

        # Run
        sampler_mock = Mock()
        sampler_mock._check_matrix_symmetric_positive_definite = check_matrix_mock

        matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

        Sampler._make_positive_definite(sampler_mock, matrix)

        # Asserts
        assert check_matrix_mock.call_count == 1
Beispiel #3
0
    def test__make_positive_definite(self):
        """Test find the nearest positive-definite matrix"""
        # Setup
        sampler = Mock(spec=Sampler)
        sampler._check_matrix_symmetric_positive_definite.return_value = True

        # Run
        matrix = np.array([[0, 1], [1, 0]])
        result = Sampler._make_positive_definite(sampler, matrix)

        # Asserts
        expected = np.array([[0.5, 0.5], [0.5, 0.5]])
        np.testing.assert_equal(result, expected)

        assert sampler._check_matrix_symmetric_positive_definite.call_count == 1
Beispiel #4
0
    def test__make_positive_definite_iterate(self):
        """Test find the nearest positive-definite matrix iterating"""
        # Setup
        sampler = Mock(spec=Sampler)
        sampler._check_matrix_symmetric_positive_definite.side_effect = [
            False, False, True
        ]

        # Run
        matrix = np.array([[-1, -5], [-3, -7]])
        result = Sampler._make_positive_definite(sampler, matrix)

        # Asserts
        expected = np.array([[0.8, -0.4], [-0.4, 0.2]])
        np.testing.assert_array_almost_equal(result, expected)

        assert sampler._check_matrix_symmetric_positive_definite.call_count == 3