test_beta_dist.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. // test_beta_dist.cpp
  2. // Copyright John Maddock 2006.
  3. // Copyright Paul A. Bristow 2007, 2009, 2010, 2012.
  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 tests for the beta Distribution.
  9. // http://members.aol.com/iandjmsmith/BETAEX.HTM beta distribution calculator
  10. // Appreas to be a 64-bit calculator showing 17 decimal digit (last is noisy).
  11. // Similar to mathCAD?
  12. // http://www.nuhertz.com/statmat/distributions.html#Beta
  13. // Pretty graphs and explanations for most distributions.
  14. // http://functions.wolfram.com/webMathematica/FunctionEvaluation.jsp
  15. // provided 40 decimal digits accuracy incomplete beta aka beta regularized == cdf
  16. // http://www.ausvet.com.au/pprev/content.php?page=PPscript
  17. // mode 0.75 5/95% 0.9 alpha 7.39 beta 3.13
  18. // http://www.epi.ucdavis.edu/diagnostictests/betabuster.html
  19. // Beta Buster also calculates alpha and beta from mode & percentile estimates.
  20. // This is NOT (yet) implemented.
  21. #ifdef _MSC_VER
  22. # pragma warning(disable: 4127) // conditional expression is constant.
  23. # pragma warning (disable : 4996) // POSIX name for this item is deprecated.
  24. # pragma warning (disable : 4224) // nonstandard extension used : formal parameter 'arg' was previously defined as a type.
  25. #endif
  26. #include <boost/math/concepts/real_concept.hpp> // for real_concept
  27. using ::boost::math::concepts::real_concept;
  28. #include <boost/math/tools/test.hpp>
  29. #include <boost/math/distributions/beta.hpp> // for beta_distribution
  30. using boost::math::beta_distribution;
  31. using boost::math::beta;
  32. #define BOOST_TEST_MAIN
  33. #include <boost/test/unit_test.hpp> // for test_main
  34. #include <boost/test/tools/floating_point_comparison.hpp> // for BOOST_CHECK_CLOSE_FRACTION
  35. #include "test_out_of_range.hpp"
  36. #include <iostream>
  37. using std::cout;
  38. using std::endl;
  39. #include <limits>
  40. using std::numeric_limits;
  41. template <class RealType>
  42. void test_spot(
  43. RealType a, // alpha a
  44. RealType b, // beta b
  45. RealType x, // Probability
  46. RealType P, // CDF of beta(a, b)
  47. RealType Q, // Complement of CDF
  48. RealType tol) // Test tolerance.
  49. {
  50. boost::math::beta_distribution<RealType> abeta(a, b);
  51. BOOST_CHECK_CLOSE_FRACTION(cdf(abeta, x), P, tol);
  52. if((P < 0.99) && (Q < 0.99))
  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. // (and similarly for Q)
  56. BOOST_CHECK_CLOSE_FRACTION(
  57. cdf(complement(abeta, x)), Q, tol);
  58. if(x != 0)
  59. {
  60. BOOST_CHECK_CLOSE_FRACTION(
  61. quantile(abeta, P), x, 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(abeta, P) < boost::math::tools::epsilon<RealType>() * 10);
  72. }
  73. } // if k
  74. if(x != 0)
  75. {
  76. BOOST_CHECK_CLOSE_FRACTION(quantile(complement(abeta, Q)), x, tol);
  77. }
  78. else
  79. { // Just check quantile is very small:
  80. if((std::numeric_limits<RealType>::max_exponent <= std::numeric_limits<double>::max_exponent) && (boost::is_floating_point<RealType>::value))
  81. { // Limit where this is checked: if exponent range is very large we may
  82. // run out of iterations in our root finding algorithm.
  83. BOOST_CHECK(quantile(complement(abeta, Q)) < boost::math::tools::epsilon<RealType>() * 10);
  84. }
  85. } // if x
  86. // Estimate alpha & beta from mean and variance:
  87. BOOST_CHECK_CLOSE_FRACTION(
  88. beta_distribution<RealType>::find_alpha(mean(abeta), variance(abeta)),
  89. abeta.alpha(), tol);
  90. BOOST_CHECK_CLOSE_FRACTION(
  91. beta_distribution<RealType>::find_beta(mean(abeta), variance(abeta)),
  92. abeta.beta(), tol);
  93. // Estimate sample alpha and beta from others:
  94. BOOST_CHECK_CLOSE_FRACTION(
  95. beta_distribution<RealType>::find_alpha(abeta.beta(), x, P),
  96. abeta.alpha(), tol);
  97. BOOST_CHECK_CLOSE_FRACTION(
  98. beta_distribution<RealType>::find_beta(abeta.alpha(), x, P),
  99. abeta.beta(), tol);
  100. } // if((P < 0.99) && (Q < 0.99)
  101. } // template <class RealType> void test_spot
  102. template <class RealType> // Any floating-point type RealType.
  103. void test_spots(RealType)
  104. {
  105. // Basic sanity checks with 'known good' values.
  106. // MathCAD test data is to double precision only,
  107. // so set tolerance to 100 eps expressed as a fraction, or
  108. // 100 eps of type double expressed as a fraction,
  109. // whichever is the larger.
  110. RealType tolerance = (std::max)
  111. (boost::math::tools::epsilon<RealType>(),
  112. static_cast<RealType>(std::numeric_limits<double>::epsilon())); // 0 if real_concept.
  113. cout << "Boost::math::tools::epsilon = " << boost::math::tools::epsilon<RealType>() <<endl;
  114. cout << "std::numeric_limits::epsilon = " << std::numeric_limits<RealType>::epsilon() <<endl;
  115. cout << "epsilon = " << tolerance;
  116. tolerance *= 100000; // Note: NO * 100 because is fraction, NOT %.
  117. cout << ", Tolerance = " << tolerance * 100 << "%." << endl;
  118. // RealType teneps = boost::math::tools::epsilon<RealType>() * 10;
  119. // Sources of spot test values:
  120. // MathCAD defines dbeta(x, s1, s2) pdf, s1 == alpha, s2 = beta, x = x in Wolfram
  121. // pbeta(x, s1, s2) cdf and qbeta(x, s1, s2) inverse of cdf
  122. // returns pr(X ,= x) when random variable X
  123. // has the beta distribution with parameters s1)alpha) and s2(beta).
  124. // s1 > 0 and s2 >0 and 0 < x < 1 (but allows x == 0! and x == 1!)
  125. // dbeta(0,1,1) = 0
  126. // dbeta(0.5,1,1) = 1
  127. using boost::math::beta_distribution;
  128. using ::boost::math::cdf;
  129. using ::boost::math::pdf;
  130. // Tests that should throw:
  131. BOOST_MATH_CHECK_THROW(mode(beta_distribution<RealType>(static_cast<RealType>(1), static_cast<RealType>(1))), std::domain_error);
  132. // mode is undefined, and throws domain_error!
  133. // BOOST_MATH_CHECK_THROW(median(beta_distribution<RealType>(static_cast<RealType>(1), static_cast<RealType>(1))), std::domain_error);
  134. // median is undefined, and throws domain_error!
  135. // But now median IS provided via derived accessor as quantile(half).
  136. BOOST_MATH_CHECK_THROW( // For various bad arguments.
  137. pdf(
  138. beta_distribution<RealType>(static_cast<RealType>(-1), static_cast<RealType>(1)), // bad alpha < 0.
  139. static_cast<RealType>(1)), std::domain_error);
  140. BOOST_MATH_CHECK_THROW(
  141. pdf(
  142. beta_distribution<RealType>(static_cast<RealType>(0), static_cast<RealType>(1)), // bad alpha == 0.
  143. static_cast<RealType>(1)), std::domain_error);
  144. BOOST_MATH_CHECK_THROW(
  145. pdf(
  146. beta_distribution<RealType>(static_cast<RealType>(1), static_cast<RealType>(0)), // bad beta == 0.
  147. static_cast<RealType>(1)), std::domain_error);
  148. BOOST_MATH_CHECK_THROW(
  149. pdf(
  150. beta_distribution<RealType>(static_cast<RealType>(1), static_cast<RealType>(-1)), // bad beta < 0.
  151. static_cast<RealType>(1)), std::domain_error);
  152. BOOST_MATH_CHECK_THROW(
  153. pdf(
  154. beta_distribution<RealType>(static_cast<RealType>(1), static_cast<RealType>(1)), // bad x < 0.
  155. static_cast<RealType>(-1)), std::domain_error);
  156. BOOST_MATH_CHECK_THROW(
  157. pdf(
  158. beta_distribution<RealType>(static_cast<RealType>(1), static_cast<RealType>(1)), // bad x > 1.
  159. static_cast<RealType>(999)), std::domain_error);
  160. // Some exact pdf values.
  161. BOOST_CHECK_EQUAL( // a = b = 1 is uniform distribution.
  162. pdf(beta_distribution<RealType>(static_cast<RealType>(1), static_cast<RealType>(1)),
  163. static_cast<RealType>(1)), // x
  164. static_cast<RealType>(1));
  165. BOOST_CHECK_EQUAL(
  166. pdf(beta_distribution<RealType>(static_cast<RealType>(1), static_cast<RealType>(1)),
  167. static_cast<RealType>(0)), // x
  168. static_cast<RealType>(1));
  169. BOOST_CHECK_CLOSE_FRACTION(
  170. pdf(beta_distribution<RealType>(static_cast<RealType>(1), static_cast<RealType>(1)),
  171. static_cast<RealType>(0.5)), // x
  172. static_cast<RealType>(1),
  173. tolerance);
  174. BOOST_CHECK_EQUAL(
  175. beta_distribution<RealType>(static_cast<RealType>(1), static_cast<RealType>(1)).alpha(),
  176. static_cast<RealType>(1) ); //
  177. BOOST_CHECK_EQUAL(
  178. mean(beta_distribution<RealType>(static_cast<RealType>(1), static_cast<RealType>(1))),
  179. static_cast<RealType>(0.5) ); // Exact one half.
  180. BOOST_CHECK_CLOSE_FRACTION(
  181. pdf(beta_distribution<RealType>(static_cast<RealType>(2), static_cast<RealType>(2)),
  182. static_cast<RealType>(0.5)), // x
  183. static_cast<RealType>(1.5), // Exactly 3/2
  184. tolerance);
  185. BOOST_CHECK_CLOSE_FRACTION(
  186. pdf(beta_distribution<RealType>(static_cast<RealType>(2), static_cast<RealType>(2)),
  187. static_cast<RealType>(0.5)), // x
  188. static_cast<RealType>(1.5), // Exactly 3/2
  189. tolerance);
  190. // CDF
  191. BOOST_CHECK_CLOSE_FRACTION(
  192. cdf(beta_distribution<RealType>(static_cast<RealType>(2), static_cast<RealType>(2)),
  193. static_cast<RealType>(0.1)), // x
  194. static_cast<RealType>(0.02800000000000000000000000000000000000000L), // Seems exact.
  195. // http://functions.wolfram.com/webMathematica/FunctionEvaluation.jsp?name=BetaRegularized&ptype=0&z=0.1&a=2&b=2&digits=40
  196. tolerance);
  197. BOOST_CHECK_CLOSE_FRACTION(
  198. cdf(beta_distribution<RealType>(static_cast<RealType>(2), static_cast<RealType>(2)),
  199. static_cast<RealType>(0.0001)), // x
  200. static_cast<RealType>(2.999800000000000000000000000000000000000e-8L),
  201. // http://members.aol.com/iandjmsmith/BETAEX.HTM 2.9998000000004
  202. // http://functions.wolfram.com/webMathematica/FunctionEvaluation.jsp?name=BetaRegularized&ptype=0&z=0.0001&a=2&b=2&digits=40
  203. tolerance);
  204. BOOST_CHECK_CLOSE_FRACTION(
  205. pdf(beta_distribution<RealType>(static_cast<RealType>(2), static_cast<RealType>(2)),
  206. static_cast<RealType>(0.0001)), // x
  207. static_cast<RealType>(0.0005999400000000004L), // http://members.aol.com/iandjmsmith/BETAEX.HTM
  208. // Slightly higher tolerance for real concept:
  209. (std::numeric_limits<RealType>::is_specialized ? 1 : 10) * tolerance);
  210. BOOST_CHECK_CLOSE_FRACTION(
  211. cdf(beta_distribution<RealType>(static_cast<RealType>(2), static_cast<RealType>(2)),
  212. static_cast<RealType>(0.9999)), // x
  213. static_cast<RealType>(0.999999970002L), // http://members.aol.com/iandjmsmith/BETAEX.HTM
  214. // Wolfram 0.9999999700020000000000000000000000000000
  215. tolerance);
  216. BOOST_CHECK_CLOSE_FRACTION(
  217. cdf(beta_distribution<RealType>(static_cast<RealType>(0.5), static_cast<RealType>(2)),
  218. static_cast<RealType>(0.9)), // x
  219. static_cast<RealType>(0.9961174629530394895796514664963063381217L),
  220. // Wolfram
  221. tolerance);
  222. BOOST_CHECK_CLOSE_FRACTION(
  223. cdf(beta_distribution<RealType>(static_cast<RealType>(0.5), static_cast<RealType>(0.5)),
  224. static_cast<RealType>(0.1)), // x
  225. static_cast<RealType>(0.2048327646991334516491978475505189480977L),
  226. // Wolfram
  227. tolerance);
  228. BOOST_CHECK_CLOSE_FRACTION(
  229. cdf(beta_distribution<RealType>(static_cast<RealType>(0.5), static_cast<RealType>(0.5)),
  230. static_cast<RealType>(0.9)), // x
  231. static_cast<RealType>(0.7951672353008665483508021524494810519023L),
  232. // Wolfram
  233. tolerance);
  234. BOOST_CHECK_CLOSE_FRACTION(
  235. quantile(beta_distribution<RealType>(static_cast<RealType>(0.5), static_cast<RealType>(0.5)),
  236. static_cast<RealType>(0.7951672353008665483508021524494810519023L)), // x
  237. static_cast<RealType>(0.9),
  238. // Wolfram
  239. tolerance);
  240. BOOST_CHECK_CLOSE_FRACTION(
  241. cdf(beta_distribution<RealType>(static_cast<RealType>(0.5), static_cast<RealType>(0.5)),
  242. static_cast<RealType>(0.6)), // x
  243. static_cast<RealType>(0.5640942168489749316118742861695149357858L),
  244. // Wolfram
  245. tolerance);
  246. BOOST_CHECK_CLOSE_FRACTION(
  247. quantile(beta_distribution<RealType>(static_cast<RealType>(0.5), static_cast<RealType>(0.5)),
  248. static_cast<RealType>(0.5640942168489749316118742861695149357858L)), // x
  249. static_cast<RealType>(0.6),
  250. // Wolfram
  251. tolerance);
  252. BOOST_CHECK_CLOSE_FRACTION(
  253. cdf(beta_distribution<RealType>(static_cast<RealType>(2), static_cast<RealType>(0.5)),
  254. static_cast<RealType>(0.6)), // x
  255. static_cast<RealType>(0.1778078083562213736802876784474931812329L),
  256. // Wolfram
  257. tolerance);
  258. BOOST_CHECK_CLOSE_FRACTION(
  259. quantile(beta_distribution<RealType>(static_cast<RealType>(2), static_cast<RealType>(0.5)),
  260. static_cast<RealType>(0.1778078083562213736802876784474931812329L)), // x
  261. static_cast<RealType>(0.6),
  262. // Wolfram
  263. tolerance); // gives
  264. BOOST_CHECK_CLOSE_FRACTION(
  265. cdf(beta_distribution<RealType>(static_cast<RealType>(1), static_cast<RealType>(1)),
  266. static_cast<RealType>(0.1)), // x
  267. static_cast<RealType>(0.1), // 0.1000000000000000000000000000000000000000
  268. // Wolfram
  269. tolerance);
  270. BOOST_CHECK_CLOSE_FRACTION(
  271. quantile(beta_distribution<RealType>(static_cast<RealType>(1), static_cast<RealType>(1)),
  272. static_cast<RealType>(0.1)), // x
  273. static_cast<RealType>(0.1), // 0.1000000000000000000000000000000000000000
  274. // Wolfram
  275. tolerance);
  276. BOOST_CHECK_CLOSE_FRACTION(
  277. cdf(complement(beta_distribution<RealType>(static_cast<RealType>(0.5), static_cast<RealType>(0.5)),
  278. static_cast<RealType>(0.1))), // complement of x
  279. static_cast<RealType>(0.7951672353008665483508021524494810519023L),
  280. // Wolfram
  281. tolerance);
  282. BOOST_CHECK_CLOSE_FRACTION(
  283. quantile(beta_distribution<RealType>(static_cast<RealType>(2), static_cast<RealType>(2)),
  284. static_cast<RealType>(0.0280000000000000000000000000000000000L)), // x
  285. static_cast<RealType>(0.1),
  286. // Wolfram
  287. tolerance);
  288. BOOST_CHECK_CLOSE_FRACTION(
  289. cdf(complement(beta_distribution<RealType>(static_cast<RealType>(2), static_cast<RealType>(2)),
  290. static_cast<RealType>(0.1))), // x
  291. static_cast<RealType>(0.9720000000000000000000000000000000000000L), // Exact.
  292. // Wolfram
  293. tolerance);
  294. BOOST_CHECK_CLOSE_FRACTION(
  295. pdf(beta_distribution<RealType>(static_cast<RealType>(2), static_cast<RealType>(2)),
  296. static_cast<RealType>(0.9999)), // x
  297. static_cast<RealType>(0.0005999399999999344L), // http://members.aol.com/iandjmsmith/BETAEX.HTM
  298. tolerance*10); // Note loss of precision calculating 1-p test value.
  299. //void test_spot(
  300. // RealType a, // alpha a
  301. // RealType b, // beta b
  302. // RealType x, // Probability
  303. // RealType P, // CDF of beta(a, b)
  304. // RealType Q, // Complement of CDF
  305. // RealType tol) // Test tolerance.
  306. // These test quantiles and complements, and parameter estimates as well.
  307. // Spot values using, for example:
  308. // http://functions.wolfram.com/webMathematica/FunctionEvaluation.jsp?name=BetaRegularized&ptype=0&z=0.1&a=0.5&b=3&digits=40
  309. test_spot(
  310. static_cast<RealType>(1), // alpha a
  311. static_cast<RealType>(1), // beta b
  312. static_cast<RealType>(0.1), // Probability p
  313. static_cast<RealType>(0.1), // Probability of result (CDF of beta), P
  314. static_cast<RealType>(0.9), // Complement of CDF Q = 1 - P
  315. tolerance); // Test tolerance.
  316. test_spot(
  317. static_cast<RealType>(2), // alpha a
  318. static_cast<RealType>(2), // beta b
  319. static_cast<RealType>(0.1), // Probability p
  320. static_cast<RealType>(0.0280000000000000000000000000000000000L), // Probability of result (CDF of beta), P
  321. static_cast<RealType>(1 - 0.0280000000000000000000000000000000000L), // Complement of CDF Q = 1 - P
  322. tolerance); // Test tolerance.
  323. test_spot(
  324. static_cast<RealType>(2), // alpha a
  325. static_cast<RealType>(2), // beta b
  326. static_cast<RealType>(0.5), // Probability p
  327. static_cast<RealType>(0.5), // Probability of result (CDF of beta), P
  328. static_cast<RealType>(0.5), // Complement of CDF Q = 1 - P
  329. tolerance); // Test tolerance.
  330. test_spot(
  331. static_cast<RealType>(2), // alpha a
  332. static_cast<RealType>(2), // beta b
  333. static_cast<RealType>(0.9), // Probability p
  334. static_cast<RealType>(0.972000000000000), // Probability of result (CDF of beta), P
  335. static_cast<RealType>(1-0.972000000000000), // Complement of CDF Q = 1 - P
  336. tolerance); // Test tolerance.
  337. test_spot(
  338. static_cast<RealType>(2), // alpha a
  339. static_cast<RealType>(2), // beta b
  340. static_cast<RealType>(0.01), // Probability p
  341. static_cast<RealType>(0.0002980000000000000000000000000000000000000L), // Probability of result (CDF of beta), P
  342. static_cast<RealType>(1-0.0002980000000000000000000000000000000000000L), // Complement of CDF Q = 1 - P
  343. tolerance); // Test tolerance.
  344. test_spot(
  345. static_cast<RealType>(2), // alpha a
  346. static_cast<RealType>(2), // beta b
  347. static_cast<RealType>(0.001), // Probability p
  348. static_cast<RealType>(2.998000000000000000000000000000000000000E-6L), // Probability of result (CDF of beta), P
  349. static_cast<RealType>(1-2.998000000000000000000000000000000000000E-6L), // Complement of CDF Q = 1 - P
  350. tolerance); // Test tolerance.
  351. test_spot(
  352. static_cast<RealType>(2), // alpha a
  353. static_cast<RealType>(2), // beta b
  354. static_cast<RealType>(0.0001), // Probability p
  355. static_cast<RealType>(2.999800000000000000000000000000000000000E-8L), // Probability of result (CDF of beta), P
  356. static_cast<RealType>(1-2.999800000000000000000000000000000000000E-8L), // Complement of CDF Q = 1 - P
  357. tolerance); // Test tolerance.
  358. test_spot(
  359. static_cast<RealType>(2), // alpha a
  360. static_cast<RealType>(2), // beta b
  361. static_cast<RealType>(0.99), // Probability p
  362. static_cast<RealType>(0.9997020000000000000000000000000000000000L), // Probability of result (CDF of beta), P
  363. static_cast<RealType>(1-0.9997020000000000000000000000000000000000L), // Complement of CDF Q = 1 - P
  364. tolerance); // Test tolerance.
  365. test_spot(
  366. static_cast<RealType>(0.5), // alpha a
  367. static_cast<RealType>(2), // beta b
  368. static_cast<RealType>(0.5), // Probability p
  369. static_cast<RealType>(0.8838834764831844055010554526310612991060L), // Probability of result (CDF of beta), P
  370. static_cast<RealType>(1-0.8838834764831844055010554526310612991060L), // Complement of CDF Q = 1 - P
  371. tolerance); // Test tolerance.
  372. test_spot(
  373. static_cast<RealType>(0.5), // alpha a
  374. static_cast<RealType>(3.), // beta b
  375. static_cast<RealType>(0.7), // Probability p
  376. static_cast<RealType>(0.9903963064097119299191611355232156905687L), // Probability of result (CDF of beta), P
  377. static_cast<RealType>(1-0.9903963064097119299191611355232156905687L), // Complement of CDF Q = 1 - P
  378. tolerance); // Test tolerance.
  379. test_spot(
  380. static_cast<RealType>(0.5), // alpha a
  381. static_cast<RealType>(3.), // beta b
  382. static_cast<RealType>(0.1), // Probability p
  383. static_cast<RealType>(0.5545844446520295253493059553548880128511L), // Probability of result (CDF of beta), P
  384. static_cast<RealType>(1-0.5545844446520295253493059553548880128511L), // Complement of CDF Q = 1 - P
  385. tolerance); // Test tolerance.
  386. //
  387. // Error checks:
  388. // Construction with 'bad' parameters.
  389. BOOST_MATH_CHECK_THROW(beta_distribution<RealType>(1, -1), std::domain_error);
  390. BOOST_MATH_CHECK_THROW(beta_distribution<RealType>(-1, 1), std::domain_error);
  391. BOOST_MATH_CHECK_THROW(beta_distribution<RealType>(1, 0), std::domain_error);
  392. BOOST_MATH_CHECK_THROW(beta_distribution<RealType>(0, 1), std::domain_error);
  393. beta_distribution<> dist;
  394. BOOST_MATH_CHECK_THROW(pdf(dist, -1), std::domain_error);
  395. BOOST_MATH_CHECK_THROW(cdf(dist, -1), std::domain_error);
  396. BOOST_MATH_CHECK_THROW(cdf(complement(dist, -1)), std::domain_error);
  397. BOOST_MATH_CHECK_THROW(quantile(dist, -1), std::domain_error);
  398. BOOST_MATH_CHECK_THROW(quantile(complement(dist, -1)), std::domain_error);
  399. BOOST_MATH_CHECK_THROW(quantile(dist, -1), std::domain_error);
  400. BOOST_MATH_CHECK_THROW(quantile(complement(dist, -1)), std::domain_error);
  401. // No longer allow any parameter to be NaN or inf, so all these tests should throw.
  402. if (std::numeric_limits<RealType>::has_quiet_NaN)
  403. {
  404. // Attempt to construct from non-finite should throw.
  405. RealType nan = std::numeric_limits<RealType>::quiet_NaN();
  406. #ifndef BOOST_NO_EXCEPTIONS
  407. BOOST_MATH_CHECK_THROW(beta_distribution<RealType> w(nan), std::domain_error);
  408. BOOST_MATH_CHECK_THROW(beta_distribution<RealType> w(1, nan), std::domain_error);
  409. #else
  410. BOOST_MATH_CHECK_THROW(beta_distribution<RealType>(nan), std::domain_error);
  411. BOOST_MATH_CHECK_THROW(beta_distribution<RealType>(1, nan), std::domain_error);
  412. #endif
  413. // Non-finite parameters should throw.
  414. beta_distribution<RealType> w(RealType(1));
  415. BOOST_MATH_CHECK_THROW(pdf(w, +nan), std::domain_error); // x = NaN
  416. BOOST_MATH_CHECK_THROW(cdf(w, +nan), std::domain_error); // x = NaN
  417. BOOST_MATH_CHECK_THROW(cdf(complement(w, +nan)), std::domain_error); // x = + nan
  418. BOOST_MATH_CHECK_THROW(quantile(w, +nan), std::domain_error); // p = + nan
  419. BOOST_MATH_CHECK_THROW(quantile(complement(w, +nan)), std::domain_error); // p = + nan
  420. } // has_quiet_NaN
  421. if (std::numeric_limits<RealType>::has_infinity)
  422. {
  423. // Attempt to construct from non-finite should throw.
  424. RealType inf = std::numeric_limits<RealType>::infinity();
  425. #ifndef BOOST_NO_EXCEPTIONS
  426. BOOST_MATH_CHECK_THROW(beta_distribution<RealType> w(inf), std::domain_error);
  427. BOOST_MATH_CHECK_THROW(beta_distribution<RealType> w(1, inf), std::domain_error);
  428. #else
  429. BOOST_MATH_CHECK_THROW(beta_distribution<RealType>(inf), std::domain_error);
  430. BOOST_MATH_CHECK_THROW(beta_distribution<RealType>(1, inf), std::domain_error);
  431. #endif
  432. // Non-finite parameters should throw.
  433. beta_distribution<RealType> w(RealType(1));
  434. #ifndef BOOST_NO_EXCEPTIONS
  435. BOOST_MATH_CHECK_THROW(beta_distribution<RealType> w(inf), std::domain_error);
  436. BOOST_MATH_CHECK_THROW(beta_distribution<RealType> w(1, inf), std::domain_error);
  437. #else
  438. BOOST_MATH_CHECK_THROW(beta_distribution<RealType>(inf), std::domain_error);
  439. BOOST_MATH_CHECK_THROW(beta_distribution<RealType>(1, inf), std::domain_error);
  440. #endif
  441. BOOST_MATH_CHECK_THROW(pdf(w, +inf), std::domain_error); // x = inf
  442. BOOST_MATH_CHECK_THROW(cdf(w, +inf), std::domain_error); // x = inf
  443. BOOST_MATH_CHECK_THROW(cdf(complement(w, +inf)), std::domain_error); // x = + inf
  444. BOOST_MATH_CHECK_THROW(quantile(w, +inf), std::domain_error); // p = + inf
  445. BOOST_MATH_CHECK_THROW(quantile(complement(w, +inf)), std::domain_error); // p = + inf
  446. } // has_infinity
  447. // Error handling checks:
  448. check_out_of_range<boost::math::beta_distribution<RealType> >(1, 1); // (All) valid constructor parameter values.
  449. // and range and non-finite.
  450. // Not needed??????
  451. BOOST_MATH_CHECK_THROW(pdf(boost::math::beta_distribution<RealType>(0, 1), 0), std::domain_error);
  452. BOOST_MATH_CHECK_THROW(pdf(boost::math::beta_distribution<RealType>(-1, 1), 0), std::domain_error);
  453. BOOST_MATH_CHECK_THROW(quantile(boost::math::beta_distribution<RealType>(1, 1), -1), std::domain_error);
  454. BOOST_MATH_CHECK_THROW(quantile(boost::math::beta_distribution<RealType>(1, 1), 2), std::domain_error);
  455. } // template <class RealType>void test_spots(RealType)
  456. BOOST_AUTO_TEST_CASE( test_main )
  457. {
  458. BOOST_MATH_CONTROL_FP;
  459. // Check that can generate beta distribution using one convenience methods:
  460. beta_distribution<> mybeta11(1., 1.); // Using default RealType double.
  461. // but that
  462. // boost::math::beta mybeta1(1., 1.); // Using typedef fails.
  463. // error C2039: 'beta' : is not a member of 'boost::math'
  464. // Basic sanity-check spot values.
  465. // Some simple checks using double only.
  466. BOOST_CHECK_EQUAL(mybeta11.alpha(), 1); //
  467. BOOST_CHECK_EQUAL(mybeta11.beta(), 1);
  468. BOOST_CHECK_EQUAL(mean(mybeta11), 0.5); // 1 / (1 + 1) = 1/2 exactly
  469. BOOST_MATH_CHECK_THROW(mode(mybeta11), std::domain_error);
  470. beta_distribution<> mybeta22(2., 2.); // pdf is dome shape.
  471. BOOST_CHECK_EQUAL(mode(mybeta22), 0.5); // 2-1 / (2+2-2) = 1/2 exactly.
  472. beta_distribution<> mybetaH2(0.5, 2.); //
  473. beta_distribution<> mybetaH3(0.5, 3.); //
  474. // Check a few values using double.
  475. BOOST_CHECK_EQUAL(pdf(mybeta11, 1), 1); // is uniform unity over 0 to 1,
  476. BOOST_CHECK_EQUAL(pdf(mybeta11, 0), 1); // including zero and unity.
  477. // Although these next three have an exact result, internally they're
  478. // *not* treated as special cases, and may be out by a couple of eps:
  479. BOOST_CHECK_CLOSE_FRACTION(pdf(mybeta11, 0.5), 1.0, 5*std::numeric_limits<double>::epsilon());
  480. BOOST_CHECK_CLOSE_FRACTION(pdf(mybeta11, 0.0001), 1.0, 5*std::numeric_limits<double>::epsilon());
  481. BOOST_CHECK_CLOSE_FRACTION(pdf(mybeta11, 0.9999), 1.0, 5*std::numeric_limits<double>::epsilon());
  482. BOOST_CHECK_CLOSE_FRACTION(cdf(mybeta11, 0.1), 0.1, 2 * std::numeric_limits<double>::epsilon());
  483. BOOST_CHECK_CLOSE_FRACTION(cdf(mybeta11, 0.5), 0.5, 2 * std::numeric_limits<double>::epsilon());
  484. BOOST_CHECK_CLOSE_FRACTION(cdf(mybeta11, 0.9), 0.9, 2 * std::numeric_limits<double>::epsilon());
  485. BOOST_CHECK_EQUAL(cdf(mybeta11, 1), 1.); // Exact unity expected.
  486. double tol = std::numeric_limits<double>::epsilon() * 10;
  487. BOOST_CHECK_EQUAL(pdf(mybeta22, 1), 0); // is dome shape.
  488. BOOST_CHECK_EQUAL(pdf(mybeta22, 0), 0);
  489. BOOST_CHECK_CLOSE_FRACTION(pdf(mybeta22, 0.5), 1.5, tol); // top of dome, expect exactly 3/2.
  490. BOOST_CHECK_CLOSE_FRACTION(pdf(mybeta22, 0.0001), 5.9994000000000E-4, tol);
  491. BOOST_CHECK_CLOSE_FRACTION(pdf(mybeta22, 0.9999), 5.9994000000000E-4, tol*50);
  492. BOOST_CHECK_EQUAL(cdf(mybeta22, 0.), 0); // cdf is a curved line from 0 to 1.
  493. BOOST_CHECK_CLOSE_FRACTION(cdf(mybeta22, 0.1), 0.028000000000000, tol);
  494. BOOST_CHECK_CLOSE_FRACTION(cdf(mybeta22, 0.5), 0.5, tol);
  495. BOOST_CHECK_CLOSE_FRACTION(cdf(mybeta22, 0.9), 0.972000000000000, tol);
  496. BOOST_CHECK_CLOSE_FRACTION(cdf(mybeta22, 0.0001), 2.999800000000000000000000000000000000000E-8, tol);
  497. BOOST_CHECK_CLOSE_FRACTION(cdf(mybeta22, 0.001), 2.998000000000000000000000000000000000000E-6, tol);
  498. BOOST_CHECK_CLOSE_FRACTION(cdf(mybeta22, 0.01), 0.0002980000000000000000000000000000000000000, tol);
  499. BOOST_CHECK_CLOSE_FRACTION(cdf(mybeta22, 0.1), 0.02800000000000000000000000000000000000000, tol); // exact
  500. BOOST_CHECK_CLOSE_FRACTION(cdf(mybeta22, 0.99), 0.9997020000000000000000000000000000000000, tol);
  501. BOOST_CHECK_EQUAL(cdf(mybeta22, 1), 1.); // Exact unity expected.
  502. // Complement
  503. BOOST_CHECK_CLOSE_FRACTION(cdf(complement(mybeta22, 0.9)), 0.028000000000000, tol);
  504. // quantile.
  505. BOOST_CHECK_CLOSE_FRACTION(quantile(mybeta22, 0.028), 0.1, tol);
  506. BOOST_CHECK_CLOSE_FRACTION(quantile(complement(mybeta22, 1 - 0.028)), 0.1, tol);
  507. BOOST_CHECK_EQUAL(kurtosis(mybeta11), 3+ kurtosis_excess(mybeta11)); // Check kurtosis_excess = kurtosis - 3;
  508. BOOST_CHECK_CLOSE_FRACTION(variance(mybeta22), 0.05, tol);
  509. BOOST_CHECK_CLOSE_FRACTION(mean(mybeta22), 0.5, tol);
  510. BOOST_CHECK_CLOSE_FRACTION(mode(mybeta22), 0.5, tol);
  511. BOOST_CHECK_CLOSE_FRACTION(median(mybeta22), 0.5, sqrt(tol)); // Theoretical maximum accuracy using Brent is sqrt(epsilon).
  512. BOOST_CHECK_CLOSE_FRACTION(skewness(mybeta22), 0.0, tol);
  513. BOOST_CHECK_CLOSE_FRACTION(kurtosis_excess(mybeta22), -144.0 / 168, tol);
  514. BOOST_CHECK_CLOSE_FRACTION(skewness(beta_distribution<>(3, 5)), 0.30983866769659335081434123198259, tol);
  515. BOOST_CHECK_CLOSE_FRACTION(beta_distribution<double>::find_alpha(mean(mybeta22), variance(mybeta22)), mybeta22.alpha(), tol); // mean, variance, probability.
  516. BOOST_CHECK_CLOSE_FRACTION(beta_distribution<double>::find_beta(mean(mybeta22), variance(mybeta22)), mybeta22.beta(), tol);// mean, variance, probability.
  517. BOOST_CHECK_CLOSE_FRACTION(mybeta22.find_alpha(mybeta22.beta(), 0.8, cdf(mybeta22, 0.8)), mybeta22.alpha(), tol);
  518. BOOST_CHECK_CLOSE_FRACTION(mybeta22.find_beta(mybeta22.alpha(), 0.8, cdf(mybeta22, 0.8)), mybeta22.beta(), tol);
  519. beta_distribution<real_concept> rcbeta22(2, 2); // Using RealType real_concept.
  520. cout << "numeric_limits<real_concept>::is_specialized " << numeric_limits<real_concept>::is_specialized << endl;
  521. cout << "numeric_limits<real_concept>::digits " << numeric_limits<real_concept>::digits << endl;
  522. cout << "numeric_limits<real_concept>::digits10 " << numeric_limits<real_concept>::digits10 << endl;
  523. cout << "numeric_limits<real_concept>::epsilon " << numeric_limits<real_concept>::epsilon() << endl;
  524. // (Parameter value, arbitrarily zero, only communicates the floating point type).
  525. test_spots(0.0F); // Test float.
  526. test_spots(0.0); // Test double.
  527. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  528. test_spots(0.0L); // Test long double.
  529. #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
  530. test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
  531. #endif
  532. #endif
  533. } // BOOST_AUTO_TEST_CASE( test_main )
  534. /*
  535. Output is:
  536. -Autorun "i:\boost-06-05-03-1300\libs\math\test\Math_test\debug\test_beta_dist.exe"
  537. Running 1 test case...
  538. numeric_limits<real_concept>::is_specialized 0
  539. numeric_limits<real_concept>::digits 0
  540. numeric_limits<real_concept>::digits10 0
  541. numeric_limits<real_concept>::epsilon 0
  542. Boost::math::tools::epsilon = 1.19209e-007
  543. std::numeric_limits::epsilon = 1.19209e-007
  544. epsilon = 1.19209e-007, Tolerance = 0.0119209%.
  545. Boost::math::tools::epsilon = 2.22045e-016
  546. std::numeric_limits::epsilon = 2.22045e-016
  547. epsilon = 2.22045e-016, Tolerance = 2.22045e-011%.
  548. Boost::math::tools::epsilon = 2.22045e-016
  549. std::numeric_limits::epsilon = 2.22045e-016
  550. epsilon = 2.22045e-016, Tolerance = 2.22045e-011%.
  551. Boost::math::tools::epsilon = 2.22045e-016
  552. std::numeric_limits::epsilon = 0
  553. epsilon = 2.22045e-016, Tolerance = 2.22045e-011%.
  554. *** No errors detected
  555. */