Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broadcasting fails in hypergeom.cdf #7985

Closed
gibbbone opened this issue Oct 6, 2017 · 4 comments
Closed

Broadcasting fails in hypergeom.cdf #7985

gibbbone opened this issue Oct 6, 2017 · 4 comments
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.stats

Comments

@gibbbone
Copy link

gibbbone commented Oct 6, 2017

Broadcasting arrays works in hypergeom.pmf but throws an error when I try to do it with hypergeom.cdf .

Reproducing code example:

import numpy as np
from scipy.stats import hypergeom

# this works
x = np.array([[1,2,3],[4,5,6],[11,12,13]])
M,n,N = [20,7,12]
hypergeom.pmf(x,M,n,N)
hypergeom.cdf(x,M,n,N)

# this works
M=np.array([25])
n=np.array([3,6,9])
N=np.array([2,4,6])
hypergeom.pmf(x,M,n,N)

# this doesn't work
hypergeom.cdf(x,M,n,N)

Error message:

TypeError                                 Traceback (most recent call last)
<ipython-input-3-49873b933b6b> in <module>()
     15 
     16 # this doesn't work
---> 17 hypergeom.cdf(x,M,n,N)

\Anaconda3\envs\env2.7bis\lib\site-packages\scipy\stats\_distn_infrastructure.pyc in cdf(self, k, *args, **kwds)
   2917         if np.any(cond):
   2918             goodargs = argsreduce(cond, *((k,)+args))
-> 2919             place(output, cond, np.clip(self._cdf(*goodargs), 0, 1))
   2920         if output.ndim == 0:
   2921             return output[()]

\Anaconda3\envs\env2.7bis\lib\site-packages\scipy\stats\_distn_infrastructure.pyc in _cdf(self, x, *args)
   2778     def _cdf(self, x, *args):
   2779         k = floor(x)
-> 2780         return self._cdfvec(k, *args)
   2781 
   2782     # generic _logcdf, _sf, _logsf, _ppf, _isf, _rvs defined in rv_generic

\Anaconda3\envs\env2.7bis\lib\site-packages\numpy\lib\function_base.pyc in __call__(self, *args, **kwargs)
   2732             vargs.extend([kwargs[_n] for _n in names])
   2733 
-> 2734         return self._vectorize_call(func=func, args=vargs)
   2735 
   2736     def _get_ufunc_and_otypes(self, func, args):

\Anaconda3\envs\env2.7bis\lib\site-packages\numpy\lib\function_base.pyc in _vectorize_call(self, func, args)
   2808                       for a in args]
   2809 
-> 2810             outputs = ufunc(*inputs)
   2811 
   2812             if ufunc.nout == 1:

\Anaconda3\envs\env2.7bis\lib\site-packages\scipy\stats\_distn_infrastructure.pyc in _cdf_single(self, k, *args)
   2773 
   2774     def _cdf_single(self, k, *args):
-> 2775         m = arange(int(self.a), k+1)
   2776         return np.sum(self._pmf(m, *args), axis=0)
   2777 

TypeError: only length-1 arrays can be converted to Python scalars

Scipy/Numpy/Python version information:

'0.19.1'
'1.13.1'
sys.version_info(major=2, minor=7, micro=12, releaselevel='final', serial=0)
@eneskemalergin
Copy link

Hello guys is anyone working on this could provide a solution. It is really weird that all other functions work but cdf will thrown the TypeError. I am currently using 1-sf, but cdf would be really helpful thanks.

@WarrenWeckesser WarrenWeckesser added the defect A clear bug or issue that prevents SciPy from being installed or used as expected label Mar 12, 2018
@WarrenWeckesser
Copy link
Member

As of version '1.1.0.dev0+c10dc21', this is not fixed. I don't know if anyone is working on it.

Until it is fixed, here's a work-around using numpy.vectorize:

In [186]: from scipy.stats import hypergeom

In [187]: hypergeom_cdf = np.vectorize(lambda x, M, n, N, loc=0: hypergeom.cdf(x, M, n, N, loc))

In [188]: x = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])

In [189]: M = np.array([12])

In [190]: n = np.array([2, 5, 8])

In [191]: N = np.array([6, 7, 8])

In [192]: hypergeom_cdf(x, M, n, N)
Out[192]: 
array([[ 0.22727273,  0.04545455,  0.        ],
       [ 1.        ,  0.97348485,  0.59393939],
       [ 1.        ,  1.        ,  1.        ]])

@sethtroisi
Copy link
Contributor

This seems fixed at head.

I thought it might have been fixed by #10091. I syncing to 10090 and code sample passed so it seems to be something else, because I can't find the PR that fixed this...

@WarrenWeckesser Would you mind testing that the code sample passes at head and closing if so?

@WarrenWeckesser
Copy link
Member

WarrenWeckesser commented May 21, 2020

@sethtroisi, I suspect it was fixed by #9900.

While verifying this issue was fixed, I found a different problem: the hypergeom methods don't handle lists of numbers in many cases, e.g.

In [60]: hypergeom.pmf(2, 12, 3, [6, 7, 8])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-60-e6197be96186> in <module>
----> 1 hypergeom.pmf(2, 12, 3, [6, 7, 8])

~/mc37scipy/lib/python3.7/site-packages/scipy-1.5.0.dev0+23b6191-py3.7-macosx-10.7-x86_64.egg/scipy/stats/_distn_infrastructure.py in pmf(self, k, *args, **kwds)
   3018         """
   3019         args, loc, _ = self._parse_args(*args, **kwds)
-> 3020         _a, _b = self._get_support(*args)
   3021         k, loc = map(asarray, (k, loc))
   3022         args = tuple(map(asarray, args))

~/mc37scipy/lib/python3.7/site-packages/scipy-1.5.0.dev0+23b6191-py3.7-macosx-10.7-x86_64.egg/scipy/stats/_discrete_distns.py in _get_support(self, M, n, N)
    432 
    433     def _get_support(self, M, n, N):
--> 434         return np.maximum(N-(M-n), 0), np.minimum(n, N)
    435 
    436     def _argcheck(self, M, n, N):

TypeError: unsupported operand type(s) for -: 'list' and 'int'

I'll create a new issue for that.

Since this issue is about broadcasting, and broadcasting now works, I'll close it.

Edit: github issue for the problem noted above: #12178

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.stats
Projects
None yet
Development

No branches or pull requests

5 participants