diff --git a/include/boost/math/distributions/non_central_beta.hpp b/include/boost/math/distributions/non_central_beta.hpp index 00938ab9e4..b78da0521d 100644 --- a/include/boost/math/distributions/non_central_beta.hpp +++ b/include/boost/math/distributions/non_central_beta.hpp @@ -257,7 +257,17 @@ namespace boost value_type c = a + b + l / 2; value_type cross = 1 - (b / c) * (1 + l / (2 * c * c)); if(l == 0) - result = cdf(boost::math::beta_distribution(a, b), x); + { + if(x < y) + { + return invert + ? cdf(complement(boost::math::beta_distribution(a, b), x)) + : cdf(boost::math::beta_distribution(a, b), x); + } + return invert + ? cdf(boost::math::beta_distribution(b, a), y) + : cdf(complement(boost::math::beta_distribution(b, a), y)); + } else if(x > cross) { // Complement is the smaller of the two: diff --git a/test/test_nc_f.cpp b/test/test_nc_f.cpp index 7ad45c1ba3..7ed04ba883 100644 --- a/test/test_nc_f.cpp +++ b/test/test_nc_f.cpp @@ -336,6 +336,9 @@ void test_spots(RealType, const char* name = nullptr) for (RealType x : x_vals) { RealType P = cdf(dist_no_centrality, x); + RealType Q = cdf(complement(dist_no_centrality, x)); + // Regression test for #1463: CDF and CCDF must remain complementary for nc=0. + BOOST_CHECK_CLOSE(P + Q, RealType(1), tolerance); BOOST_CHECK_LE(dist.find_non_centrality(x, a, b, P), tolerance); } // Case when P=1 or P=0