From f98af75a40fd51416dc70b202608fc0895b56ec9 Mon Sep 17 00:00:00 2001 From: Ben Date: Tue, 15 Sep 2020 00:00:02 -0400 Subject: [PATCH 01/19] Added tests that f([a, b, c, d]) works where f is a pdf/pmf, a cdf, a log cdf, and a log ccdf (previously was just testing f([a, b, c, d], Issue #1861) --- test/prob/test_fixture_ccdf_log.hpp | 249 +++++++++++++++++++++++++++- test/prob/test_fixture_cdf.hpp | 245 ++++++++++++++++++++++++++- test/prob/test_fixture_cdf_log.hpp | 249 +++++++++++++++++++++++++++- test/prob/test_fixture_distr.hpp | 247 ++++++++++++++++++++++++++- 4 files changed, 986 insertions(+), 4 deletions(-) diff --git a/test/prob/test_fixture_ccdf_log.hpp b/test/prob/test_fixture_ccdf_log.hpp index 4ef9795368b..aad6f40eede 100644 --- a/test/prob/test_fixture_ccdf_log.hpp +++ b/test/prob/test_fixture_ccdf_log.hpp @@ -700,6 +700,249 @@ class AgradCcdfLogTestFixture : public ::testing::Test { } } + void test_as_scalars_vs_as_vector() { + if (!any_vector::value) { + SUCCEED() << "No test for non-vector arguments"; + return; + } + vector expected_ccdf_log; + vector> parameters; + TestClass.valid_values(parameters, expected_ccdf_log); + + vector single_gradients1; + vector single_gradients2; + vector single_gradients3; + vector scalar_vars; + + vector multiple_gradients1; + vector multiple_gradients2; + vector multiple_gradients3; + vector vector_vars; + + T0 p0 = get_params(parameters, 0); + T1 p1 = get_params(parameters, 1); + T2 p2 = get_params(parameters, 2); + T3 p3 = get_params(parameters, 3); + T4 p4 = get_params(parameters, 4); + T5 p5 = get_params(parameters, 5); + + vector p0s = {get_param(parameters[0], 0)}; + vector p1s = {get_param(parameters[0], 1)}; + vector p2s = {get_param(parameters[0], 2)}; + vector p3s = {get_param(parameters[0], 3)}; + vector p4s = {get_param(parameters[0], 4)}; + vector p5s = {get_param(parameters[0], 5)}; + + T_return_type single_ccdf_log + = TestClass.template ccdf_log(p0s.back(), p1s.back(), + p2s.back(), p3s.back(), + p4s.back(), p5s.back()); + + for (size_t n = 1; n < parameters.size(); n++) { + p0s.push_back((is_vector::value) + ? get_param(parameters[n], 0) + : p0s.front()); + p1s.push_back((is_vector::value) + ? get_param(parameters[n], 1) + : p1s.front()); + p2s.push_back((is_vector::value) + ? get_param(parameters[n], 2) + : p2s.front()); + p3s.push_back((is_vector::value) + ? get_param(parameters[n], 3) + : p3s.front()); + p4s.push_back((is_vector::value) + ? get_param(parameters[n], 4) + : p4s.front()); + p5s.push_back((is_vector::value) + ? get_param(parameters[n], 5) + : p5s.front()); + + single_ccdf_log += TestClass.template ccdf_log( + p0s.back(), p1s.back(), p2s.back(), p3s.back(), p4s.back(), + p5s.back()); + } + + add_var(vector_vars, p0); + if (is_vector::value) + add_var(scalar_vars, p0s); + else + add_var(scalar_vars, p0s.front()); + + add_var(vector_vars, p1); + if (is_vector::value) + add_var(scalar_vars, p1s); + else + add_var(scalar_vars, p1s.front()); + + add_var(vector_vars, p2); + if (is_vector::value) + add_var(scalar_vars, p2s); + else + add_var(scalar_vars, p2s.front()); + + add_var(vector_vars, p3); + if (is_vector::value) + add_var(scalar_vars, p3s); + else + add_var(scalar_vars, p3s.front()); + + add_var(vector_vars, p4); + if (is_vector::value) + add_var(scalar_vars, p4s); + else + add_var(scalar_vars, p4s.front()); + + add_var(vector_vars, p5); + if (is_vector::value) + add_var(scalar_vars, p5s); + else + add_var(scalar_vars, p5s.front()); + + calculate_gradients_1storder(single_gradients1, single_ccdf_log, + scalar_vars); + calculate_gradients_2ndorder(single_gradients2, single_ccdf_log, + scalar_vars); + calculate_gradients_3rdorder(single_gradients3, single_ccdf_log, + scalar_vars); + + T_return_type multiple_ccdf_log + = TestClass.template ccdf_log(p0, p1, p2, p3, + p4, p5); + + calculate_gradients_1storder(multiple_gradients1, multiple_ccdf_log, + vector_vars); + calculate_gradients_2ndorder(multiple_gradients2, multiple_ccdf_log, + vector_vars); + calculate_gradients_3rdorder(multiple_gradients3, multiple_ccdf_log, + vector_vars); + + stan::math::recover_memory(); + + if (stan::math::is_inf(stan::math::value_of_rec(single_ccdf_log)) + && stan::math::value_of_rec(single_ccdf_log) + == stan::math::value_of_rec(multiple_ccdf_log)) { + return; + } + + EXPECT_NEAR(stan::math::value_of_rec(single_ccdf_log), + stan::math::value_of_rec(multiple_ccdf_log), 1e-7) + << "cdf_log evaluated in loop should match vectorized equivalent"; + + size_t pos_single = 0; + size_t pos_multiple = 0; + if (!is_constant_all::value && !is_empty::value + && !std::is_same>::value + && !std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_ccdf_log), + single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && !std::is_same>::value + && !std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_ccdf_log), + single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && !std::is_same>::value + && !std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_ccdf_log), + single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && !std::is_same>::value + && !std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_ccdf_log), + single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && !std::is_same>::value + && !std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_ccdf_log), + single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && !std::is_same>::value + && !std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_ccdf_log), + single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + + pos_single = 0; + pos_multiple = 0; + if (!is_constant_all::value && !is_empty::value + && (std::is_same>::value + || std::is_same>>::value)) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_ccdf_log), + single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && (std::is_same>::value + || std::is_same>>::value)) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_ccdf_log), + single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && (std::is_same>::value + || std::is_same>>::value)) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_ccdf_log), + single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && (std::is_same>::value + || std::is_same>>::value)) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_ccdf_log), + single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && (std::is_same>::value + || std::is_same>>::value)) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_ccdf_log), + single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && (std::is_same>::value + || std::is_same>>::value)) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_ccdf_log), + single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); + + pos_single = 0; + pos_multiple = 0; + if (!is_constant_all::value && !is_empty::value + && std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_ccdf_log), + single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_ccdf_log), + single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_ccdf_log), + single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_ccdf_log), + single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_ccdf_log), + single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_ccdf_log), + single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); + } + void test_lower_bound() { using stan::math::value_of; const size_t N_REPEAT = 3; @@ -832,6 +1075,10 @@ TYPED_TEST_P(AgradCcdfLogTestFixture, RepeatAsVector) { this->test_repeat_as_vector(); } +TYPED_TEST_P(AgradCcdfLogTestFixture, AsScalarsVsAsVector) { + this->test_as_scalars_vs_as_vector(); +} + TYPED_TEST_P(AgradCcdfLogTestFixture, LowerBound) { this->test_lower_bound(); } TYPED_TEST_P(AgradCcdfLogTestFixture, UpperBound) { this->test_upper_bound(); } @@ -843,7 +1090,7 @@ TYPED_TEST_P(AgradCcdfLogTestFixture, Length0Vector) { REGISTER_TYPED_TEST_SUITE_P(AgradCcdfLogTestFixture, CallAllVersions, ValidValues, InvalidValues, FiniteDiff, Function, RepeatAsVector, LowerBound, UpperBound, - Length0Vector); + AsScalarsVsAsVector, Length0Vector); GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AgradCcdfLogTestFixture); diff --git a/test/prob/test_fixture_cdf.hpp b/test/prob/test_fixture_cdf.hpp index 8f3f5556a45..fd75ef370b0 100644 --- a/test/prob/test_fixture_cdf.hpp +++ b/test/prob/test_fixture_cdf.hpp @@ -693,6 +693,244 @@ class AgradCdfTestFixture : public ::testing::Test { } } + void test_as_scalars_vs_as_vector() { + if (!any_vector::value) { + SUCCEED() << "No test for non-vector arguments"; + return; + } + vector expected_cdf; + vector> parameters; + TestClass.valid_values(parameters, expected_cdf); + + vector single_gradients1; + vector single_gradients2; + vector single_gradients3; + vector scalar_vars; + + vector multiple_gradients1; + vector multiple_gradients2; + vector multiple_gradients3; + vector vector_vars; + + T0 p0 = get_params(parameters, 0); + T1 p1 = get_params(parameters, 1); + T2 p2 = get_params(parameters, 2); + T3 p3 = get_params(parameters, 3); + T4 p4 = get_params(parameters, 4); + T5 p5 = get_params(parameters, 5); + + vector p0s = {get_param(parameters[0], 0)}; + vector p1s = {get_param(parameters[0], 1)}; + vector p2s = {get_param(parameters[0], 2)}; + vector p3s = {get_param(parameters[0], 3)}; + vector p4s = {get_param(parameters[0], 4)}; + vector p5s = {get_param(parameters[0], 5)}; + + T_return_type single_cdf + = TestClass.template cdf(p0s.back(), p1s.back(), p2s.back(), + p3s.back(), p4s.back(), p5s.back()); + + for (size_t n = 1; n < parameters.size(); n++) { + p0s.push_back((is_vector::value) + ? get_param(parameters[n], 0) + : p0s.front()); + p1s.push_back((is_vector::value) + ? get_param(parameters[n], 1) + : p1s.front()); + p2s.push_back((is_vector::value) + ? get_param(parameters[n], 2) + : p2s.front()); + p3s.push_back((is_vector::value) + ? get_param(parameters[n], 3) + : p3s.front()); + p4s.push_back((is_vector::value) + ? get_param(parameters[n], 4) + : p4s.front()); + p5s.push_back((is_vector::value) + ? get_param(parameters[n], 5) + : p5s.front()); + + single_cdf *= TestClass.template cdf( + p0s.back(), p1s.back(), p2s.back(), p3s.back(), p4s.back(), + p5s.back()); + } + + add_var(vector_vars, p0); + if (is_vector::value) + add_var(scalar_vars, p0s); + else + add_var(scalar_vars, p0s.front()); + + add_var(vector_vars, p1); + if (is_vector::value) + add_var(scalar_vars, p1s); + else + add_var(scalar_vars, p1s.front()); + + add_var(vector_vars, p2); + if (is_vector::value) + add_var(scalar_vars, p2s); + else + add_var(scalar_vars, p2s.front()); + + add_var(vector_vars, p3); + if (is_vector::value) + add_var(scalar_vars, p3s); + else + add_var(scalar_vars, p3s.front()); + + add_var(vector_vars, p4); + if (is_vector::value) + add_var(scalar_vars, p4s); + else + add_var(scalar_vars, p4s.front()); + + add_var(vector_vars, p5); + if (is_vector::value) + add_var(scalar_vars, p5s); + else + add_var(scalar_vars, p5s.front()); + + calculate_gradients_1storder(single_gradients1, single_cdf, scalar_vars); + calculate_gradients_2ndorder(single_gradients2, single_cdf, scalar_vars); + calculate_gradients_3rdorder(single_gradients3, single_cdf, scalar_vars); + + T_return_type multiple_cdf = TestClass.template cdf( + p0, p1, p2, p3, p4, p5); + + calculate_gradients_1storder(multiple_gradients1, multiple_cdf, + vector_vars); + calculate_gradients_2ndorder(multiple_gradients2, multiple_cdf, + vector_vars); + calculate_gradients_3rdorder(multiple_gradients3, multiple_cdf, + vector_vars); + + stan::math::recover_memory(); + + if (stan::math::is_inf(stan::math::value_of_rec(single_cdf)) + && stan::math::value_of_rec(single_cdf) + == stan::math::value_of_rec(multiple_cdf)) { + return; + } + + EXPECT_NEAR(stan::math::value_of_rec(single_cdf), + stan::math::value_of_rec(multiple_cdf), 1e-7) + << "cdf evaluated in loop should match vectorized equivalent"; + + size_t pos_single = 0; + size_t pos_multiple = 0; + if (!is_constant_all::value && !is_empty::value + && !std::is_same>::value + && !std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf), single_gradients1, + pos_single, multiple_gradients1, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && !std::is_same>::value + && !std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf), single_gradients1, + pos_single, multiple_gradients1, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && !std::is_same>::value + && !std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf), single_gradients1, + pos_single, multiple_gradients1, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && !std::is_same>::value + && !std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf), single_gradients1, + pos_single, multiple_gradients1, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && !std::is_same>::value + && !std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf), single_gradients1, + pos_single, multiple_gradients1, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && !std::is_same>::value + && !std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf), single_gradients1, + pos_single, multiple_gradients1, pos_multiple, 1); + + pos_single = 0; + pos_multiple = 0; + if (!is_constant_all::value && !is_empty::value + && (std::is_same>::value + || std::is_same>>::value)) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf), single_gradients2, + pos_single, multiple_gradients2, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && (std::is_same>::value + || std::is_same>>::value)) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf), single_gradients2, + pos_single, multiple_gradients2, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && (std::is_same>::value + || std::is_same>>::value)) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf), single_gradients2, + pos_single, multiple_gradients2, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && (std::is_same>::value + || std::is_same>>::value)) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf), single_gradients2, + pos_single, multiple_gradients2, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && (std::is_same>::value + || std::is_same>>::value)) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf), single_gradients2, + pos_single, multiple_gradients2, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && (std::is_same>::value + || std::is_same>>::value)) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf), single_gradients2, + pos_single, multiple_gradients2, pos_multiple, 1); + + pos_single = 0; + pos_multiple = 0; + if (!is_constant_all::value && !is_empty::value + && std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf), single_gradients3, + pos_single, multiple_gradients3, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf), single_gradients3, + pos_single, multiple_gradients3, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf), single_gradients3, + pos_single, multiple_gradients3, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf), single_gradients3, + pos_single, multiple_gradients3, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf), single_gradients3, + pos_single, multiple_gradients3, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf), single_gradients3, + pos_single, multiple_gradients3, pos_multiple, 1); + } + void test_lower_bound() { using stan::math::value_of; const size_t N_REPEAT = 3; @@ -820,6 +1058,10 @@ TYPED_TEST_P(AgradCdfTestFixture, RepeatAsVector) { this->test_repeat_as_vector(); } +TYPED_TEST_P(AgradCdfTestFixture, AsScalarsVsAsVector) { + this->test_as_scalars_vs_as_vector(); +} + TYPED_TEST_P(AgradCdfTestFixture, LowerBound) { this->test_lower_bound(); } TYPED_TEST_P(AgradCdfTestFixture, UpperBound) { this->test_upper_bound(); } @@ -830,7 +1072,8 @@ TYPED_TEST_P(AgradCdfTestFixture, Length0Vector) { REGISTER_TYPED_TEST_SUITE_P(AgradCdfTestFixture, CallAllVersions, ValidValues, InvalidValues, FiniteDiff, Function, RepeatAsVector, - LowerBound, UpperBound, Length0Vector); + LowerBound, UpperBound, AsScalarsVsAsVector, + Length0Vector); GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AgradCdfTestFixture); diff --git a/test/prob/test_fixture_cdf_log.hpp b/test/prob/test_fixture_cdf_log.hpp index 93b205ec7f2..c8fe548fc18 100644 --- a/test/prob/test_fixture_cdf_log.hpp +++ b/test/prob/test_fixture_cdf_log.hpp @@ -701,6 +701,249 @@ class AgradCdfLogTestFixture : public ::testing::Test { } } + void test_as_scalars_vs_as_vector() { + if (!any_vector::value) { + SUCCEED() << "No test for non-vector arguments"; + return; + } + vector expected_cdf_log; + vector> parameters; + TestClass.valid_values(parameters, expected_cdf_log); + + vector single_gradients1; + vector single_gradients2; + vector single_gradients3; + vector scalar_vars; + + vector multiple_gradients1; + vector multiple_gradients2; + vector multiple_gradients3; + vector vector_vars; + + T0 p0 = get_params(parameters, 0); + T1 p1 = get_params(parameters, 1); + T2 p2 = get_params(parameters, 2); + T3 p3 = get_params(parameters, 3); + T4 p4 = get_params(parameters, 4); + T5 p5 = get_params(parameters, 5); + + vector p0s = {get_param(parameters[0], 0)}; + vector p1s = {get_param(parameters[0], 1)}; + vector p2s = {get_param(parameters[0], 2)}; + vector p3s = {get_param(parameters[0], 3)}; + vector p4s = {get_param(parameters[0], 4)}; + vector p5s = {get_param(parameters[0], 5)}; + + T_return_type single_cdf_log + = TestClass.template cdf_log(p0s.back(), p1s.back(), + p2s.back(), p3s.back(), + p4s.back(), p5s.back()); + + for (size_t n = 1; n < parameters.size(); n++) { + p0s.push_back((is_vector::value) + ? get_param(parameters[n], 0) + : p0s.front()); + p1s.push_back((is_vector::value) + ? get_param(parameters[n], 1) + : p1s.front()); + p2s.push_back((is_vector::value) + ? get_param(parameters[n], 2) + : p2s.front()); + p3s.push_back((is_vector::value) + ? get_param(parameters[n], 3) + : p3s.front()); + p4s.push_back((is_vector::value) + ? get_param(parameters[n], 4) + : p4s.front()); + p5s.push_back((is_vector::value) + ? get_param(parameters[n], 5) + : p5s.front()); + + single_cdf_log += TestClass.template cdf_log( + p0s.back(), p1s.back(), p2s.back(), p3s.back(), p4s.back(), + p5s.back()); + } + + add_var(vector_vars, p0); + if (is_vector::value) + add_var(scalar_vars, p0s); + else + add_var(scalar_vars, p0s.front()); + + add_var(vector_vars, p1); + if (is_vector::value) + add_var(scalar_vars, p1s); + else + add_var(scalar_vars, p1s.front()); + + add_var(vector_vars, p2); + if (is_vector::value) + add_var(scalar_vars, p2s); + else + add_var(scalar_vars, p2s.front()); + + add_var(vector_vars, p3); + if (is_vector::value) + add_var(scalar_vars, p3s); + else + add_var(scalar_vars, p3s.front()); + + add_var(vector_vars, p4); + if (is_vector::value) + add_var(scalar_vars, p4s); + else + add_var(scalar_vars, p4s.front()); + + add_var(vector_vars, p5); + if (is_vector::value) + add_var(scalar_vars, p5s); + else + add_var(scalar_vars, p5s.front()); + + calculate_gradients_1storder(single_gradients1, single_cdf_log, + scalar_vars); + calculate_gradients_2ndorder(single_gradients2, single_cdf_log, + scalar_vars); + calculate_gradients_3rdorder(single_gradients3, single_cdf_log, + scalar_vars); + + T_return_type multiple_cdf_log + = TestClass.template cdf_log(p0, p1, p2, p3, p4, + p5); + + calculate_gradients_1storder(multiple_gradients1, multiple_cdf_log, + vector_vars); + calculate_gradients_2ndorder(multiple_gradients2, multiple_cdf_log, + vector_vars); + calculate_gradients_3rdorder(multiple_gradients3, multiple_cdf_log, + vector_vars); + + stan::math::recover_memory(); + + if (stan::math::is_inf(stan::math::value_of_rec(single_cdf_log)) + && stan::math::value_of_rec(single_cdf_log) + == stan::math::value_of_rec(multiple_cdf_log)) { + return; + } + + EXPECT_NEAR(stan::math::value_of_rec(single_cdf_log), + stan::math::value_of_rec(multiple_cdf_log), 1e-7) + << "cdf_log evaluated in loop should match vectorized equivalent"; + + size_t pos_single = 0; + size_t pos_multiple = 0; + if (!is_constant_all::value && !is_empty::value + && !std::is_same>::value + && !std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf_log), single_gradients1, + pos_single, multiple_gradients1, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && !std::is_same>::value + && !std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf_log), single_gradients1, + pos_single, multiple_gradients1, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && !std::is_same>::value + && !std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf_log), single_gradients1, + pos_single, multiple_gradients1, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && !std::is_same>::value + && !std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf_log), single_gradients1, + pos_single, multiple_gradients1, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && !std::is_same>::value + && !std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf_log), single_gradients1, + pos_single, multiple_gradients1, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && !std::is_same>::value + && !std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf_log), single_gradients1, + pos_single, multiple_gradients1, pos_multiple, 1); + + pos_single = 0; + pos_multiple = 0; + if (!is_constant_all::value && !is_empty::value + && (std::is_same>::value + || std::is_same>>::value)) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf_log), single_gradients2, + pos_single, multiple_gradients2, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && (std::is_same>::value + || std::is_same>>::value)) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf_log), single_gradients2, + pos_single, multiple_gradients2, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && (std::is_same>::value + || std::is_same>>::value)) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf_log), single_gradients2, + pos_single, multiple_gradients2, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && (std::is_same>::value + || std::is_same>>::value)) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf_log), single_gradients2, + pos_single, multiple_gradients2, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && (std::is_same>::value + || std::is_same>>::value)) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf_log), single_gradients2, + pos_single, multiple_gradients2, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && (std::is_same>::value + || std::is_same>>::value)) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf_log), single_gradients2, + pos_single, multiple_gradients2, pos_multiple, 1); + + pos_single = 0; + pos_multiple = 0; + if (!is_constant_all::value && !is_empty::value + && std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf_log), single_gradients3, + pos_single, multiple_gradients3, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf_log), single_gradients3, + pos_single, multiple_gradients3, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf_log), single_gradients3, + pos_single, multiple_gradients3, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf_log), single_gradients3, + pos_single, multiple_gradients3, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf_log), single_gradients3, + pos_single, multiple_gradients3, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && std::is_same>>::value) + test_multiple_gradient_values( + is_vector::value, value_of_rec(single_cdf_log), single_gradients3, + pos_single, multiple_gradients3, pos_multiple, 1); + } + void test_lower_bound() { using stan::math::value_of; const size_t N_REPEAT = 3; @@ -830,6 +1073,10 @@ TYPED_TEST_P(AgradCdfLogTestFixture, RepeatAsVector) { this->test_repeat_as_vector(); } +TYPED_TEST_P(AgradCdfLogTestFixture, AsScalarsVsAsVector) { + this->test_as_scalars_vs_as_vector(); +} + TYPED_TEST_P(AgradCdfLogTestFixture, LowerBound) { this->test_lower_bound(); } TYPED_TEST_P(AgradCdfLogTestFixture, UpperBound) { this->test_upper_bound(); } @@ -841,7 +1088,7 @@ TYPED_TEST_P(AgradCdfLogTestFixture, Length0Vector) { REGISTER_TYPED_TEST_SUITE_P(AgradCdfLogTestFixture, CallAllVersions, ValidValues, InvalidValues, FiniteDiff, Function, RepeatAsVector, LowerBound, UpperBound, - Length0Vector); + AsScalarsVsAsVector, Length0Vector); GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AgradCdfLogTestFixture); diff --git a/test/prob/test_fixture_distr.hpp b/test/prob/test_fixture_distr.hpp index 39d7e20cdda..88fad2d7f51 100644 --- a/test/prob/test_fixture_distr.hpp +++ b/test/prob/test_fixture_distr.hpp @@ -766,6 +766,247 @@ class AgradDistributionTestFixture : public ::testing::Test { } } + void test_as_scalars_vs_as_vector() { + if (all_constant::value) { + SUCCEED() << "No test for all double arguments"; + return; + } + if (!any_vector::value) { + SUCCEED() << "No test for non-vector arguments"; + return; + } + vector log_prob; + vector> parameters; + TestClass.valid_values(parameters, log_prob); + + vector single_gradients1; + vector single_gradients2; + vector single_gradients3; + vector scalar_vars; + + vector multiple_gradients1; + vector multiple_gradients2; + vector multiple_gradients3; + vector vector_vars; + + T0 p0 = get_params(parameters, 0); + T1 p1 = get_params(parameters, 1); + T2 p2 = get_params(parameters, 2); + T3 p3 = get_params(parameters, 3); + T4 p4 = get_params(parameters, 4); + T5 p5 = get_params(parameters, 5); + + vector p0s = {get_param(parameters[0], 0)}; + vector p1s = {get_param(parameters[0], 1)}; + vector p2s = {get_param(parameters[0], 2)}; + vector p3s = {get_param(parameters[0], 3)}; + vector p4s = {get_param(parameters[0], 4)}; + vector p5s = {get_param(parameters[0], 5)}; + + T_return_type single_lp + = TestClass.template log_prob(p0s.back(), p1s.back(), + p2s.back(), p3s.back(), + p4s.back(), p5s.back()); + + for (size_t n = 1; n < parameters.size(); n++) { + p0s.push_back((is_vector::value) + ? get_param(parameters[n], 0) + : p0s.front()); + p1s.push_back((is_vector::value) + ? get_param(parameters[n], 1) + : p1s.front()); + p2s.push_back((is_vector::value) + ? get_param(parameters[n], 2) + : p2s.front()); + p3s.push_back((is_vector::value) + ? get_param(parameters[n], 3) + : p3s.front()); + p4s.push_back((is_vector::value) + ? get_param(parameters[n], 4) + : p4s.front()); + p5s.push_back((is_vector::value) + ? get_param(parameters[n], 5) + : p5s.front()); + + single_lp += TestClass.template log_prob( + p0s.back(), p1s.back(), p2s.back(), p3s.back(), p4s.back(), + p5s.back()); + } + + add_var(vector_vars, p0); + if (is_vector::value) + add_var(scalar_vars, p0s); + else + add_var(scalar_vars, p0s.front()); + + add_var(vector_vars, p1); + if (is_vector::value) + add_var(scalar_vars, p1s); + else + add_var(scalar_vars, p1s.front()); + + add_var(vector_vars, p2); + if (is_vector::value) + add_var(scalar_vars, p2s); + else + add_var(scalar_vars, p2s.front()); + + add_var(vector_vars, p3); + if (is_vector::value) + add_var(scalar_vars, p3s); + else + add_var(scalar_vars, p3s.front()); + + add_var(vector_vars, p4); + if (is_vector::value) + add_var(scalar_vars, p4s); + else + add_var(scalar_vars, p4s.front()); + + add_var(vector_vars, p5); + if (is_vector::value) + add_var(scalar_vars, p5s); + else + add_var(scalar_vars, p5s.front()); + + calculate_gradients_1storder(single_gradients1, single_lp, scalar_vars); + calculate_gradients_2ndorder(single_gradients2, single_lp, scalar_vars); + calculate_gradients_3rdorder(single_gradients3, single_lp, scalar_vars); + + T_return_type multiple_lp + = TestClass.template log_prob( + p0, p1, p2, p3, p4, p5); + + calculate_gradients_1storder(multiple_gradients1, multiple_lp, vector_vars); + calculate_gradients_2ndorder(multiple_gradients2, multiple_lp, vector_vars); + calculate_gradients_3rdorder(multiple_gradients3, multiple_lp, vector_vars); + + stan::math::recover_memory(); + + if (stan::math::is_inf(stan::math::value_of_rec(single_lp)) + && stan::math::value_of_rec(single_lp) + == stan::math::value_of_rec(multiple_lp)) { + return; + } + + EXPECT_NEAR(stan::math::value_of_rec(single_lp), + stan::math::value_of_rec(multiple_lp), 1e-7) + << "log prob evaluated in loop should match vectorized equivalent"; + + size_t pos_single = 0; + size_t pos_multiple = 0; + if (!is_constant_all::value && !is_empty::value + && !std::is_same>::value + && !std::is_same>>::value) + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && !std::is_same>::value + && !std::is_same>>::value) + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && !std::is_same>::value + && !std::is_same>>::value) + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && !std::is_same>::value + && !std::is_same>>::value) + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && !std::is_same>::value + && !std::is_same>>::value) + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && !std::is_same>::value + && !std::is_same>>::value) + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, 1); + + pos_single = 0; + pos_multiple = 0; + if (!is_constant_all::value && !is_empty::value + && (std::is_same>::value + || std::is_same>>::value)) + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && (std::is_same>::value + || std::is_same>>::value)) + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && (std::is_same>::value + || std::is_same>>::value)) + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && (std::is_same>::value + || std::is_same>>::value)) + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && (std::is_same>::value + || std::is_same>>::value)) + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && (std::is_same>::value + || std::is_same>>::value)) + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, 1); + + pos_single = 0; + pos_multiple = 0; + if (!is_constant_all::value && !is_empty::value + && std::is_same>>::value) + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && std::is_same>>::value) + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && std::is_same>>::value) + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && std::is_same>>::value) + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && std::is_same>>::value) + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value + && std::is_same>>::value) + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, 1); + } + void test_length_0_vector() { if (!any_vector::value) { SUCCEED() << "No test for non-vector arguments"; @@ -823,13 +1064,17 @@ TYPED_TEST_P(AgradDistributionTestFixture, RepeatAsVector) { this->test_repeat_as_vector(); } +TYPED_TEST_P(AgradDistributionTestFixture, AsScalarsVsAsVector) { + this->test_as_scalars_vs_as_vector(); +} + TYPED_TEST_P(AgradDistributionTestFixture, Length0Vector) { this->test_length_0_vector(); } REGISTER_TYPED_TEST_SUITE_P(AgradDistributionTestFixture, CallAllVersions, ValidValues, InvalidValues, Propto, FiniteDiff, - Function, RepeatAsVector, Length0Vector); + Function, RepeatAsVector, AsScalarsVsAsVector, Length0Vector); GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AgradDistributionTestFixture); From cf8ead1b6576e07a4136de913e82cfe9d7dce691 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Tue, 15 Sep 2020 04:08:29 +0000 Subject: [PATCH 02/19] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final) --- test/prob/test_fixture_ccdf_log.hpp | 4 ++-- test/prob/test_fixture_cdf.hpp | 8 ++++---- test/prob/test_fixture_cdf_log.hpp | 4 ++-- test/prob/test_fixture_distr.hpp | 7 ++++--- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/test/prob/test_fixture_ccdf_log.hpp b/test/prob/test_fixture_ccdf_log.hpp index aad6f40eede..3d7e89f4315 100644 --- a/test/prob/test_fixture_ccdf_log.hpp +++ b/test/prob/test_fixture_ccdf_log.hpp @@ -828,8 +828,8 @@ class AgradCcdfLogTestFixture : public ::testing::Test { } EXPECT_NEAR(stan::math::value_of_rec(single_ccdf_log), - stan::math::value_of_rec(multiple_ccdf_log), 1e-7) - << "cdf_log evaluated in loop should match vectorized equivalent"; + stan::math::value_of_rec(multiple_ccdf_log), 1e-7) + << "cdf_log evaluated in loop should match vectorized equivalent"; size_t pos_single = 0; size_t pos_multiple = 0; diff --git a/test/prob/test_fixture_cdf.hpp b/test/prob/test_fixture_cdf.hpp index fd75ef370b0..4a0adfe6467 100644 --- a/test/prob/test_fixture_cdf.hpp +++ b/test/prob/test_fixture_cdf.hpp @@ -816,8 +816,8 @@ class AgradCdfTestFixture : public ::testing::Test { } EXPECT_NEAR(stan::math::value_of_rec(single_cdf), - stan::math::value_of_rec(multiple_cdf), 1e-7) - << "cdf evaluated in loop should match vectorized equivalent"; + stan::math::value_of_rec(multiple_cdf), 1e-7) + << "cdf evaluated in loop should match vectorized equivalent"; size_t pos_single = 0; size_t pos_multiple = 0; @@ -930,7 +930,7 @@ class AgradCdfTestFixture : public ::testing::Test { is_vector::value, value_of_rec(single_cdf), single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); } - + void test_lower_bound() { using stan::math::value_of; const size_t N_REPEAT = 3; @@ -1073,7 +1073,7 @@ TYPED_TEST_P(AgradCdfTestFixture, Length0Vector) { REGISTER_TYPED_TEST_SUITE_P(AgradCdfTestFixture, CallAllVersions, ValidValues, InvalidValues, FiniteDiff, Function, RepeatAsVector, LowerBound, UpperBound, AsScalarsVsAsVector, - Length0Vector); + Length0Vector); GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AgradCdfTestFixture); diff --git a/test/prob/test_fixture_cdf_log.hpp b/test/prob/test_fixture_cdf_log.hpp index c8fe548fc18..ae896329f80 100644 --- a/test/prob/test_fixture_cdf_log.hpp +++ b/test/prob/test_fixture_cdf_log.hpp @@ -829,8 +829,8 @@ class AgradCdfLogTestFixture : public ::testing::Test { } EXPECT_NEAR(stan::math::value_of_rec(single_cdf_log), - stan::math::value_of_rec(multiple_cdf_log), 1e-7) - << "cdf_log evaluated in loop should match vectorized equivalent"; + stan::math::value_of_rec(multiple_cdf_log), 1e-7) + << "cdf_log evaluated in loop should match vectorized equivalent"; size_t pos_single = 0; size_t pos_multiple = 0; diff --git a/test/prob/test_fixture_distr.hpp b/test/prob/test_fixture_distr.hpp index 88fad2d7f51..8eae8334a10 100644 --- a/test/prob/test_fixture_distr.hpp +++ b/test/prob/test_fixture_distr.hpp @@ -892,8 +892,8 @@ class AgradDistributionTestFixture : public ::testing::Test { } EXPECT_NEAR(stan::math::value_of_rec(single_lp), - stan::math::value_of_rec(multiple_lp), 1e-7) - << "log prob evaluated in loop should match vectorized equivalent"; + stan::math::value_of_rec(multiple_lp), 1e-7) + << "log prob evaluated in loop should match vectorized equivalent"; size_t pos_single = 0; size_t pos_multiple = 0; @@ -1074,7 +1074,8 @@ TYPED_TEST_P(AgradDistributionTestFixture, Length0Vector) { REGISTER_TYPED_TEST_SUITE_P(AgradDistributionTestFixture, CallAllVersions, ValidValues, InvalidValues, Propto, FiniteDiff, - Function, RepeatAsVector, AsScalarsVsAsVector, Length0Vector); + Function, RepeatAsVector, AsScalarsVsAsVector, + Length0Vector); GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AgradDistributionTestFixture); From ab8718ffed83c55a84d31bff7cbd3282d76e73c3 Mon Sep 17 00:00:00 2001 From: Ben Date: Tue, 15 Sep 2020 16:58:17 -0400 Subject: [PATCH 03/19] Removed unnecessary templating Enabled first order forward mode autodiff tests on probability distributions (Issue #1861) --- test/prob/test_fixture_ccdf_log.hpp | 211 +++++++++++------------- test/prob/test_fixture_cdf.hpp | 244 +++++++++++++-------------- test/prob/test_fixture_cdf_log.hpp | 245 +++++++++++++--------------- test/prob/test_fixture_distr.hpp | 242 +++++++++++++-------------- 4 files changed, 430 insertions(+), 512 deletions(-) diff --git a/test/prob/test_fixture_ccdf_log.hpp b/test/prob/test_fixture_ccdf_log.hpp index aad6f40eede..e35ae5a9119 100644 --- a/test/prob/test_fixture_ccdf_log.hpp +++ b/test/prob/test_fixture_ccdf_log.hpp @@ -493,7 +493,6 @@ class AgradCcdfLogTestFixture : public ::testing::Test { } void test_multiple_gradient_values(const bool is_vec, - const double single_ccdf_log, const vector& single_gradients, size_t& pos_single, const vector& multiple_gradients, @@ -518,6 +517,10 @@ class AgradCcdfLogTestFixture : public ::testing::Test { } } + /** + * Test that the vectorized functions work as expected when the elements + * of the vector are the same + */ void test_repeat_as_vector() { if (!any_vector::value) { SUCCEED() << "No test for non-vector arguments"; @@ -593,37 +596,37 @@ class AgradCcdfLogTestFixture : public ::testing::Test { && !std::is_same>::value && !std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_ccdf_log, single_gradients1, + is_vector::value, single_gradients1, pos_single, multiple_gradients1, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && !std::is_same>::value && !std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_ccdf_log, single_gradients1, + is_vector::value, single_gradients1, pos_single, multiple_gradients1, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && !std::is_same>::value && !std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_ccdf_log, single_gradients1, + is_vector::value, single_gradients1, pos_single, multiple_gradients1, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && !std::is_same>::value && !std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_ccdf_log, single_gradients1, + is_vector::value, single_gradients1, pos_single, multiple_gradients1, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && !std::is_same>::value && !std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_ccdf_log, single_gradients1, + is_vector::value, single_gradients1, pos_single, multiple_gradients1, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && !std::is_same>::value && !std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_ccdf_log, single_gradients1, + is_vector::value, single_gradients1, pos_single, multiple_gradients1, pos_multiple, N_REPEAT); pos_single = 0; @@ -632,37 +635,37 @@ class AgradCcdfLogTestFixture : public ::testing::Test { && (std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, single_ccdf_log, single_gradients2, + is_vector::value, single_gradients2, pos_single, multiple_gradients2, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && (std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, single_ccdf_log, single_gradients2, + is_vector::value, single_gradients2, pos_single, multiple_gradients2, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && (std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, single_ccdf_log, single_gradients2, + is_vector::value, single_gradients2, pos_single, multiple_gradients2, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && (std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, single_ccdf_log, single_gradients2, + is_vector::value, single_gradients2, pos_single, multiple_gradients2, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && (std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, single_ccdf_log, single_gradients2, + is_vector::value, single_gradients2, pos_single, multiple_gradients2, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && (std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, single_ccdf_log, single_gradients2, + is_vector::value, single_gradients2, pos_single, multiple_gradients2, pos_multiple, N_REPEAT); pos_single = 0; @@ -670,36 +673,40 @@ class AgradCcdfLogTestFixture : public ::testing::Test { if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_ccdf_log, single_gradients3, + is_vector::value, single_gradients3, pos_single, multiple_gradients3, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_ccdf_log, single_gradients3, + is_vector::value, single_gradients3, pos_single, multiple_gradients3, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_ccdf_log, single_gradients3, + is_vector::value, single_gradients3, pos_single, multiple_gradients3, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_ccdf_log, single_gradients3, + is_vector::value, single_gradients3, pos_single, multiple_gradients3, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_ccdf_log, single_gradients3, + is_vector::value, single_gradients3, pos_single, multiple_gradients3, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_ccdf_log, single_gradients3, + is_vector::value, single_gradients3, pos_single, multiple_gradients3, pos_multiple, N_REPEAT); } } + /** + * Test that the vectorized functions work as expected when the elements + * of the vector are different + */ void test_as_scalars_vs_as_vector() { if (!any_vector::value) { SUCCEED() << "No test for non-vector arguments"; @@ -734,72 +741,51 @@ class AgradCcdfLogTestFixture : public ::testing::Test { vector p5s = {get_param(parameters[0], 5)}; T_return_type single_ccdf_log - = TestClass.template ccdf_log(p0s.back(), p1s.back(), - p2s.back(), p3s.back(), - p4s.back(), p5s.back()); + = TestClass.ccdf_log(p0s.back(), p1s.back(), + p2s.back(), p3s.back(), + p4s.back(), p5s.back()); for (size_t n = 1; n < parameters.size(); n++) { - p0s.push_back((is_vector::value) - ? get_param(parameters[n], 0) - : p0s.front()); - p1s.push_back((is_vector::value) - ? get_param(parameters[n], 1) - : p1s.front()); - p2s.push_back((is_vector::value) - ? get_param(parameters[n], 2) - : p2s.front()); - p3s.push_back((is_vector::value) - ? get_param(parameters[n], 3) - : p3s.front()); - p4s.push_back((is_vector::value) - ? get_param(parameters[n], 4) - : p4s.front()); - p5s.push_back((is_vector::value) - ? get_param(parameters[n], 5) - : p5s.front()); - - single_ccdf_log += TestClass.template ccdf_log( - p0s.back(), p1s.back(), p2s.back(), p3s.back(), p4s.back(), - p5s.back()); + if(is_vector::value) + p0s.push_back(get_param(parameters[n], 0)); + + if(is_vector::value) + p1s.push_back(get_param(parameters[n], 1)); + + if(is_vector::value) + p2s.push_back(get_param(parameters[n], 2)); + + if(is_vector::value) + p3s.push_back(get_param(parameters[n], 3)); + + if(is_vector::value) + p4s.push_back(get_param(parameters[n], 4)); + + if(is_vector::value) + p5s.push_back(get_param(parameters[n], 5)); + + single_ccdf_log += TestClass.ccdf_log(p0s.back(), p1s.back(), + p2s.back(), p3s.back(), + p4s.back(), p5s.back()); } add_var(vector_vars, p0); - if (is_vector::value) - add_var(scalar_vars, p0s); - else - add_var(scalar_vars, p0s.front()); + add_var(scalar_vars, p0s); add_var(vector_vars, p1); - if (is_vector::value) - add_var(scalar_vars, p1s); - else - add_var(scalar_vars, p1s.front()); + add_var(scalar_vars, p1s); add_var(vector_vars, p2); - if (is_vector::value) - add_var(scalar_vars, p2s); - else - add_var(scalar_vars, p2s.front()); + add_var(scalar_vars, p2s); add_var(vector_vars, p3); - if (is_vector::value) - add_var(scalar_vars, p3s); - else - add_var(scalar_vars, p3s.front()); + add_var(scalar_vars, p3s); add_var(vector_vars, p4); - if (is_vector::value) - add_var(scalar_vars, p4s); - else - add_var(scalar_vars, p4s.front()); - + add_var(scalar_vars, p4s); + add_var(vector_vars, p5); - if (is_vector::value) - add_var(scalar_vars, p5s); - else - add_var(scalar_vars, p5s.front()); + add_var(scalar_vars, p5s); calculate_gradients_1storder(single_gradients1, single_ccdf_log, scalar_vars); @@ -809,8 +795,7 @@ class AgradCcdfLogTestFixture : public ::testing::Test { scalar_vars); T_return_type multiple_ccdf_log - = TestClass.template ccdf_log(p0, p1, p2, p3, - p4, p5); + = TestClass.ccdf_log(p0, p1, p2, p3, p4, p5); calculate_gradients_1storder(multiple_gradients1, multiple_ccdf_log, vector_vars); @@ -829,84 +814,78 @@ class AgradCcdfLogTestFixture : public ::testing::Test { EXPECT_NEAR(stan::math::value_of_rec(single_ccdf_log), stan::math::value_of_rec(multiple_ccdf_log), 1e-7) - << "cdf_log evaluated in loop should match vectorized equivalent"; + << "sum of scalar ccdf_logs should match vectorized result"; size_t pos_single = 0; size_t pos_multiple = 0; - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) + if (!is_constant_all::value && !is_empty::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_ccdf_log), + is_vector::value, single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) + if (!is_constant_all::value && !is_empty::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_ccdf_log), + is_vector::value, single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) + if (!is_constant_all::value && !is_empty::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_ccdf_log), + is_vector::value, single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) + if (!is_constant_all::value && !is_empty::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_ccdf_log), + is_vector::value, single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) + if (!is_constant_all::value && !is_empty::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_ccdf_log), + is_vector::value, single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) + if (!is_constant_all::value && !is_empty::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_ccdf_log), + is_vector::value, single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); pos_single = 0; pos_multiple = 0; if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value + && (std::is_same>>::value + || std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_ccdf_log), + is_vector::value, single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value + && (std::is_same>>::value + || std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_ccdf_log), + is_vector::value, single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value + && (std::is_same>>::value + || std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_ccdf_log), + is_vector::value, single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value + && (std::is_same>>::value + || std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_ccdf_log), + is_vector::value, single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value + && (std::is_same>>::value + || std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_ccdf_log), + is_vector::value, single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value + && (std::is_same>>::value + || std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_ccdf_log), + is_vector::value, single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); pos_single = 0; @@ -914,32 +893,32 @@ class AgradCcdfLogTestFixture : public ::testing::Test { if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_ccdf_log), + is_vector::value, single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_ccdf_log), + is_vector::value, single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_ccdf_log), + is_vector::value, single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_ccdf_log), + is_vector::value, single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_ccdf_log), + is_vector::value, single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_ccdf_log), + is_vector::value, single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); } diff --git a/test/prob/test_fixture_cdf.hpp b/test/prob/test_fixture_cdf.hpp index fd75ef370b0..b2d1f1b1b6e 100644 --- a/test/prob/test_fixture_cdf.hpp +++ b/test/prob/test_fixture_cdf.hpp @@ -482,7 +482,7 @@ class AgradCdfTestFixture : public ::testing::Test { } } - void test_multiple_gradient_values(const bool is_vec, const double single_cdf, + void test_multiple_gradient_values(const bool is_vec, const vector& single_gradients, size_t& pos_single, const vector& multiple_gradients, @@ -507,6 +507,10 @@ class AgradCdfTestFixture : public ::testing::Test { } } + /** + * Test that the vectorized functions work as expected when the elements + * of the vector are the same + */ void test_repeat_as_vector() { using stan::math::pow; using std::pow; @@ -586,37 +590,37 @@ class AgradCdfTestFixture : public ::testing::Test { && !std::is_same>::value && !std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_cdf, single_gradients1, pos_single, + is_vector::value, single_gradients1, pos_single, multiple_gradients1, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && !std::is_same>::value && !std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_cdf, single_gradients1, pos_single, + is_vector::value, single_gradients1, pos_single, multiple_gradients1, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && !std::is_same>::value && !std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_cdf, single_gradients1, pos_single, + is_vector::value, single_gradients1, pos_single, multiple_gradients1, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && !std::is_same>::value && !std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_cdf, single_gradients1, pos_single, + is_vector::value, single_gradients1, pos_single, multiple_gradients1, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && !std::is_same>::value && !std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_cdf, single_gradients1, pos_single, + is_vector::value, single_gradients1, pos_single, multiple_gradients1, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && !std::is_same>::value && !std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_cdf, single_gradients1, pos_single, + is_vector::value, single_gradients1, pos_single, multiple_gradients1, pos_multiple, N_REPEAT); pos_single = 0; @@ -625,37 +629,37 @@ class AgradCdfTestFixture : public ::testing::Test { && (std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, single_cdf, single_gradients2, pos_single, + is_vector::value, single_gradients2, pos_single, multiple_gradients2, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && (std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, single_cdf, single_gradients2, pos_single, + is_vector::value, single_gradients2, pos_single, multiple_gradients2, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && (std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, single_cdf, single_gradients2, pos_single, + is_vector::value, single_gradients2, pos_single, multiple_gradients2, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && (std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, single_cdf, single_gradients2, pos_single, + is_vector::value, single_gradients2, pos_single, multiple_gradients2, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && (std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, single_cdf, single_gradients2, pos_single, + is_vector::value, single_gradients2, pos_single, multiple_gradients2, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && (std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, single_cdf, single_gradients2, pos_single, + is_vector::value, single_gradients2, pos_single, multiple_gradients2, pos_multiple, N_REPEAT); pos_single = 0; @@ -663,36 +667,40 @@ class AgradCdfTestFixture : public ::testing::Test { if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_cdf, single_gradients3, pos_single, + is_vector::value, single_gradients3, pos_single, multiple_gradients3, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_cdf, single_gradients3, pos_single, + is_vector::value, single_gradients3, pos_single, multiple_gradients3, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_cdf, single_gradients3, pos_single, + is_vector::value, single_gradients3, pos_single, multiple_gradients3, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_cdf, single_gradients3, pos_single, + is_vector::value, single_gradients3, pos_single, multiple_gradients3, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_cdf, single_gradients3, pos_single, + is_vector::value, single_gradients3, pos_single, multiple_gradients3, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_cdf, single_gradients3, pos_single, + is_vector::value, single_gradients3, pos_single, multiple_gradients3, pos_multiple, N_REPEAT); } } + /** + * Test that the vectorized functions work as expected when the elements + * of the vector are different + */ void test_as_scalars_vs_as_vector() { if (!any_vector::value) { SUCCEED() << "No test for non-vector arguments"; @@ -727,78 +735,56 @@ class AgradCdfTestFixture : public ::testing::Test { vector p5s = {get_param(parameters[0], 5)}; T_return_type single_cdf - = TestClass.template cdf(p0s.back(), p1s.back(), p2s.back(), - p3s.back(), p4s.back(), p5s.back()); + = TestClass.cdf(p0s.back(), p1s.back(), p2s.back(), + p3s.back(), p4s.back(), p5s.back()); for (size_t n = 1; n < parameters.size(); n++) { - p0s.push_back((is_vector::value) - ? get_param(parameters[n], 0) - : p0s.front()); - p1s.push_back((is_vector::value) - ? get_param(parameters[n], 1) - : p1s.front()); - p2s.push_back((is_vector::value) - ? get_param(parameters[n], 2) - : p2s.front()); - p3s.push_back((is_vector::value) - ? get_param(parameters[n], 3) - : p3s.front()); - p4s.push_back((is_vector::value) - ? get_param(parameters[n], 4) - : p4s.front()); - p5s.push_back((is_vector::value) - ? get_param(parameters[n], 5) - : p5s.front()); - - single_cdf *= TestClass.template cdf( - p0s.back(), p1s.back(), p2s.back(), p3s.back(), p4s.back(), - p5s.back()); + if(is_vector::value) + p0s.push_back(get_param(parameters[n], 0)); + + if(is_vector::value) + p1s.push_back(get_param(parameters[n], 1)); + + if(is_vector::value) + p2s.push_back(get_param(parameters[n], 2)); + + if(is_vector::value) + p3s.push_back(get_param(parameters[n], 3)); + + if(is_vector::value) + p4s.push_back(get_param(parameters[n], 4)); + + if(is_vector::value) + p5s.push_back(get_param(parameters[n], 5)); + + single_cdf *= TestClass.cdf(p0s.back(), p1s.back(), + p2s.back(), p3s.back(), + p4s.back(), p5s.back()); } add_var(vector_vars, p0); - if (is_vector::value) - add_var(scalar_vars, p0s); - else - add_var(scalar_vars, p0s.front()); + add_var(scalar_vars, p0s); add_var(vector_vars, p1); - if (is_vector::value) - add_var(scalar_vars, p1s); - else - add_var(scalar_vars, p1s.front()); + add_var(scalar_vars, p1s); add_var(vector_vars, p2); - if (is_vector::value) - add_var(scalar_vars, p2s); - else - add_var(scalar_vars, p2s.front()); + add_var(scalar_vars, p2s); add_var(vector_vars, p3); - if (is_vector::value) - add_var(scalar_vars, p3s); - else - add_var(scalar_vars, p3s.front()); + add_var(scalar_vars, p3s); add_var(vector_vars, p4); - if (is_vector::value) - add_var(scalar_vars, p4s); - else - add_var(scalar_vars, p4s.front()); + add_var(scalar_vars, p4s); add_var(vector_vars, p5); - if (is_vector::value) - add_var(scalar_vars, p5s); - else - add_var(scalar_vars, p5s.front()); + add_var(scalar_vars, p5s); calculate_gradients_1storder(single_gradients1, single_cdf, scalar_vars); calculate_gradients_2ndorder(single_gradients2, single_cdf, scalar_vars); calculate_gradients_3rdorder(single_gradients3, single_cdf, scalar_vars); - T_return_type multiple_cdf = TestClass.template cdf( - p0, p1, p2, p3, p4, p5); + T_return_type multiple_cdf = TestClass.cdf(p0, p1, p2, p3, p4, p5); calculate_gradients_1storder(multiple_gradients1, multiple_cdf, vector_vars); @@ -817,118 +803,112 @@ class AgradCdfTestFixture : public ::testing::Test { EXPECT_NEAR(stan::math::value_of_rec(single_cdf), stan::math::value_of_rec(multiple_cdf), 1e-7) - << "cdf evaluated in loop should match vectorized equivalent"; + << "product of scalar cdfs should match vectorized result"; size_t pos_single = 0; size_t pos_multiple = 0; - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) + if (!is_constant_all::value && !is_empty::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf), single_gradients1, - pos_single, multiple_gradients1, pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) + is_vector::value, + single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf), single_gradients1, - pos_single, multiple_gradients1, pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) + is_vector::value, + single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf), single_gradients1, - pos_single, multiple_gradients1, pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) + is_vector::value, + single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf), single_gradients1, - pos_single, multiple_gradients1, pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) + is_vector::value, + single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf), single_gradients1, - pos_single, multiple_gradients1, pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) + is_vector::value, + single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf), single_gradients1, - pos_single, multiple_gradients1, pos_multiple, 1); + is_vector::value, + single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); pos_single = 0; pos_multiple = 0; if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value + && (std::is_same>>::value + || std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf), single_gradients2, - pos_single, multiple_gradients2, pos_multiple, 1); + is_vector::value, + single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value + && (std::is_same>>::value + || std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf), single_gradients2, - pos_single, multiple_gradients2, pos_multiple, 1); + is_vector::value, + single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value + && (std::is_same>>::value + || std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf), single_gradients2, - pos_single, multiple_gradients2, pos_multiple, 1); + is_vector::value, + single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value + && (std::is_same>>::value + || std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf), single_gradients2, - pos_single, multiple_gradients2, pos_multiple, 1); + is_vector::value, + single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value + && (std::is_same>>::value + || std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf), single_gradients2, - pos_single, multiple_gradients2, pos_multiple, 1); + is_vector::value, + single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value + && (std::is_same>>::value + || std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf), single_gradients2, - pos_single, multiple_gradients2, pos_multiple, 1); + is_vector::value, + single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); pos_single = 0; pos_multiple = 0; if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf), single_gradients3, - pos_single, multiple_gradients3, pos_multiple, 1); + is_vector::value, + single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf), single_gradients3, - pos_single, multiple_gradients3, pos_multiple, 1); + is_vector::value, + single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf), single_gradients3, - pos_single, multiple_gradients3, pos_multiple, 1); + is_vector::value, + single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf), single_gradients3, - pos_single, multiple_gradients3, pos_multiple, 1); + is_vector::value, + single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf), single_gradients3, - pos_single, multiple_gradients3, pos_multiple, 1); + is_vector::value, + single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf), single_gradients3, - pos_single, multiple_gradients3, pos_multiple, 1); + is_vector::value, + single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); } void test_lower_bound() { diff --git a/test/prob/test_fixture_cdf_log.hpp b/test/prob/test_fixture_cdf_log.hpp index c8fe548fc18..ad344aed1b8 100644 --- a/test/prob/test_fixture_cdf_log.hpp +++ b/test/prob/test_fixture_cdf_log.hpp @@ -494,7 +494,6 @@ class AgradCdfLogTestFixture : public ::testing::Test { } void test_multiple_gradient_values(const bool is_vec, - const double single_cdf_log, const vector& single_gradients, size_t& pos_single, const vector& multiple_gradients, @@ -519,6 +518,10 @@ class AgradCdfLogTestFixture : public ::testing::Test { } } + /** + * Test that the vectorized functions work as expected when the elements + * of the vector are the same + */ void test_repeat_as_vector() { if (!any_vector::value) { SUCCEED() << "No test for non-vector arguments"; @@ -594,37 +597,37 @@ class AgradCdfLogTestFixture : public ::testing::Test { && !std::is_same>::value && !std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_cdf_log, single_gradients1, pos_single, + is_vector::value, single_gradients1, pos_single, multiple_gradients1, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && !std::is_same>::value && !std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_cdf_log, single_gradients1, pos_single, + is_vector::value, single_gradients1, pos_single, multiple_gradients1, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && !std::is_same>::value && !std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_cdf_log, single_gradients1, pos_single, + is_vector::value, single_gradients1, pos_single, multiple_gradients1, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && !std::is_same>::value && !std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_cdf_log, single_gradients1, pos_single, + is_vector::value, single_gradients1, pos_single, multiple_gradients1, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && !std::is_same>::value && !std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_cdf_log, single_gradients1, pos_single, + is_vector::value, single_gradients1, pos_single, multiple_gradients1, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && !std::is_same>::value && !std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_cdf_log, single_gradients1, pos_single, + is_vector::value, single_gradients1, pos_single, multiple_gradients1, pos_multiple, N_REPEAT); pos_single = 0; @@ -633,37 +636,37 @@ class AgradCdfLogTestFixture : public ::testing::Test { && (std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, single_cdf_log, single_gradients2, pos_single, + is_vector::value, single_gradients2, pos_single, multiple_gradients2, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && (std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, single_cdf_log, single_gradients2, pos_single, + is_vector::value, single_gradients2, pos_single, multiple_gradients2, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && (std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, single_cdf_log, single_gradients2, pos_single, + is_vector::value, single_gradients2, pos_single, multiple_gradients2, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && (std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, single_cdf_log, single_gradients2, pos_single, + is_vector::value, single_gradients2, pos_single, multiple_gradients2, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && (std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, single_cdf_log, single_gradients2, pos_single, + is_vector::value, single_gradients2, pos_single, multiple_gradients2, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && (std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, single_cdf_log, single_gradients2, pos_single, + is_vector::value, single_gradients2, pos_single, multiple_gradients2, pos_multiple, N_REPEAT); pos_single = 0; @@ -671,36 +674,40 @@ class AgradCdfLogTestFixture : public ::testing::Test { if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_cdf_log, single_gradients3, pos_single, + is_vector::value, single_gradients3, pos_single, multiple_gradients3, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_cdf_log, single_gradients3, pos_single, + is_vector::value, single_gradients3, pos_single, multiple_gradients3, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_cdf_log, single_gradients3, pos_single, + is_vector::value, single_gradients3, pos_single, multiple_gradients3, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_cdf_log, single_gradients3, pos_single, + is_vector::value, single_gradients3, pos_single, multiple_gradients3, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_cdf_log, single_gradients3, pos_single, + is_vector::value, single_gradients3, pos_single, multiple_gradients3, pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, single_cdf_log, single_gradients3, pos_single, + is_vector::value, single_gradients3, pos_single, multiple_gradients3, pos_multiple, N_REPEAT); } } + /** + * Test that the vectorized functions work as expected when the elements + * of the vector are different + */ void test_as_scalars_vs_as_vector() { if (!any_vector::value) { SUCCEED() << "No test for non-vector arguments"; @@ -735,72 +742,51 @@ class AgradCdfLogTestFixture : public ::testing::Test { vector p5s = {get_param(parameters[0], 5)}; T_return_type single_cdf_log - = TestClass.template cdf_log(p0s.back(), p1s.back(), - p2s.back(), p3s.back(), - p4s.back(), p5s.back()); + = TestClass.cdf_log(p0s.back(), p1s.back(), + p2s.back(), p3s.back(), + p4s.back(), p5s.back()); for (size_t n = 1; n < parameters.size(); n++) { - p0s.push_back((is_vector::value) - ? get_param(parameters[n], 0) - : p0s.front()); - p1s.push_back((is_vector::value) - ? get_param(parameters[n], 1) - : p1s.front()); - p2s.push_back((is_vector::value) - ? get_param(parameters[n], 2) - : p2s.front()); - p3s.push_back((is_vector::value) - ? get_param(parameters[n], 3) - : p3s.front()); - p4s.push_back((is_vector::value) - ? get_param(parameters[n], 4) - : p4s.front()); - p5s.push_back((is_vector::value) - ? get_param(parameters[n], 5) - : p5s.front()); - - single_cdf_log += TestClass.template cdf_log( - p0s.back(), p1s.back(), p2s.back(), p3s.back(), p4s.back(), - p5s.back()); + if(is_vector::value) + p0s.push_back(get_param(parameters[n], 0)); + + if(is_vector::value) + p1s.push_back(get_param(parameters[n], 1)); + + if(is_vector::value) + p2s.push_back(get_param(parameters[n], 2)); + + if(is_vector::value) + p3s.push_back(get_param(parameters[n], 3)); + + if(is_vector::value) + p4s.push_back(get_param(parameters[n], 4)); + + if(is_vector::value) + p5s.push_back(get_param(parameters[n], 5)); + + single_cdf_log += TestClass.cdf_log(p0s.back(), p1s.back(), + p2s.back(), p3s.back(), + p4s.back(), p5s.back()); } add_var(vector_vars, p0); - if (is_vector::value) - add_var(scalar_vars, p0s); - else - add_var(scalar_vars, p0s.front()); + add_var(scalar_vars, p0s); add_var(vector_vars, p1); - if (is_vector::value) - add_var(scalar_vars, p1s); - else - add_var(scalar_vars, p1s.front()); + add_var(scalar_vars, p1s); add_var(vector_vars, p2); - if (is_vector::value) - add_var(scalar_vars, p2s); - else - add_var(scalar_vars, p2s.front()); + add_var(scalar_vars, p2s); add_var(vector_vars, p3); - if (is_vector::value) - add_var(scalar_vars, p3s); - else - add_var(scalar_vars, p3s.front()); + add_var(scalar_vars, p3s); add_var(vector_vars, p4); - if (is_vector::value) - add_var(scalar_vars, p4s); - else - add_var(scalar_vars, p4s.front()); + add_var(scalar_vars, p4s); add_var(vector_vars, p5); - if (is_vector::value) - add_var(scalar_vars, p5s); - else - add_var(scalar_vars, p5s.front()); + add_var(scalar_vars, p5s); calculate_gradients_1storder(single_gradients1, single_cdf_log, scalar_vars); @@ -810,8 +796,7 @@ class AgradCdfLogTestFixture : public ::testing::Test { scalar_vars); T_return_type multiple_cdf_log - = TestClass.template cdf_log(p0, p1, p2, p3, p4, - p5); + = TestClass.cdf_log(p0, p1, p2, p3, p4, p5); calculate_gradients_1storder(multiple_gradients1, multiple_cdf_log, vector_vars); @@ -830,118 +815,112 @@ class AgradCdfLogTestFixture : public ::testing::Test { EXPECT_NEAR(stan::math::value_of_rec(single_cdf_log), stan::math::value_of_rec(multiple_cdf_log), 1e-7) - << "cdf_log evaluated in loop should match vectorized equivalent"; + << "sum of scalar cdf_logs should match vectorized result"; size_t pos_single = 0; size_t pos_multiple = 0; - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) + if (!is_constant_all::value && !is_empty::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf_log), single_gradients1, - pos_single, multiple_gradients1, pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) + is_vector::value, + single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf_log), single_gradients1, - pos_single, multiple_gradients1, pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) + is_vector::value, + single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf_log), single_gradients1, - pos_single, multiple_gradients1, pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) + is_vector::value, + single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf_log), single_gradients1, - pos_single, multiple_gradients1, pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) + is_vector::value, + single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf_log), single_gradients1, - pos_single, multiple_gradients1, pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) + is_vector::value, + single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf_log), single_gradients1, - pos_single, multiple_gradients1, pos_multiple, 1); + is_vector::value, + single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); pos_single = 0; pos_multiple = 0; if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value + && (std::is_same>>::value + || std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf_log), single_gradients2, - pos_single, multiple_gradients2, pos_multiple, 1); + is_vector::value, + single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value + && (std::is_same>>::value + || std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf_log), single_gradients2, - pos_single, multiple_gradients2, pos_multiple, 1); + is_vector::value, + single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value + && (std::is_same>>::value + || std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf_log), single_gradients2, - pos_single, multiple_gradients2, pos_multiple, 1); + is_vector::value, + single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value + && (std::is_same>>::value + || std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf_log), single_gradients2, - pos_single, multiple_gradients2, pos_multiple, 1); + is_vector::value, + single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value + && (std::is_same>>::value + || std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf_log), single_gradients2, - pos_single, multiple_gradients2, pos_multiple, 1); + is_vector::value, + single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value + && (std::is_same>>::value + || std::is_same>::value || std::is_same>>::value)) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf_log), single_gradients2, - pos_single, multiple_gradients2, pos_multiple, 1); + is_vector::value, + single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); pos_single = 0; pos_multiple = 0; if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf_log), single_gradients3, - pos_single, multiple_gradients3, pos_multiple, 1); + is_vector::value, + single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf_log), single_gradients3, - pos_single, multiple_gradients3, pos_multiple, 1); + is_vector::value, + single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf_log), single_gradients3, - pos_single, multiple_gradients3, pos_multiple, 1); + is_vector::value, + single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf_log), single_gradients3, - pos_single, multiple_gradients3, pos_multiple, 1); + is_vector::value, + single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf_log), single_gradients3, - pos_single, multiple_gradients3, pos_multiple, 1); + is_vector::value, + single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) test_multiple_gradient_values( - is_vector::value, value_of_rec(single_cdf_log), single_gradients3, - pos_single, multiple_gradients3, pos_multiple, 1); + is_vector::value, + single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); } void test_lower_bound() { diff --git a/test/prob/test_fixture_distr.hpp b/test/prob/test_fixture_distr.hpp index 88fad2d7f51..ed419cd987a 100644 --- a/test/prob/test_fixture_distr.hpp +++ b/test/prob/test_fixture_distr.hpp @@ -580,6 +580,10 @@ class AgradDistributionTestFixture : public ::testing::Test { } } + /** + * Test that the vectorized functions work as expected when the elements + * of the vector are the same + */ void test_repeat_as_vector() { if (all_constant::value) { SUCCEED() << "No test for all double arguments"; @@ -766,6 +770,10 @@ class AgradDistributionTestFixture : public ::testing::Test { } } + /** + * Test that the vectorized functions work as expected when the elements + * of the vector are different + */ void test_as_scalars_vs_as_vector() { if (all_constant::value) { SUCCEED() << "No test for all double arguments"; @@ -804,80 +812,58 @@ class AgradDistributionTestFixture : public ::testing::Test { vector p5s = {get_param(parameters[0], 5)}; T_return_type single_lp - = TestClass.template log_prob(p0s.back(), p1s.back(), - p2s.back(), p3s.back(), - p4s.back(), p5s.back()); + = TestClass.template log_prob(p0s.back(), p1s.back(), + p2s.back(), p3s.back(), + p4s.back(), p5s.back()); for (size_t n = 1; n < parameters.size(); n++) { - p0s.push_back((is_vector::value) - ? get_param(parameters[n], 0) - : p0s.front()); - p1s.push_back((is_vector::value) - ? get_param(parameters[n], 1) - : p1s.front()); - p2s.push_back((is_vector::value) - ? get_param(parameters[n], 2) - : p2s.front()); - p3s.push_back((is_vector::value) - ? get_param(parameters[n], 3) - : p3s.front()); - p4s.push_back((is_vector::value) - ? get_param(parameters[n], 4) - : p4s.front()); - p5s.push_back((is_vector::value) - ? get_param(parameters[n], 5) - : p5s.front()); - - single_lp += TestClass.template log_prob( - p0s.back(), p1s.back(), p2s.back(), p3s.back(), p4s.back(), - p5s.back()); + if(is_vector::value) + p0s.push_back(get_param(parameters[n], 0)); + + if(is_vector::value) + p1s.push_back(get_param(parameters[n], 1)); + + if(is_vector::value) + p2s.push_back(get_param(parameters[n], 2)); + + if(is_vector::value) + p3s.push_back(get_param(parameters[n], 3)); + + if(is_vector::value) + p4s.push_back(get_param(parameters[n], 4)); + + if(is_vector::value) + p5s.push_back(get_param(parameters[n], 5)); + + single_lp += TestClass.log_prob(p0s.back(), p1s.back(), + p2s.back(), p3s.back(), + p4s.back(), p5s.back()); } add_var(vector_vars, p0); - if (is_vector::value) - add_var(scalar_vars, p0s); - else - add_var(scalar_vars, p0s.front()); + add_var(scalar_vars, p0s); add_var(vector_vars, p1); - if (is_vector::value) - add_var(scalar_vars, p1s); - else - add_var(scalar_vars, p1s.front()); + add_var(scalar_vars, p1s); add_var(vector_vars, p2); - if (is_vector::value) - add_var(scalar_vars, p2s); - else - add_var(scalar_vars, p2s.front()); + add_var(scalar_vars, p2s); add_var(vector_vars, p3); - if (is_vector::value) - add_var(scalar_vars, p3s); - else - add_var(scalar_vars, p3s.front()); + add_var(scalar_vars, p3s); add_var(vector_vars, p4); - if (is_vector::value) - add_var(scalar_vars, p4s); - else - add_var(scalar_vars, p4s.front()); + add_var(scalar_vars, p4s); add_var(vector_vars, p5); - if (is_vector::value) - add_var(scalar_vars, p5s); - else - add_var(scalar_vars, p5s.front()); + add_var(scalar_vars, p5s); calculate_gradients_1storder(single_gradients1, single_lp, scalar_vars); calculate_gradients_2ndorder(single_gradients2, single_lp, scalar_vars); calculate_gradients_3rdorder(single_gradients3, single_lp, scalar_vars); T_return_type multiple_lp - = TestClass.template log_prob( - p0, p1, p2, p3, p4, p5); + = TestClass.log_prob(p0, p1, p2, p3, p4, p5); calculate_gradients_1storder(multiple_gradients1, multiple_lp, vector_vars); calculate_gradients_2ndorder(multiple_gradients2, multiple_lp, vector_vars); @@ -897,114 +883,108 @@ class AgradDistributionTestFixture : public ::testing::Test { size_t pos_single = 0; size_t pos_multiple = 0; - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value) + test_multiple_gradient_values( + is_vector::value, + single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value) + test_multiple_gradient_values( + is_vector::value, + single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value) + test_multiple_gradient_values( + is_vector::value, + single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value) + test_multiple_gradient_values( + is_vector::value, + single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value) + test_multiple_gradient_values( + is_vector::value, + single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + if (!is_constant_all::value && !is_empty::value) + test_multiple_gradient_values( + is_vector::value, + single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); pos_single = 0; pos_multiple = 0; if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value + && (std::is_same>>::value + || std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, 1); + test_multiple_gradient_values( + is_vector::value, + single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value + && (std::is_same>>::value + || std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, 1); + test_multiple_gradient_values( + is_vector::value, + single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value + && (std::is_same>>::value + || std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, 1); + test_multiple_gradient_values( + is_vector::value, + single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value + && (std::is_same>>::value + || std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, 1); + test_multiple_gradient_values( + is_vector::value, + single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value + && (std::is_same>>::value + || std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, 1); + test_multiple_gradient_values( + is_vector::value, + single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value + && (std::is_same>>::value + || std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, 1); + test_multiple_gradient_values( + is_vector::value, + single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); pos_single = 0; pos_multiple = 0; if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, 1); + test_multiple_gradient_values( + is_vector::value, + single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, 1); + test_multiple_gradient_values( + is_vector::value, + single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, 1); + test_multiple_gradient_values( + is_vector::value, + single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, 1); + test_multiple_gradient_values( + is_vector::value, + single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, 1); + test_multiple_gradient_values( + is_vector::value, + single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, 1); + test_multiple_gradient_values( + is_vector::value, + single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); } void test_length_0_vector() { From 6ddeaa71743e141147ba6684a1630bee71c9ff3c Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Tue, 15 Sep 2020 17:01:25 -0400 Subject: [PATCH 04/19] [Jenkins] auto-formatting by clang-format version 6.0.1-14 (tags/RELEASE_601/final) --- test/prob/test_fixture_ccdf_log.hpp | 271 ++++++++++++++------------- test/prob/test_fixture_cdf.hpp | 268 ++++++++++++++------------- test/prob/test_fixture_cdf_log.hpp | 272 ++++++++++++++-------------- test/prob/test_fixture_distr.hpp | 160 ++++++++-------- 4 files changed, 479 insertions(+), 492 deletions(-) diff --git a/test/prob/test_fixture_ccdf_log.hpp b/test/prob/test_fixture_ccdf_log.hpp index e35ae5a9119..738e467ae92 100644 --- a/test/prob/test_fixture_ccdf_log.hpp +++ b/test/prob/test_fixture_ccdf_log.hpp @@ -595,111 +595,111 @@ class AgradCcdfLogTestFixture : public ::testing::Test { if (!is_constant_all::value && !is_empty::value && !std::is_same>::value && !std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients1, - pos_single, multiple_gradients1, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && !std::is_same>::value && !std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients1, - pos_single, multiple_gradients1, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && !std::is_same>::value && !std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients1, - pos_single, multiple_gradients1, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && !std::is_same>::value && !std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients1, - pos_single, multiple_gradients1, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && !std::is_same>::value && !std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients1, - pos_single, multiple_gradients1, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && !std::is_same>::value && !std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients1, - pos_single, multiple_gradients1, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, N_REPEAT); pos_single = 0; pos_multiple = 0; if (!is_constant_all::value && !is_empty::value && (std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, single_gradients2, - pos_single, multiple_gradients2, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && (std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, single_gradients2, - pos_single, multiple_gradients2, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && (std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, single_gradients2, - pos_single, multiple_gradients2, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && (std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, single_gradients2, - pos_single, multiple_gradients2, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && (std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, single_gradients2, - pos_single, multiple_gradients2, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && (std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, single_gradients2, - pos_single, multiple_gradients2, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, N_REPEAT); pos_single = 0; pos_multiple = 0; if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients3, - pos_single, multiple_gradients3, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients3, - pos_single, multiple_gradients3, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients3, - pos_single, multiple_gradients3, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients3, - pos_single, multiple_gradients3, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients3, - pos_single, multiple_gradients3, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients3, - pos_single, multiple_gradients3, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, N_REPEAT); } } @@ -740,33 +740,30 @@ class AgradCcdfLogTestFixture : public ::testing::Test { vector p4s = {get_param(parameters[0], 4)}; vector p5s = {get_param(parameters[0], 5)}; - T_return_type single_ccdf_log - = TestClass.ccdf_log(p0s.back(), p1s.back(), - p2s.back(), p3s.back(), - p4s.back(), p5s.back()); + T_return_type single_ccdf_log = TestClass.ccdf_log( + p0s.back(), p1s.back(), p2s.back(), p3s.back(), p4s.back(), p5s.back()); for (size_t n = 1; n < parameters.size(); n++) { - if(is_vector::value) - p0s.push_back(get_param(parameters[n], 0)); + if (is_vector::value) + p0s.push_back(get_param(parameters[n], 0)); - if(is_vector::value) - p1s.push_back(get_param(parameters[n], 1)); + if (is_vector::value) + p1s.push_back(get_param(parameters[n], 1)); - if(is_vector::value) - p2s.push_back(get_param(parameters[n], 2)); + if (is_vector::value) + p2s.push_back(get_param(parameters[n], 2)); - if(is_vector::value) - p3s.push_back(get_param(parameters[n], 3)); + if (is_vector::value) + p3s.push_back(get_param(parameters[n], 3)); - if(is_vector::value) - p4s.push_back(get_param(parameters[n], 4)); - - if(is_vector::value) - p5s.push_back(get_param(parameters[n], 5)); + if (is_vector::value) + p4s.push_back(get_param(parameters[n], 4)); - single_ccdf_log += TestClass.ccdf_log(p0s.back(), p1s.back(), - p2s.back(), p3s.back(), - p4s.back(), p5s.back()); + if (is_vector::value) + p5s.push_back(get_param(parameters[n], 5)); + + single_ccdf_log += TestClass.ccdf_log(p0s.back(), p1s.back(), p2s.back(), + p3s.back(), p4s.back(), p5s.back()); } add_var(vector_vars, p0); @@ -783,7 +780,7 @@ class AgradCcdfLogTestFixture : public ::testing::Test { add_var(vector_vars, p4); add_var(scalar_vars, p4s); - + add_var(vector_vars, p5); add_var(scalar_vars, p5s); @@ -813,113 +810,113 @@ class AgradCcdfLogTestFixture : public ::testing::Test { } EXPECT_NEAR(stan::math::value_of_rec(single_ccdf_log), - stan::math::value_of_rec(multiple_ccdf_log), 1e-7) - << "sum of scalar ccdf_logs should match vectorized result"; + stan::math::value_of_rec(multiple_ccdf_log), 1e-7) + << "sum of scalar ccdf_logs should match vectorized result"; size_t pos_single = 0; size_t pos_multiple = 0; if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, 1); pos_single = 0; pos_multiple = 0; if (!is_constant_all::value && !is_empty::value && (std::is_same>>::value - || std::is_same>::value + || std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, - single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && (std::is_same>>::value - || std::is_same>::value + || std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, - single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && (std::is_same>>::value - || std::is_same>::value + || std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, - single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && (std::is_same>>::value - || std::is_same>::value + || std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, - single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && (std::is_same>>::value - || std::is_same>::value + || std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, - single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && (std::is_same>>::value - || std::is_same>::value + || std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, - single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, 1); pos_single = 0; pos_multiple = 0; if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, 1); } void test_lower_bound() { diff --git a/test/prob/test_fixture_cdf.hpp b/test/prob/test_fixture_cdf.hpp index 461f569fcde..aa5de8fcea6 100644 --- a/test/prob/test_fixture_cdf.hpp +++ b/test/prob/test_fixture_cdf.hpp @@ -589,111 +589,111 @@ class AgradCdfTestFixture : public ::testing::Test { if (!is_constant_all::value && !is_empty::value && !std::is_same>::value && !std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients1, pos_single, - multiple_gradients1, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && !std::is_same>::value && !std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients1, pos_single, - multiple_gradients1, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && !std::is_same>::value && !std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients1, pos_single, - multiple_gradients1, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && !std::is_same>::value && !std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients1, pos_single, - multiple_gradients1, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && !std::is_same>::value && !std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients1, pos_single, - multiple_gradients1, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && !std::is_same>::value && !std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients1, pos_single, - multiple_gradients1, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, N_REPEAT); pos_single = 0; pos_multiple = 0; if (!is_constant_all::value && !is_empty::value && (std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, single_gradients2, pos_single, - multiple_gradients2, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && (std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, single_gradients2, pos_single, - multiple_gradients2, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && (std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, single_gradients2, pos_single, - multiple_gradients2, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && (std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, single_gradients2, pos_single, - multiple_gradients2, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && (std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, single_gradients2, pos_single, - multiple_gradients2, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && (std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, single_gradients2, pos_single, - multiple_gradients2, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, N_REPEAT); pos_single = 0; pos_multiple = 0; if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients3, pos_single, - multiple_gradients3, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients3, pos_single, - multiple_gradients3, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients3, pos_single, - multiple_gradients3, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients3, pos_single, - multiple_gradients3, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients3, pos_single, - multiple_gradients3, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients3, pos_single, - multiple_gradients3, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, N_REPEAT); } } @@ -734,32 +734,30 @@ class AgradCdfTestFixture : public ::testing::Test { vector p4s = {get_param(parameters[0], 4)}; vector p5s = {get_param(parameters[0], 5)}; - T_return_type single_cdf - = TestClass.cdf(p0s.back(), p1s.back(), p2s.back(), - p3s.back(), p4s.back(), p5s.back()); + T_return_type single_cdf = TestClass.cdf( + p0s.back(), p1s.back(), p2s.back(), p3s.back(), p4s.back(), p5s.back()); for (size_t n = 1; n < parameters.size(); n++) { - if(is_vector::value) - p0s.push_back(get_param(parameters[n], 0)); + if (is_vector::value) + p0s.push_back(get_param(parameters[n], 0)); - if(is_vector::value) - p1s.push_back(get_param(parameters[n], 1)); + if (is_vector::value) + p1s.push_back(get_param(parameters[n], 1)); - if(is_vector::value) - p2s.push_back(get_param(parameters[n], 2)); + if (is_vector::value) + p2s.push_back(get_param(parameters[n], 2)); - if(is_vector::value) - p3s.push_back(get_param(parameters[n], 3)); + if (is_vector::value) + p3s.push_back(get_param(parameters[n], 3)); - if(is_vector::value) - p4s.push_back(get_param(parameters[n], 4)); - - if(is_vector::value) - p5s.push_back(get_param(parameters[n], 5)); + if (is_vector::value) + p4s.push_back(get_param(parameters[n], 4)); - single_cdf *= TestClass.cdf(p0s.back(), p1s.back(), - p2s.back(), p3s.back(), - p4s.back(), p5s.back()); + if (is_vector::value) + p5s.push_back(get_param(parameters[n], 5)); + + single_cdf *= TestClass.cdf(p0s.back(), p1s.back(), p2s.back(), + p3s.back(), p4s.back(), p5s.back()); } add_var(vector_vars, p0); @@ -802,113 +800,113 @@ class AgradCdfTestFixture : public ::testing::Test { } EXPECT_NEAR(stan::math::value_of_rec(single_cdf), - stan::math::value_of_rec(multiple_cdf), 1e-7) - << "product of scalar cdfs should match vectorized result"; + stan::math::value_of_rec(multiple_cdf), 1e-7) + << "product of scalar cdfs should match vectorized result"; size_t pos_single = 0; size_t pos_multiple = 0; if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, 1); pos_single = 0; pos_multiple = 0; if (!is_constant_all::value && !is_empty::value && (std::is_same>>::value - || std::is_same>::value + || std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, - single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && (std::is_same>>::value - || std::is_same>::value + || std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, - single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && (std::is_same>>::value - || std::is_same>::value + || std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, - single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && (std::is_same>>::value - || std::is_same>::value + || std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, - single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && (std::is_same>>::value - || std::is_same>::value + || std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, - single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && (std::is_same>>::value - || std::is_same>::value + || std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, - single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, 1); pos_single = 0; pos_multiple = 0; if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, 1); } void test_lower_bound() { diff --git a/test/prob/test_fixture_cdf_log.hpp b/test/prob/test_fixture_cdf_log.hpp index ad344aed1b8..b4ab4910b9a 100644 --- a/test/prob/test_fixture_cdf_log.hpp +++ b/test/prob/test_fixture_cdf_log.hpp @@ -596,111 +596,111 @@ class AgradCdfLogTestFixture : public ::testing::Test { if (!is_constant_all::value && !is_empty::value && !std::is_same>::value && !std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients1, pos_single, - multiple_gradients1, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && !std::is_same>::value && !std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients1, pos_single, - multiple_gradients1, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && !std::is_same>::value && !std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients1, pos_single, - multiple_gradients1, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && !std::is_same>::value && !std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients1, pos_single, - multiple_gradients1, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && !std::is_same>::value && !std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients1, pos_single, - multiple_gradients1, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && !std::is_same>::value && !std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients1, pos_single, - multiple_gradients1, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, N_REPEAT); pos_single = 0; pos_multiple = 0; if (!is_constant_all::value && !is_empty::value && (std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, single_gradients2, pos_single, - multiple_gradients2, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && (std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, single_gradients2, pos_single, - multiple_gradients2, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && (std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, single_gradients2, pos_single, - multiple_gradients2, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && (std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, single_gradients2, pos_single, - multiple_gradients2, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && (std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, single_gradients2, pos_single, - multiple_gradients2, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && (std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, single_gradients2, pos_single, - multiple_gradients2, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, N_REPEAT); pos_single = 0; pos_multiple = 0; if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients3, pos_single, - multiple_gradients3, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients3, pos_single, - multiple_gradients3, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients3, pos_single, - multiple_gradients3, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients3, pos_single, - multiple_gradients3, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients3, pos_single, - multiple_gradients3, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, N_REPEAT); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, single_gradients3, pos_single, - multiple_gradients3, pos_multiple, N_REPEAT); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, N_REPEAT); } } @@ -741,33 +741,30 @@ class AgradCdfLogTestFixture : public ::testing::Test { vector p4s = {get_param(parameters[0], 4)}; vector p5s = {get_param(parameters[0], 5)}; - T_return_type single_cdf_log - = TestClass.cdf_log(p0s.back(), p1s.back(), - p2s.back(), p3s.back(), - p4s.back(), p5s.back()); + T_return_type single_cdf_log = TestClass.cdf_log( + p0s.back(), p1s.back(), p2s.back(), p3s.back(), p4s.back(), p5s.back()); for (size_t n = 1; n < parameters.size(); n++) { - if(is_vector::value) - p0s.push_back(get_param(parameters[n], 0)); + if (is_vector::value) + p0s.push_back(get_param(parameters[n], 0)); - if(is_vector::value) - p1s.push_back(get_param(parameters[n], 1)); + if (is_vector::value) + p1s.push_back(get_param(parameters[n], 1)); - if(is_vector::value) - p2s.push_back(get_param(parameters[n], 2)); + if (is_vector::value) + p2s.push_back(get_param(parameters[n], 2)); - if(is_vector::value) - p3s.push_back(get_param(parameters[n], 3)); + if (is_vector::value) + p3s.push_back(get_param(parameters[n], 3)); - if(is_vector::value) - p4s.push_back(get_param(parameters[n], 4)); - - if(is_vector::value) - p5s.push_back(get_param(parameters[n], 5)); + if (is_vector::value) + p4s.push_back(get_param(parameters[n], 4)); - single_cdf_log += TestClass.cdf_log(p0s.back(), p1s.back(), - p2s.back(), p3s.back(), - p4s.back(), p5s.back()); + if (is_vector::value) + p5s.push_back(get_param(parameters[n], 5)); + + single_cdf_log += TestClass.cdf_log(p0s.back(), p1s.back(), p2s.back(), + p3s.back(), p4s.back(), p5s.back()); } add_var(vector_vars, p0); @@ -795,8 +792,7 @@ class AgradCdfLogTestFixture : public ::testing::Test { calculate_gradients_3rdorder(single_gradients3, single_cdf_log, scalar_vars); - T_return_type multiple_cdf_log - = TestClass.cdf_log(p0, p1, p2, p3, p4, p5); + T_return_type multiple_cdf_log = TestClass.cdf_log(p0, p1, p2, p3, p4, p5); calculate_gradients_1storder(multiple_gradients1, multiple_cdf_log, vector_vars); @@ -814,113 +810,113 @@ class AgradCdfLogTestFixture : public ::testing::Test { } EXPECT_NEAR(stan::math::value_of_rec(single_cdf_log), - stan::math::value_of_rec(multiple_cdf_log), 1e-7) - << "sum of scalar cdf_logs should match vectorized result"; + stan::math::value_of_rec(multiple_cdf_log), 1e-7) + << "sum of scalar cdf_logs should match vectorized result"; size_t pos_single = 0; size_t pos_multiple = 0; if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, 1); pos_single = 0; pos_multiple = 0; if (!is_constant_all::value && !is_empty::value && (std::is_same>>::value - || std::is_same>::value + || std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, - single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && (std::is_same>>::value - || std::is_same>::value + || std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, - single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && (std::is_same>>::value - || std::is_same>::value + || std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, - single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && (std::is_same>>::value - || std::is_same>::value + || std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, - single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && (std::is_same>>::value - || std::is_same>::value + || std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, - single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && (std::is_same>>::value - || std::is_same>::value + || std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, - single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, 1); pos_single = 0; pos_multiple = 0; if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, 1); } void test_lower_bound() { diff --git a/test/prob/test_fixture_distr.hpp b/test/prob/test_fixture_distr.hpp index 81154e6d92c..2ad5946c332 100644 --- a/test/prob/test_fixture_distr.hpp +++ b/test/prob/test_fixture_distr.hpp @@ -811,33 +811,30 @@ class AgradDistributionTestFixture : public ::testing::Test { vector p4s = {get_param(parameters[0], 4)}; vector p5s = {get_param(parameters[0], 5)}; - T_return_type single_lp - = TestClass.template log_prob(p0s.back(), p1s.back(), - p2s.back(), p3s.back(), - p4s.back(), p5s.back()); + T_return_type single_lp = TestClass.template log_prob( + p0s.back(), p1s.back(), p2s.back(), p3s.back(), p4s.back(), p5s.back()); for (size_t n = 1; n < parameters.size(); n++) { - if(is_vector::value) - p0s.push_back(get_param(parameters[n], 0)); + if (is_vector::value) + p0s.push_back(get_param(parameters[n], 0)); - if(is_vector::value) - p1s.push_back(get_param(parameters[n], 1)); + if (is_vector::value) + p1s.push_back(get_param(parameters[n], 1)); - if(is_vector::value) - p2s.push_back(get_param(parameters[n], 2)); + if (is_vector::value) + p2s.push_back(get_param(parameters[n], 2)); - if(is_vector::value) - p3s.push_back(get_param(parameters[n], 3)); + if (is_vector::value) + p3s.push_back(get_param(parameters[n], 3)); - if(is_vector::value) - p4s.push_back(get_param(parameters[n], 4)); - - if(is_vector::value) - p5s.push_back(get_param(parameters[n], 5)); + if (is_vector::value) + p4s.push_back(get_param(parameters[n], 4)); - single_lp += TestClass.log_prob(p0s.back(), p1s.back(), - p2s.back(), p3s.back(), - p4s.back(), p5s.back()); + if (is_vector::value) + p5s.push_back(get_param(parameters[n], 5)); + + single_lp += TestClass.log_prob(p0s.back(), p1s.back(), p2s.back(), + p3s.back(), p4s.back(), p5s.back()); } add_var(vector_vars, p0); @@ -862,8 +859,7 @@ class AgradDistributionTestFixture : public ::testing::Test { calculate_gradients_2ndorder(single_gradients2, single_lp, scalar_vars); calculate_gradients_3rdorder(single_gradients3, single_lp, scalar_vars); - T_return_type multiple_lp - = TestClass.log_prob(p0, p1, p2, p3, p4, p5); + T_return_type multiple_lp = TestClass.log_prob(p0, p1, p2, p3, p4, p5); calculate_gradients_1storder(multiple_gradients1, multiple_lp, vector_vars); calculate_gradients_2ndorder(multiple_gradients2, multiple_lp, vector_vars); @@ -884,107 +880,107 @@ class AgradDistributionTestFixture : public ::testing::Test { size_t pos_single = 0; size_t pos_multiple = 0; if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients1, pos_single, multiple_gradients1, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients1, + pos_single, multiple_gradients1, + pos_multiple, 1); pos_single = 0; pos_multiple = 0; if (!is_constant_all::value && !is_empty::value && (std::is_same>>::value - || std::is_same>::value + || std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, - single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && (std::is_same>>::value - || std::is_same>::value + || std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, - single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && (std::is_same>>::value - || std::is_same>::value + || std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, - single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && (std::is_same>>::value - || std::is_same>::value + || std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, - single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && (std::is_same>>::value - || std::is_same>::value + || std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, - single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && (std::is_same>>::value - || std::is_same>::value + || std::is_same>::value || std::is_same>>::value)) - test_multiple_gradient_values( - is_vector::value, - single_gradients2, pos_single, multiple_gradients2, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients2, + pos_single, multiple_gradients2, + pos_multiple, 1); pos_single = 0; pos_multiple = 0; if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, 1); if (!is_constant_all::value && !is_empty::value && std::is_same>>::value) - test_multiple_gradient_values( - is_vector::value, - single_gradients3, pos_single, multiple_gradients3, pos_multiple, 1); + test_multiple_gradient_values(is_vector::value, single_gradients3, + pos_single, multiple_gradients3, + pos_multiple, 1); } void test_length_0_vector() { From fb2c63968bf4bd4e8e79be4346839f9da092c222 Mon Sep 17 00:00:00 2001 From: Ben Date: Thu, 17 Sep 2020 13:57:09 -0400 Subject: [PATCH 05/19] Updated test_as_vector to handle fvar and fvar> Replaced gradient checks (Issue #1861) --- test/prob/test_fixture_ccdf_log.hpp | 292 +++++---------------------- test/prob/test_fixture_cdf.hpp | 294 +++++---------------------- test/prob/test_fixture_cdf_log.hpp | 292 +++++---------------------- test/prob/test_fixture_distr.hpp | 296 +++++----------------------- test/prob/utility.hpp | 197 +++++------------- 5 files changed, 224 insertions(+), 1147 deletions(-) diff --git a/test/prob/test_fixture_ccdf_log.hpp b/test/prob/test_fixture_ccdf_log.hpp index 738e467ae92..16e48c4a066 100644 --- a/test/prob/test_fixture_ccdf_log.hpp +++ b/test/prob/test_fixture_ccdf_log.hpp @@ -492,31 +492,6 @@ class AgradCcdfLogTestFixture : public ::testing::Test { } } - void test_multiple_gradient_values(const bool is_vec, - const vector& single_gradients, - size_t& pos_single, - const vector& multiple_gradients, - size_t& pos_multiple, - const size_t N_REPEAT) { - if (is_vec) { - for (size_t i = 0; i < N_REPEAT; i++) { - EXPECT_NEAR(single_gradients[pos_single], - multiple_gradients[pos_multiple], 1e-7) - << "Comparison of single_gradient value to vectorized gradient " - "failed"; - pos_multiple++; - } - pos_single++; - } else { - EXPECT_NEAR(N_REPEAT * single_gradients[pos_single], - multiple_gradients[pos_multiple], 1e-7) - << "Comparison of single_gradient value to vectorized gradient " - "failed"; - pos_single++; - pos_multiple++; - } - } - /** * Test that the vectorized functions work as expected when the elements * of the vector are the same @@ -545,14 +520,18 @@ class AgradCcdfLogTestFixture : public ::testing::Test { vector s1; vector s2; vector s3; - add_vars(s1, p0_, p1_, p2_, p3_, p4_, p5_); - add_vars(s2, p0_, p1_, p2_, p3_, p4_, p5_); - add_vars(s3, p0_, p1_, p2_, p3_, p4_, p5_); + std::vector p0s_(N_REPEAT, p0_); + std::vector p1s_(N_REPEAT, p1_); + std::vector p2s_(N_REPEAT, p2_); + std::vector p3s_(N_REPEAT, p3_); + std::vector p4s_(N_REPEAT, p4_); + std::vector p5s_(N_REPEAT, p5_); + add_vars(s1, p0s_, p1s_, p2s_, p3s_, p4s_, p5s_); + add_vars(s2, p0s_, p1s_, p2s_, p3s_, p4s_, p5s_); + add_vars(s3, p0s_, p1s_, p2s_, p3s_, p4s_, p5s_); T_return_type ccdf_log - = TestClass.template ccdf_log(p0_, p1_, p2_, p3_, - p4_, p5_); + = N_REPEAT * TestClass.ccdf_log(p0_, p1_, p2_, p3_, p4_, p5_); double single_ccdf_log = calculate_gradients_1storder(single_gradients1, ccdf_log, s1); @@ -567,8 +546,7 @@ class AgradCcdfLogTestFixture : public ::testing::Test { T5 p5 = get_repeated_params(parameters[n], 5, N_REPEAT); T_return_type multiple_ccdf_log - = TestClass.template ccdf_log(p0, p1, p2, p3, - p4, p5); + = TestClass.ccdf_log(p0, p1, p2, p3, p4, p5); vector multiple_gradients1; vector multiple_gradients2; vector multiple_gradients3; @@ -585,121 +563,27 @@ class AgradCcdfLogTestFixture : public ::testing::Test { stan::math::recover_memory(); - EXPECT_NEAR(stan::math::value_of_rec(single_ccdf_log * N_REPEAT), + EXPECT_NEAR(stan::math::value_of_rec(single_ccdf_log), stan::math::value_of_rec(multiple_ccdf_log), 1e-8) << "ccdf_log with repeated vector input should match " << "a multiple of ccdf_log of single input"; - size_t pos_single = 0; - size_t pos_multiple = 0; - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, N_REPEAT); - - pos_single = 0; - pos_multiple = 0; - if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, N_REPEAT); - - pos_single = 0; - pos_multiple = 0; - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, N_REPEAT); + EXPECT_EQ(single_gradients1.size(), multiple_gradients1.size()) << + "scalar and vectorized results should produce the same number of first order gradients"; + EXPECT_EQ(single_gradients2.size(), multiple_gradients2.size()) << + "scalar and vectorized results should produce the same number of second order gradients"; + EXPECT_EQ(single_gradients3.size(), multiple_gradients3.size()) << + "scalar and vectorized results should produce the same number of third order gradients"; + + for(size_t i = 0; i < single_gradients1.size(); ++i) + EXPECT_NEAR(single_gradients1[i], multiple_gradients1[i], 1e-7) + << "scalar and vectorized results should have the same first order gradients"; + for(size_t i = 0; i < single_gradients2.size(); ++i) + EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) + << "scalar and vectorized results should have the same first order gradients"; + for(size_t i = 0; i < single_gradients3.size(); ++i) + EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) + << "scalar and vectorized results should have the same first order gradients"; } } @@ -813,110 +697,22 @@ class AgradCcdfLogTestFixture : public ::testing::Test { stan::math::value_of_rec(multiple_ccdf_log), 1e-7) << "sum of scalar ccdf_logs should match vectorized result"; - size_t pos_single = 0; - size_t pos_multiple = 0; - if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, 1); - - pos_single = 0; - pos_multiple = 0; - if (!is_constant_all::value && !is_empty::value - && (std::is_same>>::value - || std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>>::value - || std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>>::value - || std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>>::value - || std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>>::value - || std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>>::value - || std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, 1); - - pos_single = 0; - pos_multiple = 0; - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, 1); + EXPECT_EQ(single_gradients1.size(), multiple_gradients1.size()) << + "scalar and vectorized results should produce the same number of first order gradients"; + EXPECT_EQ(single_gradients2.size(), multiple_gradients2.size()) << + "scalar and vectorized results should produce the same number of second order gradients"; + EXPECT_EQ(single_gradients3.size(), multiple_gradients3.size()) << + "scalar and vectorized results should produce the same number of third order gradients"; + + for(size_t i = 0; i < single_gradients1.size(); ++i) + EXPECT_NEAR(single_gradients1[i], multiple_gradients1[i], 1e-7) + << "scalar and vectorized results should have the same first order gradients"; + for(size_t i = 0; i < single_gradients2.size(); ++i) + EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) + << "scalar and vectorized results should have the same first order gradients"; + for(size_t i = 0; i < single_gradients3.size(); ++i) + EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) + << "scalar and vectorized results should have the same first order gradients"; } void test_lower_bound() { diff --git a/test/prob/test_fixture_cdf.hpp b/test/prob/test_fixture_cdf.hpp index aa5de8fcea6..672b4f4511b 100644 --- a/test/prob/test_fixture_cdf.hpp +++ b/test/prob/test_fixture_cdf.hpp @@ -482,31 +482,6 @@ class AgradCdfTestFixture : public ::testing::Test { } } - void test_multiple_gradient_values(const bool is_vec, - const vector& single_gradients, - size_t& pos_single, - const vector& multiple_gradients, - size_t& pos_multiple, - const size_t N_REPEAT) { - if (is_vec) { - for (size_t i = 0; i < N_REPEAT; i++) { - EXPECT_NEAR(single_gradients[pos_single] / N_REPEAT, - multiple_gradients[pos_multiple], 1e-7) - << "Comparison of single_gradient value to vectorized gradient " - "failed"; - pos_multiple++; - } - pos_single++; - } else { - EXPECT_NEAR(single_gradients[pos_single], - multiple_gradients[pos_multiple], 1e-7) - << "Comparison of single_gradient value to vectorized gradient " - "failed"; - pos_single++; - pos_multiple++; - } - } - /** * Test that the vectorized functions work as expected when the elements * of the vector are the same @@ -539,14 +514,18 @@ class AgradCdfTestFixture : public ::testing::Test { vector s1; vector s2; vector s3; - add_vars(s1, p0_, p1_, p2_, p3_, p4_, p5_); - add_vars(s2, p0_, p1_, p2_, p3_, p4_, p5_); - add_vars(s3, p0_, p1_, p2_, p3_, p4_, p5_); - - T_return_type cdf = pow( - TestClass.template cdf(p0_, p1_, p2_, p3_, p4_, p5_), - N_REPEAT); + std::vector p0s_(N_REPEAT, p0_); + std::vector p1s_(N_REPEAT, p1_); + std::vector p2s_(N_REPEAT, p2_); + std::vector p3s_(N_REPEAT, p3_); + std::vector p4s_(N_REPEAT, p4_); + std::vector p5s_(N_REPEAT, p5_); + add_vars(s1, p0s_, p1s_, p2s_, p3s_, p4s_, p5s_); + add_vars(s2, p0s_, p1s_, p2s_, p3s_, p4s_, p5s_); + add_vars(s3, p0s_, p1s_, p2s_, p3s_, p4s_, p5s_); + + T_return_type cdf = + pow(TestClass.cdf(p0_, p1_, p2_, p3_, p4_, p5_), N_REPEAT); double single_cdf = calculate_gradients_1storder(single_gradients1, cdf, s1); @@ -561,8 +540,7 @@ class AgradCdfTestFixture : public ::testing::Test { T5 p5 = get_repeated_params(parameters[n], 5, N_REPEAT); T_return_type multiple_cdf - = TestClass.template cdf(p0, p1, p2, p3, p4, - p5); + = TestClass.cdf(p0, p1, p2, p3, p4, p5); vector multiple_gradients1; vector multiple_gradients2; vector multiple_gradients3; @@ -584,116 +562,22 @@ class AgradCdfTestFixture : public ::testing::Test { << "cdf with repeated vector input should match " << "a multiple of cdf of single input"; - size_t pos_single = 0; - size_t pos_multiple = 0; - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, N_REPEAT); - - pos_single = 0; - pos_multiple = 0; - if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, N_REPEAT); - - pos_single = 0; - pos_multiple = 0; - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, N_REPEAT); + EXPECT_EQ(single_gradients1.size(), multiple_gradients1.size()) << + "scalar and vectorized results should produce the same number of first order gradients"; + EXPECT_EQ(single_gradients2.size(), multiple_gradients2.size()) << + "scalar and vectorized results should produce the same number of second order gradients"; + EXPECT_EQ(single_gradients3.size(), multiple_gradients3.size()) << + "scalar and vectorized results should produce the same number of third order gradients"; + + for(size_t i = 0; i < single_gradients1.size(); ++i) + EXPECT_NEAR(single_gradients1[i], multiple_gradients1[i], 1e-7) + << "scalar and vectorized results should have the same first order gradients"; + for(size_t i = 0; i < single_gradients2.size(); ++i) + EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) + << "scalar and vectorized results should have the same first order gradients"; + for(size_t i = 0; i < single_gradients3.size(); ++i) + EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) + << "scalar and vectorized results should have the same first order gradients"; } } @@ -803,110 +687,22 @@ class AgradCdfTestFixture : public ::testing::Test { stan::math::value_of_rec(multiple_cdf), 1e-7) << "product of scalar cdfs should match vectorized result"; - size_t pos_single = 0; - size_t pos_multiple = 0; - if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, 1); - - pos_single = 0; - pos_multiple = 0; - if (!is_constant_all::value && !is_empty::value - && (std::is_same>>::value - || std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>>::value - || std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>>::value - || std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>>::value - || std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>>::value - || std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>>::value - || std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, 1); - - pos_single = 0; - pos_multiple = 0; - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, 1); + EXPECT_EQ(single_gradients1.size(), multiple_gradients1.size()) << + "scalar and vectorized results should produce the same number of first order gradients"; + EXPECT_EQ(single_gradients2.size(), multiple_gradients2.size()) << + "scalar and vectorized results should produce the same number of second order gradients"; + EXPECT_EQ(single_gradients3.size(), multiple_gradients3.size()) << + "scalar and vectorized results should produce the same number of third order gradients"; + + for(size_t i = 0; i < single_gradients1.size(); ++i) + EXPECT_NEAR(single_gradients1[i], multiple_gradients1[i], 1e-7) + << "scalar and vectorized results should have the same first order gradients"; + for(size_t i = 0; i < single_gradients2.size(); ++i) + EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) + << "scalar and vectorized results should have the same first order gradients"; + for(size_t i = 0; i < single_gradients3.size(); ++i) + EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) + << "scalar and vectorized results should have the same first order gradients"; } void test_lower_bound() { diff --git a/test/prob/test_fixture_cdf_log.hpp b/test/prob/test_fixture_cdf_log.hpp index b4ab4910b9a..53fd25056ff 100644 --- a/test/prob/test_fixture_cdf_log.hpp +++ b/test/prob/test_fixture_cdf_log.hpp @@ -493,31 +493,6 @@ class AgradCdfLogTestFixture : public ::testing::Test { } } - void test_multiple_gradient_values(const bool is_vec, - const vector& single_gradients, - size_t& pos_single, - const vector& multiple_gradients, - size_t& pos_multiple, - const size_t N_REPEAT) { - if (is_vec) { - for (size_t i = 0; i < N_REPEAT; i++) { - EXPECT_NEAR(single_gradients[pos_single], - multiple_gradients[pos_multiple], 1e-7) - << "Comparison of single_gradient value to vectorized gradient " - "failed"; - pos_multiple++; - } - pos_single++; - } else { - EXPECT_NEAR(N_REPEAT * single_gradients[pos_single], - multiple_gradients[pos_multiple], 1e-7) - << "Comparison of single_gradient value to vectorized gradient " - "failed"; - pos_single++; - pos_multiple++; - } - } - /** * Test that the vectorized functions work as expected when the elements * of the vector are the same @@ -546,14 +521,18 @@ class AgradCdfLogTestFixture : public ::testing::Test { vector s1; vector s2; vector s3; - add_vars(s1, p0_, p1_, p2_, p3_, p4_, p5_); - add_vars(s2, p0_, p1_, p2_, p3_, p4_, p5_); - add_vars(s3, p0_, p1_, p2_, p3_, p4_, p5_); + std::vector p0s_(N_REPEAT, p0_); + std::vector p1s_(N_REPEAT, p1_); + std::vector p2s_(N_REPEAT, p2_); + std::vector p3s_(N_REPEAT, p3_); + std::vector p4s_(N_REPEAT, p4_); + std::vector p5s_(N_REPEAT, p5_); + add_vars(s1, p0s_, p1s_, p2s_, p3s_, p4s_, p5s_); + add_vars(s2, p0s_, p1s_, p2s_, p3s_, p4s_, p5s_); + add_vars(s3, p0s_, p1s_, p2s_, p3s_, p4s_, p5s_); T_return_type cdf_log - = TestClass.template cdf_log(p0_, p1_, p2_, p3_, - p4_, p5_); + = N_REPEAT * TestClass.cdf_log(p0_, p1_, p2_, p3_, p4_, p5_); double single_cdf_log = calculate_gradients_1storder(single_gradients1, cdf_log, s1); @@ -568,8 +547,7 @@ class AgradCdfLogTestFixture : public ::testing::Test { T5 p5 = get_repeated_params(parameters[n], 5, N_REPEAT); T_return_type multiple_cdf_log - = TestClass.template cdf_log(p0, p1, p2, p3, - p4, p5); + = TestClass.cdf_log(p0, p1, p2, p3, p4, p5); vector multiple_gradients1; vector multiple_gradients2; vector multiple_gradients3; @@ -586,121 +564,27 @@ class AgradCdfLogTestFixture : public ::testing::Test { stan::math::recover_memory(); - EXPECT_NEAR(stan::math::value_of_rec(N_REPEAT * single_cdf_log), + EXPECT_NEAR(stan::math::value_of_rec(single_cdf_log), stan::math::value_of_rec(multiple_cdf_log), 1e-8) << "cdf_log with repeated vector input should match " << "a multiple of cdf_log of single input"; - size_t pos_single = 0; - size_t pos_multiple = 0; - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, N_REPEAT); - - pos_single = 0; - pos_multiple = 0; - if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, N_REPEAT); - - pos_single = 0; - pos_multiple = 0; - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, N_REPEAT); + EXPECT_EQ(single_gradients1.size(), multiple_gradients1.size()) << + "scalar and vectorized results should produce the same number of first order gradients"; + EXPECT_EQ(single_gradients2.size(), multiple_gradients2.size()) << + "scalar and vectorized results should produce the same number of second order gradients"; + EXPECT_EQ(single_gradients3.size(), multiple_gradients3.size()) << + "scalar and vectorized results should produce the same number of third order gradients"; + + for(size_t i = 0; i < single_gradients1.size(); ++i) + EXPECT_NEAR(single_gradients1[i], multiple_gradients1[i], 1e-7) + << "scalar and vectorized results should have the same first order gradients"; + for(size_t i = 0; i < single_gradients2.size(); ++i) + EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) + << "scalar and vectorized results should have the same first order gradients"; + for(size_t i = 0; i < single_gradients3.size(); ++i) + EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) + << "scalar and vectorized results should have the same first order gradients"; } } @@ -813,110 +697,22 @@ class AgradCdfLogTestFixture : public ::testing::Test { stan::math::value_of_rec(multiple_cdf_log), 1e-7) << "sum of scalar cdf_logs should match vectorized result"; - size_t pos_single = 0; - size_t pos_multiple = 0; - if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, 1); - - pos_single = 0; - pos_multiple = 0; - if (!is_constant_all::value && !is_empty::value - && (std::is_same>>::value - || std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>>::value - || std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>>::value - || std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>>::value - || std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>>::value - || std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>>::value - || std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, 1); - - pos_single = 0; - pos_multiple = 0; - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, 1); + EXPECT_EQ(single_gradients1.size(), multiple_gradients1.size()) << + "scalar and vectorized results should produce the same number of first order gradients"; + EXPECT_EQ(single_gradients2.size(), multiple_gradients2.size()) << + "scalar and vectorized results should produce the same number of second order gradients"; + EXPECT_EQ(single_gradients3.size(), multiple_gradients3.size()) << + "scalar and vectorized results should produce the same number of third order gradients"; + + for(size_t i = 0; i < single_gradients1.size(); ++i) + EXPECT_NEAR(single_gradients1[i], multiple_gradients1[i], 1e-7) + << "scalar and vectorized results should have the same first order gradients"; + for(size_t i = 0; i < single_gradients2.size(); ++i) + EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) + << "scalar and vectorized results should have the same first order gradients"; + for(size_t i = 0; i < single_gradients3.size(); ++i) + EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) + << "scalar and vectorized results should have the same first order gradients"; } void test_lower_bound() { diff --git a/test/prob/test_fixture_distr.hpp b/test/prob/test_fixture_distr.hpp index 2ad5946c332..bf9da7b9733 100644 --- a/test/prob/test_fixture_distr.hpp +++ b/test/prob/test_fixture_distr.hpp @@ -555,31 +555,6 @@ class AgradDistributionTestFixture : public ::testing::Test { } } - void test_multiple_gradient_values(const bool is_vec, - const vector& single_gradients, - size_t& pos_single, - const vector& multiple_gradients, - size_t& pos_multiple, - const size_t N_REPEAT) { - if (is_vec) { - for (size_t i = 0; i < N_REPEAT; i++) { - EXPECT_NEAR(single_gradients[pos_single], - multiple_gradients[pos_multiple], 1e-7) - << "Comparison of single_gradient value to vectorized gradient " - "failed"; - pos_multiple++; - } - pos_single++; - } else { - EXPECT_NEAR(single_gradients[pos_single] * double(N_REPEAT), - multiple_gradients[pos_multiple], 1e-7) - << "Comparison of single_gradient value to vectorized gradient " - "failed"; - pos_single++; - pos_multiple++; - } - } - /** * Test that the vectorized functions work as expected when the elements * of the vector are the same @@ -612,14 +587,18 @@ class AgradDistributionTestFixture : public ::testing::Test { vector s1; vector s2; vector s3; - add_vars(s1, p0_, p1_, p2_, p3_, p4_, p5_); - add_vars(s2, p0_, p1_, p2_, p3_, p4_, p5_); - add_vars(s3, p0_, p1_, p2_, p3_, p4_, p5_); + std::vector p0s_(N_REPEAT, p0_); + std::vector p1s_(N_REPEAT, p1_); + std::vector p2s_(N_REPEAT, p2_); + std::vector p3s_(N_REPEAT, p3_); + std::vector p4s_(N_REPEAT, p4_); + std::vector p5s_(N_REPEAT, p5_); + add_vars(s1, p0s_, p1s_, p2s_, p3s_, p4s_, p5s_); + add_vars(s2, p0s_, p1s_, p2s_, p3s_, p4s_, p5s_); + add_vars(s3, p0s_, p1s_, p2s_, p3s_, p4s_, p5s_); T_return_type logprob - = TestClass.template log_prob( - p0_, p1_, p2_, p3_, p4_, p5_); + = N_REPEAT * TestClass.log_prob(p0_, p1_, p2_, p3_, p4_, p5_); double single_lp = calculate_gradients_1storder(single_gradients1, logprob, s1); @@ -634,8 +613,7 @@ class AgradDistributionTestFixture : public ::testing::Test { T5 p5 = get_repeated_params(parameters[n], 5, N_REPEAT); T_return_type multiple_lp - = TestClass.template log_prob(p0, p1, p2, p3, - p4, p5); + = TestClass.log_prob(p0, p1, p2, p3, p4, p5); vector multiple_gradients1; vector multiple_gradients2; vector multiple_gradients3; @@ -652,121 +630,27 @@ class AgradDistributionTestFixture : public ::testing::Test { stan::math::recover_memory(); - EXPECT_NEAR(stan::math::value_of_rec(N_REPEAT * single_lp), + EXPECT_NEAR(stan::math::value_of_rec(single_lp), stan::math::value_of_rec(multiple_lp), 1e-8) << "log prob with repeated vector input should match " << "a multiple of log prob of single input"; - size_t pos_single = 0; - size_t pos_multiple = 0; - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && !std::is_same>::value - && !std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, N_REPEAT); - - pos_single = 0; - pos_multiple = 0; - if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, N_REPEAT); - - pos_single = 0; - pos_multiple = 0; - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, N_REPEAT); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, N_REPEAT); + EXPECT_EQ(single_gradients1.size(), multiple_gradients1.size()) << + "scalar and vectorized results should produce the same number of first order gradients"; + EXPECT_EQ(single_gradients2.size(), multiple_gradients2.size()) << + "scalar and vectorized results should produce the same number of second order gradients"; + EXPECT_EQ(single_gradients3.size(), multiple_gradients3.size()) << + "scalar and vectorized results should produce the same number of third order gradients"; + + for(size_t i = 0; i < single_gradients1.size(); ++i) + EXPECT_NEAR(single_gradients1[i], multiple_gradients1[i], 1e-7) + << "scalar and vectorized results should have the same first order gradients"; + for(size_t i = 0; i < single_gradients2.size(); ++i) + EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) + << "scalar and vectorized results should have the same first order gradients"; + for(size_t i = 0; i < single_gradients3.size(); ++i) + EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) + << "scalar and vectorized results should have the same first order gradients"; } } @@ -875,112 +759,24 @@ class AgradDistributionTestFixture : public ::testing::Test { EXPECT_NEAR(stan::math::value_of_rec(single_lp), stan::math::value_of_rec(multiple_lp), 1e-7) - << "log prob evaluated in loop should match vectorized equivalent"; - - size_t pos_single = 0; - size_t pos_multiple = 0; - if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value) - test_multiple_gradient_values(is_vector::value, single_gradients1, - pos_single, multiple_gradients1, - pos_multiple, 1); - - pos_single = 0; - pos_multiple = 0; - if (!is_constant_all::value && !is_empty::value - && (std::is_same>>::value - || std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>>::value - || std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>>::value - || std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>>::value - || std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>>::value - || std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && (std::is_same>>::value - || std::is_same>::value - || std::is_same>>::value)) - test_multiple_gradient_values(is_vector::value, single_gradients2, - pos_single, multiple_gradients2, - pos_multiple, 1); - - pos_single = 0; - pos_multiple = 0; - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, 1); - if (!is_constant_all::value && !is_empty::value - && std::is_same>>::value) - test_multiple_gradient_values(is_vector::value, single_gradients3, - pos_single, multiple_gradients3, - pos_multiple, 1); + << "sum of scalar log probs should match vectorized result"; + + EXPECT_EQ(single_gradients1.size(), multiple_gradients1.size()) << + "scalar and vectorized results should produce the same number of first order gradients"; + EXPECT_EQ(single_gradients2.size(), multiple_gradients2.size()) << + "scalar and vectorized results should produce the same number of second order gradients"; + EXPECT_EQ(single_gradients3.size(), multiple_gradients3.size()) << + "scalar and vectorized results should produce the same number of third order gradients"; + + for(size_t i = 0; i < single_gradients1.size(); ++i) + EXPECT_NEAR(single_gradients1[i], multiple_gradients1[i], 1e-7) + << "scalar and vectorized results should have the same first order gradients"; + for(size_t i = 0; i < single_gradients2.size(); ++i) + EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) + << "scalar and vectorized results should have the same first order gradients"; + for(size_t i = 0; i < single_gradients3.size(); ++i) + EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) + << "scalar and vectorized results should have the same first order gradients"; } void test_length_0_vector() { diff --git a/test/prob/utility.hpp b/test/prob/utility.hpp index 14376e04f38..153dbc9be86 100644 --- a/test/prob/utility.hpp +++ b/test/prob/utility.hpp @@ -426,169 +426,62 @@ vector>> get_params>>>( // ------------------------------------------------------------ // default template handles Eigen::Matrix -template +template * = nullptr> T get_repeated_params(const vector& parameters, const size_t p, const size_t N_REPEAT) { - T param(N_REPEAT); + T params(N_REPEAT); + stan::value_type_t param; + + if (p < parameters.size()) + param = get_param>(parameters, p); + else + param = 0; + for (size_t n = 0; n < N_REPEAT; n++) { - if (p < parameters.size()) - param(n) = get_param>(parameters, p); - else - param(n) = 0; + params(n) = param; } - return param; -} -// handle empty -template <> -empty get_repeated_params(const vector& /*parameters*/, - const size_t /*p*/, - const size_t /*N_REPEAT*/) { - return empty(); + return params; } -// handle scalars -template <> -double get_repeated_params(const vector& parameters, - const size_t p, const size_t /*N_REPEAT*/) { - double param(0); - if (p < parameters.size()) - param = parameters[p]; - return param; -} -template <> -var get_repeated_params(const vector& parameters, const size_t p, - const size_t /*N_REPEAT*/) { - var param(0); + +template * = nullptr> +T get_repeated_params(const vector& parameters, + const size_t p, const size_t N_REPEAT) { + T params(N_REPEAT); + stan::value_type_t param; + if (p < parameters.size()) - param = parameters[p]; - return param; -} -template <> -fvar get_repeated_params>(const vector& parameters, - const size_t p, - const size_t /*N_REPEAT*/) { - fvar param(0); - if (p < parameters.size()) { - param = parameters[p]; - param.d_ = 1.0; - } - return param; -} -template <> -fvar get_repeated_params>(const vector& parameters, - const size_t p, - const size_t /*N_REPEAT*/) { - fvar param(0); - if (p < parameters.size()) { - param = parameters[p]; - param.d_ = 1.0; - } - return param; -} -template <> -fvar> get_repeated_params>>( - const vector& parameters, const size_t p, - const size_t /*N_REPEAT*/) { - fvar> param(0); - if (p < parameters.size()) { - param = parameters[p]; - param.d_ = 1.0; - } - return param; -} -template <> -fvar> get_repeated_params>>( - const vector& parameters, const size_t p, - const size_t /*N_REPEAT*/) { - fvar> param(0); - if (p < parameters.size()) { - param = parameters[p]; - param.d_ = 1.0; + param = get_param>(parameters, p); + else + param = 0; + + for (size_t n = 0; n < N_REPEAT; n++) { + params[n] = param; } - return param; -} -template <> -int get_repeated_params(const vector& parameters, const size_t p, - const size_t /*N_REPEAT*/) { - int param(0); - if (p < parameters.size()) - param = (int)parameters[p]; - return param; -} -// handle vectors -template <> -vector get_repeated_params>(const vector& parameters, - const size_t p, - const size_t N_REPEAT) { - vector param(N_REPEAT); - for (size_t n = 0; n < N_REPEAT; n++) - if (p < parameters.size()) - param[n] = parameters[p]; - return param; -} -template <> -vector get_repeated_params>( - const vector& parameters, const size_t p, const size_t N_REPEAT) { - vector param(N_REPEAT); - for (size_t n = 0; n < N_REPEAT; n++) - if (p < parameters.size()) - param[n] = parameters[p]; - return param; -} -template <> -vector get_repeated_params>(const vector& parameters, - const size_t p, - const size_t N_REPEAT) { - vector param(N_REPEAT); - for (size_t n = 0; n < N_REPEAT; n++) - if (p < parameters.size()) - param[n] = parameters[p]; - return param; -} -template <> -vector> get_repeated_params>>( - const vector& parameters, const size_t p, const size_t N_REPEAT) { - vector> param(N_REPEAT); - for (size_t n = 0; n < N_REPEAT; n++) - if (p < parameters.size()) { - param[n] = parameters[p]; - param[n].d_ = 1.0; - } - return param; -} -template <> -vector> get_repeated_params>>( - const vector& parameters, const size_t p, const size_t N_REPEAT) { - vector> param(N_REPEAT); - for (size_t n = 0; n < N_REPEAT; n++) - if (p < parameters.size()) { - param[n] = parameters[p]; - param[n].d_ = 1.0; - } - return param; + return params; } -template <> -vector>> get_repeated_params>>>( - const vector& parameters, const size_t p, const size_t N_REPEAT) { - vector>> param(N_REPEAT); - for (size_t n = 0; n < N_REPEAT; n++) - if (p < parameters.size()) { - param[n] = parameters[p]; - param[n].d_ = 1.0; - } - return param; + +// handle empty +template ::value>* = nullptr> +T get_repeated_params(const vector&, + const size_t, + const size_t) { + return T(); } -template <> -vector>> get_repeated_params>>>( - const vector& parameters, const size_t p, const size_t N_REPEAT) { - vector>> param(N_REPEAT); - for (size_t n = 0; n < N_REPEAT; n++) - if (p < parameters.size()) { - param[n] = parameters[p]; - param[n].d_ = 1.0; - } - return param; + +// handle scalars +template * = nullptr> +T get_repeated_params(const vector& parameters, + const size_t p, const size_t /*N_REPEAT*/) { + if (p < parameters.size()) + return get_param(parameters, p); + else + return 0; } // ------------------------------------------------------------ From 63f6ad83edba4cc59f16343eaccc4ec4234a7271 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Thu, 17 Sep 2020 13:58:48 -0400 Subject: [PATCH 06/19] [Jenkins] auto-formatting by clang-format version 6.0.1-14 (tags/RELEASE_601/final) --- test/prob/test_fixture_ccdf_log.hpp | 70 +++++++++++++++----------- test/prob/test_fixture_cdf.hpp | 77 ++++++++++++++++------------- test/prob/test_fixture_cdf_log.hpp | 70 +++++++++++++++----------- test/prob/test_fixture_distr.hpp | 73 +++++++++++++++------------ test/prob/utility.hpp | 27 +++++----- 5 files changed, 179 insertions(+), 138 deletions(-) diff --git a/test/prob/test_fixture_ccdf_log.hpp b/test/prob/test_fixture_ccdf_log.hpp index 16e48c4a066..cfd1aae230e 100644 --- a/test/prob/test_fixture_ccdf_log.hpp +++ b/test/prob/test_fixture_ccdf_log.hpp @@ -568,22 +568,28 @@ class AgradCcdfLogTestFixture : public ::testing::Test { << "ccdf_log with repeated vector input should match " << "a multiple of ccdf_log of single input"; - EXPECT_EQ(single_gradients1.size(), multiple_gradients1.size()) << - "scalar and vectorized results should produce the same number of first order gradients"; - EXPECT_EQ(single_gradients2.size(), multiple_gradients2.size()) << - "scalar and vectorized results should produce the same number of second order gradients"; - EXPECT_EQ(single_gradients3.size(), multiple_gradients3.size()) << - "scalar and vectorized results should produce the same number of third order gradients"; - - for(size_t i = 0; i < single_gradients1.size(); ++i) - EXPECT_NEAR(single_gradients1[i], multiple_gradients1[i], 1e-7) - << "scalar and vectorized results should have the same first order gradients"; - for(size_t i = 0; i < single_gradients2.size(); ++i) - EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) - << "scalar and vectorized results should have the same first order gradients"; - for(size_t i = 0; i < single_gradients3.size(); ++i) - EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) - << "scalar and vectorized results should have the same first order gradients"; + EXPECT_EQ(single_gradients1.size(), multiple_gradients1.size()) + << "scalar and vectorized results should produce the same number of " + "first order gradients"; + EXPECT_EQ(single_gradients2.size(), multiple_gradients2.size()) + << "scalar and vectorized results should produce the same number of " + "second order gradients"; + EXPECT_EQ(single_gradients3.size(), multiple_gradients3.size()) + << "scalar and vectorized results should produce the same number of " + "third order gradients"; + + for (size_t i = 0; i < single_gradients1.size(); ++i) + EXPECT_NEAR(single_gradients1[i], multiple_gradients1[i], 1e-7) + << "scalar and vectorized results should have the same first order " + "gradients"; + for (size_t i = 0; i < single_gradients2.size(); ++i) + EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) + << "scalar and vectorized results should have the same first order " + "gradients"; + for (size_t i = 0; i < single_gradients3.size(); ++i) + EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) + << "scalar and vectorized results should have the same first order " + "gradients"; } } @@ -697,22 +703,28 @@ class AgradCcdfLogTestFixture : public ::testing::Test { stan::math::value_of_rec(multiple_ccdf_log), 1e-7) << "sum of scalar ccdf_logs should match vectorized result"; - EXPECT_EQ(single_gradients1.size(), multiple_gradients1.size()) << - "scalar and vectorized results should produce the same number of first order gradients"; - EXPECT_EQ(single_gradients2.size(), multiple_gradients2.size()) << - "scalar and vectorized results should produce the same number of second order gradients"; - EXPECT_EQ(single_gradients3.size(), multiple_gradients3.size()) << - "scalar and vectorized results should produce the same number of third order gradients"; - - for(size_t i = 0; i < single_gradients1.size(); ++i) + EXPECT_EQ(single_gradients1.size(), multiple_gradients1.size()) + << "scalar and vectorized results should produce the same number of " + "first order gradients"; + EXPECT_EQ(single_gradients2.size(), multiple_gradients2.size()) + << "scalar and vectorized results should produce the same number of " + "second order gradients"; + EXPECT_EQ(single_gradients3.size(), multiple_gradients3.size()) + << "scalar and vectorized results should produce the same number of " + "third order gradients"; + + for (size_t i = 0; i < single_gradients1.size(); ++i) EXPECT_NEAR(single_gradients1[i], multiple_gradients1[i], 1e-7) - << "scalar and vectorized results should have the same first order gradients"; - for(size_t i = 0; i < single_gradients2.size(); ++i) + << "scalar and vectorized results should have the same first order " + "gradients"; + for (size_t i = 0; i < single_gradients2.size(); ++i) EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) - << "scalar and vectorized results should have the same first order gradients"; - for(size_t i = 0; i < single_gradients3.size(); ++i) + << "scalar and vectorized results should have the same first order " + "gradients"; + for (size_t i = 0; i < single_gradients3.size(); ++i) EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) - << "scalar and vectorized results should have the same first order gradients"; + << "scalar and vectorized results should have the same first order " + "gradients"; } void test_lower_bound() { diff --git a/test/prob/test_fixture_cdf.hpp b/test/prob/test_fixture_cdf.hpp index 672b4f4511b..265a4b1b522 100644 --- a/test/prob/test_fixture_cdf.hpp +++ b/test/prob/test_fixture_cdf.hpp @@ -524,8 +524,8 @@ class AgradCdfTestFixture : public ::testing::Test { add_vars(s2, p0s_, p1s_, p2s_, p3s_, p4s_, p5s_); add_vars(s3, p0s_, p1s_, p2s_, p3s_, p4s_, p5s_); - T_return_type cdf = - pow(TestClass.cdf(p0_, p1_, p2_, p3_, p4_, p5_), N_REPEAT); + T_return_type cdf + = pow(TestClass.cdf(p0_, p1_, p2_, p3_, p4_, p5_), N_REPEAT); double single_cdf = calculate_gradients_1storder(single_gradients1, cdf, s1); @@ -539,8 +539,7 @@ class AgradCdfTestFixture : public ::testing::Test { T4 p4 = get_repeated_params(parameters[n], 4, N_REPEAT); T5 p5 = get_repeated_params(parameters[n], 5, N_REPEAT); - T_return_type multiple_cdf - = TestClass.cdf(p0, p1, p2, p3, p4, p5); + T_return_type multiple_cdf = TestClass.cdf(p0, p1, p2, p3, p4, p5); vector multiple_gradients1; vector multiple_gradients2; vector multiple_gradients3; @@ -562,22 +561,28 @@ class AgradCdfTestFixture : public ::testing::Test { << "cdf with repeated vector input should match " << "a multiple of cdf of single input"; - EXPECT_EQ(single_gradients1.size(), multiple_gradients1.size()) << - "scalar and vectorized results should produce the same number of first order gradients"; - EXPECT_EQ(single_gradients2.size(), multiple_gradients2.size()) << - "scalar and vectorized results should produce the same number of second order gradients"; - EXPECT_EQ(single_gradients3.size(), multiple_gradients3.size()) << - "scalar and vectorized results should produce the same number of third order gradients"; - - for(size_t i = 0; i < single_gradients1.size(); ++i) - EXPECT_NEAR(single_gradients1[i], multiple_gradients1[i], 1e-7) - << "scalar and vectorized results should have the same first order gradients"; - for(size_t i = 0; i < single_gradients2.size(); ++i) - EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) - << "scalar and vectorized results should have the same first order gradients"; - for(size_t i = 0; i < single_gradients3.size(); ++i) - EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) - << "scalar and vectorized results should have the same first order gradients"; + EXPECT_EQ(single_gradients1.size(), multiple_gradients1.size()) + << "scalar and vectorized results should produce the same number of " + "first order gradients"; + EXPECT_EQ(single_gradients2.size(), multiple_gradients2.size()) + << "scalar and vectorized results should produce the same number of " + "second order gradients"; + EXPECT_EQ(single_gradients3.size(), multiple_gradients3.size()) + << "scalar and vectorized results should produce the same number of " + "third order gradients"; + + for (size_t i = 0; i < single_gradients1.size(); ++i) + EXPECT_NEAR(single_gradients1[i], multiple_gradients1[i], 1e-7) + << "scalar and vectorized results should have the same first order " + "gradients"; + for (size_t i = 0; i < single_gradients2.size(); ++i) + EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) + << "scalar and vectorized results should have the same first order " + "gradients"; + for (size_t i = 0; i < single_gradients3.size(); ++i) + EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) + << "scalar and vectorized results should have the same first order " + "gradients"; } } @@ -687,22 +692,28 @@ class AgradCdfTestFixture : public ::testing::Test { stan::math::value_of_rec(multiple_cdf), 1e-7) << "product of scalar cdfs should match vectorized result"; - EXPECT_EQ(single_gradients1.size(), multiple_gradients1.size()) << - "scalar and vectorized results should produce the same number of first order gradients"; - EXPECT_EQ(single_gradients2.size(), multiple_gradients2.size()) << - "scalar and vectorized results should produce the same number of second order gradients"; - EXPECT_EQ(single_gradients3.size(), multiple_gradients3.size()) << - "scalar and vectorized results should produce the same number of third order gradients"; - - for(size_t i = 0; i < single_gradients1.size(); ++i) + EXPECT_EQ(single_gradients1.size(), multiple_gradients1.size()) + << "scalar and vectorized results should produce the same number of " + "first order gradients"; + EXPECT_EQ(single_gradients2.size(), multiple_gradients2.size()) + << "scalar and vectorized results should produce the same number of " + "second order gradients"; + EXPECT_EQ(single_gradients3.size(), multiple_gradients3.size()) + << "scalar and vectorized results should produce the same number of " + "third order gradients"; + + for (size_t i = 0; i < single_gradients1.size(); ++i) EXPECT_NEAR(single_gradients1[i], multiple_gradients1[i], 1e-7) - << "scalar and vectorized results should have the same first order gradients"; - for(size_t i = 0; i < single_gradients2.size(); ++i) + << "scalar and vectorized results should have the same first order " + "gradients"; + for (size_t i = 0; i < single_gradients2.size(); ++i) EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) - << "scalar and vectorized results should have the same first order gradients"; - for(size_t i = 0; i < single_gradients3.size(); ++i) + << "scalar and vectorized results should have the same first order " + "gradients"; + for (size_t i = 0; i < single_gradients3.size(); ++i) EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) - << "scalar and vectorized results should have the same first order gradients"; + << "scalar and vectorized results should have the same first order " + "gradients"; } void test_lower_bound() { diff --git a/test/prob/test_fixture_cdf_log.hpp b/test/prob/test_fixture_cdf_log.hpp index 53fd25056ff..e90efedb3b8 100644 --- a/test/prob/test_fixture_cdf_log.hpp +++ b/test/prob/test_fixture_cdf_log.hpp @@ -569,22 +569,28 @@ class AgradCdfLogTestFixture : public ::testing::Test { << "cdf_log with repeated vector input should match " << "a multiple of cdf_log of single input"; - EXPECT_EQ(single_gradients1.size(), multiple_gradients1.size()) << - "scalar and vectorized results should produce the same number of first order gradients"; - EXPECT_EQ(single_gradients2.size(), multiple_gradients2.size()) << - "scalar and vectorized results should produce the same number of second order gradients"; - EXPECT_EQ(single_gradients3.size(), multiple_gradients3.size()) << - "scalar and vectorized results should produce the same number of third order gradients"; - - for(size_t i = 0; i < single_gradients1.size(); ++i) - EXPECT_NEAR(single_gradients1[i], multiple_gradients1[i], 1e-7) - << "scalar and vectorized results should have the same first order gradients"; - for(size_t i = 0; i < single_gradients2.size(); ++i) - EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) - << "scalar and vectorized results should have the same first order gradients"; - for(size_t i = 0; i < single_gradients3.size(); ++i) - EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) - << "scalar and vectorized results should have the same first order gradients"; + EXPECT_EQ(single_gradients1.size(), multiple_gradients1.size()) + << "scalar and vectorized results should produce the same number of " + "first order gradients"; + EXPECT_EQ(single_gradients2.size(), multiple_gradients2.size()) + << "scalar and vectorized results should produce the same number of " + "second order gradients"; + EXPECT_EQ(single_gradients3.size(), multiple_gradients3.size()) + << "scalar and vectorized results should produce the same number of " + "third order gradients"; + + for (size_t i = 0; i < single_gradients1.size(); ++i) + EXPECT_NEAR(single_gradients1[i], multiple_gradients1[i], 1e-7) + << "scalar and vectorized results should have the same first order " + "gradients"; + for (size_t i = 0; i < single_gradients2.size(); ++i) + EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) + << "scalar and vectorized results should have the same first order " + "gradients"; + for (size_t i = 0; i < single_gradients3.size(); ++i) + EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) + << "scalar and vectorized results should have the same first order " + "gradients"; } } @@ -697,22 +703,28 @@ class AgradCdfLogTestFixture : public ::testing::Test { stan::math::value_of_rec(multiple_cdf_log), 1e-7) << "sum of scalar cdf_logs should match vectorized result"; - EXPECT_EQ(single_gradients1.size(), multiple_gradients1.size()) << - "scalar and vectorized results should produce the same number of first order gradients"; - EXPECT_EQ(single_gradients2.size(), multiple_gradients2.size()) << - "scalar and vectorized results should produce the same number of second order gradients"; - EXPECT_EQ(single_gradients3.size(), multiple_gradients3.size()) << - "scalar and vectorized results should produce the same number of third order gradients"; - - for(size_t i = 0; i < single_gradients1.size(); ++i) + EXPECT_EQ(single_gradients1.size(), multiple_gradients1.size()) + << "scalar and vectorized results should produce the same number of " + "first order gradients"; + EXPECT_EQ(single_gradients2.size(), multiple_gradients2.size()) + << "scalar and vectorized results should produce the same number of " + "second order gradients"; + EXPECT_EQ(single_gradients3.size(), multiple_gradients3.size()) + << "scalar and vectorized results should produce the same number of " + "third order gradients"; + + for (size_t i = 0; i < single_gradients1.size(); ++i) EXPECT_NEAR(single_gradients1[i], multiple_gradients1[i], 1e-7) - << "scalar and vectorized results should have the same first order gradients"; - for(size_t i = 0; i < single_gradients2.size(); ++i) + << "scalar and vectorized results should have the same first order " + "gradients"; + for (size_t i = 0; i < single_gradients2.size(); ++i) EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) - << "scalar and vectorized results should have the same first order gradients"; - for(size_t i = 0; i < single_gradients3.size(); ++i) + << "scalar and vectorized results should have the same first order " + "gradients"; + for (size_t i = 0; i < single_gradients3.size(); ++i) EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) - << "scalar and vectorized results should have the same first order gradients"; + << "scalar and vectorized results should have the same first order " + "gradients"; } void test_lower_bound() { diff --git a/test/prob/test_fixture_distr.hpp b/test/prob/test_fixture_distr.hpp index bf9da7b9733..5331a25e573 100644 --- a/test/prob/test_fixture_distr.hpp +++ b/test/prob/test_fixture_distr.hpp @@ -612,8 +612,7 @@ class AgradDistributionTestFixture : public ::testing::Test { T4 p4 = get_repeated_params(parameters[n], 4, N_REPEAT); T5 p5 = get_repeated_params(parameters[n], 5, N_REPEAT); - T_return_type multiple_lp - = TestClass.log_prob(p0, p1, p2, p3, p4, p5); + T_return_type multiple_lp = TestClass.log_prob(p0, p1, p2, p3, p4, p5); vector multiple_gradients1; vector multiple_gradients2; vector multiple_gradients3; @@ -635,22 +634,28 @@ class AgradDistributionTestFixture : public ::testing::Test { << "log prob with repeated vector input should match " << "a multiple of log prob of single input"; - EXPECT_EQ(single_gradients1.size(), multiple_gradients1.size()) << - "scalar and vectorized results should produce the same number of first order gradients"; - EXPECT_EQ(single_gradients2.size(), multiple_gradients2.size()) << - "scalar and vectorized results should produce the same number of second order gradients"; - EXPECT_EQ(single_gradients3.size(), multiple_gradients3.size()) << - "scalar and vectorized results should produce the same number of third order gradients"; - - for(size_t i = 0; i < single_gradients1.size(); ++i) - EXPECT_NEAR(single_gradients1[i], multiple_gradients1[i], 1e-7) - << "scalar and vectorized results should have the same first order gradients"; - for(size_t i = 0; i < single_gradients2.size(); ++i) - EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) - << "scalar and vectorized results should have the same first order gradients"; - for(size_t i = 0; i < single_gradients3.size(); ++i) - EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) - << "scalar and vectorized results should have the same first order gradients"; + EXPECT_EQ(single_gradients1.size(), multiple_gradients1.size()) + << "scalar and vectorized results should produce the same number of " + "first order gradients"; + EXPECT_EQ(single_gradients2.size(), multiple_gradients2.size()) + << "scalar and vectorized results should produce the same number of " + "second order gradients"; + EXPECT_EQ(single_gradients3.size(), multiple_gradients3.size()) + << "scalar and vectorized results should produce the same number of " + "third order gradients"; + + for (size_t i = 0; i < single_gradients1.size(); ++i) + EXPECT_NEAR(single_gradients1[i], multiple_gradients1[i], 1e-7) + << "scalar and vectorized results should have the same first order " + "gradients"; + for (size_t i = 0; i < single_gradients2.size(); ++i) + EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) + << "scalar and vectorized results should have the same first order " + "gradients"; + for (size_t i = 0; i < single_gradients3.size(); ++i) + EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) + << "scalar and vectorized results should have the same first order " + "gradients"; } } @@ -761,22 +766,28 @@ class AgradDistributionTestFixture : public ::testing::Test { stan::math::value_of_rec(multiple_lp), 1e-7) << "sum of scalar log probs should match vectorized result"; - EXPECT_EQ(single_gradients1.size(), multiple_gradients1.size()) << - "scalar and vectorized results should produce the same number of first order gradients"; - EXPECT_EQ(single_gradients2.size(), multiple_gradients2.size()) << - "scalar and vectorized results should produce the same number of second order gradients"; - EXPECT_EQ(single_gradients3.size(), multiple_gradients3.size()) << - "scalar and vectorized results should produce the same number of third order gradients"; - - for(size_t i = 0; i < single_gradients1.size(); ++i) + EXPECT_EQ(single_gradients1.size(), multiple_gradients1.size()) + << "scalar and vectorized results should produce the same number of " + "first order gradients"; + EXPECT_EQ(single_gradients2.size(), multiple_gradients2.size()) + << "scalar and vectorized results should produce the same number of " + "second order gradients"; + EXPECT_EQ(single_gradients3.size(), multiple_gradients3.size()) + << "scalar and vectorized results should produce the same number of " + "third order gradients"; + + for (size_t i = 0; i < single_gradients1.size(); ++i) EXPECT_NEAR(single_gradients1[i], multiple_gradients1[i], 1e-7) - << "scalar and vectorized results should have the same first order gradients"; - for(size_t i = 0; i < single_gradients2.size(); ++i) + << "scalar and vectorized results should have the same first order " + "gradients"; + for (size_t i = 0; i < single_gradients2.size(); ++i) EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) - << "scalar and vectorized results should have the same first order gradients"; - for(size_t i = 0; i < single_gradients3.size(); ++i) + << "scalar and vectorized results should have the same first order " + "gradients"; + for (size_t i = 0; i < single_gradients3.size(); ++i) EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) - << "scalar and vectorized results should have the same first order gradients"; + << "scalar and vectorized results should have the same first order " + "gradients"; } void test_length_0_vector() { diff --git a/test/prob/utility.hpp b/test/prob/utility.hpp index 153dbc9be86..04c290995b1 100644 --- a/test/prob/utility.hpp +++ b/test/prob/utility.hpp @@ -426,8 +426,7 @@ vector>> get_params>>>( // ------------------------------------------------------------ // default template handles Eigen::Matrix -template * = nullptr> +template * = nullptr> T get_repeated_params(const vector& parameters, const size_t p, const size_t N_REPEAT) { T params(N_REPEAT); @@ -437,7 +436,7 @@ T get_repeated_params(const vector& parameters, const size_t p, param = get_param>(parameters, p); else param = 0; - + for (size_t n = 0; n < N_REPEAT; n++) { params(n) = param; } @@ -445,10 +444,9 @@ T get_repeated_params(const vector& parameters, const size_t p, return params; } -template * = nullptr> -T get_repeated_params(const vector& parameters, - const size_t p, const size_t N_REPEAT) { +template * = nullptr> +T get_repeated_params(const vector& parameters, const size_t p, + const size_t N_REPEAT) { T params(N_REPEAT); stan::value_type_t param; @@ -456,7 +454,7 @@ T get_repeated_params(const vector& parameters, param = get_param>(parameters, p); else param = 0; - + for (size_t n = 0; n < N_REPEAT; n++) { params[n] = param; } @@ -466,18 +464,15 @@ T get_repeated_params(const vector& parameters, // handle empty template ::value>* = nullptr> -T get_repeated_params(const vector&, - const size_t, - const size_t) { + std::enable_if_t::value>* = nullptr> +T get_repeated_params(const vector&, const size_t, const size_t) { return T(); } // handle scalars -template * = nullptr> -T get_repeated_params(const vector& parameters, - const size_t p, const size_t /*N_REPEAT*/) { +template * = nullptr> +T get_repeated_params(const vector& parameters, const size_t p, + const size_t /*N_REPEAT*/) { if (p < parameters.size()) return get_param(parameters, p); else From b40f43983a9c3f24b5cd55bd9623a2cb6ecff98d Mon Sep 17 00:00:00 2001 From: Ben Date: Thu, 17 Sep 2020 18:57:30 -0400 Subject: [PATCH 07/19] Fixed bug with handling vectors in test_repeat_as_vector (Issue #1861) --- test/prob/test_fixture_ccdf_log.hpp | 12 ++++++------ test/prob/test_fixture_cdf.hpp | 12 ++++++------ test/prob/test_fixture_cdf_log.hpp | 12 ++++++------ test/prob/test_fixture_distr.hpp | 12 ++++++------ 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/test/prob/test_fixture_ccdf_log.hpp b/test/prob/test_fixture_ccdf_log.hpp index cfd1aae230e..d67f33a1b56 100644 --- a/test/prob/test_fixture_ccdf_log.hpp +++ b/test/prob/test_fixture_ccdf_log.hpp @@ -520,12 +520,12 @@ class AgradCcdfLogTestFixture : public ::testing::Test { vector s1; vector s2; vector s3; - std::vector p0s_(N_REPEAT, p0_); - std::vector p1s_(N_REPEAT, p1_); - std::vector p2s_(N_REPEAT, p2_); - std::vector p3s_(N_REPEAT, p3_); - std::vector p4s_(N_REPEAT, p4_); - std::vector p5s_(N_REPEAT, p5_); + std::vector p0s_((is_vector::value) ? N_REPEAT : 1, p0_); + std::vector p1s_((is_vector::value) ? N_REPEAT : 1, p1_); + std::vector p2s_((is_vector::value) ? N_REPEAT : 1, p2_); + std::vector p3s_((is_vector::value) ? N_REPEAT : 1, p3_); + std::vector p4s_((is_vector::value) ? N_REPEAT : 1, p4_); + std::vector p5s_((is_vector::value) ? N_REPEAT : 1, p5_); add_vars(s1, p0s_, p1s_, p2s_, p3s_, p4s_, p5s_); add_vars(s2, p0s_, p1s_, p2s_, p3s_, p4s_, p5s_); add_vars(s3, p0s_, p1s_, p2s_, p3s_, p4s_, p5s_); diff --git a/test/prob/test_fixture_cdf.hpp b/test/prob/test_fixture_cdf.hpp index 265a4b1b522..28f0c71e440 100644 --- a/test/prob/test_fixture_cdf.hpp +++ b/test/prob/test_fixture_cdf.hpp @@ -514,12 +514,12 @@ class AgradCdfTestFixture : public ::testing::Test { vector s1; vector s2; vector s3; - std::vector p0s_(N_REPEAT, p0_); - std::vector p1s_(N_REPEAT, p1_); - std::vector p2s_(N_REPEAT, p2_); - std::vector p3s_(N_REPEAT, p3_); - std::vector p4s_(N_REPEAT, p4_); - std::vector p5s_(N_REPEAT, p5_); + std::vector p0s_((is_vector::value) ? N_REPEAT : 1, p0_); + std::vector p1s_((is_vector::value) ? N_REPEAT : 1, p1_); + std::vector p2s_((is_vector::value) ? N_REPEAT : 1, p2_); + std::vector p3s_((is_vector::value) ? N_REPEAT : 1, p3_); + std::vector p4s_((is_vector::value) ? N_REPEAT : 1, p4_); + std::vector p5s_((is_vector::value) ? N_REPEAT : 1, p5_); add_vars(s1, p0s_, p1s_, p2s_, p3s_, p4s_, p5s_); add_vars(s2, p0s_, p1s_, p2s_, p3s_, p4s_, p5s_); add_vars(s3, p0s_, p1s_, p2s_, p3s_, p4s_, p5s_); diff --git a/test/prob/test_fixture_cdf_log.hpp b/test/prob/test_fixture_cdf_log.hpp index e90efedb3b8..b421f7e4b69 100644 --- a/test/prob/test_fixture_cdf_log.hpp +++ b/test/prob/test_fixture_cdf_log.hpp @@ -521,12 +521,12 @@ class AgradCdfLogTestFixture : public ::testing::Test { vector s1; vector s2; vector s3; - std::vector p0s_(N_REPEAT, p0_); - std::vector p1s_(N_REPEAT, p1_); - std::vector p2s_(N_REPEAT, p2_); - std::vector p3s_(N_REPEAT, p3_); - std::vector p4s_(N_REPEAT, p4_); - std::vector p5s_(N_REPEAT, p5_); + std::vector p0s_((is_vector::value) ? N_REPEAT : 1, p0_); + std::vector p1s_((is_vector::value) ? N_REPEAT : 1, p1_); + std::vector p2s_((is_vector::value) ? N_REPEAT : 1, p2_); + std::vector p3s_((is_vector::value) ? N_REPEAT : 1, p3_); + std::vector p4s_((is_vector::value) ? N_REPEAT : 1, p4_); + std::vector p5s_((is_vector::value) ? N_REPEAT : 1, p5_); add_vars(s1, p0s_, p1s_, p2s_, p3s_, p4s_, p5s_); add_vars(s2, p0s_, p1s_, p2s_, p3s_, p4s_, p5s_); add_vars(s3, p0s_, p1s_, p2s_, p3s_, p4s_, p5s_); diff --git a/test/prob/test_fixture_distr.hpp b/test/prob/test_fixture_distr.hpp index 5331a25e573..7eeba5b7053 100644 --- a/test/prob/test_fixture_distr.hpp +++ b/test/prob/test_fixture_distr.hpp @@ -587,12 +587,12 @@ class AgradDistributionTestFixture : public ::testing::Test { vector s1; vector s2; vector s3; - std::vector p0s_(N_REPEAT, p0_); - std::vector p1s_(N_REPEAT, p1_); - std::vector p2s_(N_REPEAT, p2_); - std::vector p3s_(N_REPEAT, p3_); - std::vector p4s_(N_REPEAT, p4_); - std::vector p5s_(N_REPEAT, p5_); + std::vector p0s_((is_vector::value) ? N_REPEAT : 1, p0_); + std::vector p1s_((is_vector::value) ? N_REPEAT : 1, p1_); + std::vector p2s_((is_vector::value) ? N_REPEAT : 1, p2_); + std::vector p3s_((is_vector::value) ? N_REPEAT : 1, p3_); + std::vector p4s_((is_vector::value) ? N_REPEAT : 1, p4_); + std::vector p5s_((is_vector::value) ? N_REPEAT : 1, p5_); add_vars(s1, p0s_, p1s_, p2s_, p3s_, p4s_, p5s_); add_vars(s2, p0s_, p1s_, p2s_, p3s_, p4s_, p5s_); add_vars(s3, p0s_, p1s_, p2s_, p3s_, p4s_, p5s_); From 08eb67a2c53319e695355b65280bcaab9ab85e68 Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 18 Sep 2020 15:51:00 -0400 Subject: [PATCH 08/19] Avoid 1 + small number underflows in skew normal (Issue #1861) --- stan/math/prim/prob/skew_normal_lpdf.hpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/stan/math/prim/prob/skew_normal_lpdf.hpp b/stan/math/prim/prob/skew_normal_lpdf.hpp index 1b937768b49..32c5b48fc4c 100644 --- a/stan/math/prim/prob/skew_normal_lpdf.hpp +++ b/stan/math/prim/prob/skew_normal_lpdf.hpp @@ -78,13 +78,16 @@ return_type_t skew_normal_lpdf( if (include_summand::value) { logp -= y_minus_mu_over_sigma * y_minus_mu_over_sigma / 2.0; } - logp += log(erfc(-alpha_dbl * y_minus_mu_over_sigma / SQRT_TWO)); + + T_partials_return log_erfc_alpha_z = log(erfc(-alpha_dbl * y_minus_mu_over_sigma / SQRT_TWO)); + + logp += log_erfc_alpha_z; T_partials_return deriv_logerf = TWO_OVER_SQRT_PI - * exp(-alpha_dbl * y_minus_mu_over_sigma / SQRT_TWO * alpha_dbl - * y_minus_mu_over_sigma / SQRT_TWO) - / (1 + erf(alpha_dbl * y_minus_mu_over_sigma / SQRT_TWO)); + * exp(-square(alpha_dbl * y_minus_mu_over_sigma / SQRT_TWO) + - log_erfc_alpha_z); + if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] += -y_minus_mu_over_sigma / sigma_dbl From ae3244577c69f875de1fd34edf3693f32c55c302 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Fri, 18 Sep 2020 16:16:05 -0400 Subject: [PATCH 09/19] [Jenkins] auto-formatting by clang-format version 6.0.1-14 (tags/RELEASE_601/final) --- stan/math/prim/prob/skew_normal_lpdf.hpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/stan/math/prim/prob/skew_normal_lpdf.hpp b/stan/math/prim/prob/skew_normal_lpdf.hpp index 32c5b48fc4c..eee5e22ba6b 100644 --- a/stan/math/prim/prob/skew_normal_lpdf.hpp +++ b/stan/math/prim/prob/skew_normal_lpdf.hpp @@ -79,14 +79,15 @@ return_type_t skew_normal_lpdf( logp -= y_minus_mu_over_sigma * y_minus_mu_over_sigma / 2.0; } - T_partials_return log_erfc_alpha_z = log(erfc(-alpha_dbl * y_minus_mu_over_sigma / SQRT_TWO)); + T_partials_return log_erfc_alpha_z + = log(erfc(-alpha_dbl * y_minus_mu_over_sigma / SQRT_TWO)); logp += log_erfc_alpha_z; T_partials_return deriv_logerf = TWO_OVER_SQRT_PI - * exp(-square(alpha_dbl * y_minus_mu_over_sigma / SQRT_TWO) - - log_erfc_alpha_z); + * exp(-square(alpha_dbl * y_minus_mu_over_sigma / SQRT_TWO) + - log_erfc_alpha_z); if (!is_constant_all::value) { ops_partials.edge1_.partials_[n] From f10d9eca57ee3d95cf8da245b7d759719d36d943 Mon Sep 17 00:00:00 2001 From: Ben Date: Mon, 21 Sep 2020 18:03:29 -0400 Subject: [PATCH 10/19] Updating discrete range and tests (Issue #1861) --- stan/math/prim/prob/discrete_range_cdf.hpp | 11 +++++------ stan/math/prim/prob/discrete_range_lccdf.hpp | 11 +++++------ stan/math/prim/prob/discrete_range_lcdf.hpp | 11 +++++------ .../discrete_range/discrete_range_ccdf_log_test.hpp | 6 +++++- .../discrete_range/discrete_range_cdf_log_test.hpp | 6 +++++- test/prob/discrete_range/discrete_range_cdf_test.hpp | 6 +++++- 6 files changed, 30 insertions(+), 21 deletions(-) diff --git a/stan/math/prim/prob/discrete_range_cdf.hpp b/stan/math/prim/prob/discrete_range_cdf.hpp index 9a54ec2c00d..776bc94a86a 100644 --- a/stan/math/prim/prob/discrete_range_cdf.hpp +++ b/stan/math/prim/prob/discrete_range_cdf.hpp @@ -55,17 +55,16 @@ double discrete_range_cdf(const T_y& y, const T_lower& lower, if (y_dbl < lower_vec[n]) { return 0; } - if (y_dbl > upper_vec[n]) { - return 1; - } } double cdf(1.0); for (size_t n = 0; n < N; n++) { const double y_dbl = y_vec[n]; - const double lower_dbl = lower_vec[n]; - const double upper_dbl = upper_vec[n]; - cdf *= (y_dbl - lower_dbl + 1) / (upper_dbl - lower_dbl + 1); + if (y_dbl <= upper_vec[n]) { + const double lower_dbl = lower_vec[n]; + const double upper_dbl = upper_vec[n]; + cdf *= (y_dbl - lower_dbl + 1) / (upper_dbl - lower_dbl + 1); + } } return cdf; } diff --git a/stan/math/prim/prob/discrete_range_lccdf.hpp b/stan/math/prim/prob/discrete_range_lccdf.hpp index 6f2d002b9b8..e7fcce3f84d 100644 --- a/stan/math/prim/prob/discrete_range_lccdf.hpp +++ b/stan/math/prim/prob/discrete_range_lccdf.hpp @@ -52,9 +52,6 @@ double discrete_range_lccdf(const T_y& y, const T_lower& lower, for (size_t n = 0; n < N; ++n) { const int y_dbl = y_vec[n]; - if (y_dbl < lower_vec[n]) { - return 0; - } if (y_dbl > upper_vec[n]) { return LOG_ZERO; } @@ -63,9 +60,11 @@ double discrete_range_lccdf(const T_y& y, const T_lower& lower, double ccdf(0.0); for (size_t n = 0; n < N; n++) { const int y_dbl = y_vec[n]; - const int lower_dbl = lower_vec[n]; - const int upper_dbl = upper_vec[n]; - ccdf += log(upper_dbl - y_dbl) - log(upper_dbl - lower_dbl + 1); + if (y_dbl > lower_vec[n]) { + const int lower_dbl = lower_vec[n]; + const int upper_dbl = upper_vec[n]; + ccdf += log(upper_dbl - y_dbl) - log(upper_dbl - lower_dbl + 1); + } } return ccdf; } diff --git a/stan/math/prim/prob/discrete_range_lcdf.hpp b/stan/math/prim/prob/discrete_range_lcdf.hpp index 88de348c5e5..2f937d1a71a 100644 --- a/stan/math/prim/prob/discrete_range_lcdf.hpp +++ b/stan/math/prim/prob/discrete_range_lcdf.hpp @@ -55,17 +55,16 @@ double discrete_range_lcdf(const T_y& y, const T_lower& lower, if (y_dbl < lower_vec[n]) { return LOG_ZERO; } - if (y_dbl > upper_vec[n]) { - return 0; - } } double cdf(0.0); for (size_t n = 0; n < N; n++) { const int y_dbl = y_vec[n]; - const int lower_dbl = lower_vec[n]; - const int upper_dbl = upper_vec[n]; - cdf += log(y_dbl - lower_dbl + 1) - log(upper_dbl - lower_dbl + 1); + if (y_dbl < upper_vec[n]) { + const int lower_dbl = lower_vec[n]; + const int upper_dbl = upper_vec[n]; + cdf += log(y_dbl - lower_dbl + 1) - log(upper_dbl - lower_dbl + 1); + } } return cdf; } diff --git a/test/prob/discrete_range/discrete_range_ccdf_log_test.hpp b/test/prob/discrete_range/discrete_range_ccdf_log_test.hpp index ebdf201079f..dc27ebbe79f 100644 --- a/test/prob/discrete_range/discrete_range_ccdf_log_test.hpp +++ b/test/prob/discrete_range/discrete_range_ccdf_log_test.hpp @@ -56,7 +56,11 @@ class AgradCcdfLogDiscreteRange : public AgradCcdfLogTest { stan::return_type_t ccdf_log_function( const T_y& y, const T_lower& lower, const T_upper& upper, const T3&, const T4&, const T5&) { - if (y < lower || y > upper) { + if (y < lower) { + return 1.0; + } + + if (y >= upper) { return stan::math::LOG_ZERO; } diff --git a/test/prob/discrete_range/discrete_range_cdf_log_test.hpp b/test/prob/discrete_range/discrete_range_cdf_log_test.hpp index 426564cf56f..6648197513e 100644 --- a/test/prob/discrete_range/discrete_range_cdf_log_test.hpp +++ b/test/prob/discrete_range/discrete_range_cdf_log_test.hpp @@ -54,10 +54,14 @@ class AgradCdfLogDiscreteRange : public AgradCdfLogTest { double cdf_log_function(const T_y& y, const T_lower& lower, const T_upper& upper, const T3&, const T4&, const T5&) { - if (y < lower || y > upper) { + if (y < lower) { return stan::math::LOG_ZERO; } + if (y > upper) { + return 0.0; + } + return log((y - lower + 1.0) / (upper - lower + 1.0)); } }; diff --git a/test/prob/discrete_range/discrete_range_cdf_test.hpp b/test/prob/discrete_range/discrete_range_cdf_test.hpp index c0d74b41c51..781c711e944 100644 --- a/test/prob/discrete_range/discrete_range_cdf_test.hpp +++ b/test/prob/discrete_range/discrete_range_cdf_test.hpp @@ -52,10 +52,14 @@ class AgradCdfDiscreteRange : public AgradCdfTest { typename T5> double cdf_function(const T_y& y, const T_lower& lower, const T_upper& upper, const T3&, const T4&, const T5&) { - if (y < lower || y > upper) { + if (y < lower) { return 0; } + if (y >= upper) { + return 1; + } + return (y - lower + 1.0) / (upper - lower + 1.0); } }; From f612588c3bb8fa4fa7ded521fd0bdf3e2df2681a Mon Sep 17 00:00:00 2001 From: Ben Date: Mon, 21 Sep 2020 19:59:16 -0400 Subject: [PATCH 11/19] Fixed discrete range bug (Issue #1861) --- stan/math/prim/prob/discrete_range_lccdf.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stan/math/prim/prob/discrete_range_lccdf.hpp b/stan/math/prim/prob/discrete_range_lccdf.hpp index e7fcce3f84d..ac2fae5a0fe 100644 --- a/stan/math/prim/prob/discrete_range_lccdf.hpp +++ b/stan/math/prim/prob/discrete_range_lccdf.hpp @@ -52,7 +52,7 @@ double discrete_range_lccdf(const T_y& y, const T_lower& lower, for (size_t n = 0; n < N; ++n) { const int y_dbl = y_vec[n]; - if (y_dbl > upper_vec[n]) { + if (y_dbl >= upper_vec[n]) { return LOG_ZERO; } } @@ -60,7 +60,7 @@ double discrete_range_lccdf(const T_y& y, const T_lower& lower, double ccdf(0.0); for (size_t n = 0; n < N; n++) { const int y_dbl = y_vec[n]; - if (y_dbl > lower_vec[n]) { + if (y_dbl >= lower_vec[n]) { const int lower_dbl = lower_vec[n]; const int upper_dbl = upper_vec[n]; ccdf += log(upper_dbl - y_dbl) - log(upper_dbl - lower_dbl + 1); From ebc54322fe063a89e9fc25baa0c245e09f6388fc Mon Sep 17 00:00:00 2001 From: Ben Date: Tue, 22 Sep 2020 17:30:15 -0400 Subject: [PATCH 12/19] Changed the gumbel and negative binomial tests to make the numerics easier (Issue #1861) --- test/prob/gumbel/gumbel_cdf_test.hpp | 6 ++---- test/prob/neg_binomial/neg_binomial_test.hpp | 8 -------- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/test/prob/gumbel/gumbel_cdf_test.hpp b/test/prob/gumbel/gumbel_cdf_test.hpp index 9ea5b02fbd9..d5ddb73153a 100644 --- a/test/prob/gumbel/gumbel_cdf_test.hpp +++ b/test/prob/gumbel/gumbel_cdf_test.hpp @@ -34,13 +34,11 @@ class AgradCdfGumbel : public AgradCdfTest { 0.0006179789893310934986195216040530260548886143651007); // expected // cdf - param[0] = -3.5; // y + param[0] = -1.5; // y param[1] = 1.9; // mu param[2] = 7.2; // beta parameters.push_back(param); - cdf.push_back( - 0.1203922620798295861862650786832089422663975274508450); // expected - // cdf + cdf.push_back(0.20118031381610754216); // expected cdf } void invalid_values(vector& index, vector& value) { diff --git a/test/prob/neg_binomial/neg_binomial_test.hpp b/test/prob/neg_binomial/neg_binomial_test.hpp index e7007131787..043ebc71441 100644 --- a/test/prob/neg_binomial/neg_binomial_test.hpp +++ b/test/prob/neg_binomial/neg_binomial_test.hpp @@ -23,12 +23,6 @@ class AgradDistributionsNegBinomial : public AgradDistributionTest { param[2] = 3.5; // beta parameters.push_back(param); log_prob.push_back(-142.6147368129045105434); // expected log_prob - - param[0] = 13; - param[1] = 1e11; - param[2] = 1e10; - parameters.push_back(param); - log_prob.push_back(-2.6185576442208003); // expected log_prob } void invalid_values(vector& index, vector& value) { @@ -75,8 +69,6 @@ class AgradDistributionsNegBinomial : public AgradDistributionTest { using stan::math::multiply_log; using std::log; - if (alpha > 1e10) - return -lgamma(n + 1.0) + multiply_log(n, alpha / beta) - alpha / beta; if (n != 0) return binomial_coefficient_log< typename stan::scalar_type::type>(n + alpha - 1.0, n) From 371ccd3f83f56aa19813d930fb664428f61eef8d Mon Sep 17 00:00:00 2001 From: Ben Date: Wed, 23 Sep 2020 18:08:49 -0400 Subject: [PATCH 13/19] Updated comments and warning messages (Issue #1861) --- .../discrete_range_ccdf_log_test.hpp | 2 +- test/prob/test_fixture_ccdf_log.hpp | 12 ++++++++---- test/prob/test_fixture_cdf.hpp | 12 ++++++++---- test/prob/test_fixture_cdf_log.hpp | 8 ++++++-- test/prob/test_fixture_distr.hpp | 16 ++++++++++++++-- 5 files changed, 37 insertions(+), 13 deletions(-) diff --git a/test/prob/discrete_range/discrete_range_ccdf_log_test.hpp b/test/prob/discrete_range/discrete_range_ccdf_log_test.hpp index dc27ebbe79f..91e56419239 100644 --- a/test/prob/discrete_range/discrete_range_ccdf_log_test.hpp +++ b/test/prob/discrete_range/discrete_range_ccdf_log_test.hpp @@ -57,7 +57,7 @@ class AgradCcdfLogDiscreteRange : public AgradCcdfLogTest { const T_y& y, const T_lower& lower, const T_upper& upper, const T3&, const T4&, const T5&) { if (y < lower) { - return 1.0; + return 0.0; } if (y >= upper) { diff --git a/test/prob/test_fixture_ccdf_log.hpp b/test/prob/test_fixture_ccdf_log.hpp index d67f33a1b56..569ead3f3ce 100644 --- a/test/prob/test_fixture_ccdf_log.hpp +++ b/test/prob/test_fixture_ccdf_log.hpp @@ -495,6 +495,8 @@ class AgradCcdfLogTestFixture : public ::testing::Test { /** * Test that the vectorized functions work as expected when the elements * of the vector are the same + * + * For log ccdf this means lccdf([a, a, a]) == lccdf(a) + lccdf(a) + lccdf(a) */ void test_repeat_as_vector() { if (!any_vector::value) { @@ -584,11 +586,11 @@ class AgradCcdfLogTestFixture : public ::testing::Test { "gradients"; for (size_t i = 0; i < single_gradients2.size(); ++i) EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) - << "scalar and vectorized results should have the same first order " + << "scalar and vectorized results should have the same second order " "gradients"; for (size_t i = 0; i < single_gradients3.size(); ++i) EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) - << "scalar and vectorized results should have the same first order " + << "scalar and vectorized results should have the same third order " "gradients"; } } @@ -596,6 +598,8 @@ class AgradCcdfLogTestFixture : public ::testing::Test { /** * Test that the vectorized functions work as expected when the elements * of the vector are different + * + * For log ccdf this means lccdf([a, b, c]) == lccdf(a) + lccdf(b) + lccdf(c) */ void test_as_scalars_vs_as_vector() { if (!any_vector::value) { @@ -719,11 +723,11 @@ class AgradCcdfLogTestFixture : public ::testing::Test { "gradients"; for (size_t i = 0; i < single_gradients2.size(); ++i) EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) - << "scalar and vectorized results should have the same first order " + << "scalar and vectorized results should have the same second order " "gradients"; for (size_t i = 0; i < single_gradients3.size(); ++i) EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) - << "scalar and vectorized results should have the same first order " + << "scalar and vectorized results should have the same third order " "gradients"; } diff --git a/test/prob/test_fixture_cdf.hpp b/test/prob/test_fixture_cdf.hpp index 28f0c71e440..5461692a2eb 100644 --- a/test/prob/test_fixture_cdf.hpp +++ b/test/prob/test_fixture_cdf.hpp @@ -485,6 +485,8 @@ class AgradCdfTestFixture : public ::testing::Test { /** * Test that the vectorized functions work as expected when the elements * of the vector are the same + * + * For cdf this means cdf([a, a, a]) == cdf(a) * cdf(a) * cdf(a) */ void test_repeat_as_vector() { using stan::math::pow; @@ -577,11 +579,11 @@ class AgradCdfTestFixture : public ::testing::Test { "gradients"; for (size_t i = 0; i < single_gradients2.size(); ++i) EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) - << "scalar and vectorized results should have the same first order " + << "scalar and vectorized results should have the same second order " "gradients"; for (size_t i = 0; i < single_gradients3.size(); ++i) EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) - << "scalar and vectorized results should have the same first order " + << "scalar and vectorized results should have the same third order " "gradients"; } } @@ -589,6 +591,8 @@ class AgradCdfTestFixture : public ::testing::Test { /** * Test that the vectorized functions work as expected when the elements * of the vector are different + * + * For cdf this means cdf([a, b, c]) == cdf(a) * cdf(b) * cdf(c) */ void test_as_scalars_vs_as_vector() { if (!any_vector::value) { @@ -708,11 +712,11 @@ class AgradCdfTestFixture : public ::testing::Test { "gradients"; for (size_t i = 0; i < single_gradients2.size(); ++i) EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) - << "scalar and vectorized results should have the same first order " + << "scalar and vectorized results should have the same second order " "gradients"; for (size_t i = 0; i < single_gradients3.size(); ++i) EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) - << "scalar and vectorized results should have the same first order " + << "scalar and vectorized results should have the same third order " "gradients"; } diff --git a/test/prob/test_fixture_cdf_log.hpp b/test/prob/test_fixture_cdf_log.hpp index b421f7e4b69..6404a33e16a 100644 --- a/test/prob/test_fixture_cdf_log.hpp +++ b/test/prob/test_fixture_cdf_log.hpp @@ -496,6 +496,8 @@ class AgradCdfLogTestFixture : public ::testing::Test { /** * Test that the vectorized functions work as expected when the elements * of the vector are the same + * + * For log cdf this means lcdf([a, a, a]) == lcdf(a) + lcdf(a) + lcdf(a) */ void test_repeat_as_vector() { if (!any_vector::value) { @@ -585,11 +587,11 @@ class AgradCdfLogTestFixture : public ::testing::Test { "gradients"; for (size_t i = 0; i < single_gradients2.size(); ++i) EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) - << "scalar and vectorized results should have the same first order " + << "scalar and vectorized results should have the same second order " "gradients"; for (size_t i = 0; i < single_gradients3.size(); ++i) EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) - << "scalar and vectorized results should have the same first order " + << "scalar and vectorized results should have the same third order " "gradients"; } } @@ -597,6 +599,8 @@ class AgradCdfLogTestFixture : public ::testing::Test { /** * Test that the vectorized functions work as expected when the elements * of the vector are different + * + * For log cdf this means lcdf([a, b, c]) == lcdf(a) + lcdf(b) + lcdf(c) */ void test_as_scalars_vs_as_vector() { if (!any_vector::value) { diff --git a/test/prob/test_fixture_distr.hpp b/test/prob/test_fixture_distr.hpp index 7eeba5b7053..0f28b6a4247 100644 --- a/test/prob/test_fixture_distr.hpp +++ b/test/prob/test_fixture_distr.hpp @@ -558,6 +558,12 @@ class AgradDistributionTestFixture : public ::testing::Test { /** * Test that the vectorized functions work as expected when the elements * of the vector are the same + * + * For lpdfs this means + * lpdf([a, a, a]) == lpdf(a) + lpdf(a) + lpdf(a) + * + * Similarly for lpmfs this means + * lpmf([a, a, a]) == lpmf(a) + lpmf(a) + lpmf(a) */ void test_repeat_as_vector() { if (all_constant::value) { @@ -650,11 +656,11 @@ class AgradDistributionTestFixture : public ::testing::Test { "gradients"; for (size_t i = 0; i < single_gradients2.size(); ++i) EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) - << "scalar and vectorized results should have the same first order " + << "scalar and vectorized results should have the same second order " "gradients"; for (size_t i = 0; i < single_gradients3.size(); ++i) EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) - << "scalar and vectorized results should have the same first order " + << "scalar and vectorized results should have the same third order " "gradients"; } } @@ -662,6 +668,12 @@ class AgradDistributionTestFixture : public ::testing::Test { /** * Test that the vectorized functions work as expected when the elements * of the vector are different + * + * For lpdfs this means + * lpdf([a, b, c]) == lpdf(a) + lpdf(b) + lpdf(c) + * + * Similarly for lpmfs this means + * lpmf([a, b, c]) == lpmf(a) + lpmf(b) + lpmf(c) */ void test_as_scalars_vs_as_vector() { if (all_constant::value) { From 8d9ac84313bdb7f3a6a9eb1ff54412b7102e0e00 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Wed, 23 Sep 2020 22:09:44 +0000 Subject: [PATCH 14/19] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final) --- test/prob/test_fixture_ccdf_log.hpp | 3 ++- test/prob/test_fixture_cdf.hpp | 3 ++- test/prob/test_fixture_cdf_log.hpp | 3 ++- test/prob/test_fixture_distr.hpp | 3 ++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/test/prob/test_fixture_ccdf_log.hpp b/test/prob/test_fixture_ccdf_log.hpp index 569ead3f3ce..f419b5db3cd 100644 --- a/test/prob/test_fixture_ccdf_log.hpp +++ b/test/prob/test_fixture_ccdf_log.hpp @@ -586,7 +586,8 @@ class AgradCcdfLogTestFixture : public ::testing::Test { "gradients"; for (size_t i = 0; i < single_gradients2.size(); ++i) EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) - << "scalar and vectorized results should have the same second order " + << "scalar and vectorized results should have the same second " + "order " "gradients"; for (size_t i = 0; i < single_gradients3.size(); ++i) EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) diff --git a/test/prob/test_fixture_cdf.hpp b/test/prob/test_fixture_cdf.hpp index 5461692a2eb..7f7efa3e9be 100644 --- a/test/prob/test_fixture_cdf.hpp +++ b/test/prob/test_fixture_cdf.hpp @@ -579,7 +579,8 @@ class AgradCdfTestFixture : public ::testing::Test { "gradients"; for (size_t i = 0; i < single_gradients2.size(); ++i) EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) - << "scalar and vectorized results should have the same second order " + << "scalar and vectorized results should have the same second " + "order " "gradients"; for (size_t i = 0; i < single_gradients3.size(); ++i) EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) diff --git a/test/prob/test_fixture_cdf_log.hpp b/test/prob/test_fixture_cdf_log.hpp index 6404a33e16a..c16dfa07b28 100644 --- a/test/prob/test_fixture_cdf_log.hpp +++ b/test/prob/test_fixture_cdf_log.hpp @@ -587,7 +587,8 @@ class AgradCdfLogTestFixture : public ::testing::Test { "gradients"; for (size_t i = 0; i < single_gradients2.size(); ++i) EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) - << "scalar and vectorized results should have the same second order " + << "scalar and vectorized results should have the same second " + "order " "gradients"; for (size_t i = 0; i < single_gradients3.size(); ++i) EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) diff --git a/test/prob/test_fixture_distr.hpp b/test/prob/test_fixture_distr.hpp index 0f28b6a4247..6aeacd4b22d 100644 --- a/test/prob/test_fixture_distr.hpp +++ b/test/prob/test_fixture_distr.hpp @@ -656,7 +656,8 @@ class AgradDistributionTestFixture : public ::testing::Test { "gradients"; for (size_t i = 0; i < single_gradients2.size(); ++i) EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) - << "scalar and vectorized results should have the same second order " + << "scalar and vectorized results should have the same second " + "order " "gradients"; for (size_t i = 0; i < single_gradients3.size(); ++i) EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) From 7a9d1a2fc099272a3c2c2e016d80043e668c6527 Mon Sep 17 00:00:00 2001 From: Ben Date: Wed, 30 Sep 2020 10:06:27 -0400 Subject: [PATCH 15/19] Added back negative binomial test and switched to expect_near_rel (Issue #1861) --- test/prob/neg_binomial/neg_binomial_test.hpp | 6 ++ test/prob/test_fixture_ccdf_log.hpp | 68 ++++--------------- test/prob/test_fixture_cdf.hpp | 68 ++++--------------- test/prob/test_fixture_cdf_log.hpp | 68 ++++--------------- test/prob/test_fixture_distr.hpp | 69 +++++--------------- 5 files changed, 63 insertions(+), 216 deletions(-) diff --git a/test/prob/neg_binomial/neg_binomial_test.hpp b/test/prob/neg_binomial/neg_binomial_test.hpp index 043ebc71441..b162426c3af 100644 --- a/test/prob/neg_binomial/neg_binomial_test.hpp +++ b/test/prob/neg_binomial/neg_binomial_test.hpp @@ -23,6 +23,12 @@ class AgradDistributionsNegBinomial : public AgradDistributionTest { param[2] = 3.5; // beta parameters.push_back(param); log_prob.push_back(-142.6147368129045105434); // expected log_prob + + param[0] = 13; + param[1] = 1e11; + param[2] = 1e10; + parameters.push_back(param); + log_prob.push_back(-2.6185576442208003); // expected log_prob } void invalid_values(vector& index, vector& value) { diff --git a/test/prob/test_fixture_ccdf_log.hpp b/test/prob/test_fixture_ccdf_log.hpp index f419b5db3cd..ffbdd1fb795 100644 --- a/test/prob/test_fixture_ccdf_log.hpp +++ b/test/prob/test_fixture_ccdf_log.hpp @@ -565,34 +565,13 @@ class AgradCcdfLogTestFixture : public ::testing::Test { stan::math::recover_memory(); - EXPECT_NEAR(stan::math::value_of_rec(single_ccdf_log), - stan::math::value_of_rec(multiple_ccdf_log), 1e-8) - << "ccdf_log with repeated vector input should match " - << "a multiple of ccdf_log of single input"; - - EXPECT_EQ(single_gradients1.size(), multiple_gradients1.size()) - << "scalar and vectorized results should produce the same number of " - "first order gradients"; - EXPECT_EQ(single_gradients2.size(), multiple_gradients2.size()) - << "scalar and vectorized results should produce the same number of " - "second order gradients"; - EXPECT_EQ(single_gradients3.size(), multiple_gradients3.size()) - << "scalar and vectorized results should produce the same number of " - "third order gradients"; - - for (size_t i = 0; i < single_gradients1.size(); ++i) - EXPECT_NEAR(single_gradients1[i], multiple_gradients1[i], 1e-7) - << "scalar and vectorized results should have the same first order " - "gradients"; - for (size_t i = 0; i < single_gradients2.size(); ++i) - EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) - << "scalar and vectorized results should have the same second " - "order " - "gradients"; - for (size_t i = 0; i < single_gradients3.size(); ++i) - EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) - << "scalar and vectorized results should have the same third order " - "gradients"; + stan::test::expect_near_rel("ccdf_log with repeated vector input should match a multiple of ccdf_log of single input", + stan::math::value_of_rec(single_ccdf_log), + stan::math::value_of_rec(multiple_ccdf_log)); + + stan::test::expect_near_rel("scalar and vectorized results should have the same first order gradients", single_gradients1, multiple_gradients1); + stan::test::expect_near_rel("scalar and vectorized results should have the same second order gradients", single_gradients2, multiple_gradients2); + stan::test::expect_near_rel("scalar and vectorized results should have the same third order gradients", single_gradients3, multiple_gradients3); } } @@ -704,32 +683,13 @@ class AgradCcdfLogTestFixture : public ::testing::Test { return; } - EXPECT_NEAR(stan::math::value_of_rec(single_ccdf_log), - stan::math::value_of_rec(multiple_ccdf_log), 1e-7) - << "sum of scalar ccdf_logs should match vectorized result"; - - EXPECT_EQ(single_gradients1.size(), multiple_gradients1.size()) - << "scalar and vectorized results should produce the same number of " - "first order gradients"; - EXPECT_EQ(single_gradients2.size(), multiple_gradients2.size()) - << "scalar and vectorized results should produce the same number of " - "second order gradients"; - EXPECT_EQ(single_gradients3.size(), multiple_gradients3.size()) - << "scalar and vectorized results should produce the same number of " - "third order gradients"; - - for (size_t i = 0; i < single_gradients1.size(); ++i) - EXPECT_NEAR(single_gradients1[i], multiple_gradients1[i], 1e-7) - << "scalar and vectorized results should have the same first order " - "gradients"; - for (size_t i = 0; i < single_gradients2.size(); ++i) - EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) - << "scalar and vectorized results should have the same second order " - "gradients"; - for (size_t i = 0; i < single_gradients3.size(); ++i) - EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) - << "scalar and vectorized results should have the same third order " - "gradients"; + stan::test::expect_near_rel("sum of scalar ccdf_logs should match vectorized result", + stan::math::value_of_rec(single_ccdf_log), + stan::math::value_of_rec(multiple_ccdf_log)); + + stan::test::expect_near_rel("scalar and vectorized results should have the same first order gradients", single_gradients1, multiple_gradients1); + stan::test::expect_near_rel("scalar and vectorized results should have the same second order gradients", single_gradients2, multiple_gradients2); + stan::test::expect_near_rel("scalar and vectorized results should have the same third order gradients", single_gradients3, multiple_gradients3); } void test_lower_bound() { diff --git a/test/prob/test_fixture_cdf.hpp b/test/prob/test_fixture_cdf.hpp index 7f7efa3e9be..5c4112e594e 100644 --- a/test/prob/test_fixture_cdf.hpp +++ b/test/prob/test_fixture_cdf.hpp @@ -558,34 +558,13 @@ class AgradCdfTestFixture : public ::testing::Test { stan::math::recover_memory(); - EXPECT_NEAR(stan::math::value_of_rec(single_cdf), - stan::math::value_of_rec(multiple_cdf), 1e-8) - << "cdf with repeated vector input should match " - << "a multiple of cdf of single input"; - - EXPECT_EQ(single_gradients1.size(), multiple_gradients1.size()) - << "scalar and vectorized results should produce the same number of " - "first order gradients"; - EXPECT_EQ(single_gradients2.size(), multiple_gradients2.size()) - << "scalar and vectorized results should produce the same number of " - "second order gradients"; - EXPECT_EQ(single_gradients3.size(), multiple_gradients3.size()) - << "scalar and vectorized results should produce the same number of " - "third order gradients"; - - for (size_t i = 0; i < single_gradients1.size(); ++i) - EXPECT_NEAR(single_gradients1[i], multiple_gradients1[i], 1e-7) - << "scalar and vectorized results should have the same first order " - "gradients"; - for (size_t i = 0; i < single_gradients2.size(); ++i) - EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) - << "scalar and vectorized results should have the same second " - "order " - "gradients"; - for (size_t i = 0; i < single_gradients3.size(); ++i) - EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) - << "scalar and vectorized results should have the same third order " - "gradients"; + stan::test::expect_near_rel("cdf with repeated vector input should match a multiple of cdf of single input", + stan::math::value_of_rec(single_cdf), + stan::math::value_of_rec(multiple_cdf)); + + stan::test::expect_near_rel("scalar and vectorized results should have the same first order gradients", single_gradients1, multiple_gradients1); + stan::test::expect_near_rel("scalar and vectorized results should have the same second order gradients", single_gradients2, multiple_gradients2); + stan::test::expect_near_rel("scalar and vectorized results should have the same third order gradients", single_gradients3, multiple_gradients3); } } @@ -693,32 +672,13 @@ class AgradCdfTestFixture : public ::testing::Test { return; } - EXPECT_NEAR(stan::math::value_of_rec(single_cdf), - stan::math::value_of_rec(multiple_cdf), 1e-7) - << "product of scalar cdfs should match vectorized result"; - - EXPECT_EQ(single_gradients1.size(), multiple_gradients1.size()) - << "scalar and vectorized results should produce the same number of " - "first order gradients"; - EXPECT_EQ(single_gradients2.size(), multiple_gradients2.size()) - << "scalar and vectorized results should produce the same number of " - "second order gradients"; - EXPECT_EQ(single_gradients3.size(), multiple_gradients3.size()) - << "scalar and vectorized results should produce the same number of " - "third order gradients"; - - for (size_t i = 0; i < single_gradients1.size(); ++i) - EXPECT_NEAR(single_gradients1[i], multiple_gradients1[i], 1e-7) - << "scalar and vectorized results should have the same first order " - "gradients"; - for (size_t i = 0; i < single_gradients2.size(); ++i) - EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) - << "scalar and vectorized results should have the same second order " - "gradients"; - for (size_t i = 0; i < single_gradients3.size(); ++i) - EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) - << "scalar and vectorized results should have the same third order " - "gradients"; + stan::test::expect_near_rel("product of scalar cdfs should match vectorized result", + stan::math::value_of_rec(single_cdf), + stan::math::value_of_rec(multiple_cdf)); + + stan::test::expect_near_rel("scalar and vectorized results should have the same first order gradients", single_gradients1, multiple_gradients1); + stan::test::expect_near_rel("scalar and vectorized results should have the same second order gradients", single_gradients2, multiple_gradients2); + stan::test::expect_near_rel("scalar and vectorized results should have the same third order gradients", single_gradients3, multiple_gradients3); } void test_lower_bound() { diff --git a/test/prob/test_fixture_cdf_log.hpp b/test/prob/test_fixture_cdf_log.hpp index c16dfa07b28..69c7e6a8a3c 100644 --- a/test/prob/test_fixture_cdf_log.hpp +++ b/test/prob/test_fixture_cdf_log.hpp @@ -566,34 +566,13 @@ class AgradCdfLogTestFixture : public ::testing::Test { stan::math::recover_memory(); - EXPECT_NEAR(stan::math::value_of_rec(single_cdf_log), - stan::math::value_of_rec(multiple_cdf_log), 1e-8) - << "cdf_log with repeated vector input should match " - << "a multiple of cdf_log of single input"; - - EXPECT_EQ(single_gradients1.size(), multiple_gradients1.size()) - << "scalar and vectorized results should produce the same number of " - "first order gradients"; - EXPECT_EQ(single_gradients2.size(), multiple_gradients2.size()) - << "scalar and vectorized results should produce the same number of " - "second order gradients"; - EXPECT_EQ(single_gradients3.size(), multiple_gradients3.size()) - << "scalar and vectorized results should produce the same number of " - "third order gradients"; - - for (size_t i = 0; i < single_gradients1.size(); ++i) - EXPECT_NEAR(single_gradients1[i], multiple_gradients1[i], 1e-7) - << "scalar and vectorized results should have the same first order " - "gradients"; - for (size_t i = 0; i < single_gradients2.size(); ++i) - EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) - << "scalar and vectorized results should have the same second " - "order " - "gradients"; - for (size_t i = 0; i < single_gradients3.size(); ++i) - EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) - << "scalar and vectorized results should have the same third order " - "gradients"; + stan::test::expect_near_rel("cdf_log with repeated vector input should match a multiple of cdf_log of single input", + stan::math::value_of_rec(single_cdf_log), + stan::math::value_of_rec(multiple_cdf_log)); + + stan::test::expect_near_rel("scalar and vectorized results should have the same first order gradients", single_gradients1, multiple_gradients1); + stan::test::expect_near_rel("scalar and vectorized results should have the same second order gradients", single_gradients2, multiple_gradients2); + stan::test::expect_near_rel("scalar and vectorized results should have the same third order gradients", single_gradients3, multiple_gradients3); } } @@ -704,32 +683,13 @@ class AgradCdfLogTestFixture : public ::testing::Test { return; } - EXPECT_NEAR(stan::math::value_of_rec(single_cdf_log), - stan::math::value_of_rec(multiple_cdf_log), 1e-7) - << "sum of scalar cdf_logs should match vectorized result"; - - EXPECT_EQ(single_gradients1.size(), multiple_gradients1.size()) - << "scalar and vectorized results should produce the same number of " - "first order gradients"; - EXPECT_EQ(single_gradients2.size(), multiple_gradients2.size()) - << "scalar and vectorized results should produce the same number of " - "second order gradients"; - EXPECT_EQ(single_gradients3.size(), multiple_gradients3.size()) - << "scalar and vectorized results should produce the same number of " - "third order gradients"; - - for (size_t i = 0; i < single_gradients1.size(); ++i) - EXPECT_NEAR(single_gradients1[i], multiple_gradients1[i], 1e-7) - << "scalar and vectorized results should have the same first order " - "gradients"; - for (size_t i = 0; i < single_gradients2.size(); ++i) - EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) - << "scalar and vectorized results should have the same first order " - "gradients"; - for (size_t i = 0; i < single_gradients3.size(); ++i) - EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) - << "scalar and vectorized results should have the same first order " - "gradients"; + stan::test::expect_near_rel("sum of scalar cdf_logs should match vectorized result", + stan::math::value_of_rec(single_cdf_log), + stan::math::value_of_rec(multiple_cdf_log)); + + stan::test::expect_near_rel("scalar and vectorized results should have the same first order gradients", single_gradients1, multiple_gradients1); + stan::test::expect_near_rel("scalar and vectorized results should have the same second order gradients", single_gradients2, multiple_gradients2); + stan::test::expect_near_rel("scalar and vectorized results should have the same third order gradients", single_gradients3, multiple_gradients3); } void test_lower_bound() { diff --git a/test/prob/test_fixture_distr.hpp b/test/prob/test_fixture_distr.hpp index 6aeacd4b22d..2869d39dad4 100644 --- a/test/prob/test_fixture_distr.hpp +++ b/test/prob/test_fixture_distr.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -635,34 +636,13 @@ class AgradDistributionTestFixture : public ::testing::Test { stan::math::recover_memory(); - EXPECT_NEAR(stan::math::value_of_rec(single_lp), - stan::math::value_of_rec(multiple_lp), 1e-8) - << "log prob with repeated vector input should match " - << "a multiple of log prob of single input"; - - EXPECT_EQ(single_gradients1.size(), multiple_gradients1.size()) - << "scalar and vectorized results should produce the same number of " - "first order gradients"; - EXPECT_EQ(single_gradients2.size(), multiple_gradients2.size()) - << "scalar and vectorized results should produce the same number of " - "second order gradients"; - EXPECT_EQ(single_gradients3.size(), multiple_gradients3.size()) - << "scalar and vectorized results should produce the same number of " - "third order gradients"; - - for (size_t i = 0; i < single_gradients1.size(); ++i) - EXPECT_NEAR(single_gradients1[i], multiple_gradients1[i], 1e-7) - << "scalar and vectorized results should have the same first order " - "gradients"; - for (size_t i = 0; i < single_gradients2.size(); ++i) - EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) - << "scalar and vectorized results should have the same second " - "order " - "gradients"; - for (size_t i = 0; i < single_gradients3.size(); ++i) - EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) - << "scalar and vectorized results should have the same third order " - "gradients"; + stan::test::expect_near_rel("log prob with repeated vector input should match a multiple of log prob of single input", + stan::math::value_of_rec(single_lp), + stan::math::value_of_rec(multiple_lp)); + + stan::test::expect_near_rel("scalar and vectorized results should have the same first order gradients", single_gradients1, multiple_gradients1); + stan::test::expect_near_rel("scalar and vectorized results should have the same second order gradients", single_gradients2, multiple_gradients2); + stan::test::expect_near_rel("scalar and vectorized results should have the same third order gradients", single_gradients3, multiple_gradients3); } } @@ -775,32 +755,13 @@ class AgradDistributionTestFixture : public ::testing::Test { return; } - EXPECT_NEAR(stan::math::value_of_rec(single_lp), - stan::math::value_of_rec(multiple_lp), 1e-7) - << "sum of scalar log probs should match vectorized result"; - - EXPECT_EQ(single_gradients1.size(), multiple_gradients1.size()) - << "scalar and vectorized results should produce the same number of " - "first order gradients"; - EXPECT_EQ(single_gradients2.size(), multiple_gradients2.size()) - << "scalar and vectorized results should produce the same number of " - "second order gradients"; - EXPECT_EQ(single_gradients3.size(), multiple_gradients3.size()) - << "scalar and vectorized results should produce the same number of " - "third order gradients"; - - for (size_t i = 0; i < single_gradients1.size(); ++i) - EXPECT_NEAR(single_gradients1[i], multiple_gradients1[i], 1e-7) - << "scalar and vectorized results should have the same first order " - "gradients"; - for (size_t i = 0; i < single_gradients2.size(); ++i) - EXPECT_NEAR(single_gradients2[i], multiple_gradients2[i], 1e-7) - << "scalar and vectorized results should have the same first order " - "gradients"; - for (size_t i = 0; i < single_gradients3.size(); ++i) - EXPECT_NEAR(single_gradients3[i], multiple_gradients3[i], 1e-7) - << "scalar and vectorized results should have the same first order " - "gradients"; + stan::test::expect_near_rel("sum of scalar log probs should match vectorized result", + stan::math::value_of_rec(single_lp), + stan::math::value_of_rec(multiple_lp)); + + stan::test::expect_near_rel("scalar and vectorized results should have the same first order gradients", single_gradients1, multiple_gradients1); + stan::test::expect_near_rel("scalar and vectorized results should have the same second order gradients", single_gradients2, multiple_gradients2); + stan::test::expect_near_rel("scalar and vectorized results should have the same third order gradients", single_gradients3, multiple_gradients3); } void test_length_0_vector() { From ba59e359e5d5443ba82cc9fce9b97853e8dadb03 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Wed, 30 Sep 2020 14:30:08 +0000 Subject: [PATCH 16/19] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final) --- test/prob/test_fixture_ccdf_log.hpp | 49 ++++++++++++++++++++--------- test/prob/test_fixture_cdf.hpp | 49 ++++++++++++++++++++--------- test/prob/test_fixture_cdf_log.hpp | 49 ++++++++++++++++++++--------- test/prob/test_fixture_distr.hpp | 49 ++++++++++++++++++++--------- 4 files changed, 140 insertions(+), 56 deletions(-) diff --git a/test/prob/test_fixture_ccdf_log.hpp b/test/prob/test_fixture_ccdf_log.hpp index ffbdd1fb795..19015a19ad1 100644 --- a/test/prob/test_fixture_ccdf_log.hpp +++ b/test/prob/test_fixture_ccdf_log.hpp @@ -565,13 +565,24 @@ class AgradCcdfLogTestFixture : public ::testing::Test { stan::math::recover_memory(); - stan::test::expect_near_rel("ccdf_log with repeated vector input should match a multiple of ccdf_log of single input", - stan::math::value_of_rec(single_ccdf_log), - stan::math::value_of_rec(multiple_ccdf_log)); - - stan::test::expect_near_rel("scalar and vectorized results should have the same first order gradients", single_gradients1, multiple_gradients1); - stan::test::expect_near_rel("scalar and vectorized results should have the same second order gradients", single_gradients2, multiple_gradients2); - stan::test::expect_near_rel("scalar and vectorized results should have the same third order gradients", single_gradients3, multiple_gradients3); + stan::test::expect_near_rel( + "ccdf_log with repeated vector input should match a multiple of " + "ccdf_log of single input", + stan::math::value_of_rec(single_ccdf_log), + stan::math::value_of_rec(multiple_ccdf_log)); + + stan::test::expect_near_rel( + "scalar and vectorized results should have the same first order " + "gradients", + single_gradients1, multiple_gradients1); + stan::test::expect_near_rel( + "scalar and vectorized results should have the same second order " + "gradients", + single_gradients2, multiple_gradients2); + stan::test::expect_near_rel( + "scalar and vectorized results should have the same third order " + "gradients", + single_gradients3, multiple_gradients3); } } @@ -683,13 +694,23 @@ class AgradCcdfLogTestFixture : public ::testing::Test { return; } - stan::test::expect_near_rel("sum of scalar ccdf_logs should match vectorized result", - stan::math::value_of_rec(single_ccdf_log), - stan::math::value_of_rec(multiple_ccdf_log)); - - stan::test::expect_near_rel("scalar and vectorized results should have the same first order gradients", single_gradients1, multiple_gradients1); - stan::test::expect_near_rel("scalar and vectorized results should have the same second order gradients", single_gradients2, multiple_gradients2); - stan::test::expect_near_rel("scalar and vectorized results should have the same third order gradients", single_gradients3, multiple_gradients3); + stan::test::expect_near_rel( + "sum of scalar ccdf_logs should match vectorized result", + stan::math::value_of_rec(single_ccdf_log), + stan::math::value_of_rec(multiple_ccdf_log)); + + stan::test::expect_near_rel( + "scalar and vectorized results should have the same first order " + "gradients", + single_gradients1, multiple_gradients1); + stan::test::expect_near_rel( + "scalar and vectorized results should have the same second order " + "gradients", + single_gradients2, multiple_gradients2); + stan::test::expect_near_rel( + "scalar and vectorized results should have the same third order " + "gradients", + single_gradients3, multiple_gradients3); } void test_lower_bound() { diff --git a/test/prob/test_fixture_cdf.hpp b/test/prob/test_fixture_cdf.hpp index 5c4112e594e..0ddf28251ce 100644 --- a/test/prob/test_fixture_cdf.hpp +++ b/test/prob/test_fixture_cdf.hpp @@ -558,13 +558,24 @@ class AgradCdfTestFixture : public ::testing::Test { stan::math::recover_memory(); - stan::test::expect_near_rel("cdf with repeated vector input should match a multiple of cdf of single input", - stan::math::value_of_rec(single_cdf), - stan::math::value_of_rec(multiple_cdf)); - - stan::test::expect_near_rel("scalar and vectorized results should have the same first order gradients", single_gradients1, multiple_gradients1); - stan::test::expect_near_rel("scalar and vectorized results should have the same second order gradients", single_gradients2, multiple_gradients2); - stan::test::expect_near_rel("scalar and vectorized results should have the same third order gradients", single_gradients3, multiple_gradients3); + stan::test::expect_near_rel( + "cdf with repeated vector input should match a multiple of cdf of " + "single input", + stan::math::value_of_rec(single_cdf), + stan::math::value_of_rec(multiple_cdf)); + + stan::test::expect_near_rel( + "scalar and vectorized results should have the same first order " + "gradients", + single_gradients1, multiple_gradients1); + stan::test::expect_near_rel( + "scalar and vectorized results should have the same second order " + "gradients", + single_gradients2, multiple_gradients2); + stan::test::expect_near_rel( + "scalar and vectorized results should have the same third order " + "gradients", + single_gradients3, multiple_gradients3); } } @@ -672,13 +683,23 @@ class AgradCdfTestFixture : public ::testing::Test { return; } - stan::test::expect_near_rel("product of scalar cdfs should match vectorized result", - stan::math::value_of_rec(single_cdf), - stan::math::value_of_rec(multiple_cdf)); - - stan::test::expect_near_rel("scalar and vectorized results should have the same first order gradients", single_gradients1, multiple_gradients1); - stan::test::expect_near_rel("scalar and vectorized results should have the same second order gradients", single_gradients2, multiple_gradients2); - stan::test::expect_near_rel("scalar and vectorized results should have the same third order gradients", single_gradients3, multiple_gradients3); + stan::test::expect_near_rel( + "product of scalar cdfs should match vectorized result", + stan::math::value_of_rec(single_cdf), + stan::math::value_of_rec(multiple_cdf)); + + stan::test::expect_near_rel( + "scalar and vectorized results should have the same first order " + "gradients", + single_gradients1, multiple_gradients1); + stan::test::expect_near_rel( + "scalar and vectorized results should have the same second order " + "gradients", + single_gradients2, multiple_gradients2); + stan::test::expect_near_rel( + "scalar and vectorized results should have the same third order " + "gradients", + single_gradients3, multiple_gradients3); } void test_lower_bound() { diff --git a/test/prob/test_fixture_cdf_log.hpp b/test/prob/test_fixture_cdf_log.hpp index 69c7e6a8a3c..5d98b0d6643 100644 --- a/test/prob/test_fixture_cdf_log.hpp +++ b/test/prob/test_fixture_cdf_log.hpp @@ -566,13 +566,24 @@ class AgradCdfLogTestFixture : public ::testing::Test { stan::math::recover_memory(); - stan::test::expect_near_rel("cdf_log with repeated vector input should match a multiple of cdf_log of single input", - stan::math::value_of_rec(single_cdf_log), - stan::math::value_of_rec(multiple_cdf_log)); - - stan::test::expect_near_rel("scalar and vectorized results should have the same first order gradients", single_gradients1, multiple_gradients1); - stan::test::expect_near_rel("scalar and vectorized results should have the same second order gradients", single_gradients2, multiple_gradients2); - stan::test::expect_near_rel("scalar and vectorized results should have the same third order gradients", single_gradients3, multiple_gradients3); + stan::test::expect_near_rel( + "cdf_log with repeated vector input should match a multiple of " + "cdf_log of single input", + stan::math::value_of_rec(single_cdf_log), + stan::math::value_of_rec(multiple_cdf_log)); + + stan::test::expect_near_rel( + "scalar and vectorized results should have the same first order " + "gradients", + single_gradients1, multiple_gradients1); + stan::test::expect_near_rel( + "scalar and vectorized results should have the same second order " + "gradients", + single_gradients2, multiple_gradients2); + stan::test::expect_near_rel( + "scalar and vectorized results should have the same third order " + "gradients", + single_gradients3, multiple_gradients3); } } @@ -683,13 +694,23 @@ class AgradCdfLogTestFixture : public ::testing::Test { return; } - stan::test::expect_near_rel("sum of scalar cdf_logs should match vectorized result", - stan::math::value_of_rec(single_cdf_log), - stan::math::value_of_rec(multiple_cdf_log)); - - stan::test::expect_near_rel("scalar and vectorized results should have the same first order gradients", single_gradients1, multiple_gradients1); - stan::test::expect_near_rel("scalar and vectorized results should have the same second order gradients", single_gradients2, multiple_gradients2); - stan::test::expect_near_rel("scalar and vectorized results should have the same third order gradients", single_gradients3, multiple_gradients3); + stan::test::expect_near_rel( + "sum of scalar cdf_logs should match vectorized result", + stan::math::value_of_rec(single_cdf_log), + stan::math::value_of_rec(multiple_cdf_log)); + + stan::test::expect_near_rel( + "scalar and vectorized results should have the same first order " + "gradients", + single_gradients1, multiple_gradients1); + stan::test::expect_near_rel( + "scalar and vectorized results should have the same second order " + "gradients", + single_gradients2, multiple_gradients2); + stan::test::expect_near_rel( + "scalar and vectorized results should have the same third order " + "gradients", + single_gradients3, multiple_gradients3); } void test_lower_bound() { diff --git a/test/prob/test_fixture_distr.hpp b/test/prob/test_fixture_distr.hpp index 2869d39dad4..2705ec10625 100644 --- a/test/prob/test_fixture_distr.hpp +++ b/test/prob/test_fixture_distr.hpp @@ -636,13 +636,24 @@ class AgradDistributionTestFixture : public ::testing::Test { stan::math::recover_memory(); - stan::test::expect_near_rel("log prob with repeated vector input should match a multiple of log prob of single input", - stan::math::value_of_rec(single_lp), - stan::math::value_of_rec(multiple_lp)); - - stan::test::expect_near_rel("scalar and vectorized results should have the same first order gradients", single_gradients1, multiple_gradients1); - stan::test::expect_near_rel("scalar and vectorized results should have the same second order gradients", single_gradients2, multiple_gradients2); - stan::test::expect_near_rel("scalar and vectorized results should have the same third order gradients", single_gradients3, multiple_gradients3); + stan::test::expect_near_rel( + "log prob with repeated vector input should match a multiple of log " + "prob of single input", + stan::math::value_of_rec(single_lp), + stan::math::value_of_rec(multiple_lp)); + + stan::test::expect_near_rel( + "scalar and vectorized results should have the same first order " + "gradients", + single_gradients1, multiple_gradients1); + stan::test::expect_near_rel( + "scalar and vectorized results should have the same second order " + "gradients", + single_gradients2, multiple_gradients2); + stan::test::expect_near_rel( + "scalar and vectorized results should have the same third order " + "gradients", + single_gradients3, multiple_gradients3); } } @@ -755,13 +766,23 @@ class AgradDistributionTestFixture : public ::testing::Test { return; } - stan::test::expect_near_rel("sum of scalar log probs should match vectorized result", - stan::math::value_of_rec(single_lp), - stan::math::value_of_rec(multiple_lp)); - - stan::test::expect_near_rel("scalar and vectorized results should have the same first order gradients", single_gradients1, multiple_gradients1); - stan::test::expect_near_rel("scalar and vectorized results should have the same second order gradients", single_gradients2, multiple_gradients2); - stan::test::expect_near_rel("scalar and vectorized results should have the same third order gradients", single_gradients3, multiple_gradients3); + stan::test::expect_near_rel( + "sum of scalar log probs should match vectorized result", + stan::math::value_of_rec(single_lp), + stan::math::value_of_rec(multiple_lp)); + + stan::test::expect_near_rel( + "scalar and vectorized results should have the same first order " + "gradients", + single_gradients1, multiple_gradients1); + stan::test::expect_near_rel( + "scalar and vectorized results should have the same second order " + "gradients", + single_gradients2, multiple_gradients2); + stan::test::expect_near_rel( + "scalar and vectorized results should have the same third order " + "gradients", + single_gradients3, multiple_gradients3); } void test_length_0_vector() { From 0c891f322ee4c7da283069e451b2033ec31bfd26 Mon Sep 17 00:00:00 2001 From: Ben Date: Wed, 30 Sep 2020 17:00:49 -0400 Subject: [PATCH 17/19] Made logistic test slightly numerically easier (Issue #1861) --- test/prob/logistic/logistic_test.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/prob/logistic/logistic_test.hpp b/test/prob/logistic/logistic_test.hpp index 5a473f03bd2..e47475c2669 100644 --- a/test/prob/logistic/logistic_test.hpp +++ b/test/prob/logistic/logistic_test.hpp @@ -19,9 +19,9 @@ class AgradDistributionsLogistic : public AgradDistributionTest { param[0] = -1.0; // y param[1] = 0.2; // mu - param[2] = 0.25; // sigma + param[2] = 0.5; // sigma parameters.push_back(param); - log_prob.push_back(-3.430097773556644469295); // expected log_prob + log_prob.push_back(-1.8805251237479538862); // expected log_prob } void invalid_values(vector& index, vector& value) { From cbe96eb312cb2f4144ffde6efc0e84e12a048944 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Wed, 30 Sep 2020 21:01:45 +0000 Subject: [PATCH 18/19] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final) --- test/prob/logistic/logistic_test.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/prob/logistic/logistic_test.hpp b/test/prob/logistic/logistic_test.hpp index e47475c2669..752395e4030 100644 --- a/test/prob/logistic/logistic_test.hpp +++ b/test/prob/logistic/logistic_test.hpp @@ -19,7 +19,7 @@ class AgradDistributionsLogistic : public AgradDistributionTest { param[0] = -1.0; // y param[1] = 0.2; // mu - param[2] = 0.5; // sigma + param[2] = 0.5; // sigma parameters.push_back(param); log_prob.push_back(-1.8805251237479538862); // expected log_prob } From 42fd44856b95b56ad660502b3d1a299a9e28b654 Mon Sep 17 00:00:00 2001 From: Ben Date: Thu, 1 Oct 2020 10:25:22 -0400 Subject: [PATCH 19/19] Made probability unit tests easier for negative binomial (Issue #1861) --- test/prob/neg_binomial/neg_binomial_test.hpp | 8 ++++---- test/unit/math/mix/prob/neg_binomial_test.cpp | 3 +++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/test/prob/neg_binomial/neg_binomial_test.hpp b/test/prob/neg_binomial/neg_binomial_test.hpp index b162426c3af..e1ad1f61ccf 100644 --- a/test/prob/neg_binomial/neg_binomial_test.hpp +++ b/test/prob/neg_binomial/neg_binomial_test.hpp @@ -24,11 +24,11 @@ class AgradDistributionsNegBinomial : public AgradDistributionTest { parameters.push_back(param); log_prob.push_back(-142.6147368129045105434); // expected log_prob - param[0] = 13; - param[1] = 1e11; - param[2] = 1e10; + param[0] = 10; + param[1] = 1e6; + param[2] = 1e5; parameters.push_back(param); - log_prob.push_back(-2.6185576442208003); // expected log_prob + log_prob.push_back(-2.0785666431081630812); // expected log_prob } void invalid_values(vector& index, vector& value) { diff --git a/test/unit/math/mix/prob/neg_binomial_test.cpp b/test/unit/math/mix/prob/neg_binomial_test.cpp index f106f51ab69..a17998ded93 100644 --- a/test/unit/math/mix/prob/neg_binomial_test.cpp +++ b/test/unit/math/mix/prob/neg_binomial_test.cpp @@ -16,4 +16,7 @@ TEST(mathMixScalFun, neg_binomial_lpmf_derivatives) { stan::test::expect_ad(f(6), 1.5, 4.1); stan::test::expect_ad(f(6), std::vector({1.7, 2.0}), std::vector({1.1, 2.3})); + stan::test::expect_ad(f(13), 1e11, 1e10); + stan::test::expect_ad(f(13), std::vector({1e11, 1e10}), + std::vector({1e10, 1e9})); }