test_exponential_dist.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. // Copyright John Maddock 2006.
  2. // Copyright Paul A. Bristow 2007.
  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_exponential_dist.cpp
  8. #include <boost/math/tools/test.hpp>
  9. #include <boost/math/concepts/real_concept.hpp> // for real_concept
  10. #include <boost/math/distributions/exponential.hpp>
  11. using boost::math::exponential_distribution;
  12. #define BOOST_TEST_MAIN
  13. #include <boost/test/unit_test.hpp> // Boost.Test
  14. #include <boost/test/tools/floating_point_comparison.hpp>
  15. #include "test_out_of_range.hpp"
  16. #include <iostream>
  17. using std::cout;
  18. using std::endl;
  19. using std::setprecision;
  20. template <class RealType>
  21. void test_spot(RealType l, RealType x, RealType p, RealType q, RealType tolerance)
  22. {
  23. BOOST_CHECK_CLOSE(
  24. ::boost::math::cdf(
  25. exponential_distribution<RealType>(l),
  26. x),
  27. p,
  28. tolerance); // %
  29. BOOST_CHECK_CLOSE(
  30. ::boost::math::cdf(
  31. complement(exponential_distribution<RealType>(l),
  32. x)),
  33. q,
  34. tolerance); // %
  35. if(p < 0.999)
  36. {
  37. BOOST_CHECK_CLOSE(
  38. ::boost::math::quantile(
  39. exponential_distribution<RealType>(l),
  40. p),
  41. x,
  42. tolerance); // %
  43. }
  44. if(q < 0.999)
  45. {
  46. BOOST_CHECK_CLOSE(
  47. ::boost::math::quantile(
  48. complement(exponential_distribution<RealType>(l),
  49. q)),
  50. x,
  51. tolerance); // %
  52. }
  53. }
  54. template <class RealType>
  55. void test_spots(RealType T)
  56. {
  57. // Basic sanity checks.
  58. // 50 eps as a percentage, up to a maximum of double precision
  59. // (that's the limit of our test data: obtained by punching
  60. // numbers into a calculator).
  61. RealType tolerance = (std::max)(
  62. static_cast<RealType>(boost::math::tools::epsilon<double>()),
  63. boost::math::tools::epsilon<RealType>());
  64. tolerance *= 50 * 100;
  65. // # pragma warning(disable: 4100) // unreferenced formal parameter.
  66. // prevent his spurious warning.
  67. if (T != 0)
  68. {
  69. cout << "Expect parameter T == 0!" << endl;
  70. }
  71. cout << "Tolerance for type " << typeid(T).name() << " is " << tolerance << " %" << endl;
  72. test_spot(
  73. static_cast<RealType>(0.5), // lambda
  74. static_cast<RealType>(0.125), // x
  75. static_cast<RealType>(0.060586937186524213880289175377695L), // p
  76. static_cast<RealType>(0.93941306281347578611971082462231L), //q
  77. tolerance);
  78. test_spot(
  79. static_cast<RealType>(0.5), // lambda
  80. static_cast<RealType>(5), // x
  81. static_cast<RealType>(0.91791500137610120483047132553284L), // p
  82. static_cast<RealType>(0.08208499862389879516952867446716L), //q
  83. tolerance);
  84. test_spot(
  85. static_cast<RealType>(2), // lambda
  86. static_cast<RealType>(0.125), // x
  87. static_cast<RealType>(0.22119921692859513175482973302168L), // p
  88. static_cast<RealType>(0.77880078307140486824517026697832L), //q
  89. tolerance);
  90. test_spot(
  91. static_cast<RealType>(2), // lambda
  92. static_cast<RealType>(5), // x
  93. static_cast<RealType>(0.99995460007023751514846440848444L), // p
  94. static_cast<RealType>(4.5399929762484851535591515560551e-5L), //q
  95. tolerance);
  96. //
  97. // Some spot tests generated by MathCAD pexp(x,r):
  98. //
  99. test_spot(
  100. static_cast<RealType>(1), // lambda
  101. static_cast<RealType>(1), // x
  102. static_cast<RealType>(6.321205588285580E-001L), // p
  103. static_cast<RealType>(1-6.321205588285580E-001L), //q
  104. tolerance);
  105. test_spot(
  106. static_cast<RealType>(2), // lambda
  107. static_cast<RealType>(1), // x
  108. static_cast<RealType>(8.646647167633870E-001L), // p
  109. static_cast<RealType>(1-8.646647167633870E-001L), //q
  110. tolerance);
  111. test_spot(
  112. static_cast<RealType>(1), // lambda
  113. static_cast<RealType>(0.5), // x
  114. static_cast<RealType>(3.934693402873670E-001L), // p
  115. static_cast<RealType>(1-3.934693402873670E-001L), //q
  116. tolerance);
  117. test_spot(
  118. static_cast<RealType>(0.1), // lambda
  119. static_cast<RealType>(1), // x
  120. static_cast<RealType>(9.516258196404040E-002L), // p
  121. static_cast<RealType>(1-9.516258196404040E-002L), //q
  122. tolerance);
  123. test_spot(
  124. static_cast<RealType>(10), // lambda
  125. static_cast<RealType>(1), // x
  126. static_cast<RealType>(9.999546000702380E-001L), // p
  127. static_cast<RealType>(1-9.999546000702380E-001L), //q
  128. tolerance*10000); // we loose four digits to cancellation
  129. test_spot(
  130. static_cast<RealType>(0.1), // lambda
  131. static_cast<RealType>(10), // x
  132. static_cast<RealType>(6.321205588285580E-001L), // p
  133. static_cast<RealType>(1-6.321205588285580E-001L), //q
  134. tolerance);
  135. test_spot(
  136. static_cast<RealType>(1), // lambda
  137. static_cast<RealType>(0.01), // x
  138. static_cast<RealType>(9.950166250831950E-003L), // p
  139. static_cast<RealType>(1-9.950166250831950E-003L), //q
  140. tolerance);
  141. test_spot(
  142. static_cast<RealType>(1), // lambda
  143. static_cast<RealType>(0.0001), // x
  144. static_cast<RealType>(9.999500016666250E-005L), // p
  145. static_cast<RealType>(1-9.999500016666250E-005L), //q
  146. tolerance);
  147. /*
  148. // This test data appears to be erroneous, MathCad appears
  149. // to suffer from cancellation error as x -> 0
  150. test_spot(
  151. static_cast<RealType>(1), // lambda
  152. static_cast<RealType>(0.0000001), // x
  153. static_cast<RealType>(9.999999499998730E-008L), // p
  154. static_cast<RealType>(1-9.999999499998730E-008L), //q
  155. tolerance);
  156. */
  157. BOOST_CHECK_CLOSE(
  158. ::boost::math::pdf(
  159. exponential_distribution<RealType>(0.5),
  160. static_cast<RealType>(0.125)), // x
  161. static_cast<RealType>(0.46970653140673789305985541231115L), // probability.
  162. tolerance); // %
  163. BOOST_CHECK_CLOSE(
  164. ::boost::math::pdf(
  165. exponential_distribution<RealType>(0.5),
  166. static_cast<RealType>(5)), // x
  167. static_cast<RealType>(0.04104249931194939758476433723358L), // probability.
  168. tolerance); // %
  169. BOOST_CHECK_CLOSE(
  170. ::boost::math::pdf(
  171. exponential_distribution<RealType>(2),
  172. static_cast<RealType>(0.125)), // x
  173. static_cast<RealType>(1.5576015661428097364903405339566L), // probability.
  174. tolerance); // %
  175. BOOST_CHECK_CLOSE(
  176. ::boost::math::pdf(
  177. exponential_distribution<RealType>(2),
  178. static_cast<RealType>(5)), // x
  179. static_cast<RealType>(9.0799859524969703071183031121101e-5L), // probability.
  180. tolerance); // %
  181. BOOST_CHECK_CLOSE(
  182. ::boost::math::mean(
  183. exponential_distribution<RealType>(2)),
  184. static_cast<RealType>(0.5),
  185. tolerance); // %
  186. BOOST_CHECK_CLOSE(
  187. ::boost::math::standard_deviation(
  188. exponential_distribution<RealType>(2)),
  189. static_cast<RealType>(0.5),
  190. tolerance); // %
  191. BOOST_CHECK_CLOSE(
  192. ::boost::math::mode(
  193. exponential_distribution<RealType>(2)),
  194. static_cast<RealType>(0),
  195. tolerance); // %
  196. BOOST_CHECK_CLOSE(
  197. ::boost::math::median(
  198. exponential_distribution<RealType>(4)),
  199. static_cast<RealType>(0.693147180559945309417232121458176568075500134360255254) / 4,
  200. tolerance); // %
  201. BOOST_CHECK_CLOSE(
  202. ::boost::math::skewness(
  203. exponential_distribution<RealType>(2)),
  204. static_cast<RealType>(2),
  205. tolerance); // %
  206. BOOST_CHECK_CLOSE(
  207. ::boost::math::kurtosis(
  208. exponential_distribution<RealType>(2)),
  209. static_cast<RealType>(9),
  210. tolerance); // %
  211. BOOST_CHECK_CLOSE(
  212. ::boost::math::kurtosis_excess(
  213. exponential_distribution<RealType>(2)),
  214. static_cast<RealType>(6),
  215. tolerance); // %
  216. //
  217. // Things that are errors:
  218. //
  219. exponential_distribution<RealType> dist(0.5);
  220. BOOST_MATH_CHECK_THROW(
  221. quantile(dist, RealType(1.0)),
  222. std::overflow_error);
  223. BOOST_MATH_CHECK_THROW(
  224. quantile(complement(dist, RealType(0.0))),
  225. std::overflow_error);
  226. BOOST_MATH_CHECK_THROW(
  227. pdf(dist, RealType(-1)),
  228. std::domain_error);
  229. BOOST_MATH_CHECK_THROW(
  230. cdf(dist, RealType(-1)),
  231. std::domain_error);
  232. BOOST_MATH_CHECK_THROW(
  233. cdf(exponential_distribution<RealType>(-1), RealType(1)),
  234. std::domain_error);
  235. BOOST_MATH_CHECK_THROW(
  236. quantile(dist, RealType(-1)),
  237. std::domain_error);
  238. BOOST_MATH_CHECK_THROW(
  239. quantile(dist, RealType(2)),
  240. std::domain_error);
  241. check_out_of_range<exponential_distribution<RealType> >(2);
  242. BOOST_MATH_CHECK_THROW(exponential_distribution<RealType>(0), std::domain_error);
  243. BOOST_MATH_CHECK_THROW(exponential_distribution<RealType>(-1), std::domain_error);
  244. if(std::numeric_limits<RealType>::has_infinity)
  245. {
  246. RealType inf = std::numeric_limits<RealType>::infinity();
  247. BOOST_CHECK_EQUAL(pdf(exponential_distribution<RealType>(2), inf), 0);
  248. BOOST_CHECK_EQUAL(cdf(exponential_distribution<RealType>(2), inf), 1);
  249. BOOST_CHECK_EQUAL(cdf(complement(exponential_distribution<RealType>(2), inf)), 0);
  250. }
  251. } // template <class RealType>void test_spots(RealType)
  252. BOOST_AUTO_TEST_CASE( test_main )
  253. {
  254. // Check that can generate exponential distribution using the two convenience methods:
  255. boost::math::exponential mycexp1(1.); // Using typedef
  256. exponential_distribution<> myexp2(1.); // Using default RealType double.
  257. // Basic sanity-check spot values.
  258. // (Parameter value, arbitrarily zero, only communicates the floating point type).
  259. test_spots(0.0F); // Test float. OK at decdigits = 0 tolerance = 0.0001 %
  260. test_spots(0.0); // Test double. OK at decdigits 7, tolerance = 1e07 %
  261. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  262. test_spots(0.0L); // Test long double.
  263. #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
  264. test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
  265. #endif
  266. #else
  267. std::cout << "<note>The long double tests have been disabled on this platform "
  268. "either because the long double overloads of the usual math functions are "
  269. "not available at all, or because they are too inaccurate for these tests "
  270. "to pass.</note>" << std::endl;
  271. #endif
  272. } // BOOST_AUTO_TEST_CASE( test_main )
  273. /*
  274. Output is:
  275. Running 1 test case...
  276. Tolerance for type float is 0.000596046 %
  277. Tolerance for type double is 1.11022e-012 %
  278. Tolerance for type long double is 1.11022e-012 %
  279. Tolerance for type class boost::math::concepts::real_concept is 1.11022e-012 %
  280. *** No errors detected
  281. */