[SciPy-User] Question about the behavior of hypergeom.cdf

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[SciPy-User] Question about the behavior of hypergeom.cdf

giovannibonaccorsi
Hi everyone, 
first post here. I have a doubt about broadcasting in scipy. 

I'm trying to apply broadcasting with hypergeom. The problem is that while hypergeom.pmf() accept my arrays without complaining the hypergeom.cdf() function throws out this error: 

TypeError: only length-1 arrays can be converted to Python scalars
Now, I don't know if this is the expected behavior of the function, but from the documentation it doesn't seem necessary to take particular care to arrays argument in the hypergeom.cdf() function with respect to hypergeom.pmf(). Is that right or have I missed something? 

I'm attaching a minimal example to reproduce the error, I'm using Python2.7 through Anaconda with scipy and numpy updated to the last releases.

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)

Thanks a lot for the support.,
Giovanni


_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Question about the behavior of hypergeom.cdf

Juan Nunez-Iglesias
I’m not familiar with this part of the codebase, but after a bit of playing around, I think this isn’t yet implemented, maybe because it’s too hard? The relevant in scipy/stats/_distn_infrastructure.py call is:

-> 2775         m = arange(int(self.a), k+1)

self.a is the lower bound of the cdf to be computed, in this case, an array [0, 0, 0] to match the input N = self.b = [2, 4, 6]. The problem of course is that there is no general way to vectorize np.arange, because you would get a jagged array.

I hope someone with more knowledge of the library will chime in, but I think you’ll have to implement your own looping logic in the short-to-medium term…

Juan.

On 7 Oct 2017, 8:36 PM +1100, Giovanni Bonaccorsi <[hidden email]>, wrote:
Hi everyone, 
first post here. I have a doubt about broadcasting in scipy. 

I'm trying to apply broadcasting with hypergeom. The problem is that while hypergeom.pmf() accept my arrays without complaining the hypergeom.cdf() function throws out this error: 

TypeError: only length-1 arrays can be converted to Python scalars
Now, I don't know if this is the expected behavior of the function, but from the documentation it doesn't seem necessary to take particular care to arrays argument in the hypergeom.cdf() function with respect to hypergeom.pmf(). Is that right or have I missed something? 

I'm attaching a minimal example to reproduce the error, I'm using Python2.7 through Anaconda with scipy and numpy updated to the last releases.

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)

Thanks a lot for the support.,
Giovanni

_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user

_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Question about the behavior of hypergeom.cdf

josef.pktd


On Sun, Oct 8, 2017 at 9:59 PM, Juan Nunez-Iglesias <[hidden email]> wrote:
I’m not familiar with this part of the codebase, but after a bit of playing around, I think this isn’t yet implemented, maybe because it’s too hard? The relevant in scipy/stats/_distn_infrastructure.py call is:

-> 2775         m = arange(int(self.a), k+1)

self.a is the lower bound of the cdf to be computed, in this case, an array [0, 0, 0] to match the input N = self.b = [2, 4, 6]. The problem of course is that there is no general way to vectorize np.arange, because you would get a jagged array.

I hope someone with more knowledge of the library will chime in, but I think you’ll have to implement your own looping logic in the short-to-medium term…

Juan.

On 7 Oct 2017, 8:36 PM +1100, Giovanni Bonaccorsi <[hidden email]>, wrote:
Hi everyone, 
first post here. I have a doubt about broadcasting in scipy. 

I'm trying to apply broadcasting with hypergeom. The problem is that while hypergeom.pmf() accept my arrays without complaining the hypergeom.cdf() function throws out this error: 

TypeError: only length-1 arrays can be converted to Python scalars
Now, I don't know if this is the expected behavior of the function, but from the documentation it doesn't seem necessary to take particular care to arrays argument in the hypergeom.cdf() function with respect to hypergeom.pmf(). Is that right or have I missed something? 

I'm attaching a minimal example to reproduce the error, I'm using Python2.7 through Anaconda with scipy and numpy updated to the last releases.

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)

I haven't seriously looked at the distribution code in a while.

The reason was and, I guess, is that the support of a distribution is not vectorized, i.e. the bounds.a and .b.
This means that in distributions where the support depends on a shape parameter some methods are not vectorized in that parameter. 
It might work in some cases but there is no generic support for it in the base classes. AFAIK .a and .b are always scalar

However, I don't remember much discussion about the discrete distributions like hypergeom.

Josef

 

Thanks a lot for the support.,
Giovanni

_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user

_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user



_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user