test_extreme_value.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // Copyright John Maddock 2006.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. // test_extreme_value.cpp
  7. #include <boost/math/tools/test.hpp>
  8. #include <boost/math/concepts/real_concept.hpp> // for real_concept
  9. #include <boost/math/distributions/extreme_value.hpp>
  10. using boost::math::extreme_value_distribution;
  11. #define BOOST_TEST_MAIN
  12. #include <boost/test/unit_test.hpp> // Boost.Test
  13. #include <boost/test/tools/floating_point_comparison.hpp>
  14. #include "test_out_of_range.hpp"
  15. #include <iostream>
  16. using std::cout;
  17. using std::endl;
  18. using std::setprecision;
  19. template <class RealType>
  20. void test_spot(RealType a, RealType b, RealType x, RealType p, RealType q, RealType tolerance)
  21. {
  22. BOOST_CHECK_CLOSE(
  23. ::boost::math::cdf(
  24. extreme_value_distribution<RealType>(a, b),
  25. x),
  26. p,
  27. tolerance); // %
  28. BOOST_CHECK_CLOSE(
  29. ::boost::math::cdf(
  30. complement(extreme_value_distribution<RealType>(a, b),
  31. x)),
  32. q,
  33. tolerance); // %
  34. if((p < 0.999) && (p > 0))
  35. {
  36. BOOST_CHECK_CLOSE(
  37. ::boost::math::quantile(
  38. extreme_value_distribution<RealType>(a, b),
  39. p),
  40. x,
  41. tolerance); // %
  42. }
  43. if((q < 0.999) && (q > 0))
  44. {
  45. BOOST_CHECK_CLOSE(
  46. ::boost::math::quantile(
  47. complement(extreme_value_distribution<RealType>(a, b),
  48. q)),
  49. x,
  50. tolerance); // %
  51. }
  52. }
  53. template <class RealType>
  54. void test_spots(RealType)
  55. {
  56. // Basic sanity checks.
  57. // 50eps as a percentage, up to a maximum of double precision
  58. // (that's the limit of our test data).
  59. RealType tolerance = (std::max)(
  60. static_cast<RealType>(boost::math::tools::epsilon<double>()),
  61. boost::math::tools::epsilon<RealType>());
  62. tolerance *= 50 * 100;
  63. cout << "Tolerance for type " << typeid(RealType).name() << " is " << tolerance << " %" << endl;
  64. // Results calculated by punching numbers into a calculator,
  65. // and using the formula at http://mathworld.wolfram.com/ExtremeValueDistribution.html
  66. test_spot(
  67. static_cast<RealType>(0.5), // a
  68. static_cast<RealType>(1.5), // b
  69. static_cast<RealType>(0.125), // x
  70. static_cast<RealType>(0.27692033409990891617007608217222L), // p
  71. static_cast<RealType>(0.72307966590009108382992391782778L), //q
  72. tolerance);
  73. test_spot(
  74. static_cast<RealType>(0.5), // a
  75. static_cast<RealType>(2), // b
  76. static_cast<RealType>(-5), // x
  77. static_cast<RealType>(1.6087601139887776413169427645933e-7L), // p
  78. static_cast<RealType>(0.99999983912398860112223586830572L), //q
  79. tolerance);
  80. test_spot(
  81. static_cast<RealType>(0.5), // a
  82. static_cast<RealType>(0.25), // b
  83. static_cast<RealType>(0.75), // x
  84. static_cast<RealType>(0.69220062755534635386542199718279L), // p
  85. static_cast<RealType>(0.30779937244465364613457800281721), //q
  86. tolerance);
  87. test_spot(
  88. static_cast<RealType>(0.5), // a
  89. static_cast<RealType>(0.25), // b
  90. static_cast<RealType>(5), // x
  91. static_cast<RealType>(0.99999998477002037126351248727041L), // p
  92. static_cast<RealType>(1.5229979628736487512729586276294e-8L), //q
  93. tolerance);
  94. BOOST_CHECK_CLOSE(
  95. ::boost::math::pdf(
  96. extreme_value_distribution<RealType>(0.5, 2),
  97. static_cast<RealType>(0.125)), // x
  98. static_cast<RealType>(0.18052654830890205978204427757846L), // probability.
  99. tolerance); // %
  100. BOOST_CHECK_CLOSE(
  101. ::boost::math::pdf(
  102. extreme_value_distribution<RealType>(1, 3),
  103. static_cast<RealType>(5)), // x
  104. static_cast<RealType>(0.0675057324099851209129017326286L), // probability.
  105. tolerance); // %
  106. BOOST_CHECK_CLOSE(
  107. ::boost::math::pdf(
  108. extreme_value_distribution<RealType>(1, 3),
  109. static_cast<RealType>(0)), // x
  110. static_cast<RealType>(0.11522236828583456431277265757312L), // probability.
  111. tolerance); // %
  112. BOOST_CHECK_CLOSE(
  113. ::boost::math::mean(
  114. extreme_value_distribution<RealType>(2, 3)),
  115. static_cast<RealType>(3.731646994704598581819536270246L),
  116. tolerance); // %
  117. BOOST_CHECK_CLOSE(
  118. ::boost::math::standard_deviation(
  119. extreme_value_distribution<RealType>(1, 0.5)),
  120. static_cast<RealType>(0.6412749150809320477720181798355L),
  121. tolerance); // %
  122. BOOST_CHECK_CLOSE(
  123. ::boost::math::mode(
  124. extreme_value_distribution<RealType>(2, 3)),
  125. static_cast<RealType>(2),
  126. tolerance); // %
  127. BOOST_CHECK_CLOSE(
  128. ::boost::math::median(
  129. extreme_value_distribution<RealType>(0, 1)),
  130. static_cast<RealType>(+0.36651292058166432701243915823266946945426344783710526305367771367056),
  131. tolerance); // %
  132. BOOST_CHECK_CLOSE(
  133. ::boost::math::skewness(
  134. extreme_value_distribution<RealType>(2, 3)),
  135. static_cast<RealType>(1.1395470994046486574927930193898461120875997958366L),
  136. tolerance); // %
  137. BOOST_CHECK_CLOSE(
  138. ::boost::math::kurtosis(
  139. extreme_value_distribution<RealType>(2, 3)),
  140. static_cast<RealType>(5.4),
  141. tolerance); // %
  142. BOOST_CHECK_CLOSE(
  143. ::boost::math::kurtosis_excess(
  144. extreme_value_distribution<RealType>(2, 3)),
  145. static_cast<RealType>(2.4),
  146. tolerance); // %
  147. //
  148. // Things that are errors:
  149. //
  150. extreme_value_distribution<RealType> dist(0.5, 2);
  151. BOOST_MATH_CHECK_THROW(
  152. quantile(dist, RealType(1.0)),
  153. std::overflow_error);
  154. BOOST_MATH_CHECK_THROW(
  155. quantile(complement(dist, RealType(0.0))),
  156. std::overflow_error);
  157. BOOST_MATH_CHECK_THROW(
  158. quantile(dist, RealType(0.0)),
  159. std::overflow_error);
  160. BOOST_MATH_CHECK_THROW(
  161. quantile(complement(dist, RealType(1.0))),
  162. std::overflow_error);
  163. BOOST_MATH_CHECK_THROW(
  164. cdf(extreme_value_distribution<RealType>(0, -1), RealType(1)),
  165. std::domain_error);
  166. BOOST_MATH_CHECK_THROW(
  167. quantile(dist, RealType(-1)),
  168. std::domain_error);
  169. BOOST_MATH_CHECK_THROW(
  170. quantile(dist, RealType(2)),
  171. std::domain_error);
  172. check_out_of_range<extreme_value_distribution<RealType> >(1, 2);
  173. if(std::numeric_limits<RealType>::has_infinity)
  174. {
  175. RealType inf = std::numeric_limits<RealType>::infinity();
  176. BOOST_CHECK_EQUAL(pdf(extreme_value_distribution<RealType>(), -inf), 0);
  177. BOOST_CHECK_EQUAL(pdf(extreme_value_distribution<RealType>(), inf), 0);
  178. BOOST_CHECK_EQUAL(cdf(extreme_value_distribution<RealType>(), -inf), 0);
  179. BOOST_CHECK_EQUAL(cdf(extreme_value_distribution<RealType>(), inf), 1);
  180. BOOST_CHECK_EQUAL(cdf(complement(extreme_value_distribution<RealType>(), -inf)), 1);
  181. BOOST_CHECK_EQUAL(cdf(complement(extreme_value_distribution<RealType>(), inf)), 0);
  182. }
  183. //
  184. // Bug reports:
  185. //
  186. // https://svn.boost.org/trac/boost/ticket/10938:
  187. BOOST_CHECK_CLOSE(
  188. ::boost::math::pdf(
  189. extreme_value_distribution<RealType>(0, 1),
  190. static_cast<RealType>(-1000)), // x
  191. static_cast<RealType>(0), // probability.
  192. tolerance); // %
  193. } // template <class RealType>void test_spots(RealType)
  194. BOOST_AUTO_TEST_CASE( test_main )
  195. {
  196. // Check that can generate extreme_value distribution using the two convenience methods:
  197. boost::math::extreme_value mycev1(1.); // Using typedef
  198. extreme_value_distribution<> myev2(1.); // Using default RealType double.
  199. // Basic sanity-check spot values.
  200. // (Parameter value, arbitrarily zero, only communicates the floating point type).
  201. test_spots(0.0F); // Test float. OK at decdigits = 0 tolerance = 0.0001 %
  202. test_spots(0.0); // Test double. OK at decdigits 7, tolerance = 1e07 %
  203. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  204. test_spots(0.0L); // Test long double.
  205. #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
  206. test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
  207. #endif
  208. #else
  209. std::cout << "<note>The long double tests have been disabled on this platform "
  210. "either because the long double overloads of the usual math functions are "
  211. "not available at all, or because they are too inaccurate for these tests "
  212. "to pass.</note>" << std::endl;
  213. #endif
  214. } // BOOST_AUTO_TEST_CASE( test_main )
  215. /*
  216. Output is:
  217. -Running 1 test case...
  218. Tolerance for type float is 0.000596046 %
  219. Tolerance for type double is 1.11022e-012 %
  220. Tolerance for type long double is 1.11022e-012 %
  221. Tolerance for type class boost::math::concepts::real_concept is 1.11022e-012 %
  222. *** No errors detected
  223. */