Skip to content
Open
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion stan/math/opencl/kernel_generator/elt_function_cl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions stan/math/opencl/kernels/device_functions/beta.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions stan/math/prim/fun/beta.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <stan/math/prim/meta.hpp>
#include <stan/math/prim/fun/exp.hpp>
#include <stan/math/prim/fun/lbeta.hpp>
#include <stan/math/prim/fun/lgamma.hpp>
#include <stan/math/prim/functor/apply_scalar_binary.hpp>
#include <cmath>
Expand Down Expand Up @@ -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
Expand All @@ -52,6 +58,9 @@ namespace math {
template <typename T1, typename T2, require_all_arithmetic_t<T1, T2>* = nullptr>
inline return_type_t<T1, T2> 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));
}

Expand Down
85 changes: 85 additions & 0 deletions test/unit/math/opencl/rev/beta_reference_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#ifdef STAN_OPENCL
#include <stan/math/opencl/rev.hpp>
#include <stan/math.hpp>
#include <gtest/gtest.h>
#include <test/unit/math/opencl/util.hpp>
#include <test/unit/math/prim/fun/beta_reference.hpp>
#include <cmath>
#include <limits>

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<double> a_cl(beta_reference_test::column(0));
matrix_cl<double> b_cl(beta_reference_test::column(1));
matrix_cl<double> res_cl = stan::math::beta(a_cl, b_cl);
Eigen::VectorXd res = stan::math::from_matrix_cl<Eigen::VectorXd>(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<matrix_cl<double>> a{
matrix_cl<double>(beta_reference_test::column(0))};
var_value<matrix_cl<double>> b{
matrix_cl<double>(beta_reference_test::column(1))};
var_value<matrix_cl<double>> res = stan::math::beta(a, b);
Eigen::VectorXd val = stan::math::from_matrix_cl<Eigen::VectorXd>(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<double>::infinity();
const double nan = std::numeric_limits<double>::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<double> a_cl(a);
matrix_cl<double> b_cl(b);
matrix_cl<double> res_cl = stan::math::beta(a_cl, b_cl);
Eigen::VectorXd res = stan::math::from_matrix_cl<Eigen::VectorXd>(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
38 changes: 38 additions & 0 deletions test/unit/math/prim/fun/beta_reference.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef TEST_UNIT_MATH_PRIM_FUN_BETA_REFERENCE_HPP
#define TEST_UNIT_MATH_PRIM_FUN_BETA_REFERENCE_HPP

#include <array>
#include <vector>

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<std::array<double, 3>> 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
37 changes: 37 additions & 0 deletions test/unit/math/prim/fun/beta_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <stan/math/prim.hpp>
#include <test/unit/math/prim/fun/binary_scalar_tester.hpp>
#include <test/unit/math/prim/fun/beta_reference.hpp>
#include <gtest/gtest.h>
#include <cmath>

Expand All @@ -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); };
Expand Down
32 changes: 32 additions & 0 deletions test/unit/math/rev/prob/beta_lcdf_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <stan/math/rev.hpp>
#include <test/unit/math/rev/util.hpp>
#include <gtest/gtest.h>
#include <array>
#include <cmath>
#include <vector>

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<std::array<double, 5>> 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();
}
}
Loading