📄 test_binomial.cpp
字号:
pdf( binomial_distribution<RealType>(static_cast<RealType>(0), static_cast<RealType>(0.25)), static_cast<RealType>(0)), static_cast<RealType>(1) ); BOOST_CHECK_THROW( pdf( binomial_distribution<RealType>(static_cast<RealType>(-1), static_cast<RealType>(0.25)), static_cast<RealType>(0)), std::domain_error ); BOOST_CHECK_THROW( pdf( binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(-0.25)), static_cast<RealType>(0)), std::domain_error ); BOOST_CHECK_THROW( pdf( binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(1.25)), static_cast<RealType>(0)), std::domain_error ); BOOST_CHECK_THROW( pdf( binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)), static_cast<RealType>(-1)), std::domain_error ); BOOST_CHECK_THROW( pdf( binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)), static_cast<RealType>(9)), std::domain_error ); BOOST_CHECK_THROW( cdf( binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)), static_cast<RealType>(-1)), std::domain_error ); BOOST_CHECK_THROW( cdf( binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)), static_cast<RealType>(9)), std::domain_error ); BOOST_CHECK_THROW( cdf( binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(-0.25)), static_cast<RealType>(0)), std::domain_error ); BOOST_CHECK_THROW( cdf( binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(1.25)), static_cast<RealType>(0)), std::domain_error ); BOOST_CHECK_THROW( quantile( binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(-0.25)), static_cast<RealType>(0)), std::domain_error ); BOOST_CHECK_THROW( quantile( binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(1.25)), static_cast<RealType>(0)), std::domain_error ); BOOST_CHECK_EQUAL( quantile( binomial_distribution<RealType>(static_cast<RealType>(16), static_cast<RealType>(0.25)), static_cast<RealType>(0.01)), // Less than cdf == pdf(binomial_distribution<RealType>(16, 0.25), 0) static_cast<RealType>(0) // so expect zero as best approximation. ); BOOST_CHECK_EQUAL( cdf( binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)), static_cast<RealType>(8)), static_cast<RealType>(1) ); BOOST_CHECK_EQUAL( cdf( binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0)), static_cast<RealType>(7)), static_cast<RealType>(1) ); BOOST_CHECK_EQUAL( cdf( binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(1)), static_cast<RealType>(7)), static_cast<RealType>(0) );#endif { // This is a visual sanity check that everything is OK: binomial_distribution<RealType> my8dist(8., 0.25); // Note: double values (matching the distribution definition) avoid the need for any casting. //cout << "mean(my8dist) = " << boost::math::mean(my8dist) << endl; // mean(my8dist) = 2 //cout << "my8dist.trials() = " << my8dist.trials() << endl; // my8dist.trials() = 8 //cout << "my8dist.success_fraction() = " << my8dist.success_fraction() << endl; // my8dist.success_fraction() = 0.25 BOOST_CHECK_CLOSE(my8dist.trials(), static_cast<RealType>(8), tol2); BOOST_CHECK_CLOSE(my8dist.success_fraction(), static_cast<RealType>(0.25), tol2); //{ // int n = static_cast<int>(boost::math::tools::real_cast<double>(my8dist.trials())); // RealType sumcdf = 0.; // for (int k = 0; k <= n; k++) // { // cout << k << ' ' << pdf(my8dist, static_cast<RealType>(k)); // sumcdf += pdf(my8dist, static_cast<RealType>(k)); // cout << ' ' << sumcdf; // cout << ' ' << cdf(my8dist, static_cast<RealType>(k)); // cout << ' ' << sumcdf - cdf(my8dist, static_cast<RealType>(k)) << endl; // } // for k // } // n = 8, p =0.25 //k pdf cdf //0 0.1001129150390625 0.1001129150390625 //1 0.26696777343749994 0.36708068847656244 //2 0.31146240234375017 0.67854309082031261 //3 0.20764160156249989 0.8861846923828125 //4 0.086517333984375 0.9727020263671875 //5 0.023071289062499997 0.9957733154296875 //6 0.0038452148437500009 0.9996185302734375 //7 0.00036621093749999984 0.9999847412109375 //8 1.52587890625e-005 1 1 0 }#if !defined(TEST_REAL_CONCEPT)#define T RealType#else // This reduces compile time and compiler memory usage by storing test data // as an array of long double's rather than an array of real_concept's:#define T long double#endif#include "binomial_quantile.ipp" for(unsigned i = 0; i < binomial_quantile_data.size(); ++i) { using namespace boost::math::policies; typedef policy<discrete_quantile<boost::math::policies::real> > P1; typedef policy<discrete_quantile<integer_round_down> > P2; typedef policy<discrete_quantile<integer_round_up> > P3; typedef policy<discrete_quantile<integer_round_outwards> > P4; typedef policy<discrete_quantile<integer_round_inwards> > P5; typedef policy<discrete_quantile<integer_round_nearest> > P6; RealType tol = boost::math::tools::epsilon<RealType>() * 500; if(!boost::is_floating_point<RealType>::value) tol *= 10; // no lanczos approximation implies less accuracy RealType x;#if !defined(TEST_ROUNDING) || (TEST_ROUNDING == 1) // // Check full real value first: // binomial_distribution<RealType, P1> p1(binomial_quantile_data[i][0], binomial_quantile_data[i][1]); x = quantile(p1, binomial_quantile_data[i][2]); BOOST_CHECK_CLOSE_FRACTION(x, (RealType)binomial_quantile_data[i][3], tol); x = quantile(complement(p1, (RealType)binomial_quantile_data[i][2])); BOOST_CHECK_CLOSE_FRACTION(x, (RealType)binomial_quantile_data[i][4], tol);#endif#if !defined(TEST_ROUNDING) || (TEST_ROUNDING == 2) // // Now with round down to integer: // binomial_distribution<RealType, P2> p2(binomial_quantile_data[i][0], binomial_quantile_data[i][1]); x = quantile(p2, binomial_quantile_data[i][2]); BOOST_CHECK_EQUAL(x, (RealType)floor(binomial_quantile_data[i][3])); x = quantile(complement(p2, binomial_quantile_data[i][2])); BOOST_CHECK_EQUAL(x, (RealType)floor(binomial_quantile_data[i][4]));#endif#if !defined(TEST_ROUNDING) || (TEST_ROUNDING == 3) // // Now with round up to integer: // binomial_distribution<RealType, P3> p3(binomial_quantile_data[i][0], binomial_quantile_data[i][1]); x = quantile(p3, binomial_quantile_data[i][2]); BOOST_CHECK_EQUAL(x, (RealType)ceil(binomial_quantile_data[i][3])); x = quantile(complement(p3, binomial_quantile_data[i][2])); BOOST_CHECK_EQUAL(x, (RealType)ceil(binomial_quantile_data[i][4]));#endif#if !defined(TEST_ROUNDING) || (TEST_ROUNDING == 4) // // Now with round to integer "outside": // binomial_distribution<RealType, P4> p4(binomial_quantile_data[i][0], binomial_quantile_data[i][1]); x = quantile(p4, binomial_quantile_data[i][2]); BOOST_CHECK_EQUAL(x, (RealType)(binomial_quantile_data[i][2] < 0.5f ? floor(binomial_quantile_data[i][3]) : ceil(binomial_quantile_data[i][3]))); x = quantile(complement(p4, binomial_quantile_data[i][2])); BOOST_CHECK_EQUAL(x, (RealType)(binomial_quantile_data[i][2] < 0.5f ? ceil(binomial_quantile_data[i][4]) : floor(binomial_quantile_data[i][4])));#endif#if !defined(TEST_ROUNDING) || (TEST_ROUNDING == 5) // // Now with round to integer "inside": // binomial_distribution<RealType, P5> p5(binomial_quantile_data[i][0], binomial_quantile_data[i][1]); x = quantile(p5, binomial_quantile_data[i][2]); BOOST_CHECK_EQUAL(x, (RealType)(binomial_quantile_data[i][2] < 0.5f ? ceil(binomial_quantile_data[i][3]) : floor(binomial_quantile_data[i][3]))); x = quantile(complement(p5, binomial_quantile_data[i][2])); BOOST_CHECK_EQUAL(x, (RealType)(binomial_quantile_data[i][2] < 0.5f ? floor(binomial_quantile_data[i][4]) : ceil(binomial_quantile_data[i][4])));#endif#if !defined(TEST_ROUNDING) || (TEST_ROUNDING == 6) // // Now with round to nearest integer: // binomial_distribution<RealType, P6> p6(binomial_quantile_data[i][0], binomial_quantile_data[i][1]); x = quantile(p6, binomial_quantile_data[i][2]); BOOST_CHECK_EQUAL(x, (RealType)(floor(binomial_quantile_data[i][3] + 0.5f))); x = quantile(complement(p6, binomial_quantile_data[i][2])); BOOST_CHECK_EQUAL(x, (RealType)(floor(binomial_quantile_data[i][4] + 0.5f)));#endif }} // template <class RealType>void test_spots(RealType)int test_main(int, char* []){ BOOST_MATH_CONTROL_FP; // Check that can generate binomial distribution using one convenience methods: binomial_distribution<> mybn2(1., 0.5); // Using default RealType double. // but that // boost::math::binomial mybn1(1., 0.5); // Using typedef fails // error C2039: 'binomial' : is not a member of 'boost::math' // Basic sanity-check spot values. // (Parameter value, arbitrarily zero, only communicates the floating point type).#ifdef TEST_FLOAT test_spots(0.0F); // Test float.#endif#ifdef TEST_DOUBLE test_spots(0.0); // Test double.#endif#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS#ifdef TEST_LDOUBLE test_spots(0.0L); // Test long double.#endif#if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))#ifdef TEST_REAL_CONCEPT test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.#endif#endif#else std::cout << "<note>The long double tests have been disabled on this platform " "either because the long double overloads of the usual math functions are " "not available at all, or because they are too inaccurate for these tests " "to pass.</note>" << std::cout;#endif return 0;} // int test_main(int, char* [])/*Output is:Autorun "i:\boost-06-05-03-1300\libs\math\test\Math_test\debug\test_binomial.exe"Running 1 test case...Tolerance = 0.0119209%.Tolerance = 2.22045e-011%.Tolerance = 2.22045e-011%.Tolerance = 2.22045e-011%.*** No errors detected*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -