test_rayleigh.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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_rayleigh.cpp
  7. #ifdef _MSC_VER
  8. # pragma warning(disable: 4127) // conditional expression is constant.
  9. # pragma warning(disable: 4100) // unreferenced formal parameter.
  10. #endif
  11. #include <boost/math/concepts/real_concept.hpp> // for real_concept
  12. #include <boost/math/distributions/rayleigh.hpp>
  13. using boost::math::rayleigh_distribution;
  14. #include <boost/math/tools/test.hpp>
  15. #define BOOST_TEST_MAIN
  16. #include <boost/test/unit_test.hpp> // Boost.Test
  17. #include <boost/test/tools/floating_point_comparison.hpp>
  18. #include "test_out_of_range.hpp"
  19. #include <iostream>
  20. using std::cout;
  21. using std::endl;
  22. using std::setprecision;
  23. template <class RealType>
  24. void test_spot(RealType s, RealType x, RealType p, RealType q, RealType tolerance)
  25. {
  26. BOOST_CHECK_CLOSE(
  27. ::boost::math::cdf(
  28. rayleigh_distribution<RealType>(s),
  29. x),
  30. p,
  31. tolerance); // %
  32. BOOST_CHECK_CLOSE(
  33. ::boost::math::cdf(
  34. complement(rayleigh_distribution<RealType>(s),
  35. x)),
  36. q,
  37. tolerance); // %
  38. // Special extra tests for p and q near to unity.
  39. if(p < 0.999)
  40. {
  41. BOOST_CHECK_CLOSE(
  42. ::boost::math::quantile(
  43. rayleigh_distribution<RealType>(s),
  44. p),
  45. x,
  46. tolerance); // %
  47. }
  48. if(q < 0.999)
  49. {
  50. BOOST_CHECK_CLOSE(
  51. ::boost::math::quantile(
  52. complement(rayleigh_distribution<RealType>(s),
  53. q)),
  54. x,
  55. tolerance); // %
  56. }
  57. if(std::numeric_limits<RealType>::has_infinity)
  58. {
  59. RealType inf = std::numeric_limits<RealType>::infinity();
  60. BOOST_CHECK_EQUAL(pdf(rayleigh_distribution<RealType>(s), inf), 0);
  61. BOOST_CHECK_EQUAL(cdf(rayleigh_distribution<RealType>(s), inf), 1);
  62. BOOST_CHECK_EQUAL(cdf(complement(rayleigh_distribution<RealType>(s), inf)), 0);
  63. }
  64. } // void test_spot
  65. template <class RealType>
  66. void test_spots(RealType T)
  67. {
  68. using namespace std; // ADL of std names.
  69. // Basic sanity checks.
  70. // 50 eps as a percentage, up to a maximum of double precision
  71. // (that's the limit of our test data: obtained by punching
  72. // numbers into a calculator).
  73. RealType tolerance = (std::max)(
  74. static_cast<RealType>(boost::math::tools::epsilon<double>()),
  75. boost::math::tools::epsilon<RealType>());
  76. tolerance *= 10 * 100; // 10 eps as a percent
  77. cout << "Tolerance for type " << typeid(T).name() << " is " << tolerance << " %" << endl;
  78. using namespace boost::math::constants;
  79. // Things that are errors:
  80. rayleigh_distribution<RealType> dist(0.5);
  81. check_out_of_range<rayleigh_distribution<RealType> >(1);
  82. BOOST_MATH_CHECK_THROW(
  83. quantile(dist,
  84. RealType(1.)), // quantile unity should overflow.
  85. std::overflow_error);
  86. BOOST_MATH_CHECK_THROW(
  87. quantile(complement(dist,
  88. RealType(0.))), // quantile complement zero should overflow.
  89. std::overflow_error);
  90. BOOST_MATH_CHECK_THROW(
  91. pdf(dist, RealType(-1)), // Bad negative x.
  92. std::domain_error);
  93. BOOST_MATH_CHECK_THROW(
  94. cdf(dist, RealType(-1)), // Bad negative x.
  95. std::domain_error);
  96. BOOST_MATH_CHECK_THROW(
  97. cdf(rayleigh_distribution<RealType>(-1), // bad sigma < 0
  98. RealType(1)),
  99. std::domain_error);
  100. BOOST_MATH_CHECK_THROW(
  101. cdf(rayleigh_distribution<RealType>(0), // bad sigma == 0
  102. RealType(1)),
  103. std::domain_error);
  104. BOOST_MATH_CHECK_THROW(
  105. quantile(dist, RealType(-1)), // negative quantile probability.
  106. std::domain_error);
  107. BOOST_MATH_CHECK_THROW(
  108. quantile(dist, RealType(2)), // > unity quantile probability.
  109. std::domain_error);
  110. test_spot(
  111. static_cast<RealType>(1.L), // sigma
  112. static_cast<RealType>(1.L), // x
  113. static_cast<RealType>(1 - exp_minus_half<RealType>()), // p
  114. static_cast<RealType>(exp_minus_half<RealType>()), // q
  115. tolerance);
  116. test_spot(
  117. static_cast<RealType>(0.5L), // sigma
  118. static_cast<RealType>(0.5L), // x
  119. static_cast<RealType>(1 - exp_minus_half<RealType>()), // p
  120. static_cast<RealType>(exp_minus_half<RealType>()), //q
  121. tolerance);
  122. test_spot(
  123. static_cast<RealType>(3.L), // sigma
  124. static_cast<RealType>(3.L), // x
  125. static_cast<RealType>(1 - exp_minus_half<RealType>()), // p
  126. static_cast<RealType>(exp_minus_half<RealType>()), //q
  127. tolerance);
  128. BOOST_CHECK_CLOSE(
  129. ::boost::math::pdf(
  130. rayleigh_distribution<RealType>(1.L),
  131. static_cast<RealType>(1.L)), // x
  132. static_cast<RealType>(exp_minus_half<RealType>()), // probability.
  133. tolerance); // %
  134. BOOST_CHECK_CLOSE(
  135. ::boost::math::pdf(
  136. rayleigh_distribution<RealType>(0.5L),
  137. static_cast<RealType>(0.5L)), // x
  138. static_cast<RealType>(2 * exp_minus_half<RealType>()), // probability.
  139. tolerance); // %
  140. BOOST_CHECK_CLOSE(
  141. ::boost::math::pdf(
  142. rayleigh_distribution<RealType>(2.L),
  143. static_cast<RealType>(2.L)), // x
  144. static_cast<RealType>(exp_minus_half<RealType>() /2), // probability.
  145. tolerance); // %
  146. BOOST_CHECK_CLOSE(
  147. ::boost::math::mean(
  148. rayleigh_distribution<RealType>(1.L)),
  149. static_cast<RealType>(root_half_pi<RealType>()),
  150. tolerance); // %
  151. BOOST_CHECK_CLOSE(
  152. ::boost::math::variance(
  153. rayleigh_distribution<RealType>(root_two<RealType>())),
  154. static_cast<RealType>(four_minus_pi<RealType>()),
  155. tolerance * 100); // %
  156. BOOST_CHECK_CLOSE(
  157. ::boost::math::mode(
  158. rayleigh_distribution<RealType>(1.L)),
  159. static_cast<RealType>(1.L),
  160. tolerance); // %
  161. BOOST_CHECK_CLOSE(
  162. ::boost::math::median(
  163. rayleigh_distribution<RealType>(1.L)),
  164. static_cast<RealType>(sqrt(log(4.L))), // sigma * sqrt(log_four)
  165. tolerance); // %
  166. BOOST_CHECK_CLOSE(
  167. ::boost::math::skewness(
  168. rayleigh_distribution<RealType>(1.L)),
  169. static_cast<RealType>(2.L * root_pi<RealType>()) * (pi<RealType>() - 3) / (pow((4 - pi<RealType>()), static_cast<RealType>(1.5L))),
  170. tolerance * 100); // %
  171. BOOST_CHECK_CLOSE(
  172. ::boost::math::skewness(
  173. rayleigh_distribution<RealType>(1.L)),
  174. static_cast<RealType>(0.63111065781893713819189935154422777984404221106391L),
  175. tolerance * 100); // %
  176. BOOST_CHECK_CLOSE(
  177. ::boost::math::kurtosis_excess(
  178. rayleigh_distribution<RealType>(1.L)),
  179. -static_cast<RealType>(6 * pi<RealType>() * pi<RealType>() - 24 * pi<RealType>() + 16) /
  180. ((4 - pi<RealType>()) * (4 - pi<RealType>())),
  181. // static_cast<RealType>(0.2450893006876380628486604106197544154170667057995L),
  182. tolerance * 1000); // %
  183. BOOST_CHECK_CLOSE(
  184. ::boost::math::kurtosis(
  185. rayleigh_distribution<RealType>(1.L)),
  186. static_cast<RealType>(3.2450893006876380628486604106197544154170667057995L),
  187. tolerance * 100); // %
  188. BOOST_CHECK_CLOSE(
  189. ::boost::math::kurtosis_excess(rayleigh_distribution<RealType>(2)),
  190. ::boost::math::kurtosis(rayleigh_distribution<RealType>(2)) -3,
  191. tolerance* 100); // %
  192. return;
  193. } // template <class RealType>void test_spots(RealType)
  194. BOOST_AUTO_TEST_CASE( test_main )
  195. {
  196. // Check that can generate rayleigh distribution using the two convenience methods:
  197. boost::math::rayleigh ray1(1.); // Using typedef
  198. rayleigh_distribution<> ray2(1.); // Using default RealType double.
  199. using namespace boost::math::constants;
  200. // Basic sanity-check spot values.
  201. // Double only tests.
  202. BOOST_CHECK_CLOSE_FRACTION(
  203. ::boost::math::pdf(
  204. rayleigh_distribution<double>(1.),
  205. static_cast<double>(1)), // x
  206. static_cast<double>(exp_minus_half<double>()), // p
  207. 1e-15); // %
  208. BOOST_CHECK_CLOSE_FRACTION(
  209. ::boost::math::pdf(
  210. rayleigh_distribution<double>(0.5),
  211. static_cast<double>(0.5)), // x
  212. static_cast<double>(2 * exp_minus_half<double>()), // p
  213. 1e-15); // %
  214. BOOST_CHECK_CLOSE_FRACTION(
  215. ::boost::math::pdf(
  216. rayleigh_distribution<double>(2.),
  217. static_cast<double>(2)), // x
  218. static_cast<double>(exp_minus_half<double>() /2 ), // p
  219. 1e-15); // %
  220. BOOST_CHECK_CLOSE_FRACTION(
  221. ::boost::math::cdf(
  222. rayleigh_distribution<double>(1.),
  223. static_cast<double>(1)), // x
  224. static_cast<double>(1- exp_minus_half<double>()), // p
  225. 1e-15); // %
  226. BOOST_CHECK_CLOSE_FRACTION(
  227. ::boost::math::cdf(
  228. rayleigh_distribution<double>(2.),
  229. static_cast<double>(2)), // x
  230. static_cast<double>(1- exp_minus_half<double>()), // p
  231. 1e-15); // %
  232. BOOST_CHECK_CLOSE_FRACTION(
  233. ::boost::math::cdf(
  234. rayleigh_distribution<double>(3.),
  235. static_cast<double>(3)), // x
  236. static_cast<double>(1- exp_minus_half<double>()), // p
  237. 1e-15); // %
  238. BOOST_CHECK_CLOSE_FRACTION(
  239. ::boost::math::cdf(
  240. rayleigh_distribution<double>(4.),
  241. static_cast<double>(4)), // x
  242. static_cast<double>(1- exp_minus_half<double>()), // p
  243. 1e-15); // %
  244. BOOST_CHECK_CLOSE_FRACTION(
  245. ::boost::math::cdf(complement(
  246. rayleigh_distribution<double>(4.),
  247. static_cast<double>(4))), // x
  248. static_cast<double>(exp_minus_half<double>()), // q = 1 - p
  249. 1e-15); // %
  250. BOOST_CHECK_CLOSE_FRACTION(
  251. ::boost::math::quantile(
  252. rayleigh_distribution<double>(4.),
  253. static_cast<double>(1- exp_minus_half<double>())), // x
  254. static_cast<double>(4), // p
  255. 1e-15); // %
  256. BOOST_CHECK_CLOSE_FRACTION(
  257. ::boost::math::quantile(complement(
  258. rayleigh_distribution<double>(4.),
  259. static_cast<double>(exp_minus_half<double>()))), // x
  260. static_cast<double>(4), // p
  261. 1e-15); // %
  262. // (Parameter value, arbitrarily zero, only communicates the floating point type).
  263. test_spots(0.0F); // Test float. OK at decdigits = 0 tolerance = 0.0001 %
  264. test_spots(0.0); // Test double. OK at decdigits 7, tolerance = 1e07 %
  265. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  266. test_spots(0.0L); // Test long double.
  267. #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
  268. test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
  269. #endif
  270. #else
  271. std::cout << "<note>The long double tests have been disabled on this platform "
  272. "either because the long double overloads of the usual math functions are "
  273. "not available at all, or because they are too inaccurate for these tests "
  274. "to pass.</note>" << std::endl;
  275. #endif
  276. } // BOOST_AUTO_TEST_CASE( test_main )
  277. /*
  278. Output is:
  279. Autorun "i:\boost-06-05-03-1300\libs\math\test\Math_test\debug\test_rayleigh.exe"
  280. Running 1 test case...
  281. Tolerance for type float is 0.000119209 %
  282. Tolerance for type double is 2.22045e-013 %
  283. Tolerance for type long double is 2.22045e-013 %
  284. Tolerance for type class boost::math::concepts::real_concept is 2.22045e-013 %
  285. *** No errors detected
  286. */