[Numpy-discussion] numpy.random.multinomial() cannot handle zero's
Stefan van der Walt
stefan at sun.ac.za
Sun Aug 26 19:36:27 EDT 2007
Hi Michael
On Sun, Aug 26, 2007 at 01:45:55PM +0100, Michael Nandris wrote:
> Is there an easy way around this problem, that does not involve fixing the API
> (like using NaN instead of 0.0)?
>
> >>> from numpy.random import multinomial
> >>> multinomial(100,[ 0.2, 0.4, 0.1, 0.3 ])
> array([19, 45, 10, 26])
> >>> multinomial( 100, [0.2, 0.0, 0.8, 0.0] )
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "mtrand.pyx", line 1173, in mtrand.RandomState.multinomial
> TypeError: exceptions must be strings, classes, or instances, not
> exceptions.ValueError
For some reason, the kahan_sum of [0.2,0.0,0.8,0.0] is ever so
slightly larger than 1.0 (in the order of 1e-16), but I'm not sure
why, yet (this isn't specific to kahan summation -- normal summation
shows the same behaviour).
As a quick workaround, you can subtract 1e-16 from all your
probabilities.
Regards
Stéfan
More information about the NumPy-Discussion
mailing list