Repository: pgmpy/pgmpy

Issue:
Bug in printing LinearGaussianCPD
### Subject of the issue
Calling print on an instance of LinearGaussian CPD gives the wrong result.

### Your environment
* pgmpy version 0.1.16
* Python version 3.7.1
* Operating System Windows 10

### Steps to reproduce
Taken right from https://pgmpy.org/detailed_notebooks/7.%20Parameterizing%20with%20Continuous%20Variables.html:

from pgmpy.factors.continuous import LinearGaussianCPD
cpd = LinearGaussianCPD('Y',  [0.2, -2, 3, 7], 9.6, ['X1', 'X2', 'X3'])
print(cpd)

### Expected behaviour
Output should be P(Y | X1, X2, X3) = N(-2*X1 + 3*X2 + 7*X3 + 0.2; 9.6), as indicated in the documentation.

### Actual behaviour
Output is P(Y | X1, X2, X3) = N(0.2*X1 + -2*X2 + 3*X3 + 0.2; 9.6).

### Solution
Seems to be an off-by-one error. Replacing "zip(self.mean, self.evidence)" by "zip(self.mean[1:], self.evidence)" in [this line]( https://github.com/pgmpy/pgmpy/blob/caea6ef7c914464736818fb185a1d395937ed52f/pgmpy/factors/continuous/LinearGaussianCPD.py#L235) should fix it. The first entry of self.mean is the offset beta_0 and does not go with the first parent variable.


Discussion:
codecov[bot]: # [Codecov](https://codecov.io/gh/pgmpy/pgmpy/pull/1488?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pgmpy) Report
> Merging [#1488](https://codecov.io/gh/pgmpy/pgmpy/pull/1488?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pgmpy) (a2e4acb) into [dev](https://codecov.io/gh/pgmpy/pgmpy/commit/caea6ef7c914464736818fb185a1d395937ed52f?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pgmpy) (caea6ef) will **decrease** coverage by `0.69%`.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/pgmpy/pgmpy/pull/1488/graphs/tree.svg?width=650&height=150&src=pr&token=UaJMCdHaEF&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pgmpy)](https://codecov.io/gh/pgmpy/pgmpy/pull/1488?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pgmpy)

```diff
@@            Coverage Diff             @@
##              dev    #1488      +/-   ##
==========================================
- Coverage   94.72%   94.03%   -0.70%     
==========================================
  Files         143      143              
  Lines       16450    16448       -2     
==========================================
- Hits        15583    15467     -116     
- Misses        867      981     +114     
```


| [Impacted Files](https://codecov.io/gh/pgmpy/pgmpy/pull/1488?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pgmpy) | Coverage Δ | |
|---|---|---|
| [pgmpy/factors/continuous/LinearGaussianCPD.py](https://codecov.io/gh/pgmpy/pgmpy/pull/1488/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pgmpy#diff-cGdtcHkvZmFjdG9ycy9jb250aW51b3VzL0xpbmVhckdhdXNzaWFuQ1BELnB5) | `87.87% <ø> (+6.06%)` | :arrow_up: |
| [...actors/test\_continuous/test\_Linear\_Gaussian\_CPD.py](https://codecov.io/gh/pgmpy/pgmpy/pull/1488/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pgmpy#diff-cGdtcHkvdGVzdHMvdGVzdF9mYWN0b3JzL3Rlc3RfY29udGludW91cy90ZXN0X0xpbmVhcl9HYXVzc2lhbl9DUEQucHk=) | `48.38% <ø> (ø)` | |
| [pgmpy/readwrite/XMLBeliefNetwork.py](https://codecov.io/gh/pgmpy/pgmpy/pull/1488/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pgmpy#diff-cGdtcHkvcmVhZHdyaXRlL1hNTEJlbGllZk5ldHdvcmsucHk=) | `53.62% <0.00%> (-39.14%)` | :arrow_down: |
| [...gmpy/tests/test\_readwrite/test\_XMLBeliefNetwork.py](https://codecov.io/gh/pgmpy/pgmpy/pull/1488/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pgmpy#diff-cGdtcHkvdGVzdHMvdGVzdF9yZWFkd3JpdGUvdGVzdF9YTUxCZWxpZWZOZXR3b3JrLnB5) | `75.40% <0.00%> (-18.86%)` | :arrow_down: |
| [pgmpy/readwrite/PomdpX.py](https://codecov.io/gh/pgmpy/pgmpy/pull/1488/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pgmpy#diff-cGdtcHkvcmVhZHdyaXRlL1BvbWRwWC5weQ==) | `86.62% <0.00%> (-10.37%)` | :arrow_down: |
| [pgmpy/tests/test\_readwrite/test\_PomdpX.py](https://codecov.io/gh/pgmpy/pgmpy/pull/1488/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pgmpy#diff-cGdtcHkvdGVzdHMvdGVzdF9yZWFkd3JpdGUvdGVzdF9Qb21kcFgucHk=) | `83.68% <0.00%> (-9.22%)` | :arrow_down: |
| [pgmpy/estimators/MmhcEstimator.py](https://codecov.io/gh/pgmpy/pgmpy/pull/1488/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pgmpy#diff-cGdtcHkvZXN0aW1hdG9ycy9NbWhjRXN0aW1hdG9yLnB5) | `98.11% <0.00%> (-1.89%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pgmpy/pgmpy/pull/1488?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pgmpy).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pgmpy)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pgmpy/pgmpy/pull/1488?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pgmpy). Last update [caea6ef...a2e4acb](https://codecov.io/gh/pgmpy/pgmpy/pull/1488?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pgmpy). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pgmpy).


Patch:
diff --git a/pgmpy/factors/continuous/LinearGaussianCPD.py b/pgmpy/factors/continuous/LinearGaussianCPD.py
index aa45b670..c8b7414a 100644
--- a/pgmpy/factors/continuous/LinearGaussianCPD.py
+++ b/pgmpy/factors/continuous/LinearGaussianCPD.py
@@ -232,7 +232,7 @@ class LinearGaussianCPD(BaseFactor):
                 mu=" + ".join(
                     [
                         f"{coeff}*{parent}"
-                        for coeff, parent in zip(self.mean, self.evidence)
+                        for coeff, parent in zip(self.mean[1:], self.evidence)
                     ]
                 ),
                 b_0=str(self.mean[0]),


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_Gaussian_CPD.py
similarity index 96%
rename from pgmpy/tests/test_factors/test_continuous/test_Linear_Gaussain_CPD.py
rename to pgmpy/tests/test_factors/test_continuous/test_Linear_Gaussian_CPD.py
index ab420eaf..ed125a0b 100644
--- a/pgmpy/tests/test_factors/test_continuous/test_Linear_Gaussain_CPD.py
+++ b/pgmpy/tests/test_factors/test_continuous/test_Linear_Gaussian_CPD.py
@@ -77,14 +77,13 @@ class TestLGCPD(unittest.TestCase):
         copy.beta_vector = [2.2, 2.2, 2.2]
         np_test.assert_array_equal(cpd.beta_vector, [2, 2, 2])
 
-    @unittest.skip("TODO")
     def test_str(self):
         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)",
+            "P(y | x1, x2, x3) = N(1*x1 + " "4.56*x2 + 8*x3 + 0.67; 2)",
         )