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

Val Kalatsky kalatsky at gmail.com
Tue May 29 00:11:53 EDT 2012

```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%

Val

On Mon, May 28, 2012 at 10:33 PM, Uri Laserson <uri.laserson at gmail.com>wrote:

> I am trying to sample from a Dirichlet distribution, where some of the
> shape parameters are very small.  To do so, the algorithm samples each
> component individually from a Gamma(k,1) distribution where k is the shape
> parameter for that component of the Dirichlet.  In principle, this should
> always return a positive number (as the Dirichlet is defined).  However, if
> k is very small, it will return zero:
>
> In [157]: np.random.gamma(1e-1)
> Out[157]: 4.863866491339177e-06
>
> In [158]: np.random.gamma(1e-2)
> Out[158]: 2.424451829710714e-57
>
> In [159]: np.random.gamma(1e-3)
> Out[159]: 5.1909861689757784e-197
>
> In [160]: np.random.gamma(1e-4)
> Out[160]: 0.0
>
> In [161]: np.random.gamma(1e-5)
> Out[161]: 0.0
>
> What is the best way to deal with this?
>
> Thanks!
> Uri
>
>
>
>
> ...................................................................................
> Uri Laserson
> Harvard-MIT Division of Health Sciences and Technology
> M +1 917 742 8019
> laserson at mit.edu
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20120528/d7f6ef0c/attachment.html>
```