def setUp(self):
        self.phi1 = CanonicalFactor(['x1', 'x2', 'x3'],
                                    np.array([[1.1, -1, 0], [-1, 4, -2], [0, -2, 4]]),
                                    np.array([[1], [4.7], [-1]]), -2)
        self.phi2 = CanonicalFactor(['x'], [[1]], [0], 1)
        self.phi3 = self.phi1.copy()

        self.gauss_phi1 = JGD(['x1', 'x2', 'x3'],
                              np.array([[3.13043478], [2.44347826], [0.97173913]]),
                              np.array([[1.30434783, 0.43478261, 0.2173913],
                                        [0.43478261, 0.47826087, 0.23913043],
                                        [0.2173913, 0.23913043, 0.36956522]], dtype=float))
        self.gauss_phi2 = JGD(['x'], np.array([0]), np.array([[1]]))
Example #2
0
    def test_class_init(self):
        phi1 = JGD(['x1', 'x2', 'x3'], np.array([[1], [-3], [4]]),
                   np.array([[4, 2, -2], [2, 5, -5], [-2, -5, 8]]))
        self.assertEqual(phi1.variables, ['x1', 'x2', 'x3'])
        np_test.assert_array_equal(phi1.mean,
                                   np.asarray([[1], [-3], [4]], dtype=float))
        np_test.assert_array_equal(
            phi1.covariance,
            np.asarray([[4, 2, -2], [2, 5, -5], [-2, -5, 8]], dtype=float))
        self.assertEqual(phi1._precision_matrix, None)

        phi2 = JGD(['x1', 'x2', 'x3'], [1, 2, 5],
                   np.array([[4, 2, -2], [2, 5, -5], [-2, -5, 8]]))
        self.assertEqual(phi2.variables, ['x1', 'x2', 'x3'])
        np_test.assert_array_equal(phi2.mean,
                                   np.asarray([[1], [2], [5]], dtype=float))
        np_test.assert_array_equal(
            phi2.covariance,
            np.asarray([[4, 2, -2], [2, 5, -5], [-2, -5, 8]], dtype=float))
        self.assertEqual(phi2._precision_matrix, None)

        phi3 = JGD(['x'], [0], [[1]])
        self.assertEqual(phi3.variables, ['x'])
        np_test.assert_array_equal(phi3.mean, np.asarray([[0]], dtype=float))
        np_test.assert_array_equal(phi3.covariance,
                                   np.asarray([[1]], dtype=float))
        self.assertEqual(phi3._precision_matrix, None)

        phi1 = JGD(['1', 2, (1, 2, 'x')], np.array([[1], [-3], [4]]),
                   np.array([[4, 2, -2], [2, 5, -5], [-2, -5, 8]]))
        self.assertEqual(phi1.variables, ['1', 2, (1, 2, 'x')])
        np_test.assert_array_equal(phi1.mean,
                                   np.asarray([[1], [-3], [4]], dtype=float))
        np_test.assert_array_equal(
            phi1.covariance,
            np.asarray([[4, 2, -2], [2, 5, -5], [-2, -5, 8]], dtype=float))
        self.assertEqual(phi1._precision_matrix, None)

        phi2 = JGD(['1', 7, (1, 2, 'x')], [1, 2, 5],
                   np.array([[4, 2, -2], [2, 5, -5], [-2, -5, 8]]))
        self.assertEqual(phi2.variables, ['1', 7, (1, 2, 'x')])
        np_test.assert_array_equal(phi2.mean,
                                   np.asarray([[1], [2], [5]], dtype=float))
        np_test.assert_array_equal(
            phi2.covariance,
            np.asarray([[4, 2, -2], [2, 5, -5], [-2, -5, 8]], dtype=float))
        self.assertEqual(phi2._precision_matrix, None)

        phi3 = JGD([23], [0], [[1]])
        self.assertEqual(phi3.variables, [23])
        np_test.assert_array_equal(phi3.mean, np.asarray([[0]], dtype=float))
        np_test.assert_array_equal(phi3.covariance,
                                   np.asarray([[1]], dtype=float))
        self.assertEqual(phi3._precision_matrix, None)
Example #3
0
 def setUp(self):
     mean = np.array([-1, 1, -1])
     covariance = np.array([[1, 0.6, 0.5], [0.6, 2, 0.3], [0.5, 0.3, 1]])
     self.test_model = JGD(['x', 'y', 'z'], mean, covariance)
     position = [0, 0, 0]
     momentum = [-1, -1, -1]
     self.test_with_grad_log = LeapFrog(model=self.test_model,
                                        position=position,
                                        momentum=momentum,
                                        stepsize=0.3,
                                        grad_log_pdf=GradLogPDFGaussian,
                                        grad_log_position=None)
     grad_log_position, _ = GradLogPDFGaussian(
         position, self.test_model).get_gradient_log_pdf()
     self.test_without_grad_log = LeapFrog(
         model=self.test_model,
         position=position,
         momentum=momentum,
         stepsize=0.4,
         grad_log_pdf=GradLogPDFGaussian,
         grad_log_position=grad_log_position)
Example #4
0
 def setUp(self):
     mean = np.array([0, 0])
     covariance = np.array([[-1, 0.8], [0.8, 3]])
     self.test_model = JGD(['x', 'y'], mean, covariance)
     position = [0, 0]
     momentum = [-2, 1]
     self.test_with_grad_log = ModifiedEuler(
         model=self.test_model,
         position=position,
         momentum=momentum,
         stepsize=0.5,
         grad_log_pdf=GradLogPDFGaussian,
         grad_log_position=None)
     grad_log_position, _ = GradLogPDFGaussian(
         position, self.test_model).get_gradient_log_pdf()
     self.test_without_grad_log = ModifiedEuler(
         model=self.test_model,
         position=position,
         momentum=momentum,
         stepsize=0.3,
         grad_log_pdf=GradLogPDFGaussian,
         grad_log_position=grad_log_position)
Example #5
0
 def setUp(self):
     self.phi1 = JGD(['x1', 'x2', 'x3'], np.array([[1], [-3], [4]]),
                     np.array([[4, 2, -2], [2, 5, -5], [-2, -5, 8]]))
     self.phi2 = JGD(['x'], [0], [[1]])
     self.phi3 = self.phi1.copy()
Example #6
0
 def setUp(self):
     mean = np.array([-1, 1, 0])
     covariance = np.array([[6, 0.7, 0.2], [0.7, 3, 0.9], [0.2, 0.9, 1]])
     self.test_model = JGD(['x', 'y', 'z'], mean, covariance)
     self.nuts_sampler = NUTSda(model=self.test_model,
                                grad_log_pdf=GradLogPDFGaussian)
Example #7
0
 def setUp(self):
     mean = [-1, 1, -1]
     covariance = np.array([[3, 0.8, 0.2], [0.8, 2, 0.3], [0.2, 0.3, 1]])
     self.test_model = JGD(['x', 'y', 'z'], mean, covariance)
     self.hmc_sampler = HMCda(model=self.test_model,
                              grad_log_pdf=GradLogPDFGaussian)
Example #8
0
 def setUp(self):
     mean = np.array([1, 2, 3, 4])
     covariance = np.array([[1, 0.2, 0.4, 0.7], [0.2, 2, 0.5, 0.8],
                            [0.4, 0.5, 3, 0.6], [0.7, 0.8, 0.6, 4]])
     self.test_model = JGD(['x', 'y', 'z', 't'], mean, covariance)
     self.test_gradient = GradLogPDFGaussian([0, 0, 0, 0], self.test_model)