test_gamma_dist.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // Copyright John Maddock 2006.
  2. // Copyright Paul A. Bristow 2007, 2010.
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt
  6. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. // test_gamma_dist.cpp
  8. // http://en.wikipedia.org/wiki/Gamma_distribution
  9. // http://www.itl.nist.gov/div898/handbook/eda/section3/eda366b.htm
  10. // Also:
  11. // Weisstein, Eric W. "Gamma Distribution."
  12. // From MathWorld--A Wolfram Web Resource.
  13. // http://mathworld.wolfram.com/GammaDistribution.html
  14. #include <pch.hpp> // include directory libs/math/src/tr1/ is needed.
  15. #include <boost/math/concepts/real_concept.hpp> // for real_concept
  16. #define BOOST_TEST_MAIN
  17. #include <boost/test/unit_test.hpp> // Boost.Test
  18. #include <boost/test/tools/floating_point_comparison.hpp>
  19. #include <boost/math/distributions/gamma.hpp>
  20. using boost::math::gamma_distribution;
  21. #include <boost/math/tools/test.hpp>
  22. #include "test_out_of_range.hpp"
  23. #include <iostream>
  24. #include <iomanip>
  25. using std::cout;
  26. using std::endl;
  27. using std::setprecision;
  28. #include <limits>
  29. using std::numeric_limits;
  30. template <class RealType>
  31. RealType NaivePDF(RealType shape, RealType scale, RealType x)
  32. {
  33. // Deliberately naive PDF calculator again which
  34. // we'll compare our pdf function. However some
  35. // published values to compare against would be better....
  36. using namespace std;
  37. RealType result = log(x) * (shape - 1) - x / scale - boost::math::lgamma(shape) - log(scale) * shape;
  38. return exp(result);
  39. }
  40. template <class RealType>
  41. void check_gamma(RealType shape, RealType scale, RealType x, RealType p, RealType q, RealType tol)
  42. {
  43. BOOST_CHECK_CLOSE(
  44. ::boost::math::cdf(
  45. gamma_distribution<RealType>(shape, scale), // distribution.
  46. x), // random variable.
  47. p, // probability.
  48. tol); // %tolerance.
  49. BOOST_CHECK_CLOSE(
  50. ::boost::math::cdf(
  51. complement(
  52. gamma_distribution<RealType>(shape, scale), // distribution.
  53. x)), // random variable.
  54. q, // probability complement.
  55. tol); // %tolerance.
  56. if(p < 0.999)
  57. {
  58. BOOST_CHECK_CLOSE(
  59. ::boost::math::quantile(
  60. gamma_distribution<RealType>(shape, scale), // distribution.
  61. p), // probability.
  62. x, // random variable.
  63. tol); // %tolerance.
  64. }
  65. if(q < 0.999)
  66. {
  67. BOOST_CHECK_CLOSE(
  68. ::boost::math::quantile(
  69. complement(
  70. gamma_distribution<RealType>(shape, scale), // distribution.
  71. q)), // probability complement.
  72. x, // random variable.
  73. tol); // %tolerance.
  74. }
  75. // PDF:
  76. BOOST_CHECK_CLOSE(
  77. boost::math::pdf(
  78. gamma_distribution<RealType>(shape, scale), // distribution.
  79. x), // random variable.
  80. NaivePDF(shape, scale, x), // PDF
  81. tol); // %tolerance.
  82. }
  83. template <class RealType>
  84. void test_spots(RealType)
  85. {
  86. // Basic sanity checks
  87. //
  88. // 15 decimal places expressed as a persentage.
  89. // The first tests use values generated by MathCAD,
  90. // and should be accurate to around double precision.
  91. //
  92. RealType tolerance = (std::max)(RealType(5e-14f), std::numeric_limits<RealType>::epsilon() * 20) * 100;
  93. cout << "Tolerance for type " << typeid(RealType).name() << " is " << tolerance << " %" << endl;
  94. check_gamma(
  95. static_cast<RealType>(0.5),
  96. static_cast<RealType>(1),
  97. static_cast<RealType>(0.5),
  98. static_cast<RealType>(0.682689492137085),
  99. static_cast<RealType>(1-0.682689492137085),
  100. tolerance);
  101. check_gamma(
  102. static_cast<RealType>(2),
  103. static_cast<RealType>(1),
  104. static_cast<RealType>(0.5),
  105. static_cast<RealType>(0.090204010431050),
  106. static_cast<RealType>(1-0.090204010431050),
  107. tolerance);
  108. check_gamma(
  109. static_cast<RealType>(40),
  110. static_cast<RealType>(1),
  111. static_cast<RealType>(10),
  112. static_cast<RealType>(7.34163631456064E-13),
  113. static_cast<RealType>(1-7.34163631456064E-13),
  114. tolerance);
  115. //
  116. // Some more test data generated by the online
  117. // calculator at http://espse.ed.psu.edu/edpsych/faculty/rhale/hale/507Mat/statlets/free/pdist.htm
  118. // This has the advantage of supporting the scale parameter as well
  119. // as shape, but has only a few digits accuracy, and produces
  120. // some deeply suspect values if the shape parameter is < 1
  121. // (it doesn't agree with MathCAD or this implementation).
  122. // To be fair the incomplete gamma is tricky to get right in this area...
  123. //
  124. tolerance = 1e-5f * 100; // 5 decimal places as a persentage
  125. cout << "Tolerance for type " << typeid(RealType).name() << " is " << tolerance << " %" << endl;
  126. check_gamma(
  127. static_cast<RealType>(2),
  128. static_cast<RealType>(1)/5,
  129. static_cast<RealType>(0.1),
  130. static_cast<RealType>(0.090204),
  131. static_cast<RealType>(1-0.090204),
  132. tolerance);
  133. check_gamma(
  134. static_cast<RealType>(2),
  135. static_cast<RealType>(1)/5,
  136. static_cast<RealType>(0.5),
  137. static_cast<RealType>(1-0.287298),
  138. static_cast<RealType>(0.287298),
  139. tolerance);
  140. check_gamma(
  141. static_cast<RealType>(3),
  142. static_cast<RealType>(2),
  143. static_cast<RealType>(1),
  144. static_cast<RealType>(0.014388),
  145. static_cast<RealType>(1-0.014388),
  146. tolerance * 10); // one less decimal place in the test value
  147. check_gamma(
  148. static_cast<RealType>(3),
  149. static_cast<RealType>(2),
  150. static_cast<RealType>(5),
  151. static_cast<RealType>(0.456187),
  152. static_cast<RealType>(1-0.456187),
  153. tolerance);
  154. RealType tol2 = boost::math::tools::epsilon<RealType>() * 5 * 100; // 5 eps as a persentage
  155. gamma_distribution<RealType> dist(8, 3);
  156. RealType x = static_cast<RealType>(0.125);
  157. using namespace std; // ADL of std names.
  158. // mean:
  159. BOOST_CHECK_CLOSE(
  160. mean(dist)
  161. , static_cast<RealType>(8*3), tol2);
  162. // variance:
  163. BOOST_CHECK_CLOSE(
  164. variance(dist)
  165. , static_cast<RealType>(8*3*3), tol2);
  166. // std deviation:
  167. BOOST_CHECK_CLOSE(
  168. standard_deviation(dist)
  169. , sqrt(static_cast<RealType>(8*3*3)), tol2);
  170. // hazard:
  171. BOOST_CHECK_CLOSE(
  172. hazard(dist, x)
  173. , pdf(dist, x) / cdf(complement(dist, x)), tol2);
  174. // cumulative hazard:
  175. BOOST_CHECK_CLOSE(
  176. chf(dist, x)
  177. , -log(cdf(complement(dist, x))), tol2);
  178. // coefficient_of_variation:
  179. BOOST_CHECK_CLOSE(
  180. coefficient_of_variation(dist)
  181. , standard_deviation(dist) / mean(dist), tol2);
  182. // mode:
  183. BOOST_CHECK_CLOSE(
  184. mode(dist)
  185. , static_cast<RealType>(7 * 3), tol2);
  186. // skewness:
  187. BOOST_CHECK_CLOSE(
  188. skewness(dist)
  189. , 2 / sqrt(static_cast<RealType>(8)), tol2);
  190. // kertosis:
  191. BOOST_CHECK_CLOSE(
  192. kurtosis(dist)
  193. , 3 + 6 / static_cast<RealType>(8), tol2);
  194. // kertosis excess:
  195. BOOST_CHECK_CLOSE(
  196. kurtosis_excess(dist)
  197. , 6 / static_cast<RealType>(8), tol2);
  198. BOOST_CHECK_CLOSE(
  199. median(dist), static_cast<RealType>(23.007748327502412), // double precision test value
  200. (std::max)(tol2, static_cast<RealType>(std::numeric_limits<double>::epsilon() * 2 * 100))); // 2 eps as persent
  201. // Rely on default definition in derived accessors.
  202. // error tests
  203. check_out_of_range<boost::math::gamma_distribution<RealType> >(1, 1);
  204. BOOST_MATH_CHECK_THROW(boost::math::gamma_distribution<RealType>(0, 1), std::domain_error);
  205. BOOST_MATH_CHECK_THROW(boost::math::gamma_distribution<RealType>(-1, 1), std::domain_error);
  206. BOOST_MATH_CHECK_THROW(boost::math::gamma_distribution<RealType>(1, 0), std::domain_error);
  207. BOOST_MATH_CHECK_THROW(boost::math::gamma_distribution<RealType>(1, -1), std::domain_error);
  208. } // template <class RealType>void test_spots(RealType)
  209. BOOST_AUTO_TEST_CASE( test_main )
  210. {
  211. // Basic sanity-check spot values.
  212. // (Parameter value, arbitrarily zero, only communicates the floating point type).
  213. test_spots(0.0F); // Test float. OK at decdigits = 0 tolerance = 0.0001 %
  214. test_spots(0.0); // Test double. OK at decdigits 7, tolerance = 1e07 %
  215. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  216. test_spots(0.0L); // Test long double.
  217. #ifndef BOOST_MATH_NO_REAL_CONCEPT_TESTS
  218. test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
  219. #endif
  220. #else
  221. std::cout << "<note>The long double tests have been disabled on this platform "
  222. "either because the long double overloads of the usual math functions are "
  223. "not available at all, or because they are too inaccurate for these tests "
  224. "to pass.</note>" << std::endl;
  225. #endif
  226. } // BOOST_AUTO_TEST_CASE( test_main )
  227. /*
  228. Output:
  229. Autorun "i:\boost-06-05-03-1300\libs\math\test\Math_test\debug\test_gamma_dist.exe"
  230. Running 1 test case...
  231. Tolerance for type float is 0.000238419 %
  232. Tolerance for type float is 0.001 %
  233. Tolerance for type double is 5e-012 %
  234. Tolerance for type double is 0.001 %
  235. Tolerance for type long double is 5e-012 %
  236. Tolerance for type long double is 0.001 %
  237. Tolerance for type class boost::math::concepts::real_concept is 5e-012 %
  238. Tolerance for type class boost::math::concepts::real_concept is 0.001 %
  239. *** No errors detected
  240. */