Repository: scipy/scipy

Issue:
scipy.stats multinomial pmf return nan
I'm trying to use the multinominal.pmf function from scipy.stats.

When I use this function where all probabilities in the input bigger than zero, it work fine. The problem is when I want to use the function when one of the probabilities is zero.

### Reproducing code example:

In [18]: multinomial.pmf([3, 3, 0], 6, [1/3.0, 1/3.0, 1/3.0])
Out[18]: 0.027434842249657095

In [19]: multinomial.pmf([3, 3, 0], 6, [2/3.0, 1/3.0, 0])
Out[19]: nan

As can be seen, in the first time where all probabilities > 0 there is no problem to use the function. However when I change one of the probabilities to zero, the function return nan, even through the function should return 0.21948.

### Scipy/Numpy/Python version information:
('1.0.0', '1.13.1', sys.version_info(major=2, minor=7, micro=13, releaselevel='final', serial=0))



scipy.stats multinomial pmf return nan
I'm trying to use the multinominal.pmf function from scipy.stats.

When I use this function where all probabilities in the input bigger than zero, it work fine. The problem is when I want to use the function when one of the probabilities is zero.

### Reproducing code example:

In [18]: multinomial.pmf([3, 3, 0], 6, [1/3.0, 1/3.0, 1/3.0])
Out[18]: 0.027434842249657095

In [19]: multinomial.pmf([3, 3, 0], 6, [2/3.0, 1/3.0, 0])
Out[19]: nan

As can be seen, in the first time where all probabilities > 0 there is no problem to use the function. However when I change one of the probabilities to zero, the function return nan, even through the function should return 0.21948.

### Scipy/Numpy/Python version information:
('1.0.0', '1.13.1', sys.version_info(major=2, minor=7, micro=13, releaselevel='final', serial=0))



scipy.stats multinomial pmf return nan
I'm trying to use the multinominal.pmf function from scipy.stats.

When I use this function where all probabilities in the input bigger than zero, it work fine. The problem is when I want to use the function when one of the probabilities is zero.

### Reproducing code example:

In [18]: multinomial.pmf([3, 3, 0], 6, [1/3.0, 1/3.0, 1/3.0])
Out[18]: 0.027434842249657095

In [19]: multinomial.pmf([3, 3, 0], 6, [2/3.0, 1/3.0, 0])
Out[19]: nan

As can be seen, in the first time where all probabilities > 0 there is no problem to use the function. However when I change one of the probabilities to zero, the function return nan, even through the function should return 0.21948.

### Scipy/Numpy/Python version information:
('1.0.0', '1.13.1', sys.version_info(major=2, minor=7, micro=13, releaselevel='final', serial=0))





Discussion:
In [ scipy/_multivariate.py](https://github.com/scipy/scipy/blob/master/scipy/stats/_multivariate.py), line 2950:

   `pcond = np.any(p <= 0, axis=-1)`

should be:

   `pcond = np.any(p < 0, axis=-1)`
@rfire01, thanks for reporting the issue, and @jagaudin thanks for tracking down the culprit.  


The issue was first [reported on stackoverflow](https://stackoverflow.com/questions/47894446/scipy-multinomial-pmf-return-nan) where @jagaudin gave an answer with more details about the bug.
@jagaudin do you mind submitting a fix PR for this too? 
Not at all. I need some dinner now, but will do ASAP.
In [ scipy/_multivariate.py](https://github.com/scipy/scipy/blob/master/scipy/stats/_multivariate.py), line 2950:

   `pcond = np.any(p <= 0, axis=-1)`

should be:

   `pcond = np.any(p < 0, axis=-1)`
@rfire01, thanks for reporting the issue, and @jagaudin thanks for tracking down the culprit.  


The issue was first [reported on stackoverflow](https://stackoverflow.com/questions/47894446/scipy-multinomial-pmf-return-nan) where @jagaudin gave an answer with more details about the bug.
@jagaudin do you mind submitting a fix PR for this too? 
Not at all. I need some dinner now, but will do ASAP.
In [ scipy/_multivariate.py](https://github.com/scipy/scipy/blob/master/scipy/stats/_multivariate.py), line 2950:

   `pcond = np.any(p <= 0, axis=-1)`

should be:

   `pcond = np.any(p < 0, axis=-1)`
@rfire01, thanks for reporting the issue, and @jagaudin thanks for tracking down the culprit.  


The issue was first [reported on stackoverflow](https://stackoverflow.com/questions/47894446/scipy-multinomial-pmf-return-nan) where @jagaudin gave an answer with more details about the bug.
@jagaudin do you mind submitting a fix PR for this too? 
Not at all. I need some dinner now, but will do ASAP.

Resolving patch:
diff --git a/scipy/stats/_multivariate.py b/scipy/stats/_multivariate.py
--- a/scipy/stats/_multivariate.py
+++ b/scipy/stats/_multivariate.py
@@ -2947,7 +2947,7 @@ def _process_parameters(self, n, p):
         p[...,-1] = 1. - p[...,:-1].sum(axis=-1)
 
         # true for bad p
-        pcond = np.any(p <= 0, axis=-1)
+        pcond = np.any(p < 0, axis=-1)
         pcond |= np.any(p > 1, axis=-1)
 
         n = np.array(n, dtype=np.int, copy=True)


Test patch:
diff --git a/scipy/stats/tests/test_multivariate.py b/scipy/stats/tests/test_multivariate.py
--- a/scipy/stats/tests/test_multivariate.py
+++ b/scipy/stats/tests/test_multivariate.py
@@ -1029,7 +1029,7 @@ def test_logpmf(self):
 
         vals3 = multinomial.logpmf([3, 4], 0, [-2, 3])
         assert_allclose(vals3, np.NAN, rtol=1e-8)
-
+        
     def test_reduces_binomial(self):
         # test that the multinomial pmf reduces to the binomial pmf in the 2d
         # case
@@ -1082,6 +1082,9 @@ def test_pmf(self):
 
         vals4 = multinomial.pmf([1,2], 4, (.3, .7))
         assert_allclose(vals4, 0, rtol=1e-8)
+        
+        vals5 = multinomial.pmf([3, 3, 0], 6, [2/3.0, 1/3.0, 0])
+        assert_allclose(vals5, 0.219478737997, rtol=1e-8)
 
     def test_pmf_broadcasting(self):
         vals0 = multinomial.pmf([1, 2], 3, [[.1, .9], [.2, .8]])