Skip to content

Commit

Permalink
Renamed tests (Issue #2197)
Browse files Browse the repository at this point in the history
  • Loading branch information
bbbales2 committed Feb 24, 2021
1 parent f84f7eb commit 5019637
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <sstream>
#include <vector>

namespace integrate_1d_new_test {
namespace integrate_1d_impl_test {

std::ostringstream *msgs = nullptr;

Expand Down Expand Up @@ -235,16 +235,16 @@ double order(double down, double up, const std::vector<double> &theta,

double v;

v = stan::math::integrate_1d_new(rank_density_functor__(),
v = stan::math::integrate_1d_impl(rank_density_functor__(),
down, up,
1e-8, pstream__,
theta, x_r, x_i);
return v;
}
} // namespace integrate_1d_new_test
} // namespace integrate_1d_impl_test
/*
* test_integration is a helper function to make it easy to test the
* integrate_1d_new function.
* integrate_1d_impl function.
*
* It takes in a callable function object, integration limits, parameters, real
* and integer data. It integrates the provided function and compares the
Expand All @@ -271,13 +271,13 @@ double order(double down, double up, const std::vector<double> &theta,
template <typename F, typename... Args>
void test_integration(const F &f, double a, double b,
double val, const Args&... args) {
using stan::math::integrate_1d_new;
using stan::math::integrate_1d_impl;

std::vector<double> tolerances = {1e-4, 1e-6, 1e-8};

for (auto tolerance : tolerances) {
EXPECT_LE(std::abs(integrate_1d_new(f, a, b, tolerance,
integrate_1d_new_test::msgs, args...)
EXPECT_LE(std::abs(integrate_1d_impl(f, a, b, tolerance,
integrate_1d_impl_test::msgs, args...)
- val),
tolerance);
// Flip the domain of integration and check that the integral is working
Expand All @@ -287,151 +287,151 @@ void test_integration(const F &f, double a, double b,
return f(-x, -xc, msgs, args...);
};

EXPECT_LE(std::abs(integrate_1d_new(flipped, -b, -a, tolerance,
integrate_1d_new_test::msgs, args...)
EXPECT_LE(std::abs(integrate_1d_impl(flipped, -b, -a, tolerance,
integrate_1d_impl_test::msgs, args...)
- val),
tolerance);
}
}

TEST(StanMath_integrate_1d_new_prim, TestThrows) {
TEST(StanMath_integrate_1d_impl_prim, TestThrows) {
// Left limit of integration must be less than or equal to right limit
EXPECT_THROW(stan::math::integrate_1d_new(integrate_1d_new_test::f2{}, 1.0, 0.0,
0.0, integrate_1d_new_test::msgs),
EXPECT_THROW(stan::math::integrate_1d_impl(integrate_1d_impl_test::f2{}, 1.0, 0.0,
0.0, integrate_1d_impl_test::msgs),
std::domain_error);
// NaN limits not okay
EXPECT_THROW(
stan::math::integrate_1d_new(integrate_1d_new_test::f2{}, 0.0,
stan::math::integrate_1d_impl(integrate_1d_impl_test::f2{}, 0.0,
std::numeric_limits<double>::quiet_NaN(),
0.0, integrate_1d_new_test::msgs),
0.0, integrate_1d_impl_test::msgs),
std::domain_error);
EXPECT_THROW(
stan::math::integrate_1d_new(
integrate_1d_new_test::f2{},
stan::math::integrate_1d_impl(
integrate_1d_impl_test::f2{},
std::numeric_limits<double>::quiet_NaN(),
0.0, 0.0, integrate_1d_new_test::msgs),
0.0, 0.0, integrate_1d_impl_test::msgs),
std::domain_error);
EXPECT_THROW(
stan::math::integrate_1d_new(
integrate_1d_new_test::f2{}, std::numeric_limits<double>::quiet_NaN(),
stan::math::integrate_1d_impl(
integrate_1d_impl_test::f2{}, std::numeric_limits<double>::quiet_NaN(),
std::numeric_limits<double>::quiet_NaN(), 0.0,
integrate_1d_new_test::msgs),
integrate_1d_impl_test::msgs),
std::domain_error);
// Two of the same inf limits not okay
EXPECT_THROW(
stan::math::integrate_1d_new(
integrate_1d_new_test::f2{}, -std::numeric_limits<double>::infinity(),
stan::math::integrate_1d_impl(
integrate_1d_impl_test::f2{}, -std::numeric_limits<double>::infinity(),
-std::numeric_limits<double>::infinity(), 0.0,
integrate_1d_new_test::msgs),
integrate_1d_impl_test::msgs),
std::domain_error);

EXPECT_THROW(stan::math::integrate_1d_new(integrate_1d_new_test::f2{},
EXPECT_THROW(stan::math::integrate_1d_impl(integrate_1d_impl_test::f2{},
std::numeric_limits<double>::infinity(),
std::numeric_limits<double>::infinity(),
0.0, integrate_1d_new_test::msgs),
0.0, integrate_1d_impl_test::msgs),
std::domain_error);
// xc should be nan if there are infinite limits
EXPECT_THROW(stan::math::integrate_1d_new(integrate_1d_new_test::f11{}, 0.0,
EXPECT_THROW(stan::math::integrate_1d_impl(integrate_1d_impl_test::f11{}, 0.0,
std::numeric_limits<double>::infinity(),
0.0, integrate_1d_new_test::msgs),
0.0, integrate_1d_impl_test::msgs),
std::runtime_error);
EXPECT_THROW(stan::math::integrate_1d_new(integrate_1d_new_test::f11{},
EXPECT_THROW(stan::math::integrate_1d_impl(integrate_1d_impl_test::f11{},
std::numeric_limits<double>::infinity(),
0.0, 0.0, integrate_1d_new_test::msgs),
0.0, 0.0, integrate_1d_impl_test::msgs),
std::domain_error);
EXPECT_THROW(stan::math::integrate_1d_new(integrate_1d_new_test::f11{},
EXPECT_THROW(stan::math::integrate_1d_impl(integrate_1d_impl_test::f11{},
std::numeric_limits<double>::infinity(),
std::numeric_limits<double>::infinity(),
0.0, integrate_1d_new_test::msgs),
0.0, integrate_1d_impl_test::msgs),
std::domain_error);
// But not otherwise
EXPECT_NO_THROW(stan::math::integrate_1d_new(integrate_1d_new_test::f11{}, 0.0, 1.0,
0.0, integrate_1d_new_test::msgs));
EXPECT_NO_THROW(stan::math::integrate_1d_impl(integrate_1d_impl_test::f11{}, 0.0, 1.0,
0.0, integrate_1d_impl_test::msgs));
}

TEST(StanMath_integrate_1d_new_prim, test_integer_arguments) {
TEST(StanMath_integrate_1d_impl_prim, test_integer_arguments) {
double v;
EXPECT_NO_THROW(v = stan::math::integrate_1d_new(integrate_1d_new_test::f2{}, 0, 1,
0.0, integrate_1d_new_test::msgs));
EXPECT_NO_THROW(v = stan::math::integrate_1d_new(integrate_1d_new_test::f2{}, 0.0, 1,
0.0, integrate_1d_new_test::msgs));
EXPECT_NO_THROW(v = stan::math::integrate_1d_new(integrate_1d_new_test::f2{}, 0, 1.0,
0.0, integrate_1d_new_test::msgs));
EXPECT_NO_THROW(v = stan::math::integrate_1d_impl(integrate_1d_impl_test::f2{}, 0, 1,
0.0, integrate_1d_impl_test::msgs));
EXPECT_NO_THROW(v = stan::math::integrate_1d_impl(integrate_1d_impl_test::f2{}, 0.0, 1,
0.0, integrate_1d_impl_test::msgs));
EXPECT_NO_THROW(v = stan::math::integrate_1d_impl(integrate_1d_impl_test::f2{}, 0, 1.0,
0.0, integrate_1d_impl_test::msgs));
}

TEST(StanMath_integrate_1d_new_prim, test1) {
TEST(StanMath_integrate_1d_impl_prim, test1) {
// Tricky integral from Boost docs + limit at infinity
test_integration(integrate_1d_new_test::f1{}, 0.0,
test_integration(integrate_1d_impl_test::f1{}, 0.0,
std::numeric_limits<double>::infinity(),
1.772453850905516);
// Tricky integral from Boost 1d integration docs
test_integration(integrate_1d_new_test::f2{}, 0.0, 1.0, 1.198140234735592);
test_integration(integrate_1d_impl_test::f2{}, 0.0, 1.0, 1.198140234735592);
// Tricky integral from Boost 1d integration docs
test_integration(integrate_1d_new_test::f2{}, 0, 1, 1.198140234735592);
test_integration(integrate_1d_impl_test::f2{}, 0, 1, 1.198140234735592);
// Zero crossing integral + limit at infinity
test_integration(integrate_1d_new_test::f3{}, -2.0,
test_integration(integrate_1d_impl_test::f3{}, -2.0,
std::numeric_limits<double>::infinity(), 7.38905609893065);
// Easy integrals
test_integration(integrate_1d_new_test::f4{}, 0.2, 0.7,
test_integration(integrate_1d_impl_test::f4{}, 0.2, 0.7,
1.0423499493102901, std::vector<double>({0.5}));
test_integration(integrate_1d_new_test::f5{}, -0.2, 0.7,
test_integration(integrate_1d_impl_test::f5{}, -0.2, 0.7,
1.396621954392482, std::vector<double>({0.4, 0.4}));
test_integration(integrate_1d_new_test::f4{}, 0.0, 0.0, 0.0,
test_integration(integrate_1d_impl_test::f4{}, 0.0, 0.0, 0.0,
std::vector<double>({0.5}));
test_integration(integrate_1d_new_test::f5{}, 1.0, 1.0, 0.0,
test_integration(integrate_1d_impl_test::f5{}, 1.0, 1.0, 0.0,
std::vector<double>({0.4, 0.4}));
// Test x_i
test_integration(integrate_1d_new_test::f6{}, -0.2, 2.9,
test_integration(integrate_1d_impl_test::f6{}, -0.2, 2.9,
4131.985414616364, std::vector<double>({6.0, 5.1}),
std::vector<int>({4}));
// Test x_r
test_integration(integrate_1d_new_test::f7{}, -0.2, 2.9,
test_integration(integrate_1d_impl_test::f7{}, -0.2, 2.9,
24219.985414616367,
std::vector<double>({4.0, 6.0, 5.1}));
// Both limits at infinity + test x_r/x_i
test_integration(integrate_1d_new_test::f8{},
test_integration(integrate_1d_impl_test::f8{},
-std::numeric_limits<double>::infinity(),
std::numeric_limits<double>::infinity(),
3.013171546539377,
std::vector<double>({5.0}),
std::vector<double>({1.7}),
std::vector<int>({2}));
// Both limits at infinity + test x_i
test_integration(integrate_1d_new_test::f9{},
test_integration(integrate_1d_impl_test::f9{},
-std::numeric_limits<double>::infinity(),
std::numeric_limits<double>::infinity(),
2.372032924895055,
std::vector<double>({1.3}),
std::vector<int>({4}));
// Various integrals of beta function
test_integration(integrate_1d_new_test::f10{}, 0.0, 1.0,
test_integration(integrate_1d_impl_test::f10{}, 0.0, 1.0,
19.71463948905016, std::vector<double>({0.1, 0.1}));
test_integration(integrate_1d_new_test::f10{}, 0.0, 1.0,
test_integration(integrate_1d_impl_test::f10{}, 0.0, 1.0,
11.32308697521577, std::vector<double>({0.1, 0.5}));
test_integration(integrate_1d_new_test::f10{}, 0.0, 1.0,
test_integration(integrate_1d_impl_test::f10{}, 0.0, 1.0,
11.32308697521577, std::vector<double>({0.5, 0.1}));
test_integration(integrate_1d_new_test::f10{}, 0.0, 1.0,
test_integration(integrate_1d_impl_test::f10{}, 0.0, 1.0,
0.00952380952380952, std::vector<double>({5.0, 3.0}));

// Integrals from
// http://crd-legacy.lbl.gov/~dhbailey/dhbpapers/dhb-tanh-sinh.pdf
test_integration(integrate_1d_new_test::f12{}, 0.0,
test_integration(integrate_1d_impl_test::f12{}, 0.0,
std::numeric_limits<double>::infinity(), 2.0);
test_integration(integrate_1d_new_test::f13{}, 0.0,
test_integration(integrate_1d_impl_test::f13{}, 0.0,
std::numeric_limits<double>::infinity(), 1.0);
test_integration(integrate_1d_new_test::f14{}, 0.0, 1.0,
test_integration(integrate_1d_impl_test::f14{}, 0.0, 1.0,
exp(1) * sqrt(stan::math::pi()) * stan::math::erf(1.0));

// Integrals from http://crd-legacy.lbl.gov/~dhbailey/dhbpapers/quadrature.pdf
// works normally but not to tolerance when limits of integration are flipped
// test_integration(f15{}, 0.0, 1.0, {}, {}, {},
// stan::math::square(stan::math::pi()) * (2 - sqrt(2.0)) /
// 32);
test_integration(integrate_1d_new_test::f16{}, 0.0, stan::math::pi(),
test_integration(integrate_1d_impl_test::f16{}, 0.0, stan::math::pi(),
stan::math::square(stan::math::pi()) / 4);
}

TEST(StanMath_integrate_1d_new_prim, TestTolerance) {
TEST(StanMath_integrate_1d_impl_prim, TestTolerance) {
std::ostringstream *msgs = nullptr;

double t = 0.5;
Expand All @@ -444,5 +444,5 @@ TEST(StanMath_integrate_1d_new_prim, TestTolerance) {
std::vector<double> theta = {t, A, v1, v2, s};
std::vector<double> x_r;

EXPECT_NO_THROW(integrate_1d_new_test::order(-10, 0.67, theta, x_r, msgs));
EXPECT_NO_THROW(integrate_1d_impl_test::order(-10, 0.67, theta, x_r, msgs));
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ void test_derivatives(const F &f, double a, double b,
for (size_t i = 0; i < thetas.size(); ++i)
thetas_[i] = thetas[i];

var integral = stan::math::integrate_1d_new(f, a_, b_, tolerance, msgs, thetas_, x_r, x_i);
var integral = stan::math::integrate_1d_impl(f, a_, b_, tolerance, msgs, thetas_, x_r, x_i);
integral.grad();
EXPECT_LE(std::abs(val - integral.val()), tolerance);
if (stan::is_var<T_theta>::value) {
Expand All @@ -239,11 +239,11 @@ TEST(StanMath_integrate_1d_rev, test_integer_arguments) {
std::vector<double> x_r;
std::vector<int> x_i;
EXPECT_NO_THROW(
v = stan::math::integrate_1d_new(f2{}, 0, 1, 1e-6, msgs, theta, x_r, x_i));
v = stan::math::integrate_1d_impl(f2{}, 0, 1, 1e-6, msgs, theta, x_r, x_i));
EXPECT_NO_THROW(
v = stan::math::integrate_1d_new(f2{}, 0.0, 1, 1e-6, msgs, theta, x_r, x_i));
v = stan::math::integrate_1d_impl(f2{}, 0.0, 1, 1e-6, msgs, theta, x_r, x_i));
EXPECT_NO_THROW(
v = stan::math::integrate_1d_new(f2{}, 0, 1.0, 1e-6, msgs, theta, x_r, x_i));
v = stan::math::integrate_1d_impl(f2{}, 0, 1.0, 1e-6, msgs, theta, x_r, x_i));
}

TEST(StanMath_integrate_1d_rev, TestDerivatives_easy) {
Expand Down

0 comments on commit 5019637

Please sign in to comment.