📄 test_binomial.cpp
字号:
test_spot( static_cast<RealType>(500), // Sample size, N static_cast<RealType>(470), // Number of successes, k static_cast<RealType>(0.95), // Probability of success, p static_cast<RealType>(0.176470742656766), // Probability of result (CDF), P static_cast<RealType>(1 - 0.176470742656766), // Q = 1 - P tolerance * 10); // Note higher tolerance on this test! test_spot( static_cast<RealType>(500), // Sample size, N static_cast<RealType>(400), // Number of successes, k static_cast<RealType>(0.05), // Probability of success, p static_cast<RealType>(1), // Probability of result (CDF), P static_cast<RealType>(0), // Q = 1 - P tolerance); test_spot( static_cast<RealType>(500), // Sample size, N static_cast<RealType>(400), // Number of successes, k static_cast<RealType>(0.9), // Probability of success, p static_cast<RealType>(1.80180425681923E-11), // Probability of result (CDF), P static_cast<RealType>(1 - 1.80180425681923E-11), // Q = 1 - P tolerance); test_spot( static_cast<RealType>(500), // Sample size, N static_cast<RealType>(5), // Number of successes, k static_cast<RealType>(0.05), // Probability of success, p static_cast<RealType>(9.181808267643E-7), // Probability of result (CDF), P static_cast<RealType>(1 - 9.181808267643E-7), // Q = 1 - P tolerance); test_spot( static_cast<RealType>(2), // Sample size, N static_cast<RealType>(1), // Number of successes, k static_cast<RealType>(0.5), // Probability of success, p static_cast<RealType>(0.75), // Probability of result (CDF), P static_cast<RealType>(0.25), // Q = 1 - P tolerance); test_spot( static_cast<RealType>(8), // Sample size, N static_cast<RealType>(3), // Number of successes, k static_cast<RealType>(0.25), // Probability of success, p static_cast<RealType>(0.8861846923828125), // Probability of result (CDF), P static_cast<RealType>(1 - 0.8861846923828125), // Q = 1 - P tolerance); test_spot( static_cast<RealType>(8), // Sample size, N static_cast<RealType>(0), // Number of successes, k static_cast<RealType>(0.25), // Probability of success, p static_cast<RealType>(0.1001129150390625), // Probability of result (CDF), P static_cast<RealType>(1 - 0.1001129150390625), // Q = 1 - P tolerance); test_spot( static_cast<RealType>(8), // Sample size, N static_cast<RealType>(1), // Number of successes, k static_cast<RealType>(0.25), // Probability of success, p static_cast<RealType>(0.36708068847656244), // Probability of result (CDF), P static_cast<RealType>(1 - 0.36708068847656244), // Q = 1 - P tolerance); test_spot( static_cast<RealType>(8), // Sample size, N static_cast<RealType>(4), // Number of successes, k static_cast<RealType>(0.25), // Probability of success, p static_cast<RealType>(0.9727020263671875), // Probability of result (CDF), P static_cast<RealType>(1 - 0.9727020263671875), // Q = 1 - P tolerance); test_spot( static_cast<RealType>(8), // Sample size, N static_cast<RealType>(7), // Number of successes, k static_cast<RealType>(0.25), // Probability of success, p static_cast<RealType>(0.9999847412109375), // Probability of result (CDF), P static_cast<RealType>(1 - 0.9999847412109375), // Q = 1 - P tolerance); // Tests on PDF follow: BOOST_CHECK_CLOSE( pdf(binomial_distribution<RealType>(static_cast<RealType>(20), static_cast<RealType>(0.75)), static_cast<RealType>(10)), // k. static_cast<RealType>(0.00992227527967770583927631378173), // 0.00992227527967770583927631378173 tolerance); BOOST_CHECK_CLOSE( pdf(binomial_distribution<RealType>(static_cast<RealType>(20), static_cast<RealType>(0.5)), static_cast<RealType>(10)), // k. static_cast<RealType>(0.17619705200195312500000000000000000000), // get k=10 0.049611376398388612 p = 0.25 tolerance); // Binomial pdf Test values from // http://www.adsciengineering.com/bpdcalc/index.php for example // http://www.adsciengineering.com/bpdcalc/index.php?n=20&p=0.25&start=0&stop=20&Submit=Generate // Appears to use at least 80-bit long double for 32 decimal digits accuracy, // but loses accuracy of display if leading zeros? // (if trailings zero then are exact values?) // so useful for testing 64-bit double accuracy. // P = 0.25, n = 20, k = 0 to 20 //0 C(20,0) * 0.25^0 * 0.75^20 0.00317121193893399322405457496643 //1 C(20,1) * 0.25^1 * 0.75^19 0.02114141292622662149369716644287 //2 C(20,2) * 0.25^2 * 0.75^18 0.06694780759971763473004102706909 //3 C(20,3) * 0.25^3 * 0.75^17 0.13389561519943526946008205413818 //4 C(20,4) * 0.25^4 * 0.75^16 0.18968545486586663173511624336242 //5 C(20,5) * 0.25^5 * 0.75^15 0.20233115185692440718412399291992 //6 C(20,6) * 0.25^6 * 0.75^14 0.16860929321410367265343666076660 //7 C(20,7) * 0.25^7 * 0.75^13 0.11240619547606911510229110717773 //8 C(20,8) * 0.25^8 * 0.75^12 0.06088668921620410401374101638793 //9 C(20,9) * 0.25^9 * 0.75^11 0.02706075076275737956166267395019 //10 C(20,10) * 0.25^10 * 0.75^10 0.00992227527967770583927631378173 //11 C(20,11) * 0.25^11 * 0.75^9 0.00300675008475081995129585266113 //12 C(20,12) * 0.25^12 * 0.75^8 0.00075168752118770498782396316528 //13 C(20,13) * 0.25^13 * 0.75^7 0.00015419231203850358724594116210 //14 C(20,14) * 0.25^14 * 0.75^6 0.00002569871867308393120765686035 //15 C(20,15) * 0.25^15 * 0.75^5 0.00000342649582307785749435424804 //16 C(20,16) * 0.25^16 * 0.75^4 0.00000035692664823727682232856750 //17 C(20,17) * 0.25^17 * 0.75^3 0.00000002799424692057073116302490 //18 C(20,18) * 0.25^18 * 0.75^2 0.00000000155523594003170728683471 //19 C(20,19) * 0.25^19 * 0.75^1 0.00000000005456968210637569427490 //20 C(20,20) * 0.25^20 * 0.75^0 0.00000000000090949470177292823791 BOOST_CHECK_CLOSE( pdf(binomial_distribution<RealType>(static_cast<RealType>(20), static_cast<RealType>(0.25)), static_cast<RealType>(10)), // k. static_cast<RealType>(0.00992227527967770583927631378173), // k=10 p = 0.25 tolerance); BOOST_CHECK_CLOSE( // k = 0 use different formula - only exp so more accurate. pdf(binomial_distribution<RealType>(static_cast<RealType>(20), static_cast<RealType>(0.25)), static_cast<RealType>(0)), // k. static_cast<RealType>(0.00317121193893399322405457496643), // k=0 p = 0.25 tolerance); BOOST_CHECK_CLOSE( // k = 20 use different formula - only exp so more accurate. pdf(binomial_distribution<RealType>(static_cast<RealType>(20), static_cast<RealType>(0.25)), static_cast<RealType>(20)), // k == n. static_cast<RealType>(0.00000000000090949470177292823791), // k=20 p = 0.25 tolerance); BOOST_CHECK_CLOSE( // k = 1. pdf(binomial_distribution<RealType>(static_cast<RealType>(20), static_cast<RealType>(0.25)), static_cast<RealType>(1)), // k. static_cast<RealType>(0.02114141292622662149369716644287), // k=1 p = 0.25 tolerance); // Some exact (probably) values. BOOST_CHECK_CLOSE( pdf(binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)), static_cast<RealType>(0)), // k. static_cast<RealType>(0.10011291503906250000000000000000), // k=0 p = 0.25 tolerance); BOOST_CHECK_CLOSE( // k = 1. pdf(binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)), static_cast<RealType>(1)), // k. static_cast<RealType>(0.26696777343750000000000000000000), // k=1 p = 0.25 tolerance); BOOST_CHECK_CLOSE( // k = 2. pdf(binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)), static_cast<RealType>(2)), // k. static_cast<RealType>(0.31146240234375000000000000000000), // k=2 p = 0.25 tolerance); BOOST_CHECK_CLOSE( // k = 3. pdf(binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)), static_cast<RealType>(3)), // k. static_cast<RealType>(0.20764160156250000000000000000000), // k=3 p = 0.25 tolerance); BOOST_CHECK_CLOSE( // k = 7. pdf(binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)), static_cast<RealType>(7)), // k. static_cast<RealType>(0.00036621093750000000000000000000), // k=7 p = 0.25 tolerance); BOOST_CHECK_CLOSE( // k = 8. pdf(binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)), static_cast<RealType>(8)), // k = n. static_cast<RealType>(0.00001525878906250000000000000000), // k=8 p = 0.25 tolerance); binomial_distribution<RealType> dist(static_cast<RealType>(8), static_cast<RealType>(0.25)); RealType x = static_cast<RealType>(0.125); using namespace std; // ADL of std names. // mean: BOOST_CHECK_CLOSE( mean(dist) , static_cast<RealType>(8 * 0.25), tol2); // variance: BOOST_CHECK_CLOSE( variance(dist) , static_cast<RealType>(8 * 0.25 * 0.75), tol2); // std deviation: BOOST_CHECK_CLOSE( standard_deviation(dist) , static_cast<RealType>(sqrt(8 * 0.25L * 0.75L)), tol2); // hazard: BOOST_CHECK_CLOSE( hazard(dist, x) , pdf(dist, x) / cdf(complement(dist, x)), tol2); // cumulative hazard: BOOST_CHECK_CLOSE( chf(dist, x) , -log(cdf(complement(dist, x))), tol2); // coefficient_of_variation: BOOST_CHECK_CLOSE( coefficient_of_variation(dist) , standard_deviation(dist) / mean(dist), tol2); // mode: BOOST_CHECK_CLOSE( mode(dist) , static_cast<RealType>(std::floor(9 * 0.25)), tol2); // skewness: BOOST_CHECK_CLOSE( skewness(dist) , static_cast<RealType>(0.40824829046386301636621401245098L), (std::max)(tol2, static_cast<RealType>(5e-29))); // test data has 32 digits only. // kurtosis: BOOST_CHECK_CLOSE( kurtosis(dist) , static_cast<RealType>(2.916666666666666666666666666666666666L), tol2); // kurtosis excess: BOOST_CHECK_CLOSE( kurtosis_excess(dist) , static_cast<RealType>(-0.08333333333333333333333333333333333333L), tol2); // Check kurtosis_excess == kurtosis -3; BOOST_CHECK_EQUAL(kurtosis(dist), static_cast<RealType>(3) + kurtosis_excess(dist)); // special cases for PDF: BOOST_CHECK_EQUAL( pdf( binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0)), static_cast<RealType>(0)), static_cast<RealType>(1) ); BOOST_CHECK_EQUAL( pdf( binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0)), static_cast<RealType>(0.0001)), static_cast<RealType>(0) ); BOOST_CHECK_EQUAL( pdf( binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(1)), static_cast<RealType>(0.001)), static_cast<RealType>(0) ); BOOST_CHECK_EQUAL( pdf( binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(1)), static_cast<RealType>(8)), static_cast<RealType>(1) ); BOOST_CHECK_EQUAL(
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -