Re: [Numpy-discussion] numpy.random.gamma returns 0 for small shape parameters
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. Massimo On May 29, 2012, at 9:45 AM, Pauli Virtanen wrote: > Val Kalatsky 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=100)!=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@scipy.org > http://mail.scipy.org/mailman/listinfo/numpy-discussion ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] numpy.random.gamma returns 0 for small shape parameters
Val Kalatsky 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=100)!=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@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] numpy.random.gamma returns 0 for small shape parameters
You'll need some patience to get non-zeros, especially for k=1e-5 In [84]: np.sum(np.random.gamma(1e-5,size=100)!=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 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 > Graduate Student, Biomedical Engineering > Harvard-MIT Division of Health Sciences and Technology > M +1 917 742 8019 > laser...@mit.edu > > ___ > NumPy-Discussion mailing list > NumPy-Discussion@scipy.org > http://mail.scipy.org/mailman/listinfo/numpy-discussion > > ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
[Numpy-discussion] numpy.random.gamma returns 0 for small shape parameters
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 Graduate Student, Biomedical Engineering Harvard-MIT Division of Health Sciences and Technology M +1 917 742 8019 laser...@mit.edu ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion