Repository: pgmpy/pgmpy
Pull request #778 (2016-12-10 12:00:04)

Changes API parameter in GaussianLinearCPD
Fixes issue #767 


Patch:
diff --git a/pgmpy/factors/continuous/LinearGaussianCPD.py b/pgmpy/factors/continuous/LinearGaussianCPD.py
index 9913a3da..5b34d674 100644
--- a/pgmpy/factors/continuous/LinearGaussianCPD.py
+++ b/pgmpy/factors/continuous/LinearGaussianCPD.py
@@ -29,7 +29,7 @@ class LinearGaussianCPD(ContinuousFactor):
     p(Y |x) = N(β0 + β.T * x ; σ2)
 
     """
-    def __init__(self, variable, beta_0, variance, evidence=[], beta_vector=[]):
+    def __init__(self, variable, beta, variance, evidence=[]):
         """
         Parameters
         ----------
@@ -37,25 +37,22 @@ class LinearGaussianCPD(ContinuousFactor):
         variable: any hashable python object
             The variable whose CPD is defined.
 
-        beta_0: int, float
-            Represents the constant term in the linear equation.
+        beta: iterable of int or float
+            An iterable representing the coefficient vector of the linear equation.
+            The first term represents the constant term in the linear equation.
 
         variance: int, float
             The variance of the variable defined.
 
         evidence: iterable of any hashabale python objects
-            An iterable of the parents of the variable. None
-            if there are no parents.
-
-        beta_vector: iterable of int or float
-            An iterable representing the coefficient vector of the linear equation.
+            An iterable of the parents of the variable. None if there are no parents.
 
         Examples
         --------
 
         # For P(Y| X1, X2, X3) = N(-2x1 + 3x2 + 7x3 + 0.2; 9.6)
 
-        >>> cpd = LinearGaussianCPD('Y', 0.2, 9.6, ['X1', 'X2', 'X3'], [-2, 3, 7])
+        >>> cpd = LinearGaussianCPD('Y',  [0.2, -2, 3, 7], 9.6, ['X1', 'X2', 'X3'])
         >>> cpd.variable
         'Y'
         >>> cpd.variance
@@ -63,21 +60,20 @@ class LinearGaussianCPD(ContinuousFactor):
         >>> cpd.evidence
         ['x1', 'x2', 'x3']
         >>> cpd.beta_vector
-        [-2, 3, 7]
-        >>> cpd.beta_0
-        0.2
+        [0.2, -2, 3, 7]
 
         """
         self.variable = variable
-        self.beta_0 = beta_0
+        self.beta = beta
+        self.beta_0 = beta[0]
         self.variance = variance
 
-        if len(evidence) != len(beta_vector):
-            raise ValueError("The number of variables in evidence must be equal to the "
+        if len(evidence) != len(beta) - 1:
+            raise ValueError("The number of variables in evidence must be one less than the "
                              "length of the beta vector.")
 
         self.evidence = evidence
-        self.beta_vector = np.asarray(beta_vector)
+        self.beta_vector = np.asarray(beta[1:])
 
         variables = [variable] + evidence
         super(LinearGaussianCPD, self).__init__(variables, None)
@@ -104,15 +100,15 @@ class LinearGaussianCPD(ContinuousFactor):
         Examples
         --------
         >>> from pgmpy.factors.continuous import LinearGaussianCPD
-        >>> cpd = LinearGaussianCPD('Y', 0.2, 9.6, ['X1', 'X2', 'X3'], [-2, 3, 7])
+        >>> cpd = LinearGaussianCPD('Y',  [0.2, -2, 3, 7], 9.6, ['X1', 'X2', 'X3'])
         >>> copy_cpd = cpd.copy()
         >>> copy_cpd.variable
         'Y'
         >>> copy_cpd.evidence
         ['X1', 'X2', 'X3']
         """
-        copy_cpd = LinearGaussianCPD(self.variable, self.beta_0, self.variance,
-                                     list(self.evidence), self.beta_vector)
+        copy_cpd = LinearGaussianCPD(self.variable, self.beta, self.variance,
+                                     list(self.evidence))
 
         return copy_cpd
 
diff --git a/pgmpy/models/LinearGaussianBayesianNetwork.py b/pgmpy/models/LinearGaussianBayesianNetwork.py
index e53ab72a..90cdc165 100644
--- a/pgmpy/models/LinearGaussianBayesianNetwork.py
+++ b/pgmpy/models/LinearGaussianBayesianNetwork.py
@@ -35,9 +35,9 @@ class LinearGaussianBayesianNetwork(BayesianModel):
         >>> from pgmpy.models import LinearGaussianBayesianNetwork
         >>> from pgmpy.factors.continuous import LinearGaussianCPD
         >>> model = LinearGaussianBayesianNetwork([('x1', 'x2'), ('x2', 'x3')])
-        >>> cpd1 = LinearGaussianCPD('x1', 1, 4)
-        >>> cpd2 = LinearGaussianCPD('x2', -5, 4, ['x1'], [0.5])
-        >>> cpd3 = LinearGaussianCPD('x3', 4, 3, ['x2'], [-1])
+        >>> cpd1 = LinearGaussianCPD('x1', [1], 4)
+        >>> cpd2 = LinearGaussianCPD('x2', [-5, 0.5], 4, ['x1'])
+        >>> cpd3 = LinearGaussianCPD('x3', [4, -1], 3, ['x2'])
         >>> model.add_cpds(cpd1, cpd2, cpd3)
         >>> for cpd in model.cpds:
                 print(cpd)
@@ -83,9 +83,9 @@ class LinearGaussianBayesianNetwork(BayesianModel):
         >>> from pgmpy.models import LinearGaussianBayesianNetwork
         >>> from pgmpy.factors.continuous import LinearGaussianCPD
         >>> model = LinearGaussianBayesianNetwork([('x1', 'x2'), ('x2', 'x3')])
-        >>> cpd1 = LinearGaussianCPD('x1', 1, 4)
-        >>> cpd2 = LinearGaussianCPD('x2', -5, 4, ['x1'], [0.5])
-        >>> cpd3 = LinearGaussianCPD('x3', 4, 3, ['x2'], [-1])
+        >>> cpd1 = LinearGaussianCPD('x1', [1], 4)
+        >>> cpd2 = LinearGaussianCPD('x2', [-5, 0.5], 4, ['x1'])
+        >>> cpd3 = LinearGaussianCPD('x3', [4, -1], 3, ['x2'])
         >>> model.add_cpds(cpd1, cpd2, cpd3)
         >>> model.get_cpds()
         """
@@ -106,9 +106,9 @@ class LinearGaussianBayesianNetwork(BayesianModel):
         >>> from pgmpy.models import LinearGaussianBayesianNetwork
         >>> from pgmpy.factors.continuous import LinearGaussianCPD
         >>> model = LinearGaussianBayesianNetwork([('x1', 'x2'), ('x2', 'x3')])
-        >>> cpd1 = LinearGaussianCPD('x1', 1, 4)
-        >>> cpd2 = LinearGaussianCPD('x2', -5, 4, ['x1'], [0.5])
-        >>> cpd3 = LinearGaussianCPD('x3', 4, 3, ['x2'], [-1])
+        >>> cpd1 = LinearGaussianCPD('x1', [1], 4)
+        >>> cpd2 = LinearGaussianCPD('x2', [-5, 0.5], 4, ['x1'])
+        >>> cpd3 = LinearGaussianCPD('x3', [4, -1], 3, ['x2'])
         >>> model.add_cpds(cpd1, cpd2, cpd3)
         >>> for cpd in model.get_cpds():
                 print(cpd)
@@ -147,9 +147,9 @@ class LinearGaussianBayesianNetwork(BayesianModel):
         >>> from pgmpy.models import LinearGaussianBayesianNetwork
         >>> from pgmpy.factors.continuous import LinearGaussianCPD
         >>> model = LinearGaussianBayesianNetwork([('x1', 'x2'), ('x2', 'x3')])
-        >>> cpd1 = LinearGaussianCPD('x1', 1, 4)
-        >>> cpd2 = LinearGaussianCPD('x2', -5, 4, ['x1'], [0.5])
-        >>> cpd3 = LinearGaussianCPD('x3', 4, 3, ['x2'], [-1])
+        >>> cpd1 = LinearGaussianCPD('x1', [1], 4)
+        >>> cpd2 = LinearGaussianCPD('x2', [-5, 0.5], 4, ['x1'])
+        >>> cpd3 = LinearGaussianCPD('x3', [4, -1], 3, ['x2'])
         >>> model.add_cpds(cpd1, cpd2, cpd3)
         >>> jgd = model.to_joint_gaussian()
         >>> jgd.variables


Test patch:
diff --git a/pgmpy/tests/test_factors/test_continuous/test_Linear_Gaussain_CPD.py b/pgmpy/tests/test_factors/test_continuous/test_Linear_Gaussain_CPD.py
index f9d10946..38d426d0 100644
--- a/pgmpy/tests/test_factors/test_continuous/test_Linear_Gaussain_CPD.py
+++ b/pgmpy/tests/test_factors/test_continuous/test_Linear_Gaussain_CPD.py
@@ -7,34 +7,32 @@ from pgmpy.factors.continuous import LinearGaussianCPD
 
 class TestLGCPD(unittest.TestCase):
     def test_class_init(self):
-        cpd1 = LinearGaussianCPD('x', 0.23, 0.56)
+        cpd1 = LinearGaussianCPD('x', [0.23], 0.56)
         self.assertEqual(cpd1.variable, 'x')
         self.assertEqual(cpd1.beta_0, 0.23)
         self.assertEqual(cpd1.variance, 0.56)
 
-        cpd2 = LinearGaussianCPD('y', 0.67, 2, ['x1', 'x2', 'x3'],
-                                 [1, 4.56, 8])
+        cpd2 = LinearGaussianCPD('y', [0.67, 1, 4.56, 8], 2,
+                                 ['x1', 'x2', 'x3'])
         self.assertEqual(cpd2.variable, 'y')
         self.assertEqual(cpd2.beta_0, 0.67)
         self.assertEqual(cpd2.variance, 2)
         self.assertEqual(cpd2.evidence, ['x1', 'x2', 'x3'])
         np_test.assert_array_equal(cpd2.beta_vector, [1, 4.56, 8])
 
-        self.assertRaises(ValueError, LinearGaussianCPD, 'x', 1, 2,
-                          ['a', 'b', 'c'], [1, 2])
-        self.assertRaises(ValueError, LinearGaussianCPD, 'x', 1, 2,
-                          ['a', 'b'], [1, 2, 3])
+        self.assertRaises(ValueError, LinearGaussianCPD, 'x', [1, 1, 2], 2,
+                          ['a', 'b', 'c'])
+        self.assertRaises(ValueError, LinearGaussianCPD, 'x', [1, 1, 2, 3], 2,
+                          ['a', 'b'])
 
     def test_pdf(self):
-        cpd1 = LinearGaussianCPD('x', 0.23, 0.56)
-        cpd2 = LinearGaussianCPD('y', 0.67, 2, ['x1', 'x2', 'x3'],
-                                 [1, 4.56, 8])
+        cpd1 = LinearGaussianCPD('x', [0.23], 0.56)
+        cpd2 = LinearGaussianCPD('y', [0.67, 1, 4.56, 8], 2, ['x1', 'x2', 'x3'])
         np_test.assert_almost_equal(cpd1.assignment(1), 0.3139868)
         np_test.assert_almost_equal(cpd2.assignment(1, 1.2, 2.3, 3.4), 1.076e-162)
 
     def test_copy(self):
-        cpd = LinearGaussianCPD('y', 0.67, 2, ['x1', 'x2', 'x3'],
-                                [1, 4.56, 8])
+        cpd = LinearGaussianCPD('y', [0.67, 1, 4.56, 8], 2, ['x1', 'x2', 'x3'])
         copy = cpd.copy()
 
         self.assertEqual(cpd.variable, copy.variable)
@@ -68,9 +66,8 @@ class TestLGCPD(unittest.TestCase):
         np_test.assert_array_equal(cpd.beta_vector, [2, 2, 2])
 
     def test_str(self):
-        cpd1 = LinearGaussianCPD('x', 0.23, 0.56)
-        cpd2 = LinearGaussianCPD('y', 0.67, 2, ['x1', 'x2', 'x3'],
-                                 [1, 4.56, 8])
+        cpd1 = LinearGaussianCPD('x', [0.23], 0.56)
+        cpd2 = LinearGaussianCPD('y', [0.67, 1, 4.56, 8], 2, ['x1', 'x2', 'x3'])
         self.assertEqual(cpd1.__str__(), "P(x) = N(0.23; 0.56)")
         self.assertEqual(cpd2.__str__(), "P(y | x1, x2, x3) = N(1.0*x1 + "
                                          "4.56*x2 + 8.0*x3 + 0.67; 2)")
diff --git a/pgmpy/tests/test_models/test_LinearGaussianBayesianNetwork.py b/pgmpy/tests/test_models/test_LinearGaussianBayesianNetwork.py
index 749f6593..fc0be6f0 100644
--- a/pgmpy/tests/test_models/test_LinearGaussianBayesianNetwork.py
+++ b/pgmpy/tests/test_models/test_LinearGaussianBayesianNetwork.py
@@ -11,9 +11,9 @@ from pgmpy.models import LinearGaussianBayesianNetwork
 class TestLGBNMethods(unittest.TestCase):
     def setUp(self):
         self.model = LinearGaussianBayesianNetwork([('x1', 'x2'), ('x2', 'x3')])
-        self.cpd1 = LinearGaussianCPD('x1', 1, 4)
-        self.cpd2 = LinearGaussianCPD('x2', -5, 4, ['x1'], [0.5])
-        self.cpd3 = LinearGaussianCPD('x3', 4, 3, ['x2'], [-1])
+        self.cpd1 = LinearGaussianCPD('x1', [1], 4)
+        self.cpd2 = LinearGaussianCPD('x2', [-5, 0.5], 4, ['x1'])
+        self.cpd3 = LinearGaussianCPD('x3', [4, -1], 3, ['x2'])
 
     def test_add_cpds(self):
         self.model.add_cpds(self.cpd1)
@@ -60,7 +60,7 @@ class TestLGBNMethods(unittest.TestCase):
         self.assertEqual(self.model.check_model(), True)
 
         self.model.add_edge('x1', 'x4')
-        cpd4 = LinearGaussianCPD('x4', 4, 3, ['x2'], [-1])
+        cpd4 = LinearGaussianCPD('x4', [4, -1], 3, ['x2'])
         self.model.add_cpds(cpd4)
 
         self.assertRaises(ValueError, self.model.check_model)