test_geometric.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. // test_geometric.cpp
  2. // Copyright Paul A. Bristow 2010.
  3. // Copyright John Maddock 2010.
  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 Geometric 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>
  23. #include <boost/math/concepts/real_concept.hpp> // for real_concept
  24. using ::boost::math::concepts::real_concept;
  25. #include <boost/math/distributions/geometric.hpp> // for geometric_distribution
  26. using boost::math::geometric_distribution;
  27. using boost::math::geometric; // using typedef for geometric_distribution<double>
  28. #include <boost/math/distributions/negative_binomial.hpp> // for some comparisons.
  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_FRACTION
  32. #include "test_out_of_range.hpp"
  33. #include <iostream>
  34. using std::cout;
  35. using std::endl;
  36. using std::setprecision;
  37. using std::showpoint;
  38. #include <limits>
  39. using std::numeric_limits;
  40. template <class RealType>
  41. void test_spot( // Test a single spot value against 'known good' values.
  42. RealType k, // Number of failures.
  43. RealType p, // Probability of success_fraction.
  44. RealType P, // CDF probability.
  45. RealType Q, // Complement of CDF.
  46. RealType tol) // Test tolerance.
  47. {
  48. boost::math::geometric_distribution<RealType> g(p);
  49. BOOST_CHECK_EQUAL(p, g.success_fraction());
  50. BOOST_CHECK_CLOSE_FRACTION(cdf(g, k), P, tol);
  51. if((P < 0.99) && (Q < 0.99))
  52. {
  53. // We can only check this if P is not too close to 1,
  54. // so that we can guarantee that Q is free of error:
  55. //
  56. BOOST_CHECK_CLOSE_FRACTION(
  57. cdf(complement(g, k)), Q, tol);
  58. if(k != 0)
  59. {
  60. BOOST_CHECK_CLOSE_FRACTION(
  61. quantile(g, 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)
  67. && (boost::is_floating_point<RealType>::value))
  68. {
  69. // Limit where this is checked: if exponent range is very large we may
  70. // run out of iterations in our root finding algorithm.
  71. BOOST_CHECK(quantile(g, P) < boost::math::tools::epsilon<RealType>() * 10);
  72. }
  73. }
  74. if(k != 0)
  75. {
  76. BOOST_CHECK_CLOSE_FRACTION(
  77. quantile(complement(g, Q)), k, tol);
  78. }
  79. else
  80. {
  81. // Just check quantile is very small:
  82. if((std::numeric_limits<RealType>::max_exponent <= std::numeric_limits<double>::max_exponent)
  83. && (boost::is_floating_point<RealType>::value))
  84. {
  85. // Limit where this is checked: if exponent range is very large we may
  86. // run out of iterations in our root finding algorithm.
  87. BOOST_CHECK(quantile(complement(g, Q)) < boost::math::tools::epsilon<RealType>() * 10);
  88. }
  89. }
  90. } // if((P < 0.99) && (Q < 0.99))
  91. // Parameter estimation test: estimate success ratio:
  92. BOOST_CHECK_CLOSE_FRACTION(
  93. geometric_distribution<RealType>::find_lower_bound_on_p(
  94. 1+k, P),
  95. p, 0.02); // Wide tolerance needed for some tests.
  96. // Note we bump up the sample size here, purely for the sake of the test,
  97. // internally the function has to adjust the sample size so that we get
  98. // the right upper bound, our test undoes this, so we can verify the result.
  99. BOOST_CHECK_CLOSE_FRACTION(
  100. geometric_distribution<RealType>::find_upper_bound_on_p(
  101. 1+k+1, Q),
  102. p, 0.02);
  103. if(Q < P)
  104. {
  105. //
  106. // We check two things here, that the upper and lower bounds
  107. // are the right way around, and that they do actually bracket
  108. // the naive estimate of p = successes / (sample size)
  109. //
  110. BOOST_CHECK(
  111. geometric_distribution<RealType>::find_lower_bound_on_p(
  112. 1+k, Q)
  113. <=
  114. geometric_distribution<RealType>::find_upper_bound_on_p(
  115. 1+k, Q)
  116. );
  117. BOOST_CHECK(
  118. geometric_distribution<RealType>::find_lower_bound_on_p(
  119. 1+k, Q)
  120. <=
  121. 1 / (1+k)
  122. );
  123. BOOST_CHECK(
  124. 1 / (1+k)
  125. <=
  126. geometric_distribution<RealType>::find_upper_bound_on_p(
  127. 1+k, Q)
  128. );
  129. }
  130. else
  131. {
  132. // As above but when P is small.
  133. BOOST_CHECK(
  134. geometric_distribution<RealType>::find_lower_bound_on_p(
  135. 1+k, P)
  136. <=
  137. geometric_distribution<RealType>::find_upper_bound_on_p(
  138. 1+k, P)
  139. );
  140. BOOST_CHECK(
  141. geometric_distribution<RealType>::find_lower_bound_on_p(
  142. 1+k, P)
  143. <=
  144. 1 / (1+k)
  145. );
  146. BOOST_CHECK(
  147. 1 / (1+k)
  148. <=
  149. geometric_distribution<RealType>::find_upper_bound_on_p(
  150. 1+k, P)
  151. );
  152. }
  153. // Estimate sample size:
  154. BOOST_CHECK_CLOSE_FRACTION(
  155. geometric_distribution<RealType>::find_minimum_number_of_trials(
  156. k, p, P),
  157. 1+k, 0.02); // Can differ 50 to 51 for small p
  158. BOOST_CHECK_CLOSE_FRACTION(
  159. geometric_distribution<RealType>::find_maximum_number_of_trials(
  160. k, p, Q),
  161. 1+k, 0.02);
  162. } // test_spot
  163. template <class RealType> // Any floating-point type RealType.
  164. void test_spots(RealType)
  165. {
  166. // Basic sanity checks.
  167. // Most test data is to double precision (17 decimal digits) only,
  168. cout << "Floating point Type is " << typeid(RealType).name() << endl;
  169. // so set tolerance to 1000 eps expressed as a fraction,
  170. // or 1000 eps of type double expressed as a fraction,
  171. // whichever is the larger.
  172. RealType tolerance = (std::max)
  173. (boost::math::tools::epsilon<RealType>(),
  174. static_cast<RealType>(std::numeric_limits<double>::epsilon()));
  175. tolerance *= 10; // 10 eps
  176. cout << "Tolerance = " << tolerance << "." << endl;
  177. RealType tol1eps = boost::math::tools::epsilon<RealType>(); // Very tight, suit exact values.
  178. //RealType tol2eps = boost::math::tools::epsilon<RealType>() * 2; // Tight, values.
  179. RealType tol5eps = boost::math::tools::epsilon<RealType>() * 5; // Wider 5 epsilon.
  180. cout << "Tolerance 5 eps = " << tol5eps << "." << endl;
  181. // Sources of spot test values are mainly R.
  182. using boost::math::geometric_distribution;
  183. using boost::math::geometric;
  184. using boost::math::cdf;
  185. using boost::math::pdf;
  186. using boost::math::quantile;
  187. using boost::math::complement;
  188. BOOST_MATH_STD_USING // for std math functions
  189. // Test geometric using cdf spot values R
  190. // These test quantiles and complements as well.
  191. test_spot( //
  192. static_cast<RealType>(2), // Number of failures, k
  193. static_cast<RealType>(0.5), // Probability of success as fraction, p
  194. static_cast<RealType>(0.875L), // Probability of result (CDF), P
  195. static_cast<RealType>(0.125L), // complement CCDF Q = 1 - P
  196. tolerance);
  197. test_spot( //
  198. static_cast<RealType>(0), // Number of failures, k
  199. static_cast<RealType>(0.25), // Probability of success as fraction, p
  200. static_cast<RealType>(0.25), // Probability of result (CDF), P
  201. static_cast<RealType>(0.75), // Q = 1 - P
  202. tolerance);
  203. test_spot(
  204. // R formatC(pgeom(10,0.25), digits=17) [1] "0.95776486396789551"
  205. // formatC(pgeom(10,0.25, FALSE), digits=17) [1] "0.042235136032104499"
  206. static_cast<RealType>(10), // Number of failures, k
  207. static_cast<RealType>(0.25), // Probability of success, p
  208. static_cast<RealType>(0.95776486396789551L), // Probability of result (CDF), P
  209. static_cast<RealType>(0.042235136032104499L), // Q = 1 - P
  210. tolerance);
  211. test_spot( //
  212. // > R formatC(pgeom(50,0.25, TRUE), digits=17) [1] "0.99999957525875771"
  213. // > R formatC(pgeom(50,0.25, FALSE), digits=17) [1] "4.2474124232020353e-07"
  214. static_cast<RealType>(50), // Number of failures, k
  215. static_cast<RealType>(0.25), // Probability of success, p
  216. static_cast<RealType>(0.99999957525875771), // Probability of result (CDF), P
  217. static_cast<RealType>(4.2474124232020353e-07), // Q = 1 - P
  218. tolerance);
  219. /*
  220. // This causes failures in find_upper_bound_on_p p is small branch.
  221. test_spot( // formatC(pgeom(50,0.01, TRUE), digits=17)[1] "0.40104399353383874"
  222. // > formatC(pgeom(50,0.01, FALSE), digits=17) [1] "0.59895600646616121"
  223. static_cast<RealType>(50), // Number of failures, k
  224. static_cast<RealType>(0.01), // Probability of success, p
  225. static_cast<RealType>(0.40104399353383874), // Probability of result (CDF), P
  226. static_cast<RealType>(0.59895600646616121), // Q = 1 - P
  227. tolerance);
  228. */
  229. test_spot( // > formatC(pgeom(50,0.99, TRUE), digits=17) [1] " 1"
  230. // formatC(pgeom(50,0.99, FALSE), digits=17) [1] "1.0000000000000364e-102"
  231. static_cast<RealType>(50), // Number of failures, k
  232. static_cast<RealType>(0.99), // Probability of success, p
  233. static_cast<RealType>(1), // Probability of result (CDF), P
  234. static_cast<RealType>(1.0000000000000364e-102), // Q = 1 - P
  235. tolerance);
  236. test_spot( // > formatC(pgeom(1,0.99, TRUE), digits=17) [1] "0.99990000000000001"
  237. // > formatC(pgeom(1,0.99, FALSE), digits=17) [1] "0.00010000000000000009"
  238. static_cast<RealType>(1), // Number of failures, k
  239. static_cast<RealType>(0.99), // Probability of success, p
  240. static_cast<RealType>(0.9999), // Probability of result (CDF), P
  241. static_cast<RealType>(0.0001), // Q = 1 - P
  242. tolerance);
  243. if(std::numeric_limits<RealType>::is_specialized)
  244. { // An extreme value test that is more accurate than using negative binomial.
  245. // Since geometric only uses exp and log functions.
  246. test_spot( // > formatC(pgeom(10000, 0.001, TRUE), digits=17) [1] "0.99995487182736897"
  247. // > formatC(pgeom(10000,0.001, FALSE), digits=17) [1] "4.5128172631071587e-05"
  248. static_cast<RealType>(10000L), // Number of failures, k
  249. static_cast<RealType>(0.001L), // Probability of success, p
  250. static_cast<RealType>(0.99995487182736897L), // Probability of result (CDF), P
  251. static_cast<RealType>(4.5128172631071587e-05L), // Q = 1 - P
  252. tolerance); //
  253. } // numeric_limit is specialized
  254. // End of single spot tests using RealType
  255. // Tests on PDF:
  256. BOOST_CHECK_CLOSE_FRACTION( //> formatC(dgeom(0,0.5), digits=17)[1] " 0.5"
  257. pdf(geometric_distribution<RealType>(static_cast<RealType>(0.5)),
  258. static_cast<RealType>(0.0) ), // Number of failures, k is very small but not integral,
  259. static_cast<RealType>(0.5), // nearly success probability.
  260. tolerance);
  261. BOOST_CHECK_CLOSE_FRACTION( //> formatC(dgeom(0,0.5), digits=17)[1] " 0.5"
  262. // R treates geom as a discrete distribution.
  263. // > formatC(dgeom(1.999999,0.5, FALSE), digits=17) [1] " 0"
  264. // Warning message:
  265. // In dgeom(1.999999, 0.5, FALSE) : non-integer x = 1.999999
  266. pdf(geometric_distribution<RealType>(static_cast<RealType>(0.5)),
  267. static_cast<RealType>(0.0001L) ), // Number of failures, k is very small but not integral,
  268. static_cast<RealType>(0.4999653438420768L), // nearly success probability.
  269. tolerance);
  270. BOOST_CHECK_CLOSE_FRACTION( // > formatC(pgeom(0.0001,0.5, TRUE), digits=17)[1] " 0.5"
  271. // > formatC(pgeom(0.0001,0.5, FALSE), digits=17) [1] " 0.5"
  272. // R treates geom as a discrete distribution.
  273. pdf(geometric_distribution<RealType>(static_cast<RealType>(0.5)),
  274. static_cast<RealType>(0.0001L) ), // Number of failures, k is very small but not integral,
  275. static_cast<RealType>(0.4999653438420768L), // nearly success probability.
  276. tolerance);
  277. BOOST_CHECK_CLOSE_FRACTION( // formatC(dgeom(1,0.01), digits=17)[1] "0.0099000000000000008"
  278. pdf(geometric_distribution<RealType>(static_cast<RealType>(0.01L)),
  279. static_cast<RealType>(1) ), // Number of failures, k
  280. static_cast<RealType>(0.0099000000000000008), //
  281. tolerance);
  282. BOOST_CHECK_CLOSE_FRACTION( //> formatC(dgeom(1,0.99), digits=17)[1] "0.0099000000000000043"
  283. pdf(geometric_distribution<RealType>(static_cast<RealType>(0.99L)),
  284. static_cast<RealType>(1) ), // Number of failures, k
  285. static_cast<RealType>(0.00990000000000000043L), //
  286. tolerance);
  287. BOOST_CHECK_CLOSE_FRACTION( //> > formatC(dgeom(0,0.99), digits=17)[1] "0.98999999999999999"
  288. pdf(geometric_distribution<RealType>(static_cast<RealType>(0.99L)),
  289. static_cast<RealType>(0) ), // Number of failures, k
  290. static_cast<RealType>(0.98999999999999999L), //
  291. tolerance);
  292. // p near unity.
  293. BOOST_CHECK_CLOSE_FRACTION( // > formatC(dgeom(100,0.99), digits=17)[1] "9.9000000000003448e-201"
  294. pdf(geometric_distribution<RealType>(static_cast<RealType>(0.99L)),
  295. static_cast<RealType>(100) ), // Number of failures, k
  296. static_cast<RealType>(9.9000000000003448e-201L), //
  297. 100 * tolerance); // Note difference
  298. // p nearer unity.
  299. BOOST_CHECK_CLOSE_FRACTION( //
  300. pdf(geometric_distribution<RealType>(static_cast<RealType>(0.9999)),
  301. static_cast<RealType>(10) ), // Number of failures, k
  302. // static_cast<double>(9.9989999999889024e-41), // Boost.Math
  303. // static_cast<float>(1.00156406e-040)
  304. static_cast<RealType>(9.999e-41), // exact from 100 digit calculator.
  305. 2e3 * tolerance); // Note bigger tolerance needed.
  306. // Moshier Cephes 100 digits calculator says 9.999e-41
  307. //0.9999*pow(1-0.9999,10)
  308. // 9.9990000000000000000000000000000000000000000000000000000000000000000000E-41
  309. // 9.998999999988988e-041
  310. // > formatC(dgeom(10, 0.9999), digits=17) [1] "9.9989999999889024e-41"
  311. // p * pow(q, k) 9.9989999999889880e-041
  312. // exp(p * k * log1p(-p)) 9.9989999999889024e-041
  313. // 0.9999999999 * pow(1-0.9999999999,10)= 9.9999999990E-101
  314. // > formatC(dgeom(10,0.9999999999), digits=17) [1] "1.0000008273040127e-100"
  315. BOOST_CHECK_CLOSE_FRACTION( //
  316. pdf(geometric_distribution<RealType>(static_cast<RealType>(0.9999999999L)),
  317. static_cast<RealType>(10) ), //
  318. static_cast<RealType>(9.9999999990E-101L), // 1.0000008273040179e-100
  319. 1e9 * tolerance); // Note big tolerance needed.
  320. // 1.0000008273040179e-100 Boost.Math
  321. // 1.0000008273040127e-100 R
  322. // 0.9999999990000004e-100 100 digit calculator 'exact'
  323. BOOST_CHECK_CLOSE_FRACTION( //
  324. pdf(geometric_distribution<RealType>(static_cast<RealType>(0.00000000001L)),
  325. static_cast<RealType>(10) ), //
  326. static_cast<RealType>(9.999999999e-12L), // get 9.9999999989999994e-012
  327. 1 * tolerance); // Note small tolerance needed.
  328. BOOST_CHECK_CLOSE_FRACTION( //
  329. pdf(geometric_distribution<RealType>(static_cast<RealType>(0.00000000001L)),
  330. static_cast<RealType>(1000) ), //
  331. static_cast<RealType>(9.9999999e-12L), // get 9.9999998999999913e-012
  332. tolerance); // Note small tolerance needed.
  333. ///////////////////////////////////////////////////
  334. BOOST_CHECK_CLOSE_FRACTION( //
  335. // > formatC(dgeom(0.0001,0.5, FALSE), digits=17) [1] " 0.5"
  336. // R treates geom as a discrete distribution.
  337. // But Boost.Math is continuous, so if you want R behaviour,
  338. // make number of failures, k into an integer with the floor function.
  339. pdf(geometric_distribution<RealType>(static_cast<RealType>(0.5)),
  340. static_cast<RealType>(floor(0.0001L)) ), // Number of failures, k is very small but MADE integral,
  341. static_cast<RealType>(0.5), // nearly success probability.
  342. tolerance);
  343. // R switches over at about 1e7 from k = 0, returning 0.5, to k = 1, returning 0.25.
  344. // Boost.Math does not do this, even for 0.9999999999999999
  345. // > formatC(pgeom(0.999999,0.5, FALSE), digits=17) [1] " 0.5"
  346. // > formatC(pgeom(0.9999999,0.5, FALSE), digits=17) [1] " 0.25"
  347. BOOST_CHECK_CLOSE_FRACTION( // > formatC(pgeom(0.0001,0.5, TRUE), digits=17)[1] " 0.5"
  348. // > formatC(pgeom(0.0001,0.5, FALSE), digits=17) [1] " 0.5"
  349. // R treates geom as a discrete distribution.
  350. // But Boost.Math is continuous, so if you want R behaviour,
  351. // make number of failures, k into an integer with the floor function.
  352. pdf(geometric_distribution<RealType>(static_cast<RealType>(0.5)),
  353. static_cast<RealType>(floor(0.9999999999999999L)) ), // Number of failures, k is very small but MADE integral,
  354. static_cast<RealType>(0.5), // nearly success probability.
  355. tolerance);
  356. BOOST_CHECK_CLOSE_FRACTION( // > formatC(pgeom(0.0001,0.5, TRUE), digits=17)[1] " 0.5"
  357. // > formatC(pgeom(0.0001,0.5, FALSE), digits=17) [1] " 0.5"
  358. // R treates geom as a discrete distribution.
  359. // But Boost.Math is continuous, so if you want R behaviour,
  360. // make number of failures, k into an integer with the floor function.
  361. pdf(geometric_distribution<RealType>(static_cast<RealType>(0.5)),
  362. static_cast<RealType>(floor(1. - tolerance)) ),
  363. // Number of failures, k is very small but MADE integral,
  364. // Need to use tolerance here,
  365. // as epsilon is ill-defined for Real concept:
  366. // numeric_limits<RealType>::epsilon() 0
  367. static_cast<RealType>(0.5), // nearly success probability.
  368. tolerance * 10);
  369. BOOST_CHECK_CLOSE_FRACTION(
  370. pdf(geometric_distribution<RealType>(static_cast<RealType>(0.0001L)),
  371. static_cast<RealType>(2)), // k = 2.
  372. static_cast<RealType>(9.99800010e-5L), // 'exact '
  373. tolerance);
  374. //> formatC(dgeom(2, 0.9999), digits=17) [1] "9.9989999999977806e-09"
  375. BOOST_CHECK_CLOSE_FRACTION(
  376. pdf(geometric_distribution<RealType>(static_cast<RealType>(0.9999L)),
  377. static_cast<RealType>(2)), // k = 0
  378. static_cast<RealType>(9.999e-9L), // 'exact'
  379. 1000*tolerance);
  380. BOOST_CHECK_CLOSE_FRACTION(
  381. pdf(geometric_distribution<RealType>(static_cast<RealType>(0.9999L)),
  382. static_cast<RealType>(3)), // k = 3
  383. static_cast<RealType>(9.999e-13L), // get
  384. 1000*tolerance);
  385. BOOST_CHECK_CLOSE_FRACTION(
  386. pdf(geometric_distribution<RealType>(static_cast<RealType>(0.9999L)),
  387. static_cast<RealType>(5)), // k = 5
  388. static_cast<RealType>(9.999e-21L), // 9.9989999999944947e-021
  389. 1000*tolerance);
  390. BOOST_CHECK_CLOSE_FRACTION(
  391. pdf(geometric_distribution<RealType>( static_cast<RealType>(0.0001L)),
  392. static_cast<RealType>(3)), // k = 0.
  393. static_cast<RealType>(9.99700029999e-5L), //
  394. tolerance);
  395. // Tests on cdf:
  396. // MathCAD pgeom k, r, p) == failures, successes, probability.
  397. BOOST_CHECK_CLOSE_FRACTION(cdf(
  398. geometric_distribution<RealType>(static_cast<RealType>(0.5)), // prob 0.5
  399. static_cast<RealType>(0) ), // k = 0
  400. static_cast<RealType>(0.5), // probability =p
  401. tolerance);
  402. BOOST_CHECK_CLOSE_FRACTION(cdf(complement(
  403. geometric_distribution<RealType>(static_cast<RealType>(0.5)), //
  404. static_cast<RealType>(0) )), // k = 0
  405. static_cast<RealType>(0.5), // probability =
  406. tolerance);
  407. BOOST_CHECK_CLOSE_FRACTION(cdf(
  408. geometric_distribution<RealType>(static_cast<RealType>(0.25)), // prob 0.5
  409. static_cast<RealType>(1) ), // k = 0
  410. static_cast<RealType>(0.4375L), // probability =p
  411. tolerance);
  412. BOOST_CHECK_CLOSE_FRACTION(cdf(complement(
  413. geometric_distribution<RealType>(static_cast<RealType>(0.25)), //
  414. static_cast<RealType>(1) )), // k = 0
  415. static_cast<RealType>(1-0.4375L), // probability =
  416. tolerance);
  417. BOOST_CHECK_CLOSE_FRACTION(cdf(complement(
  418. geometric_distribution<RealType>(static_cast<RealType>(0.5)), //
  419. static_cast<RealType>(1) )), // k = 0
  420. static_cast<RealType>(0.25), // probability = exact 0.25
  421. tolerance);
  422. BOOST_CHECK_CLOSE_FRACTION( //
  423. cdf(geometric_distribution<RealType>(static_cast<RealType>(0.5)),
  424. static_cast<RealType>(4)), // k =4.
  425. static_cast<RealType>(0.96875L), // exact
  426. tolerance);
  427. // Tests of other functions, mean and other moments ...
  428. geometric_distribution<RealType> dist(static_cast<RealType>(0.25));
  429. // mean:
  430. BOOST_CHECK_CLOSE_FRACTION(
  431. mean(dist), static_cast<RealType>((1 - 0.25) /0.25), tol5eps);
  432. BOOST_CHECK_CLOSE_FRACTION(
  433. mode(dist), static_cast<RealType>(0), tol1eps);
  434. // variance:
  435. BOOST_CHECK_CLOSE_FRACTION(
  436. variance(dist), static_cast<RealType>((1 - 0.25) / (0.25 * 0.25)), tol5eps);
  437. // std deviation:
  438. // sqrt(0.75/0.125)
  439. BOOST_CHECK_CLOSE_FRACTION(
  440. standard_deviation(dist), //
  441. static_cast<RealType>(sqrt((1.0L - 0.25L) / (0.25L * 0.25L))), // using 100 digit calc
  442. tol5eps);
  443. BOOST_CHECK_CLOSE_FRACTION(
  444. skewness(dist), //
  445. static_cast<RealType>((2-0.25L) /sqrt(0.75L)),
  446. // using calculator
  447. tol5eps);
  448. BOOST_CHECK_CLOSE_FRACTION(
  449. kurtosis_excess(dist), //
  450. static_cast<RealType>(6 + 0.0625L/0.75L), //
  451. tol5eps);
  452. // 6.083333333333333 6.166666666666667
  453. BOOST_CHECK_CLOSE_FRACTION(
  454. kurtosis(dist), // true
  455. static_cast<RealType>(9 + 0.0625L/0.75L), //
  456. tol5eps);
  457. // hazard:
  458. RealType x = static_cast<RealType>(0.125);
  459. BOOST_CHECK_CLOSE_FRACTION(
  460. hazard(dist, x)
  461. , pdf(dist, x) / cdf(complement(dist, x)), tol5eps);
  462. // cumulative hazard:
  463. BOOST_CHECK_CLOSE_FRACTION(
  464. chf(dist, x), -log(cdf(complement(dist, x))), tol5eps);
  465. // coefficient_of_variation:
  466. BOOST_CHECK_CLOSE_FRACTION(
  467. coefficient_of_variation(dist)
  468. , standard_deviation(dist) / mean(dist), tol5eps);
  469. // Special cases for PDF:
  470. BOOST_CHECK_EQUAL(
  471. pdf(
  472. geometric_distribution<RealType>(static_cast<RealType>(0)), //
  473. static_cast<RealType>(0)),
  474. static_cast<RealType>(0) );
  475. BOOST_CHECK_EQUAL(
  476. pdf(
  477. geometric_distribution<RealType>(static_cast<RealType>(0)),
  478. static_cast<RealType>(0.0001)),
  479. static_cast<RealType>(0) );
  480. BOOST_CHECK_EQUAL(
  481. pdf(
  482. geometric_distribution<RealType>(static_cast<RealType>(1)),
  483. static_cast<RealType>(0.001)),
  484. static_cast<RealType>(0) );
  485. BOOST_CHECK_EQUAL(
  486. pdf(
  487. geometric_distribution<RealType>(static_cast<RealType>(1)),
  488. static_cast<RealType>(8)),
  489. static_cast<RealType>(0) );
  490. BOOST_CHECK_SMALL(
  491. pdf(
  492. geometric_distribution<RealType>(static_cast<RealType>(0.25)),
  493. static_cast<RealType>(0))-
  494. static_cast<RealType>(0.25),
  495. 2 * boost::math::tools::epsilon<RealType>() ); // Expect exact, but not quite.
  496. // numeric_limits<RealType>::epsilon()); // Not suitable for real concept!
  497. // Quantile boundary cases checks:
  498. BOOST_CHECK_EQUAL(
  499. quantile( // zero P < cdf(0) so should be exactly zero.
  500. geometric_distribution<RealType>(static_cast<RealType>(0.25)),
  501. static_cast<RealType>(0)),
  502. static_cast<RealType>(0));
  503. BOOST_CHECK_EQUAL(
  504. quantile( // min P < cdf(0) so should be exactly zero.
  505. geometric_distribution<RealType>(static_cast<RealType>(0.25)),
  506. static_cast<RealType>(boost::math::tools::min_value<RealType>())),
  507. static_cast<RealType>(0));
  508. BOOST_CHECK_CLOSE_FRACTION(
  509. quantile( // Small P < cdf(0) so should be near zero.
  510. geometric_distribution<RealType>(static_cast<RealType>(0.25)),
  511. static_cast<RealType>(boost::math::tools::epsilon<RealType>())), //
  512. static_cast<RealType>(0),
  513. tol5eps);
  514. BOOST_CHECK_CLOSE_FRACTION(
  515. quantile( // Small P < cdf(0) so should be exactly zero.
  516. geometric_distribution<RealType>(static_cast<RealType>(0.25)),
  517. static_cast<RealType>(0.0001)),
  518. static_cast<RealType>(0),
  519. tolerance);
  520. //BOOST_CHECK( // Fails with overflow for real_concept
  521. //quantile( // Small P near 1 so k failures should be big.
  522. //geometric_distribution<RealType>(static_cast<RealType>(8), static_cast<RealType>(0.25)),
  523. //static_cast<RealType>(1 - boost::math::tools::epsilon<RealType>())) <=
  524. //static_cast<RealType>(189.56999032670058) // 106.462769 for float
  525. //);
  526. if(std::numeric_limits<RealType>::has_infinity)
  527. { // BOOST_CHECK tests for infinity using std::numeric_limits<>::infinity()
  528. // Note that infinity is not implemented for real_concept, so these tests
  529. // are only done for types, like built-in float, double.. that have infinity.
  530. // Note that these assume that BOOST_MATH_OVERFLOW_ERROR_POLICY is NOT throw_on_error.
  531. // #define BOOST_MATH_THROW_ON_OVERFLOW_POLICY == throw_on_error would throw here.
  532. // #define BOOST_MAT_DOMAIN_ERROR_POLICY IS defined throw_on_error,
  533. // so the throw path of error handling is tested below with BOOST_MATH_CHECK_THROW tests.
  534. BOOST_CHECK(
  535. quantile( // At P == 1 so k failures should be infinite.
  536. geometric_distribution<RealType>(static_cast<RealType>(0.25)),
  537. static_cast<RealType>(1)) ==
  538. //static_cast<RealType>(boost::math::tools::infinity<RealType>())
  539. static_cast<RealType>(std::numeric_limits<RealType>::infinity()) );
  540. BOOST_CHECK_EQUAL(
  541. quantile( // At 1 == P so should be infinite.
  542. geometric_distribution<RealType>( static_cast<RealType>(0.25)),
  543. static_cast<RealType>(1)), //
  544. std::numeric_limits<RealType>::infinity() );
  545. BOOST_CHECK_EQUAL(
  546. quantile(complement( // Q zero 1 so P == 1 < cdf(0) so should be exactly infinity.
  547. geometric_distribution<RealType>(static_cast<RealType>(0.25)),
  548. static_cast<RealType>(0))),
  549. std::numeric_limits<RealType>::infinity() );
  550. } // test for infinity using std::numeric_limits<>::infinity()
  551. else
  552. { // real_concept case, so check it throws rather than returning infinity.
  553. BOOST_CHECK_EQUAL(
  554. quantile( // At P == 1 so k failures should be infinite.
  555. geometric_distribution<RealType>(static_cast<RealType>(0.25)),
  556. static_cast<RealType>(1)),
  557. boost::math::tools::max_value<RealType>() );
  558. BOOST_CHECK_EQUAL(
  559. quantile(complement( // Q zero 1 so P == 1 < cdf(0) so should be exactly infinity.
  560. geometric_distribution<RealType>(static_cast<RealType>(0.25)),
  561. static_cast<RealType>(0))),
  562. boost::math::tools::max_value<RealType>());
  563. } // has infinity
  564. BOOST_CHECK( // Should work for built-in and real_concept.
  565. quantile(complement( // Q near to 1 so P nearly 1, so should be large > 300.
  566. geometric_distribution<RealType>(static_cast<RealType>(0.25)),
  567. static_cast<RealType>(boost::math::tools::min_value<RealType>())))
  568. >= static_cast<RealType>(300) );
  569. BOOST_CHECK_EQUAL(
  570. quantile( // P == 0 < cdf(0) so should be zero.
  571. geometric_distribution<RealType>(static_cast<RealType>(0.25)),
  572. static_cast<RealType>(0)),
  573. static_cast<RealType>(0));
  574. // Quantile Complement boundary cases:
  575. BOOST_CHECK_EQUAL(
  576. quantile(complement( // Q = 1 so P = 0 < cdf(0) so should be exactly zero.
  577. geometric_distribution<RealType>( static_cast<RealType>(0.25)),
  578. static_cast<RealType>(1))),
  579. static_cast<RealType>(0)
  580. );
  581. BOOST_CHECK_EQUAL(
  582. quantile(complement( // Q very near 1 so P == epsilon < cdf(0) so should be exactly zero.
  583. geometric_distribution<RealType>(static_cast<RealType>(0.25)),
  584. static_cast<RealType>(1 - boost::math::tools::epsilon<RealType>()))),
  585. static_cast<RealType>(0)
  586. );
  587. // Check that duff arguments throw domain_error:
  588. BOOST_MATH_CHECK_THROW(
  589. pdf( // Negative success_fraction!
  590. geometric_distribution<RealType>(static_cast<RealType>(-0.25)),
  591. static_cast<RealType>(0)), std::domain_error);
  592. BOOST_MATH_CHECK_THROW(
  593. pdf( // Success_fraction > 1!
  594. geometric_distribution<RealType>(static_cast<RealType>(1.25)),
  595. static_cast<RealType>(0)),
  596. std::domain_error);
  597. BOOST_MATH_CHECK_THROW(
  598. pdf( // Negative k argument !
  599. geometric_distribution<RealType>(static_cast<RealType>(0.25)),
  600. static_cast<RealType>(-1)),
  601. std::domain_error);
  602. //BOOST_MATH_CHECK_THROW(
  603. //pdf( // check limit on k (failures)
  604. //geometric_distribution<RealType>(static_cast<RealType>(0.25)),
  605. //std::numeric_limits<RealType>infinity()),
  606. //std::domain_error);
  607. BOOST_MATH_CHECK_THROW(
  608. cdf( // Negative k argument !
  609. geometric_distribution<RealType>(static_cast<RealType>(0.25)),
  610. static_cast<RealType>(-1)),
  611. std::domain_error);
  612. BOOST_MATH_CHECK_THROW(
  613. cdf( // Negative success_fraction!
  614. geometric_distribution<RealType>(static_cast<RealType>(-0.25)),
  615. static_cast<RealType>(0)), std::domain_error);
  616. BOOST_MATH_CHECK_THROW(
  617. cdf( // Success_fraction > 1!
  618. geometric_distribution<RealType>(static_cast<RealType>(1.25)),
  619. static_cast<RealType>(0)), std::domain_error);
  620. BOOST_MATH_CHECK_THROW(
  621. quantile( // Negative success_fraction!
  622. geometric_distribution<RealType>(static_cast<RealType>(-0.25)),
  623. static_cast<RealType>(0)), std::domain_error);
  624. BOOST_MATH_CHECK_THROW(
  625. quantile( // Success_fraction > 1!
  626. geometric_distribution<RealType>(static_cast<RealType>(1.25)),
  627. static_cast<RealType>(0)), std::domain_error);
  628. check_out_of_range<geometric_distribution<RealType> >(0.5);
  629. // End of check throwing 'duff' out-of-domain values.
  630. { // Compare geometric and negative binomial functions.
  631. using boost::math::negative_binomial_distribution;
  632. using boost::math::geometric_distribution;
  633. RealType k = static_cast<RealType>(2.L);
  634. RealType alpha = static_cast<RealType>(0.05L);
  635. RealType p = static_cast<RealType>(0.5L);
  636. BOOST_CHECK_CLOSE_FRACTION( // Successes parameter in negative binomial is 1 for geometric.
  637. geometric_distribution<RealType>::find_lower_bound_on_p(k, alpha),
  638. negative_binomial_distribution<RealType>::find_lower_bound_on_p(k, static_cast<RealType>(1), alpha),
  639. tolerance);
  640. BOOST_CHECK_CLOSE_FRACTION( // Successes parameter in negative binomial is 1 for geometric.
  641. geometric_distribution<RealType>::find_upper_bound_on_p(k, alpha),
  642. negative_binomial_distribution<RealType>::find_upper_bound_on_p(k, static_cast<RealType>(1), alpha),
  643. tolerance);
  644. BOOST_CHECK_CLOSE_FRACTION( // Should be identical - successes parameter is not used.
  645. geometric_distribution<RealType>::find_maximum_number_of_trials(k, p, alpha),
  646. negative_binomial_distribution<RealType>::find_maximum_number_of_trials(k, p, alpha),
  647. tolerance);
  648. }
  649. //geometric::find_upper_bound_on_p(k, alpha);
  650. return;
  651. } // template <class RealType> void test_spots(RealType) // Any floating-point type RealType.
  652. BOOST_AUTO_TEST_CASE( test_main )
  653. {
  654. // Check that can generate geometric distribution using the two convenience methods:
  655. using namespace boost::math;
  656. geometric g05d(0.5); // Using typedef - default type is double.
  657. geometric_distribution<> g05dd(0.5); // Using default RealType double.
  658. // Basic sanity-check spot values.
  659. // Test some simple double only examples.
  660. geometric_distribution<double> mydist(0.25);
  661. // success fraction == 0.25 == 25% or 1 in 4 successes.
  662. // Note: double values (matching the distribution definition) avoid the need for any casting.
  663. // Check accessor functions return exact values for double at least.
  664. BOOST_CHECK_EQUAL(mydist.success_fraction(), static_cast<double>(1./4.));
  665. //cout << numeric_limits<RealType>::epsilon() << endl;
  666. // (Parameter value, arbitrarily zero, only communicates the floating point type).
  667. #ifdef TEST_FLOAT
  668. test_spots(0.0F); // Test float.
  669. #endif
  670. #ifdef TEST_DOUBLE
  671. test_spots(0.0); // Test double.
  672. #endif
  673. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  674. #ifdef TEST_LDOUBLE
  675. test_spots(0.0L); // Test long double.
  676. #endif
  677. #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
  678. #ifdef TEST_REAL_CONCEPT
  679. test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
  680. #endif
  681. #endif
  682. #else
  683. std::cout << "<note>The long double tests have been disabled on this platform "
  684. "either because the long double overloads of the usual math functions are "
  685. "not available at all, or because they are too inaccurate for these tests "
  686. "to pass.</note>" << std::endl;
  687. #endif
  688. } // BOOST_AUTO_TEST_CASE( test_main )
  689. /*
  690. */