Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for binomial distribution quantile edge case #890

Merged
merged 1 commit into from
Dec 4, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions include/boost/math/distributions/binomial.hpp
Original file line number Diff line number Diff line change
@@ -224,7 +224,7 @@ namespace boost
// but zero is the best we can do:
return 0;
}
if(p == 1)
if(p == 1 || success_fraction == 1)
{ // Probability of n or fewer successes is always one,
// so n is the most sensible answer here:
return trials;
@@ -233,10 +233,6 @@ namespace boost
{ // p <= pdf(dist, 0) == cdf(dist, 0)
return 0; // So the only reasonable result is zero.
} // And root finder would fail otherwise.
if(success_fraction == 1)
{ // our formulae break down in this case:
return p > 0.5f ? trials : 0;
}

// Solve for quantile numerically:
//
1 change: 1 addition & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
@@ -893,6 +893,7 @@ test-suite distribution_tests :
[ run git_issue_800.cpp ../../test/build//boost_unit_test_framework ]
[ run git_issue_845.cpp ../../test/build//boost_unit_test_framework ]
[ run scipy_issue_17146.cpp ../../test/build//boost_unit_test_framework ]
[ run scipy_issue_17388.cpp ../../test/build//boost_unit_test_framework ]
;

test-suite mp :
27 changes: 27 additions & 0 deletions test/scipy_issue_17388.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright Matt Borland, 2022
// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0. (See accompanying file
// LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See: https://github.com/boostorg/math/issues/889
// See: https://github.com/scipy/scipy/issues/17388

#include <array>
#include <numeric>
#include <boost/math/distributions/binomial.hpp>
#include "math_unit_test.hpp"

int main(void)
{
// Test case 1: Goes against scipy convention but is correct
const auto binom_dist_1 {boost::math::binomial_distribution<double>(4, 0.5)};
// https://www.wolframalpha.com/input?i=quantile%28binomial+distribution%284%2C0.5%29%2C0%29
CHECK_ULP_CLOSE(boost::math::quantile(binom_dist_1, 0.0), 0.0, 1);

// Test case 2 from linked issue
const auto binom_dist_2 {boost::math::binomial_distribution<double>(4, 1)};
// https://www.wolframalpha.com/input?i=quantile%28binomial+distribution%284%2C1%29%2C0.3%29
CHECK_ULP_CLOSE(4.0, boost::math::quantile(binom_dist_2, 0.3), 1);

return boost::math::test::report_errors();
}