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..737a4745504 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 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. + * * @param a First value * @param b Second value * @return beta(a, b) */ double beta(double a, double b) { + if (a > 0 && b > 0 && isfinite(a) && isfinite(b)) { + 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..0e0577ef0dd 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 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. Other 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 && std::isfinite(a) && std::isfinite(b)) { + 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..36d5d5f05af --- /dev/null +++ b/test/unit/math/opencl/rev/beta_reference_test.cpp @@ -0,0 +1,85 @@ +#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-12 * 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-12 * rows[i][2]); + } + stan::math::recover_memory(); +} + +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, 1.7e308}; + 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 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)); + } + } +} +#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..2a03c3f1561 --- /dev/null +++ b/test/unit/math/prim/fun/beta_reference.hpp @@ -0,0 +1,38 @@ +#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 (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}, + {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}, + {0.5, 1.7e+308, 1.3594100479922279e-154}, + {1.7e+308, 0.5, 1.3594100479922279e-154}, +}}; +} // 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..a514737b613 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 @@ -22,6 +23,42 @@ TEST(MathFunctions, beta_nan) { 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_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; + // 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) { + EXPECT_EQ(std::exp(lgamma(a) + lgamma(b) - lgamma(a + b)), beta(a, b)); + } + } +} + TEST(MathFunctions, beta_vec) { auto f = [](const auto& x1, const auto& x2) { return stan::math::beta(x1, x2); }; 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..127a737488c --- /dev/null +++ b/test/unit/math/rev/prob/beta_lcdf_test.cpp @@ -0,0 +1,32 @@ +#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}, + }}; + // 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]); + var y = r[0]; + var lp = stan::math::beta_lcdf(y, r[1], r[2]); + lp.grad(); + EXPECT_NEAR(r[3], lp.val(), tol * std::fabs(r[3])); + EXPECT_NEAR(r[4], y.adj(), tol * r[4]); + stan::math::recover_memory(); + } +}