test_binomial.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. // test_binomial.cpp
  2. // Copyright John Maddock 2006.
  3. // Copyright Paul A. Bristow 2007.
  4. // Use, modification and distribution are subject to the
  5. // Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt
  7. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. // Basic sanity test for Binomial Cumulative Distribution Function.
  9. #define BOOST_MATH_DISCRETE_QUANTILE_POLICY real
  10. #if !defined(TEST_FLOAT) && !defined(TEST_DOUBLE) && !defined(TEST_LDOUBLE) && !defined(TEST_REAL_CONCEPT)
  11. # define TEST_FLOAT
  12. # define TEST_DOUBLE
  13. # define TEST_LDOUBLE
  14. # define TEST_REAL_CONCEPT
  15. #endif
  16. #ifdef _MSC_VER
  17. # pragma warning(disable: 4127) // conditional expression is constant.
  18. # pragma warning(disable: 4100) // unreferenced formal parameter.
  19. // Seems an entirely spurious warning - formal parameter T IS used - get error if /* T */
  20. //# pragma warning(disable: 4535) // calling _set_se_translator() requires /EHa (in Boost.test)
  21. // Enable C++ Exceptions Yes With SEH Exceptions (/EHa) prevents warning 4535.
  22. #endif
  23. #include <boost/math/tools/test.hpp>
  24. #include <boost/math/concepts/real_concept.hpp> // for real_concept
  25. using ::boost::math::concepts::real_concept;
  26. #include <boost/math/distributions/binomial.hpp> // for binomial_distribution
  27. using boost::math::binomial_distribution;
  28. #define BOOST_TEST_MAIN
  29. #include <boost/test/unit_test.hpp> // for test_main
  30. #include <boost/test/tools/floating_point_comparison.hpp> // for BOOST_CHECK_CLOSE
  31. #include "table_type.hpp"
  32. #include "test_out_of_range.hpp"
  33. #include <iostream>
  34. using std::cout;
  35. using std::endl;
  36. #include <limits>
  37. using std::numeric_limits;
  38. template <class RealType>
  39. void test_spot(
  40. RealType N, // Number of trials
  41. RealType k, // Number of successes
  42. RealType p, // Probability of success
  43. RealType P, // CDF
  44. RealType Q, // Complement of CDF
  45. RealType tol) // Test tolerance
  46. {
  47. boost::math::binomial_distribution<RealType> bn(N, p);
  48. BOOST_CHECK_CLOSE(
  49. cdf(bn, k), P, tol);
  50. if((P < 0.99) && (Q < 0.99))
  51. {
  52. //
  53. // We can only check this if P is not too close to 1,
  54. // so that we can guarantee Q is free of error:
  55. //
  56. BOOST_CHECK_CLOSE(
  57. cdf(complement(bn, k)), Q, tol);
  58. if(k != 0)
  59. {
  60. BOOST_CHECK_CLOSE(
  61. quantile(bn, P), k, tol);
  62. }
  63. else
  64. {
  65. // Just check quantile is very small:
  66. if((std::numeric_limits<RealType>::max_exponent <= std::numeric_limits<double>::max_exponent) && (boost::is_floating_point<RealType>::value))
  67. {
  68. // Limit where this is checked: if exponent range is very large we may
  69. // run out of iterations in our root finding algorithm.
  70. BOOST_CHECK(quantile(bn, P) < boost::math::tools::epsilon<RealType>() * 10);
  71. }
  72. }
  73. if(k != 0)
  74. {
  75. BOOST_CHECK_CLOSE(
  76. quantile(complement(bn, Q)), k, tol);
  77. }
  78. else
  79. {
  80. // Just check quantile is very small:
  81. if((std::numeric_limits<RealType>::max_exponent <= std::numeric_limits<double>::max_exponent) && (boost::is_floating_point<RealType>::value))
  82. {
  83. // Limit where this is checked: if exponent range is very large we may
  84. // run out of iterations in our root finding algorithm.
  85. BOOST_CHECK(quantile(complement(bn, Q)) < boost::math::tools::epsilon<RealType>() * 10);
  86. }
  87. }
  88. if(k > 0)
  89. {
  90. // estimate success ratio:
  91. // Note lower bound uses a different formual internally
  92. // from upper bound, have to adjust things to prevent
  93. // fencepost errors:
  94. BOOST_CHECK_CLOSE(
  95. binomial_distribution<RealType>::find_lower_bound_on_p(
  96. N, k+1, Q),
  97. p, tol);
  98. BOOST_CHECK_CLOSE(
  99. binomial_distribution<RealType>::find_upper_bound_on_p(
  100. N, k, P),
  101. p, tol);
  102. if(Q < P)
  103. {
  104. // Default method (Clopper Pearson)
  105. BOOST_CHECK(
  106. binomial_distribution<RealType>::find_lower_bound_on_p(
  107. N, k, Q)
  108. <=
  109. binomial_distribution<RealType>::find_upper_bound_on_p(
  110. N, k, Q)
  111. );
  112. BOOST_CHECK((
  113. binomial_distribution<RealType>::find_lower_bound_on_p(
  114. N, k, Q)
  115. <= k/N) && (k/N <=
  116. binomial_distribution<RealType>::find_upper_bound_on_p(
  117. N, k, Q))
  118. );
  119. // Bayes Method (Jeffreys Prior)
  120. BOOST_CHECK(
  121. binomial_distribution<RealType>::find_lower_bound_on_p(
  122. N, k, Q, binomial_distribution<RealType>::jeffreys_prior_interval)
  123. <=
  124. binomial_distribution<RealType>::find_upper_bound_on_p(
  125. N, k, Q, binomial_distribution<RealType>::jeffreys_prior_interval)
  126. );
  127. BOOST_CHECK((
  128. binomial_distribution<RealType>::find_lower_bound_on_p(
  129. N, k, Q, binomial_distribution<RealType>::jeffreys_prior_interval)
  130. <= k/N) && (k/N <=
  131. binomial_distribution<RealType>::find_upper_bound_on_p(
  132. N, k, Q, binomial_distribution<RealType>::jeffreys_prior_interval))
  133. );
  134. }
  135. else
  136. {
  137. // Default method (Clopper Pearson)
  138. BOOST_CHECK(
  139. binomial_distribution<RealType>::find_lower_bound_on_p(
  140. N, k, P)
  141. <=
  142. binomial_distribution<RealType>::find_upper_bound_on_p(
  143. N, k, P)
  144. );
  145. BOOST_CHECK(
  146. (binomial_distribution<RealType>::find_lower_bound_on_p(
  147. N, k, P)
  148. <= k / N) && (k/N <=
  149. binomial_distribution<RealType>::find_upper_bound_on_p(
  150. N, k, P))
  151. );
  152. // Bayes Method (Jeffreys Prior)
  153. BOOST_CHECK(
  154. binomial_distribution<RealType>::find_lower_bound_on_p(
  155. N, k, P, binomial_distribution<RealType>::jeffreys_prior_interval)
  156. <=
  157. binomial_distribution<RealType>::find_upper_bound_on_p(
  158. N, k, P, binomial_distribution<RealType>::jeffreys_prior_interval)
  159. );
  160. BOOST_CHECK(
  161. (binomial_distribution<RealType>::find_lower_bound_on_p(
  162. N, k, P, binomial_distribution<RealType>::jeffreys_prior_interval)
  163. <= k / N) && (k/N <=
  164. binomial_distribution<RealType>::find_upper_bound_on_p(
  165. N, k, P, binomial_distribution<RealType>::jeffreys_prior_interval))
  166. );
  167. }
  168. }
  169. //
  170. // estimate sample size:
  171. //
  172. BOOST_CHECK_CLOSE(
  173. binomial_distribution<RealType>::find_minimum_number_of_trials(
  174. k, p, P),
  175. N, tol);
  176. BOOST_CHECK_CLOSE(
  177. binomial_distribution<RealType>::find_maximum_number_of_trials(
  178. k, p, Q),
  179. N, tol);
  180. }
  181. // Double check consistency of CDF and PDF by computing
  182. // the finite sum:
  183. RealType sum = 0;
  184. for(unsigned i = 0; i <= k; ++i)
  185. sum += pdf(bn, RealType(i));
  186. BOOST_CHECK_CLOSE(
  187. sum, P, tol);
  188. // And complement as well:
  189. sum = 0;
  190. for(RealType i = N; i > k; i -= 1)
  191. sum += pdf(bn, i);
  192. if(P < 0.99)
  193. {
  194. BOOST_CHECK_CLOSE(
  195. sum, Q, tol);
  196. }
  197. else
  198. {
  199. // Not enough information content in P for Q to be meaningful
  200. RealType tol = (std::max)(2 * Q, boost::math::tools::epsilon<RealType>());
  201. BOOST_CHECK(sum < tol);
  202. }
  203. }
  204. template <class RealType> // Any floating-point type RealType.
  205. void test_spots(RealType T)
  206. {
  207. // Basic sanity checks, test data is to double precision only
  208. // so set tolerance to 100eps expressed as a persent, or
  209. // 100eps of type double expressed as a persent, whichever
  210. // is the larger.
  211. RealType tolerance = (std::max)
  212. (boost::math::tools::epsilon<RealType>(),
  213. static_cast<RealType>(std::numeric_limits<double>::epsilon()));
  214. tolerance *= 100 * 1000;
  215. RealType tol2 = boost::math::tools::epsilon<RealType>() * 5 * 100; // 5 eps as a persent
  216. cout << "Tolerance for type " << typeid(T).name() << " is " << tolerance << " %" << endl;
  217. // Sources of spot test values:
  218. // MathCAD defines pbinom(k, n, p)
  219. // returns pr(X ,=k) when random variable X has the binomial distribution with parameters n and p.
  220. // 0 <= k ,= n
  221. // 0 <= p <= 1
  222. // P = pbinom(30, 500, 0.05) = 0.869147702104609
  223. using boost::math::binomial_distribution;
  224. using ::boost::math::cdf;
  225. using ::boost::math::pdf;
  226. #if !defined(TEST_ROUNDING) || (TEST_ROUNDING == 0)
  227. // Test binomial using cdf spot values from MathCAD.
  228. // These test quantiles and complements as well.
  229. test_spot(
  230. static_cast<RealType>(500), // Sample size, N
  231. static_cast<RealType>(30), // Number of successes, k
  232. static_cast<RealType>(0.05), // Probability of success, p
  233. static_cast<RealType>(0.869147702104609), // Probability of result (CDF), P
  234. static_cast<RealType>(1 - 0.869147702104609), // Q = 1 - P
  235. tolerance);
  236. test_spot(
  237. static_cast<RealType>(500), // Sample size, N
  238. static_cast<RealType>(250), // Number of successes, k
  239. static_cast<RealType>(0.05), // Probability of success, p
  240. static_cast<RealType>(1), // Probability of result (CDF), P
  241. static_cast<RealType>(0), // Q = 1 - P
  242. tolerance);
  243. test_spot(
  244. static_cast<RealType>(500), // Sample size, N
  245. static_cast<RealType>(470), // Number of successes, k
  246. static_cast<RealType>(0.95), // Probability of success, p
  247. static_cast<RealType>(0.176470742656766), // Probability of result (CDF), P
  248. static_cast<RealType>(1 - 0.176470742656766), // Q = 1 - P
  249. tolerance * 10); // Note higher tolerance on this test!
  250. test_spot(
  251. static_cast<RealType>(500), // Sample size, N
  252. static_cast<RealType>(400), // Number of successes, k
  253. static_cast<RealType>(0.05), // Probability of success, p
  254. static_cast<RealType>(1), // Probability of result (CDF), P
  255. static_cast<RealType>(0), // Q = 1 - P
  256. tolerance);
  257. test_spot(
  258. static_cast<RealType>(500), // Sample size, N
  259. static_cast<RealType>(400), // Number of successes, k
  260. static_cast<RealType>(0.9), // Probability of success, p
  261. static_cast<RealType>(1.80180425681923E-11), // Probability of result (CDF), P
  262. static_cast<RealType>(1 - 1.80180425681923E-11), // Q = 1 - P
  263. tolerance);
  264. test_spot(
  265. static_cast<RealType>(500), // Sample size, N
  266. static_cast<RealType>(5), // Number of successes, k
  267. static_cast<RealType>(0.05), // Probability of success, p
  268. static_cast<RealType>(9.181808267643E-7), // Probability of result (CDF), P
  269. static_cast<RealType>(1 - 9.181808267643E-7), // Q = 1 - P
  270. tolerance);
  271. test_spot(
  272. static_cast<RealType>(2), // Sample size, N
  273. static_cast<RealType>(1), // Number of successes, k
  274. static_cast<RealType>(0.5), // Probability of success, p
  275. static_cast<RealType>(0.75), // Probability of result (CDF), P
  276. static_cast<RealType>(0.25), // Q = 1 - P
  277. tolerance);
  278. test_spot(
  279. static_cast<RealType>(8), // Sample size, N
  280. static_cast<RealType>(3), // Number of successes, k
  281. static_cast<RealType>(0.25), // Probability of success, p
  282. static_cast<RealType>(0.8861846923828125), // Probability of result (CDF), P
  283. static_cast<RealType>(1 - 0.8861846923828125), // Q = 1 - P
  284. tolerance);
  285. test_spot(
  286. static_cast<RealType>(8), // Sample size, N
  287. static_cast<RealType>(0), // Number of successes, k
  288. static_cast<RealType>(0.25), // Probability of success, p
  289. static_cast<RealType>(0.1001129150390625), // Probability of result (CDF), P
  290. static_cast<RealType>(1 - 0.1001129150390625), // Q = 1 - P
  291. tolerance);
  292. test_spot(
  293. static_cast<RealType>(8), // Sample size, N
  294. static_cast<RealType>(1), // Number of successes, k
  295. static_cast<RealType>(0.25), // Probability of success, p
  296. static_cast<RealType>(0.36708068847656244), // Probability of result (CDF), P
  297. static_cast<RealType>(1 - 0.36708068847656244), // Q = 1 - P
  298. tolerance);
  299. test_spot(
  300. static_cast<RealType>(8), // Sample size, N
  301. static_cast<RealType>(4), // Number of successes, k
  302. static_cast<RealType>(0.25), // Probability of success, p
  303. static_cast<RealType>(0.9727020263671875), // Probability of result (CDF), P
  304. static_cast<RealType>(1 - 0.9727020263671875), // Q = 1 - P
  305. tolerance);
  306. test_spot(
  307. static_cast<RealType>(8), // Sample size, N
  308. static_cast<RealType>(7), // Number of successes, k
  309. static_cast<RealType>(0.25), // Probability of success, p
  310. static_cast<RealType>(0.9999847412109375), // Probability of result (CDF), P
  311. static_cast<RealType>(1 - 0.9999847412109375), // Q = 1 - P
  312. tolerance);
  313. // Tests on PDF follow:
  314. BOOST_CHECK_CLOSE(
  315. pdf(binomial_distribution<RealType>(static_cast<RealType>(20), static_cast<RealType>(0.75)),
  316. static_cast<RealType>(10)), // k.
  317. static_cast<RealType>(0.00992227527967770583927631378173), // 0.00992227527967770583927631378173
  318. tolerance);
  319. BOOST_CHECK_CLOSE(
  320. pdf(binomial_distribution<RealType>(static_cast<RealType>(20), static_cast<RealType>(0.5)),
  321. static_cast<RealType>(10)), // k.
  322. static_cast<RealType>(0.17619705200195312500000000000000000000), // get k=10 0.049611376398388612 p = 0.25
  323. tolerance);
  324. // Binomial pdf Test values from
  325. // http://www.adsciengineering.com/bpdcalc/index.php for example
  326. // http://www.adsciengineering.com/bpdcalc/index.php?n=20&p=0.25&start=0&stop=20&Submit=Generate
  327. // Appears to use at least 80-bit long double for 32 decimal digits accuracy,
  328. // but loses accuracy of display if leading zeros?
  329. // (if trailings zero then are exact values?)
  330. // so useful for testing 64-bit double accuracy.
  331. // P = 0.25, n = 20, k = 0 to 20
  332. //0 C(20,0) * 0.25^0 * 0.75^20 0.00317121193893399322405457496643
  333. //1 C(20,1) * 0.25^1 * 0.75^19 0.02114141292622662149369716644287
  334. //2 C(20,2) * 0.25^2 * 0.75^18 0.06694780759971763473004102706909
  335. //3 C(20,3) * 0.25^3 * 0.75^17 0.13389561519943526946008205413818
  336. //4 C(20,4) * 0.25^4 * 0.75^16 0.18968545486586663173511624336242
  337. //5 C(20,5) * 0.25^5 * 0.75^15 0.20233115185692440718412399291992
  338. //6 C(20,6) * 0.25^6 * 0.75^14 0.16860929321410367265343666076660
  339. //7 C(20,7) * 0.25^7 * 0.75^13 0.11240619547606911510229110717773
  340. //8 C(20,8) * 0.25^8 * 0.75^12 0.06088668921620410401374101638793
  341. //9 C(20,9) * 0.25^9 * 0.75^11 0.02706075076275737956166267395019
  342. //10 C(20,10) * 0.25^10 * 0.75^10 0.00992227527967770583927631378173
  343. //11 C(20,11) * 0.25^11 * 0.75^9 0.00300675008475081995129585266113
  344. //12 C(20,12) * 0.25^12 * 0.75^8 0.00075168752118770498782396316528
  345. //13 C(20,13) * 0.25^13 * 0.75^7 0.00015419231203850358724594116210
  346. //14 C(20,14) * 0.25^14 * 0.75^6 0.00002569871867308393120765686035
  347. //15 C(20,15) * 0.25^15 * 0.75^5 0.00000342649582307785749435424804
  348. //16 C(20,16) * 0.25^16 * 0.75^4 0.00000035692664823727682232856750
  349. //17 C(20,17) * 0.25^17 * 0.75^3 0.00000002799424692057073116302490
  350. //18 C(20,18) * 0.25^18 * 0.75^2 0.00000000155523594003170728683471
  351. //19 C(20,19) * 0.25^19 * 0.75^1 0.00000000005456968210637569427490
  352. //20 C(20,20) * 0.25^20 * 0.75^0 0.00000000000090949470177292823791
  353. BOOST_CHECK_CLOSE(
  354. pdf(binomial_distribution<RealType>(static_cast<RealType>(20), static_cast<RealType>(0.25)),
  355. static_cast<RealType>(10)), // k.
  356. static_cast<RealType>(0.00992227527967770583927631378173), // k=10 p = 0.25
  357. tolerance);
  358. BOOST_CHECK_CLOSE( // k = 0 use different formula - only exp so more accurate.
  359. pdf(binomial_distribution<RealType>(static_cast<RealType>(20), static_cast<RealType>(0.25)),
  360. static_cast<RealType>(0)), // k.
  361. static_cast<RealType>(0.00317121193893399322405457496643), // k=0 p = 0.25
  362. tolerance);
  363. BOOST_CHECK_CLOSE( // k = 20 use different formula - only exp so more accurate.
  364. pdf(binomial_distribution<RealType>(static_cast<RealType>(20), static_cast<RealType>(0.25)),
  365. static_cast<RealType>(20)), // k == n.
  366. static_cast<RealType>(0.00000000000090949470177292823791), // k=20 p = 0.25
  367. tolerance);
  368. BOOST_CHECK_CLOSE( // k = 1.
  369. pdf(binomial_distribution<RealType>(static_cast<RealType>(20), static_cast<RealType>(0.25)),
  370. static_cast<RealType>(1)), // k.
  371. static_cast<RealType>(0.02114141292622662149369716644287), // k=1 p = 0.25
  372. tolerance);
  373. // Some exact (probably) values.
  374. BOOST_CHECK_CLOSE(
  375. pdf(binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  376. static_cast<RealType>(0)), // k.
  377. static_cast<RealType>(0.10011291503906250000000000000000), // k=0 p = 0.25
  378. tolerance);
  379. BOOST_CHECK_CLOSE( // k = 1.
  380. pdf(binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  381. static_cast<RealType>(1)), // k.
  382. static_cast<RealType>(0.26696777343750000000000000000000), // k=1 p = 0.25
  383. tolerance);
  384. BOOST_CHECK_CLOSE( // k = 2.
  385. pdf(binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  386. static_cast<RealType>(2)), // k.
  387. static_cast<RealType>(0.31146240234375000000000000000000), // k=2 p = 0.25
  388. tolerance);
  389. BOOST_CHECK_CLOSE( // k = 3.
  390. pdf(binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  391. static_cast<RealType>(3)), // k.
  392. static_cast<RealType>(0.20764160156250000000000000000000), // k=3 p = 0.25
  393. tolerance);
  394. BOOST_CHECK_CLOSE( // k = 7.
  395. pdf(binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  396. static_cast<RealType>(7)), // k.
  397. static_cast<RealType>(0.00036621093750000000000000000000), // k=7 p = 0.25
  398. tolerance);
  399. BOOST_CHECK_CLOSE( // k = 8.
  400. pdf(binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  401. static_cast<RealType>(8)), // k = n.
  402. static_cast<RealType>(0.00001525878906250000000000000000), // k=8 p = 0.25
  403. tolerance);
  404. binomial_distribution<RealType> dist(static_cast<RealType>(8), static_cast<RealType>(0.25));
  405. RealType x = static_cast<RealType>(0.125);
  406. using namespace std; // ADL of std names.
  407. // mean:
  408. BOOST_CHECK_CLOSE(
  409. mean(dist)
  410. , static_cast<RealType>(8 * 0.25), tol2);
  411. // variance:
  412. BOOST_CHECK_CLOSE(
  413. variance(dist)
  414. , static_cast<RealType>(8 * 0.25 * 0.75), tol2);
  415. // std deviation:
  416. BOOST_CHECK_CLOSE(
  417. standard_deviation(dist)
  418. , static_cast<RealType>(sqrt(8 * 0.25L * 0.75L)), tol2);
  419. // hazard:
  420. BOOST_CHECK_CLOSE(
  421. hazard(dist, x)
  422. , pdf(dist, x) / cdf(complement(dist, x)), tol2);
  423. // cumulative hazard:
  424. BOOST_CHECK_CLOSE(
  425. chf(dist, x)
  426. , -log(cdf(complement(dist, x))), tol2);
  427. // coefficient_of_variation:
  428. BOOST_CHECK_CLOSE(
  429. coefficient_of_variation(dist)
  430. , standard_deviation(dist) / mean(dist), tol2);
  431. // mode:
  432. BOOST_CHECK_CLOSE(
  433. mode(dist)
  434. , static_cast<RealType>(std::floor(9 * 0.25)), tol2);
  435. // skewness:
  436. BOOST_CHECK_CLOSE(
  437. skewness(dist)
  438. , static_cast<RealType>(0.40824829046386301636621401245098L), (std::max)(tol2, static_cast<RealType>(5e-29))); // test data has 32 digits only.
  439. // kurtosis:
  440. BOOST_CHECK_CLOSE(
  441. kurtosis(dist)
  442. , static_cast<RealType>(2.916666666666666666666666666666666666L), tol2);
  443. // kurtosis excess:
  444. BOOST_CHECK_CLOSE(
  445. kurtosis_excess(dist)
  446. , static_cast<RealType>(-0.08333333333333333333333333333333333333L), tol2);
  447. // Check kurtosis_excess == kurtosis -3;
  448. BOOST_CHECK_EQUAL(kurtosis(dist), static_cast<RealType>(3) + kurtosis_excess(dist));
  449. // special cases for PDF:
  450. BOOST_CHECK_EQUAL(
  451. pdf(
  452. binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0)),
  453. static_cast<RealType>(0)), static_cast<RealType>(1)
  454. );
  455. BOOST_CHECK_EQUAL(
  456. pdf(
  457. binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0)),
  458. static_cast<RealType>(0.0001)), static_cast<RealType>(0)
  459. );
  460. BOOST_CHECK_EQUAL(
  461. pdf(
  462. binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(1)),
  463. static_cast<RealType>(0.001)), static_cast<RealType>(0)
  464. );
  465. BOOST_CHECK_EQUAL(
  466. pdf(
  467. binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(1)),
  468. static_cast<RealType>(8)), static_cast<RealType>(1)
  469. );
  470. BOOST_CHECK_EQUAL(
  471. pdf(
  472. binomial_distribution<RealType>(static_cast<RealType>(0), static_cast<RealType>(0.25)),
  473. static_cast<RealType>(0)), static_cast<RealType>(1)
  474. );
  475. BOOST_MATH_CHECK_THROW(
  476. pdf(
  477. binomial_distribution<RealType>(static_cast<RealType>(-1), static_cast<RealType>(0.25)),
  478. static_cast<RealType>(0)), std::domain_error
  479. );
  480. BOOST_MATH_CHECK_THROW(
  481. pdf(
  482. binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(-0.25)),
  483. static_cast<RealType>(0)), std::domain_error
  484. );
  485. BOOST_MATH_CHECK_THROW(
  486. pdf(
  487. binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(1.25)),
  488. static_cast<RealType>(0)), std::domain_error
  489. );
  490. BOOST_MATH_CHECK_THROW(
  491. pdf(
  492. binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  493. static_cast<RealType>(-1)), std::domain_error
  494. );
  495. BOOST_MATH_CHECK_THROW(
  496. pdf(
  497. binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  498. static_cast<RealType>(9)), std::domain_error
  499. );
  500. BOOST_MATH_CHECK_THROW(
  501. cdf(
  502. binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  503. static_cast<RealType>(-1)), std::domain_error
  504. );
  505. BOOST_MATH_CHECK_THROW(
  506. cdf(
  507. binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  508. static_cast<RealType>(9)), std::domain_error
  509. );
  510. BOOST_MATH_CHECK_THROW(
  511. cdf(
  512. binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(-0.25)),
  513. static_cast<RealType>(0)), std::domain_error
  514. );
  515. BOOST_MATH_CHECK_THROW(
  516. cdf(
  517. binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(1.25)),
  518. static_cast<RealType>(0)), std::domain_error
  519. );
  520. BOOST_MATH_CHECK_THROW(
  521. quantile(
  522. binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(-0.25)),
  523. static_cast<RealType>(0)), std::domain_error
  524. );
  525. BOOST_MATH_CHECK_THROW(
  526. quantile(
  527. binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(1.25)),
  528. static_cast<RealType>(0)), std::domain_error
  529. );
  530. BOOST_CHECK_EQUAL(
  531. quantile(
  532. binomial_distribution<RealType>(static_cast<RealType>(16), static_cast<RealType>(0.25)),
  533. static_cast<RealType>(0.01)), // Less than cdf == pdf(binomial_distribution<RealType>(16, 0.25), 0)
  534. static_cast<RealType>(0) // so expect zero as best approximation.
  535. );
  536. BOOST_CHECK_EQUAL(
  537. cdf(
  538. binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  539. static_cast<RealType>(8)), static_cast<RealType>(1)
  540. );
  541. BOOST_CHECK_EQUAL(
  542. cdf(
  543. binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0)),
  544. static_cast<RealType>(7)), static_cast<RealType>(1)
  545. );
  546. BOOST_CHECK_EQUAL(
  547. cdf(
  548. binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(1)),
  549. static_cast<RealType>(7)), static_cast<RealType>(0)
  550. );
  551. #endif
  552. {
  553. // This is a visual sanity check that everything is OK:
  554. binomial_distribution<RealType> my8dist(8., 0.25); // Note: double values (matching the distribution definition) avoid the need for any casting.
  555. //cout << "mean(my8dist) = " << boost::math::mean(my8dist) << endl; // mean(my8dist) = 2
  556. //cout << "my8dist.trials() = " << my8dist.trials() << endl; // my8dist.trials() = 8
  557. //cout << "my8dist.success_fraction() = " << my8dist.success_fraction() << endl; // my8dist.success_fraction() = 0.25
  558. BOOST_CHECK_CLOSE(my8dist.trials(), static_cast<RealType>(8), tol2);
  559. BOOST_CHECK_CLOSE(my8dist.success_fraction(), static_cast<RealType>(0.25), tol2);
  560. //{
  561. // int n = static_cast<int>(boost::math::tools::real_cast<double>(my8dist.trials()));
  562. // RealType sumcdf = 0.;
  563. // for (int k = 0; k <= n; k++)
  564. // {
  565. // cout << k << ' ' << pdf(my8dist, static_cast<RealType>(k));
  566. // sumcdf += pdf(my8dist, static_cast<RealType>(k));
  567. // cout << ' ' << sumcdf;
  568. // cout << ' ' << cdf(my8dist, static_cast<RealType>(k));
  569. // cout << ' ' << sumcdf - cdf(my8dist, static_cast<RealType>(k)) << endl;
  570. // } // for k
  571. // }
  572. // n = 8, p =0.25
  573. //k pdf cdf
  574. //0 0.1001129150390625 0.1001129150390625
  575. //1 0.26696777343749994 0.36708068847656244
  576. //2 0.31146240234375017 0.67854309082031261
  577. //3 0.20764160156249989 0.8861846923828125
  578. //4 0.086517333984375 0.9727020263671875
  579. //5 0.023071289062499997 0.9957733154296875
  580. //6 0.0038452148437500009 0.9996185302734375
  581. //7 0.00036621093749999984 0.9999847412109375
  582. //8 1.52587890625e-005 1 1 0
  583. }
  584. #define T RealType
  585. #include "binomial_quantile.ipp"
  586. for(unsigned i = 0; i < binomial_quantile_data.size(); ++i)
  587. {
  588. using namespace boost::math::policies;
  589. RealType tol = boost::math::tools::epsilon<RealType>() * 500;
  590. if(!boost::is_floating_point<RealType>::value)
  591. tol *= 10; // no lanczos approximation implies less accuracy
  592. RealType x;
  593. #if !defined(TEST_ROUNDING) || (TEST_ROUNDING == 1)
  594. //
  595. // Check full real value first:
  596. //
  597. typedef policy<discrete_quantile<boost::math::policies::real> > P1;
  598. binomial_distribution<RealType, P1> p1(binomial_quantile_data[i][0], binomial_quantile_data[i][1]);
  599. x = quantile(p1, binomial_quantile_data[i][2]);
  600. BOOST_CHECK_CLOSE_FRACTION(x, (RealType)binomial_quantile_data[i][3], tol);
  601. x = quantile(complement(p1, (RealType)binomial_quantile_data[i][2]));
  602. BOOST_CHECK_CLOSE_FRACTION(x, (RealType)binomial_quantile_data[i][4], tol);
  603. #endif
  604. #if !defined(TEST_ROUNDING) || (TEST_ROUNDING == 2)
  605. //
  606. // Now with round down to integer:
  607. //
  608. typedef policy<discrete_quantile<integer_round_down> > P2;
  609. binomial_distribution<RealType, P2> p2(binomial_quantile_data[i][0], binomial_quantile_data[i][1]);
  610. x = quantile(p2, binomial_quantile_data[i][2]);
  611. BOOST_CHECK_EQUAL(x, (RealType)floor(binomial_quantile_data[i][3]));
  612. x = quantile(complement(p2, binomial_quantile_data[i][2]));
  613. BOOST_CHECK_EQUAL(x, (RealType)floor(binomial_quantile_data[i][4]));
  614. #endif
  615. #if !defined(TEST_ROUNDING) || (TEST_ROUNDING == 3)
  616. //
  617. // Now with round up to integer:
  618. //
  619. typedef policy<discrete_quantile<integer_round_up> > P3;
  620. binomial_distribution<RealType, P3> p3(binomial_quantile_data[i][0], binomial_quantile_data[i][1]);
  621. x = quantile(p3, binomial_quantile_data[i][2]);
  622. BOOST_CHECK_EQUAL(x, (RealType)ceil(binomial_quantile_data[i][3]));
  623. x = quantile(complement(p3, binomial_quantile_data[i][2]));
  624. BOOST_CHECK_EQUAL(x, (RealType)ceil(binomial_quantile_data[i][4]));
  625. #endif
  626. #if !defined(TEST_ROUNDING) || (TEST_ROUNDING == 4)
  627. //
  628. // Now with round to integer "outside":
  629. //
  630. typedef policy<discrete_quantile<integer_round_outwards> > P4;
  631. binomial_distribution<RealType, P4> p4(binomial_quantile_data[i][0], binomial_quantile_data[i][1]);
  632. x = quantile(p4, binomial_quantile_data[i][2]);
  633. BOOST_CHECK_EQUAL(x, (RealType)(binomial_quantile_data[i][2] < 0.5f ? floor(binomial_quantile_data[i][3]) : ceil(binomial_quantile_data[i][3])));
  634. x = quantile(complement(p4, binomial_quantile_data[i][2]));
  635. BOOST_CHECK_EQUAL(x, (RealType)(binomial_quantile_data[i][2] < 0.5f ? ceil(binomial_quantile_data[i][4]) : floor(binomial_quantile_data[i][4])));
  636. #endif
  637. #if !defined(TEST_ROUNDING) || (TEST_ROUNDING == 5)
  638. //
  639. // Now with round to integer "inside":
  640. //
  641. typedef policy<discrete_quantile<integer_round_inwards> > P5;
  642. binomial_distribution<RealType, P5> p5(binomial_quantile_data[i][0], binomial_quantile_data[i][1]);
  643. x = quantile(p5, binomial_quantile_data[i][2]);
  644. BOOST_CHECK_EQUAL(x, (RealType)(binomial_quantile_data[i][2] < 0.5f ? ceil(binomial_quantile_data[i][3]) : floor(binomial_quantile_data[i][3])));
  645. x = quantile(complement(p5, binomial_quantile_data[i][2]));
  646. BOOST_CHECK_EQUAL(x, (RealType)(binomial_quantile_data[i][2] < 0.5f ? floor(binomial_quantile_data[i][4]) : ceil(binomial_quantile_data[i][4])));
  647. #endif
  648. #if !defined(TEST_ROUNDING) || (TEST_ROUNDING == 6)
  649. //
  650. // Now with round to nearest integer:
  651. //
  652. typedef policy<discrete_quantile<integer_round_nearest> > P6;
  653. binomial_distribution<RealType, P6> p6(binomial_quantile_data[i][0], binomial_quantile_data[i][1]);
  654. x = quantile(p6, binomial_quantile_data[i][2]);
  655. BOOST_CHECK_EQUAL(x, (RealType)(floor(binomial_quantile_data[i][3] + 0.5f)));
  656. x = quantile(complement(p6, binomial_quantile_data[i][2]));
  657. BOOST_CHECK_EQUAL(x, (RealType)(floor(binomial_quantile_data[i][4] + 0.5f)));
  658. #endif
  659. }
  660. check_out_of_range<boost::math::binomial_distribution<RealType> >(1, 1); // (All) valid constructor parameter values.
  661. } // template <class RealType>void test_spots(RealType)
  662. BOOST_AUTO_TEST_CASE( test_main )
  663. {
  664. BOOST_MATH_CONTROL_FP;
  665. // Check that can generate binomial distribution using one convenience methods:
  666. binomial_distribution<> mybn2(1., 0.5); // Using default RealType double.
  667. // but that
  668. // boost::math::binomial mybn1(1., 0.5); // Using typedef fails
  669. // error C2039: 'binomial' : is not a member of 'boost::math'
  670. // Basic sanity-check spot values.
  671. // (Parameter value, arbitrarily zero, only communicates the floating point type).
  672. #ifdef TEST_FLOAT
  673. test_spots(0.0F); // Test float.
  674. #endif
  675. #ifdef TEST_DOUBLE
  676. test_spots(0.0); // Test double.
  677. #endif
  678. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  679. #ifdef TEST_LDOUBLE
  680. test_spots(0.0L); // Test long double.
  681. #endif
  682. #if !defined(BOOST_MATH_NO_REAL_CONCEPT_TESTS)
  683. #ifdef TEST_REAL_CONCEPT
  684. test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
  685. #endif
  686. #endif
  687. #else
  688. std::cout << "<note>The long double tests have been disabled on this platform "
  689. "either because the long double overloads of the usual math functions are "
  690. "not available at all, or because they are too inaccurate for these tests "
  691. "to pass.</note>" << std::endl;
  692. #endif
  693. } // BOOST_AUTO_TEST_CASE( test_main )
  694. /*
  695. Output is:
  696. Description: Autorun "J:\Cpp\MathToolkit\test\Math_test\Debug\test_binomial.exe"
  697. Running 1 test case...
  698. Tolerance for type float is 0.0119209 %
  699. Tolerance for type double is 2.22045e-011 %
  700. Tolerance for type long double is 2.22045e-011 %
  701. Tolerance for type class boost::math::concepts::real_concept is 2.22045e-011 %
  702. *** No errors detected
  703. ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
  704. */