Opened 4 years ago

Closed 4 years ago

#8308 closed Bugs (fixed)

Poisson Quantile function returns wrong results in certain cases

Reported by: Caleb.Welton@… Owned by: johnmaddock
Milestone: To Be Determined Component: math
Version: Boost 1.52.0 Severity: Problem
Keywords: Cc:


Sample use case:

main() {
  double pi = 3.1415927;
  boost::math::poisson poisson(pi);

  //  std::cout << quantile(poisson, 0.00) << " ";  
  std::cout << quantile(poisson, 0.05) << " ";
  std::cout << quantile(poisson, 0.30) << " ";
  std::cout << quantile(poisson, pi/6) << " ";
  std::cout << quantile(poisson, 0.7) << " ";
  std::cout << quantile(poisson, 1-1e-10) << " ";
  // std:: cout << quantile(poisson, 1) << std::endl;  

Produces the results (error), 0, 1, 3, 4, 20, (error)

Similar code in R or MATLAB results in: (0),1,2,3,4,20,(Inf)

R> qpois(c(0,0.05,0.3,pi/6,0.7,1-1e-10,1),pi)
[1]   0   1   2   3   4  20 Inf

MATLAB> f := stats::poissonQuantile(PI):
MATLAB> f(0), f(1/20), f(0.3), f(PI/6), f(0.7), f(1-1/10^10), f(1)

The errors on the edge cases are less troubling than the seemingly incorrect results at low probabilities.

Attachments (0)

Change History (2)

comment:1 Changed 4 years ago by johnmaddock

I'll look into the edge cases (0 and 1) shortly - the differences in the other values you see are intended behaviour, given:

poisson pd(constants::pi<double>());

then the default behaviour of:

quantile(pd, 0.05)

is to return the largest integer i such that cdf(pd, i) <= 0.05, which really is the value 0, not 1 as the other programs suggest.

Whether that is actually the correct thing to do depends upon the actual test you are conducting (I assume R and Mathlab calculate the quantile as a real-value and then round to the nearest int). You can configure Boost.Math to do whatever you want here, see:

comment:2 Changed 4 years ago by johnmaddock

  • Resolution set to fixed
  • Status changed from new to closed

(In [83512]) Fix edge case in Halley iteration where the derivative is flatlining. Fix edge cases in poisson distribution quantile. Fixes #8314. Fixes #8308. Update and regenerate docs.

Add Comment

Modify Ticket

Change Properties
Set your email in Preferences
as closed The owner will remain johnmaddock.
The resolution will be deleted. Next status will be 'reopened'.

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.