Repository: pgmpy/pgmpy
Pull request #783 (2016-12-13 12:53:17)

Fixes issue #780
#780


Patch:
diff --git a/pgmpy/factors/continuous/CanonicalFactor.py b/pgmpy/factors/continuous/CanonicalFactor.py
index 2c64f4ae..993696ec 100644
--- a/pgmpy/factors/continuous/CanonicalFactor.py
+++ b/pgmpy/factors/continuous/CanonicalFactor.py
@@ -260,9 +260,7 @@ class CanonicalFactor(ContinuousFactor):
         h_j = self.h[index_to_reduce]
 
         # The values for the reduced variables.
-        y = np.array([value for index,
-                      value in sorted([(self.variables.index(var), value) for var,
-                                      value in values])]).reshape(len(index_to_reduce), 1)
+        y = np.array([value for var, value in values]).reshape(len(index_to_reduce), 1)
 
         phi.variables = [self.variables[index] for index in index_to_keep]
         phi.K = K_i_i
diff --git a/pgmpy/factors/continuous/JointGaussianDistribution.py b/pgmpy/factors/continuous/JointGaussianDistribution.py
index 6a093742..4aaabc7c 100644
--- a/pgmpy/factors/continuous/JointGaussianDistribution.py
+++ b/pgmpy/factors/continuous/JointGaussianDistribution.py
@@ -230,9 +230,8 @@ class JointGaussianDistribution(ContinuousFactor):
 
         mu_j = self.mean[index_to_keep]
         mu_i = self.mean[index_to_reduce]
-        x_i = np.array([value for index,
-                        value in sorted([(self.variables.index(var), value) for var,
-                                         value in values])]).reshape(len(index_to_reduce), 1)
+        x_i = np.array([value for var, value in values]).reshape(len(index_to_reduce), 1)
+
         sig_i_j = self.covariance[np.ix_(index_to_reduce, index_to_keep)]
         sig_j_i = self.covariance[np.ix_(index_to_keep, index_to_reduce)]
         sig_i_i_inv = np.linalg.inv(self.covariance[np.ix_(index_to_reduce, index_to_reduce)])
@@ -331,7 +330,7 @@ class JointGaussianDistribution(ContinuousFactor):
         >>> phi.g
         -6.51533
         """
-        from pgmpy.factors import CanonicalFactor
+        from pgmpy.factors.continuous import CanonicalFactor
 
         mu = self.mean
         sigma = self.covariance


Test patch:
diff --git a/pgmpy/tests/test_factors/test_continuous/test_Canonical_Factor.py b/pgmpy/tests/test_factors/test_continuous/test_Canonical_Factor.py
index 99a9bbda..b1a5cef0 100644
--- a/pgmpy/tests/test_factors/test_continuous/test_Canonical_Factor.py
+++ b/pgmpy/tests/test_factors/test_continuous/test_Canonical_Factor.py
@@ -96,14 +96,19 @@ class TestJGDMethods(unittest.TestCase):
         np_test.assert_almost_equal(self.phi1.h, np.array([[11.7], [-1.0]]))
         np_test.assert_almost_equal(self.phi1.g, -21.95)
 
-        self.phi1 = self.phi3
+        self.phi1 = self.phi3.copy()
         self.phi1.reduce([('x1', 4), ('x2', 1.23)])
         self.assertEqual(self.phi1.variables, ['x3'])
         np_test.assert_almost_equal(self.phi1.K, np.array([[4.0]]))
         np_test.assert_almost_equal(self.phi1.h, np.array([[1.46]]))
         np_test.assert_almost_equal(self.phi1.g, 0.8752)
 
-        self.phi1 = self.phi3
+        self.phi1 = self.phi3.copy()
+        self.phi1.reduce([('x2', 1.23), ('x1', 4)])
+        self.assertEqual(self.phi1.variables, ['x3'])
+        np_test.assert_almost_equal(self.phi1.K, np.array([[4.0]]))
+        np_test.assert_almost_equal(self.phi1.h, np.array([[1.46]]))
+        np_test.assert_almost_equal(self.phi1.g, 0.8752)
 
     def test_marginalize(self):
         phi = self.phi1.marginalize(['x1'], inplace=False)
diff --git a/pgmpy/tests/test_factors/test_continuous/test_JointGaussianDistribution.py b/pgmpy/tests/test_factors/test_continuous/test_JointGaussianDistribution.py
index 5fef3df8..d899334d 100644
--- a/pgmpy/tests/test_factors/test_continuous/test_JointGaussianDistribution.py
+++ b/pgmpy/tests/test_factors/test_continuous/test_JointGaussianDistribution.py
@@ -183,7 +183,7 @@ class TestJGDMethods(unittest.TestCase):
                                    np.asarray([[4, -4], [-4, 7]], dtype=float))
         self.assertEqual(self.phi1._precision_matrix, None)
 
-        self.phi1 = self.phi3
+        self.phi1 = self.phi3.copy()
         self.phi1.reduce([('x1', 3), ('x2', 1)])
         self.assertEqual(self.phi1.variables, ['x3'])
         np_test.assert_array_equal(self.phi1.mean, np.asarray([[0]], dtype=float))
@@ -191,7 +191,13 @@ class TestJGDMethods(unittest.TestCase):
                                    np.asarray([[3]], dtype=float))
         self.assertEqual(self.phi1._precision_matrix, None)
 
-        self.phi1 = self.phi3
+        self.phi1 = self.phi3.copy()
+        self.phi1.reduce([('x2', 1), ('x1', 3)])
+        self.assertEqual(self.phi1.variables, ['x3'])
+        np_test.assert_array_equal(self.phi1.mean, np.asarray([[0]], dtype=float))
+        np_test.assert_array_equal(self.phi1.covariance,
+                                   np.asarray([[3]], dtype=float))
+        self.assertEqual(self.phi1._precision_matrix, None)
 
     def test_normalize(self):
         phi = self.phi1.copy()