test_negative_binomial.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. // test_negative_binomial.cpp
  2. // Copyright Paul A. Bristow 2007.
  3. // Copyright John Maddock 2006.
  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. // Tests for Negative Binomial Distribution.
  9. // Note that these defines must be placed BEFORE #includes.
  10. #define BOOST_MATH_OVERFLOW_ERROR_POLICY ignore_error
  11. // because several tests overflow & underflow by design.
  12. #define BOOST_MATH_DISCRETE_QUANTILE_POLICY real
  13. #ifdef _MSC_VER
  14. # pragma warning(disable: 4127) // conditional expression is constant.
  15. #endif
  16. #if !defined(TEST_FLOAT) && !defined(TEST_DOUBLE) && !defined(TEST_LDOUBLE) && !defined(TEST_REAL_CONCEPT)
  17. # define TEST_FLOAT
  18. # define TEST_DOUBLE
  19. # define TEST_LDOUBLE
  20. # define TEST_REAL_CONCEPT
  21. #endif
  22. #include <boost/math/tools/test.hpp> // for real_concept
  23. #include <boost/math/concepts/real_concept.hpp> // for real_concept
  24. using ::boost::math::concepts::real_concept;
  25. #include <boost/math/distributions/negative_binomial.hpp> // for negative_binomial_distribution
  26. using boost::math::negative_binomial_distribution;
  27. #include <boost/math/special_functions/gamma.hpp>
  28. using boost::math::lgamma; // log gamma
  29. #define BOOST_TEST_MAIN
  30. #include <boost/test/unit_test.hpp> // for test_main
  31. #include <boost/test/tools/floating_point_comparison.hpp> // for BOOST_CHECK_CLOSE
  32. #include "table_type.hpp"
  33. #include "test_out_of_range.hpp"
  34. #include <iostream>
  35. using std::cout;
  36. using std::endl;
  37. using std::setprecision;
  38. using std::showpoint;
  39. #include <limits>
  40. using std::numeric_limits;
  41. template <class RealType>
  42. void test_spot( // Test a single spot value against 'known good' values.
  43. RealType N, // Number of successes.
  44. RealType k, // Number of failures.
  45. RealType p, // Probability of success_fraction.
  46. RealType P, // CDF probability.
  47. RealType Q, // Complement of CDF.
  48. RealType tol) // Test tolerance.
  49. {
  50. boost::math::negative_binomial_distribution<RealType> bn(N, p);
  51. BOOST_CHECK_EQUAL(N, bn.successes());
  52. BOOST_CHECK_EQUAL(p, bn.success_fraction());
  53. BOOST_CHECK_CLOSE(
  54. cdf(bn, k), P, tol);
  55. if((P < 0.99) && (Q < 0.99))
  56. {
  57. // We can only check this if P is not too close to 1,
  58. // so that we can guarantee that Q is free of error:
  59. //
  60. BOOST_CHECK_CLOSE(
  61. cdf(complement(bn, k)), Q, tol);
  62. if(k != 0)
  63. {
  64. BOOST_CHECK_CLOSE(
  65. quantile(bn, P), k, tol);
  66. }
  67. else
  68. {
  69. // Just check quantile is very small:
  70. if((std::numeric_limits<RealType>::max_exponent <= std::numeric_limits<double>::max_exponent)
  71. && (boost::is_floating_point<RealType>::value))
  72. {
  73. // Limit where this is checked: if exponent range is very large we may
  74. // run out of iterations in our root finding algorithm.
  75. BOOST_CHECK(quantile(bn, P) < boost::math::tools::epsilon<RealType>() * 10);
  76. }
  77. }
  78. if(k != 0)
  79. {
  80. BOOST_CHECK_CLOSE(
  81. quantile(complement(bn, Q)), k, tol);
  82. }
  83. else
  84. {
  85. // Just check quantile is very small:
  86. if((std::numeric_limits<RealType>::max_exponent <= std::numeric_limits<double>::max_exponent)
  87. && (boost::is_floating_point<RealType>::value))
  88. {
  89. // Limit where this is checked: if exponent range is very large we may
  90. // run out of iterations in our root finding algorithm.
  91. BOOST_CHECK(quantile(complement(bn, Q)) < boost::math::tools::epsilon<RealType>() * 10);
  92. }
  93. }
  94. // estimate success ratio:
  95. BOOST_CHECK_CLOSE(
  96. negative_binomial_distribution<RealType>::find_lower_bound_on_p(
  97. N+k, N, P),
  98. p, tol);
  99. // Note we bump up the sample size here, purely for the sake of the test,
  100. // internally the function has to adjust the sample size so that we get
  101. // the right upper bound, our test undoes this, so we can verify the result.
  102. BOOST_CHECK_CLOSE(
  103. negative_binomial_distribution<RealType>::find_upper_bound_on_p(
  104. N+k+1, N, Q),
  105. p, tol);
  106. if(Q < P)
  107. {
  108. //
  109. // We check two things here, that the upper and lower bounds
  110. // are the right way around, and that they do actually bracket
  111. // the naive estimate of p = successes / (sample size)
  112. //
  113. BOOST_CHECK(
  114. negative_binomial_distribution<RealType>::find_lower_bound_on_p(
  115. N+k, N, Q)
  116. <=
  117. negative_binomial_distribution<RealType>::find_upper_bound_on_p(
  118. N+k, N, Q)
  119. );
  120. BOOST_CHECK(
  121. negative_binomial_distribution<RealType>::find_lower_bound_on_p(
  122. N+k, N, Q)
  123. <=
  124. N / (N+k)
  125. );
  126. BOOST_CHECK(
  127. N / (N+k)
  128. <=
  129. negative_binomial_distribution<RealType>::find_upper_bound_on_p(
  130. N+k, N, Q)
  131. );
  132. }
  133. else
  134. {
  135. // As above but when P is small.
  136. BOOST_CHECK(
  137. negative_binomial_distribution<RealType>::find_lower_bound_on_p(
  138. N+k, N, P)
  139. <=
  140. negative_binomial_distribution<RealType>::find_upper_bound_on_p(
  141. N+k, N, P)
  142. );
  143. BOOST_CHECK(
  144. negative_binomial_distribution<RealType>::find_lower_bound_on_p(
  145. N+k, N, P)
  146. <=
  147. N / (N+k)
  148. );
  149. BOOST_CHECK(
  150. N / (N+k)
  151. <=
  152. negative_binomial_distribution<RealType>::find_upper_bound_on_p(
  153. N+k, N, P)
  154. );
  155. }
  156. // Estimate sample size:
  157. BOOST_CHECK_CLOSE(
  158. negative_binomial_distribution<RealType>::find_minimum_number_of_trials(
  159. k, p, P),
  160. N+k, tol);
  161. BOOST_CHECK_CLOSE(
  162. negative_binomial_distribution<RealType>::find_maximum_number_of_trials(
  163. k, p, Q),
  164. N+k, tol);
  165. // Double check consistency of CDF and PDF by computing the finite sum:
  166. RealType sum = 0;
  167. for(unsigned i = 0; i <= k; ++i)
  168. {
  169. sum += pdf(bn, RealType(i));
  170. }
  171. BOOST_CHECK_CLOSE(sum, P, tol);
  172. // Complement is not possible since sum is to infinity.
  173. } //
  174. } // test_spot
  175. template <class RealType> // Any floating-point type RealType.
  176. void test_spots(RealType)
  177. {
  178. // Basic sanity checks, test data is to double precision only
  179. // so set tolerance to 1000 eps expressed as a percent, or
  180. // 1000 eps of type double expressed as a percent, whichever
  181. // is the larger.
  182. RealType tolerance = (std::max)
  183. (boost::math::tools::epsilon<RealType>(),
  184. static_cast<RealType>(std::numeric_limits<double>::epsilon()));
  185. tolerance *= 100 * 100000.0f;
  186. cout << "Tolerance = " << tolerance << "%." << endl;
  187. RealType tol1eps = boost::math::tools::epsilon<RealType>() * 2; // Very tight, suit exact values.
  188. //RealType tol2eps = boost::math::tools::epsilon<RealType>() * 2; // Tight, suit exact values.
  189. RealType tol5eps = boost::math::tools::epsilon<RealType>() * 5; // Wider 5 epsilon.
  190. cout << "Tolerance 5 eps = " << tol5eps << "%." << endl;
  191. // Sources of spot test values:
  192. // MathCAD defines pbinom(k, r, p) (at about 64-bit double precision, about 16 decimal digits)
  193. // returns pr(X , k) when random variable X has the binomial distribution with parameters r and p.
  194. // 0 <= k
  195. // r > 0
  196. // 0 <= p <= 1
  197. // P = pbinom(30, 500, 0.05) = 0.869147702104609
  198. // And functions.wolfram.com
  199. using boost::math::negative_binomial_distribution;
  200. using ::boost::math::negative_binomial;
  201. using ::boost::math::cdf;
  202. using ::boost::math::pdf;
  203. // Test negative binomial using cdf spot values from MathCAD cdf = pnbinom(k, r, p).
  204. // These test quantiles and complements as well.
  205. test_spot( // pnbinom(1,2,0.5) = 0.5
  206. static_cast<RealType>(2), // successes r
  207. static_cast<RealType>(1), // Number of failures, k
  208. static_cast<RealType>(0.5), // Probability of success as fraction, p
  209. static_cast<RealType>(0.5), // Probability of result (CDF), P
  210. static_cast<RealType>(0.5), // complement CCDF Q = 1 - P
  211. tolerance);
  212. test_spot( // pbinom(0, 2, 0.25)
  213. static_cast<RealType>(2), // successes r
  214. static_cast<RealType>(0), // Number of failures, k
  215. static_cast<RealType>(0.25),
  216. static_cast<RealType>(0.0625), // Probability of result (CDF), P
  217. static_cast<RealType>(0.9375), // Q = 1 - P
  218. tolerance);
  219. test_spot( // pbinom(48,8,0.25)
  220. static_cast<RealType>(8), // successes r
  221. static_cast<RealType>(48), // Number of failures, k
  222. static_cast<RealType>(0.25), // Probability of success, p
  223. static_cast<RealType>(9.826582228110670E-1), // Probability of result (CDF), P
  224. static_cast<RealType>(1 - 9.826582228110670E-1), // Q = 1 - P
  225. tolerance);
  226. test_spot( // pbinom(2,5,0.4)
  227. static_cast<RealType>(5), // successes r
  228. static_cast<RealType>(2), // Number of failures, k
  229. static_cast<RealType>(0.4), // Probability of success, p
  230. static_cast<RealType>(9.625600000000020E-2), // Probability of result (CDF), P
  231. static_cast<RealType>(1 - 9.625600000000020E-2), // Q = 1 - P
  232. tolerance);
  233. test_spot( // pbinom(10,100,0.9)
  234. static_cast<RealType>(100), // successes r
  235. static_cast<RealType>(10), // Number of failures, k
  236. static_cast<RealType>(0.9), // Probability of success, p
  237. static_cast<RealType>(4.535522887695670E-1), // Probability of result (CDF), P
  238. static_cast<RealType>(1 - 4.535522887695670E-1), // Q = 1 - P
  239. tolerance);
  240. test_spot( // pbinom(1,100,0.991)
  241. static_cast<RealType>(100), // successes r
  242. static_cast<RealType>(1), // Number of failures, k
  243. static_cast<RealType>(0.991), // Probability of success, p
  244. static_cast<RealType>(7.693413044217000E-1), // Probability of result (CDF), P
  245. static_cast<RealType>(1 - 7.693413044217000E-1), // Q = 1 - P
  246. tolerance);
  247. test_spot( // pbinom(10,100,0.991)
  248. static_cast<RealType>(100), // successes r
  249. static_cast<RealType>(10), // Number of failures, k
  250. static_cast<RealType>(0.991), // Probability of success, p
  251. static_cast<RealType>(9.999999940939000E-1), // Probability of result (CDF), P
  252. static_cast<RealType>(1 - 9.999999940939000E-1), // Q = 1 - P
  253. tolerance);
  254. if(std::numeric_limits<RealType>::is_specialized)
  255. { // An extreme value test that takes 3 minutes using the real concept type
  256. // for which numeric_limits<RealType>::is_specialized == false, deliberately
  257. // and for which there is no Lanczos approximation defined (also deliberately)
  258. // giving a very slow computation, but with acceptable accuracy.
  259. // A possible enhancement might be to use a normal approximation for
  260. // extreme values, but this is not implemented.
  261. test_spot( // pbinom(100000,100,0.001)
  262. static_cast<RealType>(100), // successes r
  263. static_cast<RealType>(100000), // Number of failures, k
  264. static_cast<RealType>(0.001), // Probability of success, p
  265. static_cast<RealType>(5.173047534260320E-1), // Probability of result (CDF), P
  266. static_cast<RealType>(1 - 5.173047534260320E-1), // Q = 1 - P
  267. tolerance*1000); // *1000 is OK 0.51730475350664229 versus
  268. // functions.wolfram.com
  269. // for I[0.001](100, 100000+1) gives:
  270. // Wolfram 0.517304753506834882009032744488738352004003696396461766326713
  271. // JM nonLanczos 0.51730475350664229 differs at the 13th decimal digit.
  272. // MathCAD 0.51730475342603199 differs at 10th decimal digit.
  273. // Error tests:
  274. check_out_of_range<negative_binomial_distribution<RealType> >(20, 0.5);
  275. BOOST_MATH_CHECK_THROW(negative_binomial_distribution<RealType>(0, 0.5), std::domain_error);
  276. BOOST_MATH_CHECK_THROW(negative_binomial_distribution<RealType>(-2, 0.5), std::domain_error);
  277. BOOST_MATH_CHECK_THROW(negative_binomial_distribution<RealType>(20, -0.5), std::domain_error);
  278. BOOST_MATH_CHECK_THROW(negative_binomial_distribution<RealType>(20, 1.5), std::domain_error);
  279. }
  280. // End of single spot tests using RealType
  281. // Tests on PDF:
  282. BOOST_CHECK_CLOSE(
  283. pdf(negative_binomial_distribution<RealType>(static_cast<RealType>(2), static_cast<RealType>(0.5)),
  284. static_cast<RealType>(0) ), // k = 0.
  285. static_cast<RealType>(0.25), // 0
  286. tolerance);
  287. BOOST_CHECK_CLOSE(
  288. pdf(negative_binomial_distribution<RealType>(static_cast<RealType>(4), static_cast<RealType>(0.5)),
  289. static_cast<RealType>(0)), // k = 0.
  290. static_cast<RealType>(0.0625), // exact 1/16
  291. tolerance);
  292. BOOST_CHECK_CLOSE(
  293. pdf(negative_binomial_distribution<RealType>(static_cast<RealType>(20), static_cast<RealType>(0.25)),
  294. static_cast<RealType>(0)), // k = 0
  295. static_cast<RealType>(9.094947017729270E-13), // pbinom(0,20,0.25) = 9.094947017729270E-13
  296. tolerance);
  297. BOOST_CHECK_CLOSE(
  298. pdf(negative_binomial_distribution<RealType>(static_cast<RealType>(20), static_cast<RealType>(0.2)),
  299. static_cast<RealType>(0)), // k = 0
  300. static_cast<RealType>(1.0485760000000003e-014), // MathCAD 1.048576000000000E-14
  301. tolerance);
  302. BOOST_CHECK_CLOSE(
  303. pdf(negative_binomial_distribution<RealType>(static_cast<RealType>(10), static_cast<RealType>(0.1)),
  304. static_cast<RealType>(0)), // k = 0.
  305. static_cast<RealType>(1e-10), // MathCAD says zero, but suffers cancellation error?
  306. tolerance);
  307. BOOST_CHECK_CLOSE(
  308. pdf(negative_binomial_distribution<RealType>(static_cast<RealType>(20), static_cast<RealType>(0.1)),
  309. static_cast<RealType>(0)), // k = 0.
  310. static_cast<RealType>(1e-20), // MathCAD says zero, but suffers cancellation error?
  311. tolerance);
  312. BOOST_CHECK_CLOSE( // .
  313. pdf(negative_binomial_distribution<RealType>(static_cast<RealType>(20), static_cast<RealType>(0.9)),
  314. static_cast<RealType>(0)), // k.
  315. static_cast<RealType>(1.215766545905690E-1), // k=20 p = 0.9
  316. tolerance);
  317. // Tests on cdf:
  318. // MathCAD pbinom k, r, p) == failures, successes, probability.
  319. BOOST_CHECK_CLOSE(cdf(
  320. negative_binomial_distribution<RealType>(static_cast<RealType>(2), static_cast<RealType>(0.5)), // successes = 2,prob 0.25
  321. static_cast<RealType>(0) ), // k = 0
  322. static_cast<RealType>(0.25), // probability 1/4
  323. tolerance);
  324. BOOST_CHECK_CLOSE(cdf(complement(
  325. negative_binomial_distribution<RealType>(static_cast<RealType>(2), static_cast<RealType>(0.5)), // successes = 2,prob 0.25
  326. static_cast<RealType>(0) )), // k = 0
  327. static_cast<RealType>(0.75), // probability 3/4
  328. tolerance);
  329. BOOST_CHECK_CLOSE( // k = 1.
  330. cdf(negative_binomial_distribution<RealType>(static_cast<RealType>(20), static_cast<RealType>(0.25)),
  331. static_cast<RealType>(1)), // k =1.
  332. static_cast<RealType>(1.455191522836700E-11),
  333. tolerance);
  334. BOOST_CHECK_SMALL( // Check within an epsilon with CHECK_SMALL
  335. cdf(negative_binomial_distribution<RealType>(static_cast<RealType>(20), static_cast<RealType>(0.25)),
  336. static_cast<RealType>(1)) -
  337. static_cast<RealType>(1.455191522836700E-11),
  338. tolerance );
  339. // Some exact (probably - judging by trailing zeros) values.
  340. BOOST_CHECK_CLOSE(
  341. cdf(negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  342. static_cast<RealType>(0)), // k.
  343. static_cast<RealType>(1.525878906250000E-5),
  344. tolerance);
  345. BOOST_CHECK_CLOSE(
  346. cdf(negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  347. static_cast<RealType>(0)), // k.
  348. static_cast<RealType>(1.525878906250000E-5),
  349. tolerance);
  350. BOOST_CHECK_SMALL(
  351. cdf(negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  352. static_cast<RealType>(0)) -
  353. static_cast<RealType>(1.525878906250000E-5),
  354. tolerance );
  355. BOOST_CHECK_CLOSE( // k = 1.
  356. cdf(negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  357. static_cast<RealType>(1)), // k.
  358. static_cast<RealType>(1.068115234375010E-4),
  359. tolerance);
  360. BOOST_CHECK_CLOSE( // k = 2.
  361. cdf(negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  362. static_cast<RealType>(2)), // k.
  363. static_cast<RealType>(4.158020019531300E-4),
  364. tolerance);
  365. BOOST_CHECK_CLOSE( // k = 3.
  366. cdf(negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  367. static_cast<RealType>(3)), // k.bristow
  368. static_cast<RealType>(1.188278198242200E-3),
  369. tolerance);
  370. BOOST_CHECK_CLOSE( // k = 4.
  371. cdf(negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  372. static_cast<RealType>(4)), // k.
  373. static_cast<RealType>(2.781510353088410E-3),
  374. tolerance);
  375. BOOST_CHECK_CLOSE( // k = 5.
  376. cdf(negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  377. static_cast<RealType>(5)), // k.
  378. static_cast<RealType>(5.649328231811500E-3),
  379. tolerance);
  380. BOOST_CHECK_CLOSE( // k = 6.
  381. cdf(negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  382. static_cast<RealType>(6)), // k.
  383. static_cast<RealType>(1.030953228473680E-2),
  384. tolerance);
  385. BOOST_CHECK_CLOSE( // k = 7.
  386. cdf(negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  387. static_cast<RealType>(7)), // k.
  388. static_cast<RealType>(1.729983836412430E-2),
  389. tolerance);
  390. BOOST_CHECK_CLOSE( // k = 8.
  391. cdf(negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  392. static_cast<RealType>(8)), // k = n.
  393. static_cast<RealType>(2.712995628826370E-2),
  394. tolerance);
  395. BOOST_CHECK_CLOSE( //
  396. cdf(negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  397. static_cast<RealType>(48)), // k
  398. static_cast<RealType>(9.826582228110670E-1),
  399. tolerance);
  400. BOOST_CHECK_CLOSE( //
  401. cdf(negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  402. static_cast<RealType>(64)), // k
  403. static_cast<RealType>(9.990295004935590E-1),
  404. tolerance);
  405. BOOST_CHECK_CLOSE( //
  406. cdf(negative_binomial_distribution<RealType>(static_cast<RealType>(5), static_cast<RealType>(0.4)),
  407. static_cast<RealType>(26)), // k
  408. static_cast<RealType>(9.989686246611190E-1),
  409. tolerance);
  410. BOOST_CHECK_CLOSE( //
  411. cdf(negative_binomial_distribution<RealType>(static_cast<RealType>(5), static_cast<RealType>(0.4)),
  412. static_cast<RealType>(2)), // k failures
  413. static_cast<RealType>(9.625600000000020E-2),
  414. tolerance);
  415. BOOST_CHECK_CLOSE( //
  416. cdf(negative_binomial_distribution<RealType>(static_cast<RealType>(50), static_cast<RealType>(0.9)),
  417. static_cast<RealType>(20)), // k
  418. static_cast<RealType>(9.999970854144170E-1),
  419. tolerance);
  420. BOOST_CHECK_CLOSE( //
  421. cdf(negative_binomial_distribution<RealType>(static_cast<RealType>(500), static_cast<RealType>(0.7)),
  422. static_cast<RealType>(200)), // k
  423. static_cast<RealType>(2.172846379930550E-1),
  424. tolerance* 2);
  425. BOOST_CHECK_CLOSE( //
  426. cdf(negative_binomial_distribution<RealType>(static_cast<RealType>(50), static_cast<RealType>(0.7)),
  427. static_cast<RealType>(20)), // k
  428. static_cast<RealType>(4.550203671301790E-1),
  429. tolerance);
  430. // Tests of other functions, mean and other moments ...
  431. negative_binomial_distribution<RealType> dist(static_cast<RealType>(8), static_cast<RealType>(0.25));
  432. using namespace std; // ADL of std names.
  433. // mean:
  434. BOOST_CHECK_CLOSE(
  435. mean(dist), static_cast<RealType>(8 * (1 - 0.25) /0.25), tol5eps);
  436. BOOST_CHECK_CLOSE(
  437. mode(dist), static_cast<RealType>(21), tol1eps);
  438. // variance:
  439. BOOST_CHECK_CLOSE(
  440. variance(dist), static_cast<RealType>(8 * (1 - 0.25) / (0.25 * 0.25)), tol5eps);
  441. // std deviation:
  442. BOOST_CHECK_CLOSE(
  443. standard_deviation(dist), // 9.79795897113271239270
  444. static_cast<RealType>(9.797958971132712392789136298823565567864L), // using functions.wolfram.com
  445. // 9.79795897113271152534 == sqrt(8 * (1 - 0.25) / (0.25 * 0.25)))
  446. tol5eps * 100);
  447. BOOST_CHECK_CLOSE(
  448. skewness(dist), //
  449. static_cast<RealType>(0.71443450831176036),
  450. // using http://mathworld.wolfram.com/skewness.html
  451. tolerance);
  452. BOOST_CHECK_CLOSE(
  453. kurtosis_excess(dist), //
  454. static_cast<RealType>(0.7604166666666666666666666666666666666666L), // using Wikipedia Kurtosis(excess) formula
  455. tol5eps * 100);
  456. BOOST_CHECK_CLOSE(
  457. kurtosis(dist), // true
  458. static_cast<RealType>(3.76041666666666666666666666666666666666666L), //
  459. tol5eps * 100);
  460. // hazard:
  461. RealType x = static_cast<RealType>(0.125);
  462. BOOST_CHECK_CLOSE(
  463. hazard(dist, x)
  464. , pdf(dist, x) / cdf(complement(dist, x)), tol5eps);
  465. // cumulative hazard:
  466. BOOST_CHECK_CLOSE(
  467. chf(dist, x), -log(cdf(complement(dist, x))), tol5eps);
  468. // coefficient_of_variation:
  469. BOOST_CHECK_CLOSE(
  470. coefficient_of_variation(dist)
  471. , standard_deviation(dist) / mean(dist), tol5eps);
  472. // Special cases for PDF:
  473. BOOST_CHECK_EQUAL(
  474. pdf(
  475. negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0)), //
  476. static_cast<RealType>(0)),
  477. static_cast<RealType>(0) );
  478. BOOST_CHECK_EQUAL(
  479. pdf(
  480. negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0)),
  481. static_cast<RealType>(0.0001)),
  482. static_cast<RealType>(0) );
  483. BOOST_CHECK_EQUAL(
  484. pdf(
  485. negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(1)),
  486. static_cast<RealType>(0.001)),
  487. static_cast<RealType>(0) );
  488. BOOST_CHECK_EQUAL(
  489. pdf(
  490. negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(1)),
  491. static_cast<RealType>(8)),
  492. static_cast<RealType>(0) );
  493. BOOST_CHECK_SMALL(
  494. pdf(
  495. negative_binomial_distribution<RealType>(static_cast<RealType>(2), static_cast<RealType>(0.25)),
  496. static_cast<RealType>(0))-
  497. static_cast<RealType>(0.0625),
  498. 2 * boost::math::tools::epsilon<RealType>() ); // Expect exact, but not quite.
  499. // numeric_limits<RealType>::epsilon()); // Not suitable for real concept!
  500. // Quantile boundary cases checks:
  501. BOOST_CHECK_EQUAL(
  502. quantile( // zero P < cdf(0) so should be exactly zero.
  503. negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  504. static_cast<RealType>(0)),
  505. static_cast<RealType>(0));
  506. BOOST_CHECK_EQUAL(
  507. quantile( // min P < cdf(0) so should be exactly zero.
  508. negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  509. static_cast<RealType>(boost::math::tools::min_value<RealType>())),
  510. static_cast<RealType>(0));
  511. BOOST_CHECK_CLOSE_FRACTION(
  512. quantile( // Small P < cdf(0) so should be near zero.
  513. negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  514. static_cast<RealType>(boost::math::tools::epsilon<RealType>())), //
  515. static_cast<RealType>(0),
  516. tol5eps);
  517. BOOST_CHECK_CLOSE(
  518. quantile( // Small P < cdf(0) so should be exactly zero.
  519. negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  520. static_cast<RealType>(0.0001)),
  521. static_cast<RealType>(0.95854156929288470),
  522. tolerance);
  523. //BOOST_CHECK( // Fails with overflow for real_concept
  524. //quantile( // Small P near 1 so k failures should be big.
  525. //negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  526. //static_cast<RealType>(1 - boost::math::tools::epsilon<RealType>())) <=
  527. //static_cast<RealType>(189.56999032670058) // 106.462769 for float
  528. //);
  529. if(std::numeric_limits<RealType>::has_infinity)
  530. { // BOOST_CHECK tests for infinity using std::numeric_limits<>::infinity()
  531. // Note that infinity is not implemented for real_concept, so these tests
  532. // are only done for types, like built-in float, double.. that have infinity.
  533. // Note that these assume that BOOST_MATH_OVERFLOW_ERROR_POLICY is NOT throw_on_error.
  534. // #define BOOST_MATH_THROW_ON_OVERFLOW_POLICY == throw_on_error would throw here.
  535. // #define BOOST_MAT_DOMAIN_ERROR_POLICY IS defined throw_on_error,
  536. // so the throw path of error handling is tested below with BOOST_MATH_CHECK_THROW tests.
  537. BOOST_CHECK(
  538. quantile( // At P == 1 so k failures should be infinite.
  539. negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  540. static_cast<RealType>(1)) ==
  541. //static_cast<RealType>(boost::math::tools::infinity<RealType>())
  542. static_cast<RealType>(std::numeric_limits<RealType>::infinity()) );
  543. BOOST_CHECK_EQUAL(
  544. quantile( // At 1 == P so should be infinite.
  545. negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  546. static_cast<RealType>(1)), //
  547. std::numeric_limits<RealType>::infinity() );
  548. BOOST_CHECK_EQUAL(
  549. quantile(complement( // Q zero 1 so P == 1 < cdf(0) so should be exactly infinity.
  550. negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  551. static_cast<RealType>(0))),
  552. std::numeric_limits<RealType>::infinity() );
  553. } // test for infinity using std::numeric_limits<>::infinity()
  554. else
  555. { // real_concept case, so check it throws rather than returning infinity.
  556. BOOST_CHECK_EQUAL(
  557. quantile( // At P == 1 so k failures should be infinite.
  558. negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  559. static_cast<RealType>(1)),
  560. boost::math::tools::max_value<RealType>() );
  561. BOOST_CHECK_EQUAL(
  562. quantile(complement( // Q zero 1 so P == 1 < cdf(0) so should be exactly infinity.
  563. negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  564. static_cast<RealType>(0))),
  565. boost::math::tools::max_value<RealType>());
  566. }
  567. BOOST_CHECK( // Should work for built-in and real_concept.
  568. quantile(complement( // Q very near to 1 so P nearly 1 < so should be large > 384.
  569. negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  570. static_cast<RealType>(boost::math::tools::min_value<RealType>())))
  571. >= static_cast<RealType>(384) );
  572. BOOST_CHECK_EQUAL(
  573. quantile( // P == 0 < cdf(0) so should be zero.
  574. negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  575. static_cast<RealType>(0)),
  576. static_cast<RealType>(0));
  577. // Quantile Complement boundary cases:
  578. BOOST_CHECK_EQUAL(
  579. quantile(complement( // Q = 1 so P = 0 < cdf(0) so should be exactly zero.
  580. negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  581. static_cast<RealType>(1))),
  582. static_cast<RealType>(0)
  583. );
  584. BOOST_CHECK_EQUAL(
  585. quantile(complement( // Q very near 1 so P == epsilon < cdf(0) so should be exactly zero.
  586. negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  587. static_cast<RealType>(1 - boost::math::tools::epsilon<RealType>()))),
  588. static_cast<RealType>(0)
  589. );
  590. // Check that duff arguments throw domain_error:
  591. BOOST_MATH_CHECK_THROW(
  592. pdf( // Negative successes!
  593. negative_binomial_distribution<RealType>(static_cast<RealType>(-1), static_cast<RealType>(0.25)),
  594. static_cast<RealType>(0)), std::domain_error
  595. );
  596. BOOST_MATH_CHECK_THROW(
  597. pdf( // Negative success_fraction!
  598. negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(-0.25)),
  599. static_cast<RealType>(0)), std::domain_error
  600. );
  601. BOOST_MATH_CHECK_THROW(
  602. pdf( // Success_fraction > 1!
  603. negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(1.25)),
  604. static_cast<RealType>(0)),
  605. std::domain_error
  606. );
  607. BOOST_MATH_CHECK_THROW(
  608. pdf( // Negative k argument !
  609. negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  610. static_cast<RealType>(-1)),
  611. std::domain_error
  612. );
  613. //BOOST_MATH_CHECK_THROW(
  614. //pdf( // Unlike binomial there is NO limit on k (failures)
  615. //negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  616. //static_cast<RealType>(9)), std::domain_error
  617. //);
  618. BOOST_MATH_CHECK_THROW(
  619. cdf( // Negative k argument !
  620. negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  621. static_cast<RealType>(-1)),
  622. std::domain_error
  623. );
  624. BOOST_MATH_CHECK_THROW(
  625. cdf( // Negative success_fraction!
  626. negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(-0.25)),
  627. static_cast<RealType>(0)), std::domain_error
  628. );
  629. BOOST_MATH_CHECK_THROW(
  630. cdf( // Success_fraction > 1!
  631. negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(1.25)),
  632. static_cast<RealType>(0)), std::domain_error
  633. );
  634. BOOST_MATH_CHECK_THROW(
  635. quantile( // Negative success_fraction!
  636. negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(-0.25)),
  637. static_cast<RealType>(0)), std::domain_error
  638. );
  639. BOOST_MATH_CHECK_THROW(
  640. quantile( // Success_fraction > 1!
  641. negative_binomial_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(1.25)),
  642. static_cast<RealType>(0)), std::domain_error
  643. );
  644. // End of check throwing 'duff' out-of-domain values.
  645. #define T RealType
  646. #include "negative_binomial_quantile.ipp"
  647. for(unsigned i = 0; i < negative_binomial_quantile_data.size(); ++i)
  648. {
  649. using namespace boost::math::policies;
  650. typedef policy<discrete_quantile<boost::math::policies::real> > P1;
  651. typedef policy<discrete_quantile<integer_round_down> > P2;
  652. typedef policy<discrete_quantile<integer_round_up> > P3;
  653. typedef policy<discrete_quantile<integer_round_outwards> > P4;
  654. typedef policy<discrete_quantile<integer_round_inwards> > P5;
  655. typedef policy<discrete_quantile<integer_round_nearest> > P6;
  656. RealType tol = boost::math::tools::epsilon<RealType>() * 700;
  657. if(!boost::is_floating_point<RealType>::value)
  658. tol *= 10; // no lanczos approximation implies less accuracy
  659. //
  660. // Check full real value first:
  661. //
  662. negative_binomial_distribution<RealType, P1> p1(negative_binomial_quantile_data[i][0], negative_binomial_quantile_data[i][1]);
  663. RealType x = quantile(p1, negative_binomial_quantile_data[i][2]);
  664. BOOST_CHECK_CLOSE_FRACTION(x, negative_binomial_quantile_data[i][3], tol);
  665. x = quantile(complement(p1, negative_binomial_quantile_data[i][2]));
  666. BOOST_CHECK_CLOSE_FRACTION(x, negative_binomial_quantile_data[i][4], tol);
  667. //
  668. // Now with round down to integer:
  669. //
  670. negative_binomial_distribution<RealType, P2> p2(negative_binomial_quantile_data[i][0], negative_binomial_quantile_data[i][1]);
  671. x = quantile(p2, negative_binomial_quantile_data[i][2]);
  672. BOOST_CHECK_EQUAL(x, floor(negative_binomial_quantile_data[i][3]));
  673. x = quantile(complement(p2, negative_binomial_quantile_data[i][2]));
  674. BOOST_CHECK_EQUAL(x, floor(negative_binomial_quantile_data[i][4]));
  675. //
  676. // Now with round up to integer:
  677. //
  678. negative_binomial_distribution<RealType, P3> p3(negative_binomial_quantile_data[i][0], negative_binomial_quantile_data[i][1]);
  679. x = quantile(p3, negative_binomial_quantile_data[i][2]);
  680. BOOST_CHECK_EQUAL(x, ceil(negative_binomial_quantile_data[i][3]));
  681. x = quantile(complement(p3, negative_binomial_quantile_data[i][2]));
  682. BOOST_CHECK_EQUAL(x, ceil(negative_binomial_quantile_data[i][4]));
  683. //
  684. // Now with round to integer "outside":
  685. //
  686. negative_binomial_distribution<RealType, P4> p4(negative_binomial_quantile_data[i][0], negative_binomial_quantile_data[i][1]);
  687. x = quantile(p4, negative_binomial_quantile_data[i][2]);
  688. BOOST_CHECK_EQUAL(x, negative_binomial_quantile_data[i][2] < 0.5f ? floor(negative_binomial_quantile_data[i][3]) : ceil(negative_binomial_quantile_data[i][3]));
  689. x = quantile(complement(p4, negative_binomial_quantile_data[i][2]));
  690. BOOST_CHECK_EQUAL(x, negative_binomial_quantile_data[i][2] < 0.5f ? ceil(negative_binomial_quantile_data[i][4]) : floor(negative_binomial_quantile_data[i][4]));
  691. //
  692. // Now with round to integer "inside":
  693. //
  694. negative_binomial_distribution<RealType, P5> p5(negative_binomial_quantile_data[i][0], negative_binomial_quantile_data[i][1]);
  695. x = quantile(p5, negative_binomial_quantile_data[i][2]);
  696. BOOST_CHECK_EQUAL(x, negative_binomial_quantile_data[i][2] < 0.5f ? ceil(negative_binomial_quantile_data[i][3]) : floor(negative_binomial_quantile_data[i][3]));
  697. x = quantile(complement(p5, negative_binomial_quantile_data[i][2]));
  698. BOOST_CHECK_EQUAL(x, negative_binomial_quantile_data[i][2] < 0.5f ? floor(negative_binomial_quantile_data[i][4]) : ceil(negative_binomial_quantile_data[i][4]));
  699. //
  700. // Now with round to nearest integer:
  701. //
  702. negative_binomial_distribution<RealType, P6> p6(negative_binomial_quantile_data[i][0], negative_binomial_quantile_data[i][1]);
  703. x = quantile(p6, negative_binomial_quantile_data[i][2]);
  704. BOOST_CHECK_EQUAL(x, floor(negative_binomial_quantile_data[i][3] + 0.5f));
  705. x = quantile(complement(p6, negative_binomial_quantile_data[i][2]));
  706. BOOST_CHECK_EQUAL(x, floor(negative_binomial_quantile_data[i][4] + 0.5f));
  707. }
  708. return;
  709. } // template <class RealType> void test_spots(RealType) // Any floating-point type RealType.
  710. BOOST_AUTO_TEST_CASE( test_main )
  711. {
  712. // Check that can generate negative_binomial distribution using the two convenience methods:
  713. using namespace boost::math;
  714. negative_binomial mynb1(2., 0.5); // Using typedef - default type is double.
  715. negative_binomial_distribution<> myf2(2., 0.5); // Using default RealType double.
  716. // Basic sanity-check spot values.
  717. // Test some simple double only examples.
  718. negative_binomial_distribution<double> my8dist(8., 0.25);
  719. // 8 successes (r), 0.25 success fraction = 35% or 1 in 4 successes.
  720. // Note: double values (matching the distribution definition) avoid the need for any casting.
  721. // Check accessor functions return exact values for double at least.
  722. BOOST_CHECK_EQUAL(my8dist.successes(), static_cast<double>(8));
  723. BOOST_CHECK_EQUAL(my8dist.success_fraction(), static_cast<double>(1./4.));
  724. // (Parameter value, arbitrarily zero, only communicates the floating point type).
  725. #ifdef TEST_FLOAT
  726. test_spots(0.0F); // Test float.
  727. #endif
  728. #ifdef TEST_DOUBLE
  729. test_spots(0.0); // Test double.
  730. #endif
  731. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  732. #ifdef TEST_LDOUBLE
  733. test_spots(0.0L); // Test long double.
  734. #endif
  735. #ifndef BOOST_MATH_NO_REAL_CONCEPT_TESTS
  736. #ifdef TEST_REAL_CONCEPT
  737. test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
  738. #endif
  739. #endif
  740. #else
  741. std::cout << "<note>The long double tests have been disabled on this platform "
  742. "either because the long double overloads of the usual math functions are "
  743. "not available at all, or because they are too inaccurate for these tests "
  744. "to pass.</note>" << std::endl;
  745. #endif
  746. } // BOOST_AUTO_TEST_CASE( test_main )
  747. /*
  748. Autorun "i:\boost-06-05-03-1300\libs\math\test\Math_test\debug\test_negative_binomial.exe"
  749. Running 1 test case...
  750. Tolerance = 0.0119209%.
  751. Tolerance 5 eps = 5.96046e-007%.
  752. Tolerance = 2.22045e-011%.
  753. Tolerance 5 eps = 1.11022e-015%.
  754. Tolerance = 2.22045e-011%.
  755. Tolerance 5 eps = 1.11022e-015%.
  756. Tolerance = 2.22045e-011%.
  757. Tolerance 5 eps = 1.11022e-015%.
  758. *** No errors detected
  759. */