qweytr1 m@ili@g off m@il@ustc@edu@c@
2018-Dec-03 14:06 UTC
[Rd] Bug report: Function ppois(0:20, lambda=0.9) does not generate a non-decreasing result.
function ppois is a function calculate the CDF of Poisson distribution, it should generate a non-decreasing result, but what I got is:> any(diff(ppois(0:19,lambda=0.9))<0)[1] TRUE Actually,> ppois(19,lambda=0.9)<ppois(18,lambda=0.9)[1] TRUE Which could not be TRUE. Code is tested in both R 3.5.1 and Microsoft R Open 3.5.1. _ platform x86_64-w64-mingw32 arch x86_64 os mingw32 system x86_64, mingw32 status major 3 minor 5.1 year 2018 month 07 day 02 svn rev 74947 language R version.string R version 3.5.1 (2018-07-02) nickname Feather Spray PS. if set lambda=1, such bug will not appear. [[alternative HTML version deleted]]
IƱaki Ucar
2018-Dec-04 10:27 UTC
[Rd] Bug report: Function ppois(0:20, lambda=0.9) does not generate a non-decreasing result.
On Tue, 4 Dec 2018 at 11:12, <qweytr1 at mail.ustc.edu.cn> wrote:> > function ppois is a function calculate the CDF of Poisson distribution, it should generate a non-decreasing result, but what I got is: > > > any(diff(ppois(0:19,lambda=0.9))<0) > [1] TRUE > > Actually, > > > ppois(19,lambda=0.9)<ppois(18,lambda=0.9) > [1] TRUE > > Which could not be TRUE.This is just another manifestation of 0.1 * 3 > 0.3 #> [1] TRUE This discussion returns to this list from time to time. TLDR; this is not an R issue, but an unavoidable floating point issue. Solution: work with log-probabilities instead. any(diff(ppois(0:40, lambda=0.9, log.p=TRUE))<0) #> [1] FALSE I?aki
Serguei Sokol
2018-Dec-04 10:46 UTC
[Rd] Bug report: Function ppois(0:20, lambda=0.9) does not generate a non-decreasing result.
Le 04/12/2018 ? 11:27, I?aki Ucar a ?crit?:> On Tue, 4 Dec 2018 at 11:12, <qweytr1 at mail.ustc.edu.cn> wrote: >> function ppois is a function calculate the CDF of Poisson distribution, it should generate a non-decreasing result, but what I got is: >> >>> any(diff(ppois(0:19,lambda=0.9))<0) >> [1] TRUE >> >> Actually, >> >>> ppois(19,lambda=0.9)<ppois(18,lambda=0.9) >> [1] TRUE >> >> Which could not be TRUE. > This is just another manifestation of > > 0.1 * 3 > 0.3 > #> [1] TRUE > > This discussion returns to this list from time to time. TLDR; this is > not an R issue, but an unavoidable floating point issue.Well, here the request may be interpreted not as "do it without round error" which is indeed unavoidable but rather "please cope with rounding errors in a way that return consistent result for ppois()". You have indicated one way to do so (I have just added exp() in the row): any(diff(exp(ppois(0:19, lambda=0.9, log.p=TRUE))) < 0) #[1] FALSE But may be there is another, more economic way? Serguei.> Solution: > work with log-probabilities instead. > > any(diff(ppois(0:40, lambda=0.9, log.p=TRUE))<0) > #> [1] FALSE > > I?aki > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >-- Serguei Sokol Ingenieur de recherche INRA Cellule math?matiques LISBP, INSA/INRA UMR 792, INSA/CNRS UMR 5504 135 Avenue de Rangueil 31077 Toulouse Cedex 04 tel: +33 5 62 25 01 27 email: sokol at insa-toulouse.fr http://www.lisbp.fr