From de37bf24845907bab7ca9fbc2392c4849a47d945 Mon Sep 17 00:00:00 2001 From: "Paul A. Bristow" Date: Sat, 30 Dec 2006 11:01:41 +0000 Subject: [PATCH] 1st commit of tests [SVN r3580] --- test/test_bernoulli.cpp | 281 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 281 insertions(+) create mode 100644 test/test_bernoulli.cpp diff --git a/test/test_bernoulli.cpp b/test/test_bernoulli.cpp new file mode 100644 index 000000000..b3ca3ab68 --- /dev/null +++ b/test/test_bernoulli.cpp @@ -0,0 +1,281 @@ +// test_bernoulli.cpp + +// Copyright John Maddock 2006. +// Copyright Paul A. Bristow 2006. + +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt +// or copy at http://www.boost.org/LICENSE_1_0.txt) + +// Basic sanity test for Bernoulli Cumulative Distribution Function. + +#define BOOST_MATH_THROW_ON_DOMAIN_ERROR + +#ifdef _MSC_VER +# pragma warning(disable: 4127) // conditional expression is constant. +# pragma warning(disable: 4100) // unreferenced formal parameter. +# pragma warning(disable: 4512) // assignment operator could not be generated. +#endif + +#include // for bernoulli_distribution +using boost::math::bernoulli_distribution; + +#include // for real_concept +using ::boost::math::concepts::real_concept; + +#include // for test_main +#include // for BOOST_CHECK_CLOSE_FRACTION, BOOST_CHECK_EQUAL... + +#include +using std::cout; +using std::endl; +using std::fixed; +using std::right; +using std::left; +using std::showpoint; +using std::showpos; +using std::setw; +using std::setprecision; + +#include +using std::numeric_limits; + +template // Any floating-point type RealType. +void test_spots(RealType) +{ // Parameter only provides the type, float, double... value ignored. + + // Basic sanity checks, test data is to double precision only + // so set tolerance to 100 eps expressed as a fraction, + // or 100 eps of type double expressed as a fraction, + // whichever is the larger. + + RealType tolerance = (std::max) + (boost::math::tools::epsilon(), + static_cast(std::numeric_limits::epsilon())); + tolerance *= 100; + + cout << "Tolerance for type " << typeid(RealType).name() << " is " + << setprecision(3) << tolerance << " (or " << tolerance * 100 << "%)." << endl; + + // Sources of spot test values: + + using boost::math::bernoulli_distribution; // of type RealType. + using ::boost::math::cdf; + using ::boost::math::pdf; + + BOOST_CHECK_EQUAL(bernoulli_distribution(static_cast(0.5)).success_fraction(), static_cast(0.5)); + BOOST_CHECK_EQUAL(bernoulli_distribution(static_cast(0.1L)).success_fraction(), static_cast(0.1L)); + BOOST_CHECK_EQUAL(bernoulli_distribution(static_cast(0.9L)).success_fraction(), static_cast(0.9L)); + + BOOST_CHECK_THROW( // Constructor success_fraction outside 0 to 1. + bernoulli_distribution(static_cast(2)), std::domain_error); + BOOST_CHECK_THROW( + bernoulli_distribution(static_cast(-2)), std::domain_error); + + BOOST_CHECK_THROW( + pdf( // pdf k neither 0 nor 1. + bernoulli_distribution(static_cast(0.25L)), static_cast(-1)), std::domain_error); + + BOOST_CHECK_THROW( + pdf( // pdf k neither 0 nor 1. + bernoulli_distribution(static_cast(0.25L)), static_cast(2)), std::domain_error); + + BOOST_CHECK_EQUAL( + pdf( // OK k (or n) + bernoulli_distribution(static_cast(0.5L)), static_cast(0)), + static_cast(0.5)); // Expect 1 - p. + + BOOST_CHECK_CLOSE_FRACTION( + pdf( // OK k (or n) + bernoulli_distribution(static_cast(0.6L)), static_cast(0)), + static_cast(0.4L), tolerance); // Expect 1 - p. + + BOOST_CHECK_CLOSE_FRACTION( + pdf( // OK k (or n) + bernoulli_distribution(static_cast(0.6L)), static_cast(0)), + static_cast(0.4L), tolerance); // Expect 1- p. + + BOOST_CHECK_CLOSE_FRACTION( + pdf( // OK k (or n) + bernoulli_distribution(static_cast(0.4L)), static_cast(0)), + static_cast(0.6L), tolerance); // Expect 1- p. + + BOOST_CHECK_EQUAL( + mean(bernoulli_distribution(static_cast(0.5L))), static_cast(0.5L)); + + BOOST_CHECK_EQUAL( + mean(bernoulli_distribution(static_cast(0.1L))), + static_cast(0.1L)); + + BOOST_CHECK_CLOSE_FRACTION( + variance(bernoulli_distribution(static_cast(0.1L))), + static_cast(0.09L), + tolerance); + + BOOST_CHECK_CLOSE_FRACTION( + skewness(bernoulli_distribution(static_cast(0.1L))), + static_cast(2.666666666666666666666666666666666666666666L), + tolerance); + + BOOST_CHECK_CLOSE_FRACTION( + kurtosis(bernoulli_distribution(static_cast(0.1L))), + static_cast(5.11111111111111111111111111111111111111111111L), + tolerance); + // median NOT implemented. + BOOST_CHECK_THROW( + median(bernoulli_distribution(static_cast(0.5L))), std::domain_error); + + + BOOST_CHECK_THROW( + quantile( + bernoulli_distribution(static_cast(2)), // prob >1 + static_cast(0)), std::domain_error + ); + BOOST_CHECK_THROW( + quantile( + bernoulli_distribution(static_cast(-1)), // prob < 0 + static_cast(0)), std::domain_error + ); + BOOST_CHECK_THROW( + quantile( + bernoulli_distribution(static_cast(0.5L)), // k >1 + static_cast(-1)), std::domain_error + ); + BOOST_CHECK_THROW( + quantile( + bernoulli_distribution(static_cast(0.5L)), // k < 0 + static_cast(2)), std::domain_error + ); + + BOOST_CHECK_CLOSE_FRACTION( + cdf( + bernoulli_distribution(static_cast(0.6L)), + static_cast(0)), + static_cast(0.4L), // 1 - p + tolerance + ); + + BOOST_CHECK_CLOSE_FRACTION( + cdf( + bernoulli_distribution(static_cast(0.6L)), + static_cast(1)), + static_cast(1), // p + tolerance + ); + + BOOST_CHECK_CLOSE_FRACTION( + cdf(complement( + bernoulli_distribution(static_cast(0.6L)), + static_cast(1))), + static_cast(0), + tolerance + ); + + BOOST_CHECK_CLOSE_FRACTION( + cdf(complement( + bernoulli_distribution(static_cast(0.6L)), + static_cast(0))), + static_cast(0.6L), + tolerance + ); + + BOOST_CHECK_EQUAL( + quantile( + bernoulli_distribution(static_cast(0.6L)), + static_cast(0.1L)), // < p + static_cast(0) + ); + + BOOST_CHECK_EQUAL( + quantile( + bernoulli_distribution(static_cast(0.6L)), + static_cast(0.9L)), // > p + static_cast(1) + ); + + BOOST_CHECK_EQUAL( + quantile(complement( + bernoulli_distribution(static_cast(0.6L)), + static_cast(0.1L))), // < p + static_cast(1) + ); + + BOOST_CHECK_EQUAL( + quantile(complement( + bernoulli_distribution(static_cast(0.6L)), + static_cast(0.9L))), // > p + static_cast(0) + ); +} // template void test_spots(RealType) + +int test_main(int, char* []) +{ + // Check that can generate bernoulli distribution using both convenience methods: + bernoulli_distribution bn1(0.5); // Using default RealType double. + boost::math::bernoulli bn2(0.5); // Using typedef. + + BOOST_CHECK_EQUAL(bn1.success_fraction(), 0.5); + BOOST_CHECK_EQUAL(bn2.success_fraction(), 0.5); + + BOOST_CHECK_THROW(bernoulli_distribution(-1), std::domain_error); // p outside 0 to 1. + BOOST_CHECK_THROW(bernoulli_distribution(+2), std::domain_error); // p outside 0 to 1. + BOOST_CHECK_THROW(bernoulli_distribution bn3(std::numeric_limits::quiet_NaN() ), std::domain_error); // p outside 0 to 1. + BOOST_CHECK_THROW(bernoulli_distribution bn4(std::numeric_limits::infinity() ), std::domain_error); // p outside 0 to 1. + + + BOOST_CHECK_EQUAL(kurtosis(bn2) -3, kurtosis_excess(bn2)); + BOOST_CHECK_EQUAL(kurtosis(bn2), -2); + BOOST_CHECK_EQUAL(kurtosis_excess(bn2), -5); + BOOST_CHECK_EQUAL(kurtosis_excess(bn2), -5); + + //using namespace boost::math; or + using boost::math::bernoulli; + + double tol5eps = std::numeric_limits::epsilon() * 5; // 5 eps as a fraction. + + BOOST_CHECK_CLOSE_FRACTION(kurtosis_excess(bernoulli(0.1)), 2.11111111111111111111111111111111111111111111111111, tol5eps); + BOOST_CHECK_CLOSE_FRACTION(kurtosis_excess(bernoulli(0.9)), 2.11111111111111111111111111111111111111111111111111, tol5eps); + BOOST_CHECK_CLOSE_FRACTION(kurtosis(bernoulli(0.6)), -1.8333333333333333333333333333333333333333333333333, tol5eps); + BOOST_CHECK_EQUAL(kurtosis(bernoulli(0)), +std::numeric_limits::infinity()); + BOOST_CHECK_EQUAL(kurtosis(bernoulli(1)), +std::numeric_limits::infinity()); + // + + // Basic sanity-check spot values. +#ifdef BOOST_MATH_THROW_ON_DOMAIN_ERROR + cout << "BOOST_MATH_THROW_ON_DOMAIN_ERROR" << " is defined to throw on domain error." << endl; +#else + cout << "BOOST_MATH_THROW_ON_DOMAIN_ERROR" << " is NOT defined, so NO throw on domain error." << endl; +#endif + + // (Parameter value, arbitrarily zero, only communicates the floating point type). + test_spots(0.0F); // Test float. + test_spots(0.0); // Test double. + test_spots(0.0L); // Test long double. +#if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582)) + test_spots(boost::math::concepts::real_concept(0.)); // Test real concept. +#endif + + return 0; +} // int test_main(int, char* []) + +/* + + +Output is: + +test_bernoulli.cpp +Linking... +Autorun "i:\boost-06-05-03-1300\libs\math\test\Math_test\debug\test_bernouilli.exe" +Running 1 test case... +BOOST_MATH_THROW_ON_DOMAIN_ERROR is defined to throw on domain error. +Tolerance for type float is 1.19e-005 (or 0.00119%). +Tolerance for type double is 2.22e-014 (or 2.22e-012%). +Tolerance for type long double is 2.22e-014 (or 2.22e-012%). +Tolerance for type class boost::math::concepts::real_concept is 2.22e-014 (or 2.22e-012%). +*** No errors detected +Build Time 0:05 + + + +*/ \ No newline at end of file