From 43e1bc0d0e2753e4c7894c3d144f34b6889ffb20 Mon Sep 17 00:00:00 2001 From: Aki Vehtari Date: Sun, 1 Nov 2026 19:40:06 +0200 Subject: [PATCH 1/6] Compute beta(a, b) as exp(lbeta(a, b)) for nonnegative arguments --- .../kernel_generator/elt_function_cl.hpp | 5 +- .../opencl/kernels/device_functions/beta.hpp | 9 +++ stan/math/prim/fun/beta.hpp | 9 +++ .../math/opencl/rev/beta_reference_test.cpp | 75 +++++++++++++++++++ test/unit/math/prim/fun/beta_reference.hpp | 35 +++++++++ test/unit/math/prim/fun/beta_test.cpp | 38 +++++++++- test/unit/math/rev/prob/beta_lcdf_test.cpp | 28 +++++++ 7 files changed, 196 insertions(+), 3 deletions(-) create mode 100644 test/unit/math/opencl/rev/beta_reference_test.cpp create mode 100644 test/unit/math/prim/fun/beta_reference.hpp create mode 100644 test/unit/math/rev/prob/beta_lcdf_test.cpp diff --git a/stan/math/opencl/kernel_generator/elt_function_cl.hpp b/stan/math/opencl/kernel_generator/elt_function_cl.hpp index e691182d154..e5be4518ab5 100644 --- a/stan/math/opencl/kernel_generator/elt_function_cl.hpp +++ b/stan/math/opencl/kernel_generator/elt_function_cl.hpp @@ -352,7 +352,10 @@ ADD_BINARY_FUNCTION_WITH_INCLUDES(copysign) ADD_BINARY_FUNCTION_WITH_INCLUDES( erfcx_derivative, stan::math::opencl_kernels::erfcx_device_function) ADD_BINARY_FUNCTION_WITH_INCLUDES( - beta, stan::math::opencl_kernels::beta_device_function) + beta, stan::math::opencl_kernels::lgamma_stirling_device_function, + stan::math::opencl_kernels::lgamma_stirling_diff_device_function, + stan::math::opencl_kernels::lbeta_device_function, + stan::math::opencl_kernels::beta_device_function) ADD_BINARY_FUNCTION_WITH_INCLUDES( binomial_coefficient_log, stan::math::opencl_kernels::lgamma_stirling_device_function, diff --git a/stan/math/opencl/kernels/device_functions/beta.hpp b/stan/math/opencl/kernels/device_functions/beta.hpp index e634cddb07c..1c656dd7a52 100644 --- a/stan/math/opencl/kernels/device_functions/beta.hpp +++ b/stan/math/opencl/kernels/device_functions/beta.hpp @@ -18,11 +18,20 @@ static constexpr const char* beta_device_function * Return the beta function applied to the specified * arguments. * + * For nonnegative arguments the value is exp(stan_lbeta(a, b)), + * which keeps its accuracy for large arguments. Negative and + * NaN arguments use the lgamma difference. The kernel must + * include the lgamma_stirling, lgamma_stirling_diff and lbeta + * device functions before this one. + * * @param a First value * @param b Second value * @return beta(a, b) */ double beta(double a, double b) { + if (a >= 0 && b >= 0) { + return exp(stan_lbeta(a, b)); + } return exp(lgamma(a) + lgamma(b) - lgamma(a + b)); } // \cond diff --git a/stan/math/prim/fun/beta.hpp b/stan/math/prim/fun/beta.hpp index fefcf44b479..0c1cbe5f473 100644 --- a/stan/math/prim/fun/beta.hpp +++ b/stan/math/prim/fun/beta.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -43,6 +44,11 @@ namespace math { \end{cases} \f] * + * For nonnegative arguments the value is exp(lbeta(a, b)). The + * difference lgamma(a) + lgamma(b) - lgamma(a + b) has a relative error + * of about b log(b) times the machine epsilon for large b; lbeta does + * not. Negative and NaN arguments use the lgamma difference. + * * @tparam T1 type of first value * @tparam T2 type of second value * @param a First value @@ -52,6 +58,9 @@ namespace math { template * = nullptr> inline return_type_t beta(const T1 a, const T2 b) { using std::exp; + if (a >= 0 && b >= 0) { + return exp(lbeta(a, b)); + } return exp(lgamma(a) + lgamma(b) - lgamma(a + b)); } diff --git a/test/unit/math/opencl/rev/beta_reference_test.cpp b/test/unit/math/opencl/rev/beta_reference_test.cpp new file mode 100644 index 00000000000..8828ccf461b --- /dev/null +++ b/test/unit/math/opencl/rev/beta_reference_test.cpp @@ -0,0 +1,75 @@ +#ifdef STAN_OPENCL +#include +#include +#include +#include +#include +#include +#include + +namespace beta_reference_test { + +Eigen::VectorXd column(int k) { + const auto& rows = stan::test::beta_large_reference; + Eigen::VectorXd v(rows.size()); + for (size_t i = 0; i < rows.size(); ++i) { + v[i] = rows[i][k]; + } + return v; +} + +} // namespace beta_reference_test + +TEST(OpenCL_beta, prim_matches_reference) { + using stan::math::matrix_cl; + const auto& rows = stan::test::beta_large_reference; + matrix_cl a_cl(beta_reference_test::column(0)); + matrix_cl b_cl(beta_reference_test::column(1)); + matrix_cl res_cl = stan::math::beta(a_cl, b_cl); + Eigen::VectorXd res = stan::math::from_matrix_cl(res_cl); + for (size_t i = 0; i < rows.size(); ++i) { + SCOPED_TRACE(::testing::Message() + << "a = " << rows[i][0] << ", b = " << rows[i][1]); + EXPECT_NEAR(rows[i][2], res[i], 1e-13 * rows[i][2]); + } +} + +TEST(OpenCL_beta, rev_value_matches_reference) { + using stan::math::matrix_cl; + using stan::math::var_value; + const auto& rows = stan::test::beta_large_reference; + var_value> a{ + matrix_cl(beta_reference_test::column(0))}; + var_value> b{ + matrix_cl(beta_reference_test::column(1))}; + var_value> res = stan::math::beta(a, b); + Eigen::VectorXd val = stan::math::from_matrix_cl(res.val()); + for (size_t i = 0; i < rows.size(); ++i) { + SCOPED_TRACE(::testing::Message() + << "a = " << rows[i][0] << ", b = " << rows[i][1]); + EXPECT_NEAR(rows[i][2], val[i], 1e-13 * rows[i][2]); + } + stan::math::recover_memory(); +} + +TEST(OpenCL_beta, limits) { + using stan::math::matrix_cl; + const double inf = std::numeric_limits::infinity(); + const double nan = std::numeric_limits::quiet_NaN(); + Eigen::VectorXd a(9); + Eigen::VectorXd b(9); + a << inf, 1.65, inf, 1e-300, 0.0, 0.0, 0.0, inf, nan; + b << 2.16, inf, inf, inf, 2.16, 0.0, inf, 0.0, inf; + matrix_cl a_cl(a); + matrix_cl b_cl(b); + matrix_cl res_cl = stan::math::beta(a_cl, b_cl); + Eigen::VectorXd res = stan::math::from_matrix_cl(res_cl); + for (int i = 0; i < 4; ++i) { + EXPECT_EQ(0.0, res[i]) << "i = " << i; + } + for (int i = 4; i < 8; ++i) { + EXPECT_EQ(inf, res[i]) << "i = " << i; + } + EXPECT_TRUE(std::isnan(res[8])); +} +#endif diff --git a/test/unit/math/prim/fun/beta_reference.hpp b/test/unit/math/prim/fun/beta_reference.hpp new file mode 100644 index 00000000000..10f464f4bd6 --- /dev/null +++ b/test/unit/math/prim/fun/beta_reference.hpp @@ -0,0 +1,35 @@ +#ifndef TEST_UNIT_MATH_PRIM_FUN_BETA_REFERENCE_HPP +#define TEST_UNIT_MATH_PRIM_FUN_BETA_REFERENCE_HPP + +#include +#include + +namespace stan { +namespace test { +// Reference values of beta(a, b) from mpmath at 50 digits (mp.beta, checked +// against exp(loggamma(a) + loggamma(b) - loggamma(a + b))). Columns: a, b, +// beta(a, b). One argument is large, in both argument orders. +inline const std::vector> beta_large_reference{{ + {0.5, 1000000.0, 0.0017724540724622611}, + {1000000.0, 0.5, 0.0017724540724622611}, + {0.5, 1000000000.0, 5.6049912170985526e-05}, + {1000000000.0, 0.5, 5.6049912170985526e-05}, + {0.5, 2147483648.0, 3.824811210291909e-05}, + {2147483648.0, 0.5, 3.824811210291909e-05}, + {2.0, 1000000.0, 9.99999000001e-13}, + {1000000.0, 2.0, 9.99999000001e-13}, + {2.0, 1000000000.0, 9.99999999e-19}, + {1000000000.0, 2.0, 9.99999999e-19}, + {2.0, 2147483648.0, 2.168404343961267e-19}, + {2147483648.0, 2.0, 2.168404343961267e-19}, + {10.0, 1000000.0, 3.628636708191183e-55}, + {1000000.0, 10.0, 3.628636708191183e-55}, + {10.0, 1000000000.0, 3.6287998367040043e-85}, + {1000000000.0, 10.0, 3.6287998367040043e-85}, + {10.0, 2147483648.0, 1.7396599603107283e-88}, + {2147483648.0, 10.0, 1.7396599603107283e-88}, +}}; +} // namespace test +} // namespace stan + +#endif diff --git a/test/unit/math/prim/fun/beta_test.cpp b/test/unit/math/prim/fun/beta_test.cpp index 371bec37bd5..7c45dcdf8e6 100644 --- a/test/unit/math/prim/fun/beta_test.cpp +++ b/test/unit/math/prim/fun/beta_test.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -17,9 +18,42 @@ TEST(MathFunctions, beta_nan) { EXPECT_TRUE(std::isnan(beta(NOT_A_NUMBER, 2.16))); EXPECT_TRUE(std::isnan(beta(1.65, NOT_A_NUMBER))); + EXPECT_TRUE(std::isnan(beta(NOT_A_NUMBER, INFTY))); + EXPECT_TRUE(std::isnan(beta(0.0, NOT_A_NUMBER))); +} + +TEST(MathFunctions, beta_limits) { + using stan::math::beta; + using stan::math::INFTY; + + EXPECT_EQ(0.0, beta(INFTY, 2.16)); + EXPECT_EQ(0.0, beta(1.65, INFTY)); + EXPECT_EQ(0.0, beta(INFTY, INFTY)); + EXPECT_EQ(0.0, beta(1e-300, INFTY)); + EXPECT_EQ(INFTY, beta(0.0, 2.16)); + EXPECT_EQ(INFTY, beta(0.0, 0.0)); + EXPECT_EQ(INFTY, beta(0.0, INFTY)); + EXPECT_EQ(INFTY, beta(INFTY, 0.0)); +} - EXPECT_TRUE(std::isnan(beta(INFTY, 2.16))); - EXPECT_TRUE(std::isnan(beta(1.65, INFTY))); +TEST(MathFunctions, beta_large_arguments) { + using stan::math::beta; + for (const auto& r : stan::test::beta_large_reference) { + SCOPED_TRACE(::testing::Message() << "a = " << r[0] << ", b = " << r[1]); + EXPECT_NEAR(r[2], beta(r[0], r[1]), 1e-13 * r[2]); + } +} + +TEST(MathFunctions, beta_small_arguments_unchanged) { + using stan::math::beta; + using stan::math::lgamma; + // Below 10, lbeta sums the same three lgamma values. + const double xs[] = {1e-3, 0.37, 1.0, 2.5, 7.25, 9.99}; + for (double a : xs) { + for (double b : xs) { + EXPECT_EQ(std::exp(lgamma(a) + lgamma(b) - lgamma(a + b)), beta(a, b)); + } + } } TEST(MathFunctions, beta_vec) { diff --git a/test/unit/math/rev/prob/beta_lcdf_test.cpp b/test/unit/math/rev/prob/beta_lcdf_test.cpp new file mode 100644 index 00000000000..ffac0b53add --- /dev/null +++ b/test/unit/math/rev/prob/beta_lcdf_test.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +#include +#include +#include + +TEST_F(AgradRev, ProbDistributionsBeta_lcdf_large_beta_d_y) { + using stan::math::var; + // Reference values from mpmath at 50 digits. Columns: y, alpha, beta, + // beta_lcdf(y | alpha, beta), its derivative with respect to y. y and + // 1 - y are exact, so the derivative depends on beta(alpha, beta). + const std::vector> rows{{ + {1.862645149230957e-09, 2.0, 1073741824.0, -0.5208858063911924, + 489281430.6449945}, + {2.3283064365386963e-10, 2.0, 2147483648.0, -2.40568139077329, + 7219826854.641048}, + }}; + for (const auto& r : rows) { + SCOPED_TRACE(::testing::Message() << "beta = " << r[2]); + var y = r[0]; + var lp = stan::math::beta_lcdf(y, r[1], r[2]); + lp.grad(); + EXPECT_NEAR(r[3], lp.val(), 1e-12 * std::fabs(r[3])); + EXPECT_NEAR(r[4], y.adj(), 1e-12 * r[4]); + stan::math::recover_memory(); + } +} From a64c3679faab9dd4f1608c42e91072dd9fe02190 Mon Sep 17 00:00:00 2001 From: Aki Vehtari Date: Sun, 1 Nov 2026 22:31:43 +0200 Subject: [PATCH 2/6] Use exp(lbeta) in beta() only for finite positive arguments --- .../opencl/kernels/device_functions/beta.hpp | 9 +++--- stan/math/prim/fun/beta.hpp | 7 ++-- .../math/opencl/rev/beta_reference_test.cpp | 32 ++++++++++++------- test/unit/math/prim/fun/beta_test.cpp | 17 ++-------- 4 files changed, 32 insertions(+), 33 deletions(-) diff --git a/stan/math/opencl/kernels/device_functions/beta.hpp b/stan/math/opencl/kernels/device_functions/beta.hpp index 1c656dd7a52..964fd57f32f 100644 --- a/stan/math/opencl/kernels/device_functions/beta.hpp +++ b/stan/math/opencl/kernels/device_functions/beta.hpp @@ -18,9 +18,10 @@ static constexpr const char* beta_device_function * Return the beta function applied to the specified * arguments. * - * For nonnegative arguments the value is exp(stan_lbeta(a, b)), - * which keeps its accuracy for large arguments. Negative and - * NaN arguments use the lgamma difference. The kernel must + * For finite positive arguments the value is + * exp(stan_lbeta(a, b)), which keeps its accuracy for large + * arguments. Other arguments use the lgamma difference. The + * kernel must * include the lgamma_stirling, lgamma_stirling_diff and lbeta * device functions before this one. * @@ -29,7 +30,7 @@ static constexpr const char* beta_device_function * @return beta(a, b) */ double beta(double a, double b) { - if (a >= 0 && b >= 0) { + if (a > 0 && b > 0 && a < INFINITY && b < INFINITY) { return exp(stan_lbeta(a, b)); } return exp(lgamma(a) + lgamma(b) - lgamma(a + b)); diff --git a/stan/math/prim/fun/beta.hpp b/stan/math/prim/fun/beta.hpp index 0c1cbe5f473..ab92908dc0a 100644 --- a/stan/math/prim/fun/beta.hpp +++ b/stan/math/prim/fun/beta.hpp @@ -2,6 +2,7 @@ #define STAN_MATH_PRIM_FUN_BETA_HPP #include +#include #include #include #include @@ -44,10 +45,10 @@ namespace math { \end{cases} \f] * - * For nonnegative arguments the value is exp(lbeta(a, b)). The + * For finite positive arguments the value is exp(lbeta(a, b)). The * difference lgamma(a) + lgamma(b) - lgamma(a + b) has a relative error * of about b log(b) times the machine epsilon for large b; lbeta does - * not. Negative and NaN arguments use the lgamma difference. + * not. Other arguments use the lgamma difference. * * @tparam T1 type of first value * @tparam T2 type of second value @@ -58,7 +59,7 @@ namespace math { template * = nullptr> inline return_type_t beta(const T1 a, const T2 b) { using std::exp; - if (a >= 0 && b >= 0) { + if (a > 0 && b > 0 && a < INFTY && b < INFTY) { return exp(lbeta(a, b)); } return exp(lgamma(a) + lgamma(b) - lgamma(a + b)); diff --git a/test/unit/math/opencl/rev/beta_reference_test.cpp b/test/unit/math/opencl/rev/beta_reference_test.cpp index 8828ccf461b..19069b10c43 100644 --- a/test/unit/math/opencl/rev/beta_reference_test.cpp +++ b/test/unit/math/opencl/rev/beta_reference_test.cpp @@ -52,24 +52,34 @@ TEST(OpenCL_beta, rev_value_matches_reference) { stan::math::recover_memory(); } -TEST(OpenCL_beta, limits) { +TEST(OpenCL_beta, edge_values_match_cpu) { using stan::math::matrix_cl; const double inf = std::numeric_limits::infinity(); const double nan = std::numeric_limits::quiet_NaN(); - Eigen::VectorXd a(9); - Eigen::VectorXd b(9); - a << inf, 1.65, inf, 1e-300, 0.0, 0.0, 0.0, inf, nan; - b << 2.16, inf, inf, inf, 2.16, 0.0, inf, 0.0, inf; + const double edges[] = {nan, 0.0, -0.5, -2.5, inf, 1e-300, 0.5, 2.0}; + const int m = sizeof(edges) / sizeof(edges[0]); + Eigen::VectorXd a(m * m); + Eigen::VectorXd b(m * m); + for (int i = 0; i < m; ++i) { + for (int j = 0; j < m; ++j) { + a[i * m + j] = edges[i]; + b[i * m + j] = edges[j]; + } + } matrix_cl a_cl(a); matrix_cl b_cl(b); matrix_cl res_cl = stan::math::beta(a_cl, b_cl); Eigen::VectorXd res = stan::math::from_matrix_cl(res_cl); - for (int i = 0; i < 4; ++i) { - EXPECT_EQ(0.0, res[i]) << "i = " << i; - } - for (int i = 4; i < 8; ++i) { - EXPECT_EQ(inf, res[i]) << "i = " << i; + for (int k = 0; k < m * m; ++k) { + SCOPED_TRACE(::testing::Message() << "a = " << a[k] << ", b = " << b[k]); + const double cpu = stan::math::beta(a[k], b[k]); + if (std::isnan(cpu)) { + EXPECT_TRUE(std::isnan(res[k])); + } else if (std::isinf(cpu) || cpu == 0) { + EXPECT_EQ(cpu, res[k]); + } else { + EXPECT_NEAR(cpu, res[k], 1e-11 * std::fabs(cpu)); + } } - EXPECT_TRUE(std::isnan(res[8])); } #endif diff --git a/test/unit/math/prim/fun/beta_test.cpp b/test/unit/math/prim/fun/beta_test.cpp index 7c45dcdf8e6..0e38b90ff46 100644 --- a/test/unit/math/prim/fun/beta_test.cpp +++ b/test/unit/math/prim/fun/beta_test.cpp @@ -18,22 +18,9 @@ TEST(MathFunctions, beta_nan) { EXPECT_TRUE(std::isnan(beta(NOT_A_NUMBER, 2.16))); EXPECT_TRUE(std::isnan(beta(1.65, NOT_A_NUMBER))); - EXPECT_TRUE(std::isnan(beta(NOT_A_NUMBER, INFTY))); - EXPECT_TRUE(std::isnan(beta(0.0, NOT_A_NUMBER))); -} - -TEST(MathFunctions, beta_limits) { - using stan::math::beta; - using stan::math::INFTY; - EXPECT_EQ(0.0, beta(INFTY, 2.16)); - EXPECT_EQ(0.0, beta(1.65, INFTY)); - EXPECT_EQ(0.0, beta(INFTY, INFTY)); - EXPECT_EQ(0.0, beta(1e-300, INFTY)); - EXPECT_EQ(INFTY, beta(0.0, 2.16)); - EXPECT_EQ(INFTY, beta(0.0, 0.0)); - EXPECT_EQ(INFTY, beta(0.0, INFTY)); - EXPECT_EQ(INFTY, beta(INFTY, 0.0)); + EXPECT_TRUE(std::isnan(beta(INFTY, 2.16))); + EXPECT_TRUE(std::isnan(beta(1.65, INFTY))); } TEST(MathFunctions, beta_large_arguments) { From b40012df742fd9b7cdc32c7b9c080a7004012d03 Mon Sep 17 00:00:00 2001 From: Aki Vehtari Date: Mon, 2 Nov 2026 00:42:12 +0200 Subject: [PATCH 3/6] Test beta() at huge and int arguments and loosen the OpenCL tolerance --- .../opencl/kernels/device_functions/beta.hpp | 5 ++--- .../math/opencl/rev/beta_reference_test.cpp | 6 +++--- test/unit/math/prim/fun/beta_reference.hpp | 9 ++++++--- test/unit/math/prim/fun/beta_test.cpp | 20 +++++++++++++++++-- 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/stan/math/opencl/kernels/device_functions/beta.hpp b/stan/math/opencl/kernels/device_functions/beta.hpp index 964fd57f32f..870be7324d8 100644 --- a/stan/math/opencl/kernels/device_functions/beta.hpp +++ b/stan/math/opencl/kernels/device_functions/beta.hpp @@ -21,9 +21,8 @@ static constexpr const char* beta_device_function * For finite positive arguments the value is * exp(stan_lbeta(a, b)), which keeps its accuracy for large * arguments. Other arguments use the lgamma difference. The - * kernel must - * include the lgamma_stirling, lgamma_stirling_diff and lbeta - * device functions before this one. + * kernel must include the lgamma_stirling, lgamma_stirling_diff + * and lbeta device functions before this one. * * @param a First value * @param b Second value diff --git a/test/unit/math/opencl/rev/beta_reference_test.cpp b/test/unit/math/opencl/rev/beta_reference_test.cpp index 19069b10c43..36d5d5f05af 100644 --- a/test/unit/math/opencl/rev/beta_reference_test.cpp +++ b/test/unit/math/opencl/rev/beta_reference_test.cpp @@ -30,7 +30,7 @@ TEST(OpenCL_beta, prim_matches_reference) { for (size_t i = 0; i < rows.size(); ++i) { SCOPED_TRACE(::testing::Message() << "a = " << rows[i][0] << ", b = " << rows[i][1]); - EXPECT_NEAR(rows[i][2], res[i], 1e-13 * rows[i][2]); + EXPECT_NEAR(rows[i][2], res[i], 1e-12 * rows[i][2]); } } @@ -47,7 +47,7 @@ TEST(OpenCL_beta, rev_value_matches_reference) { for (size_t i = 0; i < rows.size(); ++i) { SCOPED_TRACE(::testing::Message() << "a = " << rows[i][0] << ", b = " << rows[i][1]); - EXPECT_NEAR(rows[i][2], val[i], 1e-13 * rows[i][2]); + EXPECT_NEAR(rows[i][2], val[i], 1e-12 * rows[i][2]); } stan::math::recover_memory(); } @@ -56,7 +56,7 @@ TEST(OpenCL_beta, edge_values_match_cpu) { using stan::math::matrix_cl; const double inf = std::numeric_limits::infinity(); const double nan = std::numeric_limits::quiet_NaN(); - const double edges[] = {nan, 0.0, -0.5, -2.5, inf, 1e-300, 0.5, 2.0}; + const double edges[] = {nan, 0.0, -0.5, -2.5, inf, 1e-300, 0.5, 2.0, 1.7e308}; const int m = sizeof(edges) / sizeof(edges[0]); Eigen::VectorXd a(m * m); Eigen::VectorXd b(m * m); diff --git a/test/unit/math/prim/fun/beta_reference.hpp b/test/unit/math/prim/fun/beta_reference.hpp index 10f464f4bd6..2a03c3f1561 100644 --- a/test/unit/math/prim/fun/beta_reference.hpp +++ b/test/unit/math/prim/fun/beta_reference.hpp @@ -6,9 +6,10 @@ namespace stan { namespace test { -// Reference values of beta(a, b) from mpmath at 50 digits (mp.beta, checked -// against exp(loggamma(a) + loggamma(b) - loggamma(a + b))). Columns: a, b, -// beta(a, b). One argument is large, in both argument orders. +// Reference values of beta(a, b) from mpmath (mp.beta, checked against +// exp(loggamma(a) + loggamma(b) - loggamma(a + b)) and, for the argument 2, +// against 1 / (b (b + 1))). Columns: a, b, beta(a, b). One argument is +// large, in both argument orders. inline const std::vector> beta_large_reference{{ {0.5, 1000000.0, 0.0017724540724622611}, {1000000.0, 0.5, 0.0017724540724622611}, @@ -28,6 +29,8 @@ inline const std::vector> beta_large_reference{{ {1000000000.0, 10.0, 3.6287998367040043e-85}, {10.0, 2147483648.0, 1.7396599603107283e-88}, {2147483648.0, 10.0, 1.7396599603107283e-88}, + {0.5, 1.7e+308, 1.3594100479922279e-154}, + {1.7e+308, 0.5, 1.3594100479922279e-154}, }}; } // namespace test } // namespace stan diff --git a/test/unit/math/prim/fun/beta_test.cpp b/test/unit/math/prim/fun/beta_test.cpp index 0e38b90ff46..a514737b613 100644 --- a/test/unit/math/prim/fun/beta_test.cpp +++ b/test/unit/math/prim/fun/beta_test.cpp @@ -31,10 +31,26 @@ TEST(MathFunctions, beta_large_arguments) { } } -TEST(MathFunctions, beta_small_arguments_unchanged) { +TEST(MathFunctions, beta_huge_argument_underflow) { + using stan::math::beta; + // beta(2, b) = 1 / (b (b + 1)) is below the smallest subnormal. + EXPECT_EQ(0.0, beta(2.0, 1.7e308)); + EXPECT_EQ(0.0, beta(1.7e308, 2.0)); +} + +TEST(MathFunctions, beta_int_arguments) { + using stan::math::beta; + const double expected = 9.99999999e-19; // 1 / (1e9 (1e9 + 1)) + EXPECT_NEAR(expected, beta(2, 1000000000), 1e-13 * expected); + EXPECT_NEAR(expected, beta(1000000000, 2), 1e-13 * expected); + EXPECT_NEAR(expected, beta(2.0, 1000000000), 1e-13 * expected); + EXPECT_NEAR(expected, beta(1000000000, 2.0), 1e-13 * expected); +} + +TEST(MathFunctions, beta_small_arguments_match_lgamma_formula) { using stan::math::beta; using stan::math::lgamma; - // Below 10, lbeta sums the same three lgamma values. + // Both arguments below 10: lbeta is the sum of the three lgamma values. const double xs[] = {1e-3, 0.37, 1.0, 2.5, 7.25, 9.99}; for (double a : xs) { for (double b : xs) { From 2695def2998066456cfbbffbc1b66ea02b3803e1 Mon Sep 17 00:00:00 2001 From: Aki Vehtari Date: Tue, 3 Nov 2026 23:21:48 +0200 Subject: [PATCH 4/6] Use isfinite for the finite-argument test in beta --- stan/math/opencl/kernels/device_functions/beta.hpp | 2 +- stan/math/prim/fun/beta.hpp | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/stan/math/opencl/kernels/device_functions/beta.hpp b/stan/math/opencl/kernels/device_functions/beta.hpp index 870be7324d8..737a4745504 100644 --- a/stan/math/opencl/kernels/device_functions/beta.hpp +++ b/stan/math/opencl/kernels/device_functions/beta.hpp @@ -29,7 +29,7 @@ static constexpr const char* beta_device_function * @return beta(a, b) */ double beta(double a, double b) { - if (a > 0 && b > 0 && a < INFINITY && b < INFINITY) { + if (a > 0 && b > 0 && isfinite(a) && isfinite(b)) { return exp(stan_lbeta(a, b)); } return exp(lgamma(a) + lgamma(b) - lgamma(a + b)); diff --git a/stan/math/prim/fun/beta.hpp b/stan/math/prim/fun/beta.hpp index ab92908dc0a..0e0577ef0dd 100644 --- a/stan/math/prim/fun/beta.hpp +++ b/stan/math/prim/fun/beta.hpp @@ -2,7 +2,6 @@ #define STAN_MATH_PRIM_FUN_BETA_HPP #include -#include #include #include #include @@ -59,7 +58,7 @@ namespace math { template * = nullptr> inline return_type_t beta(const T1 a, const T2 b) { using std::exp; - if (a > 0 && b > 0 && a < INFTY && b < INFTY) { + if (a > 0 && b > 0 && std::isfinite(a) && std::isfinite(b)) { return exp(lbeta(a, b)); } return exp(lgamma(a) + lgamma(b) - lgamma(a + b)); From 372cebb637c74b498e13af2ce9e09021914db368 Mon Sep 17 00:00:00 2001 From: Aki Vehtari Date: Tue, 3 Nov 2026 23:22:39 +0200 Subject: [PATCH 5/6] Loosen beta_lcdf large-beta test tolerance to 1e-7 for platform math libraries --- test/unit/math/rev/prob/beta_lcdf_test.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/test/unit/math/rev/prob/beta_lcdf_test.cpp b/test/unit/math/rev/prob/beta_lcdf_test.cpp index ffac0b53add..3dc831de14a 100644 --- a/test/unit/math/rev/prob/beta_lcdf_test.cpp +++ b/test/unit/math/rev/prob/beta_lcdf_test.cpp @@ -16,13 +16,18 @@ TEST_F(AgradRev, ProbDistributionsBeta_lcdf_large_beta_d_y) { {2.3283064365386963e-10, 2.0, 2147483648.0, -2.40568139077329, 7219826854.641048}, }}; + // beta() computed as exp(lgamma(a) + lgamma(b) - lgamma(a + b)) gives + // relative derivative errors of 2e-6 to 3e-6 here. With the math library + // of some platforms (Windows x64), inc_beta and the density give relative + // errors of about 2e-8 here. + const double tol = 1e-7; for (const auto& r : rows) { SCOPED_TRACE(::testing::Message() << "beta = " << r[2]); var y = r[0]; var lp = stan::math::beta_lcdf(y, r[1], r[2]); lp.grad(); - EXPECT_NEAR(r[3], lp.val(), 1e-12 * std::fabs(r[3])); - EXPECT_NEAR(r[4], y.adj(), 1e-12 * r[4]); + EXPECT_NEAR(r[3], lp.val(), tol * std::fabs(r[3])); + EXPECT_NEAR(r[4], y.adj(), tol * r[4]); stan::math::recover_memory(); } } From 47159a62cabf9578cc74da4a8b2f5a09fa3fd1a8 Mon Sep 17 00:00:00 2001 From: Aki Vehtari Date: Wed, 4 Nov 2026 00:00:27 +0200 Subject: [PATCH 6/6] State only measured errors in the beta_lcdf test comment --- test/unit/math/rev/prob/beta_lcdf_test.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/test/unit/math/rev/prob/beta_lcdf_test.cpp b/test/unit/math/rev/prob/beta_lcdf_test.cpp index 3dc831de14a..127a737488c 100644 --- a/test/unit/math/rev/prob/beta_lcdf_test.cpp +++ b/test/unit/math/rev/prob/beta_lcdf_test.cpp @@ -16,10 +16,9 @@ TEST_F(AgradRev, ProbDistributionsBeta_lcdf_large_beta_d_y) { {2.3283064365386963e-10, 2.0, 2147483648.0, -2.40568139077329, 7219826854.641048}, }}; - // beta() computed as exp(lgamma(a) + lgamma(b) - lgamma(a + b)) gives - // relative derivative errors of 2e-6 to 3e-6 here. With the math library - // of some platforms (Windows x64), inc_beta and the density give relative - // errors of about 2e-8 here. + // The lgamma-difference formula for beta() gives relative derivative + // errors of 2e-6 to 3e-6 at these points. Windows x64 (MinGW) gives + // relative errors up to about 2e-8 in the value and the derivative. const double tol = 1e-7; for (const auto& r : rows) { SCOPED_TRACE(::testing::Message() << "beta = " << r[2]);