[Numpy-discussion] numpy.random.gamma returns 0 for small shape parameters

Massimo Di Pierro massimo.dipierro at gmail.com
Tue May 29 10:57:28 EDT 2012

Another possible solution is to sort the numbers and add them in a  
binary tree. It reduces the truncation error but makes the problem n- 
log-n and therefore not worth the trouble.


On May 29, 2012, at 9:45 AM, Pauli Virtanen wrote:

> Val Kalatsky <kalatsky <at> gmail.com> writes:
>> You'll need some patience to get non-zeros, especially for k=1e-5
>> In [84]: np.sum(np.random.gamma(1e-5,size=1000000)!=0.0)
>> Out[84]: 7259
>> that's less than 1%. For k=1e-4 it's ~7%
> To clarify: the distribution is peaked at numbers
> that are too small to be represented as floating-point
> numbers in the computer. The returned zeros indicate
> underflow, i.e., some positive numbers between zero
> and the floating point number closest to zero (~ 1e-308).
> To work around this, you need to do some math to redefine
> the problem so that the numbers involved fall into
> a region where the floating point numbers are dense.
> -- 
> Pauli Virtanen
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

More information about the NumPy-Discussion mailing list