test_inverse_gaussian.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. // Copyright Paul A. Bristow 2010.
  2. // Copyright John Maddock 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. #ifdef _MSC_VER
  8. # pragma warning (disable : 4224) // nonstandard extension used : formal parameter 'type' was previously defined as a type
  9. // in Boost.test and lexical_cast
  10. # pragma warning (disable : 4310) // cast truncates constant value
  11. # pragma warning (disable : 4512) // assignment operator could not be generated
  12. #endif
  13. //#include <pch.hpp> // include directory libs/math/src/tr1/ is needed.
  14. #include <boost/math/tools/test.hpp>
  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/inverse_gaussian.hpp>
  20. using boost::math::inverse_gaussian_distribution;
  21. using boost::math::inverse_gaussian;
  22. #include <boost/math/tools/test.hpp>
  23. #include "test_out_of_range.hpp"
  24. #include <iostream>
  25. #include <iomanip>
  26. using std::cout;
  27. using std::endl;
  28. using std::setprecision;
  29. #include <limits>
  30. using std::numeric_limits;
  31. template <class RealType>
  32. void check_inverse_gaussian(RealType mean, RealType scale, RealType x, RealType p, RealType q, RealType tol)
  33. {
  34. using boost::math::inverse_gaussian_distribution;
  35. BOOST_CHECK_CLOSE_FRACTION(
  36. ::boost::math::cdf( // Check cdf
  37. inverse_gaussian_distribution<RealType>(mean, scale), // distribution.
  38. x), // random variable.
  39. p, // probability.
  40. tol); // tolerance.
  41. BOOST_CHECK_CLOSE_FRACTION(
  42. ::boost::math::cdf( // Check cdf complement
  43. complement(
  44. inverse_gaussian_distribution<RealType>(mean, scale), // distribution.
  45. x)), // random variable.
  46. q, // probability complement.
  47. tol); // %tolerance.
  48. BOOST_CHECK_CLOSE_FRACTION(
  49. ::boost::math::quantile( // Check quantile
  50. inverse_gaussian_distribution<RealType>(mean, scale), // distribution.
  51. p), // probability.
  52. x, // random variable.
  53. tol); // tolerance.
  54. BOOST_CHECK_CLOSE_FRACTION(
  55. ::boost::math::quantile( // Check quantile complement
  56. complement(
  57. inverse_gaussian_distribution<RealType>(mean, scale), // distribution.
  58. q)), // probability complement.
  59. x, // random variable.
  60. tol); // tolerance.
  61. inverse_gaussian_distribution<RealType> dist (mean, scale);
  62. if((p < 0.999) && (q < 0.999))
  63. { // We can only check this if P is not too close to 1,
  64. // so that we can guarantee Q is accurate:
  65. BOOST_CHECK_CLOSE_FRACTION(
  66. cdf(complement(dist, x)), q, tol); // 1 - cdf
  67. BOOST_CHECK_CLOSE_FRACTION(
  68. quantile(dist, p), x, tol); // quantile(cdf) = x
  69. BOOST_CHECK_CLOSE_FRACTION(
  70. quantile(complement(dist, q)), x, tol); // quantile(complement(1 - cdf)) = x
  71. }
  72. }
  73. template <class RealType>
  74. void test_spots(RealType)
  75. {
  76. // Basic sanity checks
  77. RealType tolerance = static_cast<RealType>(1e-4L); //
  78. cout << "Tolerance for type " << typeid(RealType).name() << " is " << tolerance << endl;
  79. // Check some bad parameters to the distribution,
  80. #ifndef BOOST_NO_EXCEPTIONS
  81. BOOST_MATH_CHECK_THROW(boost::math::inverse_gaussian_distribution<RealType> nbad1(0, 0), std::domain_error); // zero scale
  82. BOOST_MATH_CHECK_THROW(boost::math::inverse_gaussian_distribution<RealType> nbad1(0, -1), std::domain_error); // negative scale
  83. #else
  84. BOOST_MATH_CHECK_THROW(boost::math::inverse_gaussian_distribution<RealType>(0, 0), std::domain_error); // zero scale
  85. BOOST_MATH_CHECK_THROW(boost::math::inverse_gaussian_distribution<RealType>(0, -1), std::domain_error); // negative scale
  86. #endif
  87. inverse_gaussian_distribution<RealType> w11;
  88. // Error tests:
  89. check_out_of_range<inverse_gaussian_distribution<RealType> >(0.25, 1);
  90. // Check complements.
  91. BOOST_CHECK_CLOSE_FRACTION(
  92. cdf(complement(w11, 1.)), static_cast<RealType>(1) - cdf(w11, 1.), tolerance); // cdf complement
  93. // cdf(complement = 1 - cdf - but if cdf near unity, then loss of accuracy in cdf,
  94. // but cdf complement is near zero but more accurate.
  95. BOOST_CHECK_CLOSE_FRACTION( // quantile(complement p) == quantile(1 - p)
  96. quantile(complement(w11, static_cast<RealType>(0.5))),
  97. quantile(w11, 1 - static_cast<RealType>(0.5)),
  98. tolerance); // cdf complement
  99. check_inverse_gaussian(
  100. static_cast<RealType>(2),
  101. static_cast<RealType>(3),
  102. static_cast<RealType>(1),
  103. static_cast<RealType>(0.28738674440477374),
  104. static_cast<RealType>(1 - 0.28738674440477374),
  105. tolerance);
  106. RealType tolfeweps = boost::math::tools::epsilon<RealType>() * 5;
  107. inverse_gaussian_distribution<RealType> dist(2, 3);
  108. using namespace std; // ADL of std names.
  109. // mean:
  110. BOOST_CHECK_CLOSE_FRACTION(mean(dist),
  111. static_cast<RealType>(2), tolfeweps);
  112. BOOST_CHECK_CLOSE_FRACTION(scale(dist),
  113. static_cast<RealType>(3), tolfeweps);
  114. // variance:
  115. BOOST_CHECK_CLOSE_FRACTION(variance(dist),
  116. static_cast<RealType>(2.6666666666666666666666666666666666666666666666666666666667L), 1000*tolfeweps);
  117. // std deviation:
  118. BOOST_CHECK_CLOSE_FRACTION(standard_deviation(dist),
  119. static_cast<RealType>(1.632993L), 1000 * tolerance);
  120. //// hazard:
  121. //BOOST_CHECK_CLOSE_FRACTION(hazard(dist, x),
  122. // pdf(dist, x) / cdf(complement(dist, x)), tolerance);
  123. //// cumulative hazard:
  124. //BOOST_CHECK_CLOSE_FRACTION(chf(dist, x),
  125. // -log(cdf(complement(dist, x))), tolerance);
  126. // coefficient_of_variation:
  127. BOOST_CHECK_CLOSE_FRACTION(coefficient_of_variation(dist),
  128. standard_deviation(dist) / mean(dist), tolerance);
  129. // mode:
  130. BOOST_CHECK_CLOSE_FRACTION(mode(dist),
  131. static_cast<RealType>(0.8284271L), tolerance);
  132. // median
  133. BOOST_CHECK_CLOSE_FRACTION(median(dist),
  134. static_cast<RealType>(1.5122506636053668L), tolerance);
  135. // Fails for real_concept - because std::numeric_limits<RealType>::digits = 0
  136. // skewness:
  137. BOOST_CHECK_CLOSE_FRACTION(skewness(dist),
  138. static_cast<RealType>(2.449490L), tolerance);
  139. // kurtosis:
  140. BOOST_CHECK_CLOSE_FRACTION(kurtosis(dist),
  141. static_cast<RealType>(10-3), tolerance);
  142. BOOST_CHECK_CLOSE_FRACTION(kurtosis_excess(dist),
  143. static_cast<RealType>(10), tolerance);
  144. } // template <class RealType>void test_spots(RealType)
  145. BOOST_AUTO_TEST_CASE( test_main )
  146. {
  147. using boost::math::inverse_gaussian;
  148. using boost::math::inverse_gaussian_distribution;
  149. //int precision = 17; // std::numeric_limits<double::max_digits10;
  150. double tolfeweps = numeric_limits<double>::epsilon() * 5;
  151. //double tol6decdigits = numeric_limits<float>::epsilon() * 2;
  152. // Check that can generate inverse_gaussian distribution using the two convenience methods:
  153. boost::math::inverse_gaussian w12(1., 2); // Using typedef
  154. inverse_gaussian_distribution<> w23(2., 3); // Using default RealType double.
  155. boost::math::inverse_gaussian w11; // Use default unity values for mean and scale.
  156. // Note NOT myn01() as the compiler will interpret as a function!
  157. BOOST_CHECK_EQUAL(w11.mean(), 1);
  158. BOOST_CHECK_EQUAL(w11.scale(), 1);
  159. BOOST_CHECK_EQUAL(w23.mean(), 2);
  160. BOOST_CHECK_EQUAL(w23.scale(), 3);
  161. BOOST_CHECK_EQUAL(w23.shape(), 1.5L);
  162. // Check the synonyms, provided to allow generic use of find_location and find_scale.
  163. BOOST_CHECK_EQUAL(w11.mean(), w11.location());
  164. BOOST_CHECK_EQUAL(w11.scale(), w11.scale());
  165. BOOST_CHECK_CLOSE_FRACTION(mean(w11), static_cast<double>(1), tolfeweps); // Default mean == unity
  166. BOOST_CHECK_CLOSE_FRACTION(scale(w11), static_cast<double>(1), tolfeweps); // Default mean == unity
  167. // median
  168. // (test double because fails for real_concept because numeric_limits<real_concept>::digits = 0)
  169. BOOST_CHECK_CLOSE_FRACTION(median(w11),
  170. static_cast<double>(0.67584130569523893), tolfeweps);
  171. BOOST_CHECK_CLOSE_FRACTION(median(w23),
  172. static_cast<double>(1.5122506636053668), tolfeweps);
  173. // Initial spot tests using double values from R.
  174. // library(SuppDists)
  175. // formatC(SuppDists::dinverse_gaussian(1, 1, 1), digits=17) ...
  176. BOOST_CHECK_CLOSE_FRACTION( // x = 1
  177. pdf(w11, 1.), static_cast<double>(0.3989422804014327), tolfeweps); // pdf
  178. BOOST_CHECK_CLOSE_FRACTION(
  179. cdf(w11, 1.), static_cast<double>(0.66810200122317065), 10 * tolfeweps); // cdf
  180. BOOST_CHECK_CLOSE_FRACTION(
  181. pdf(w11, 0.1), static_cast<double>(0.21979480031862672), tolfeweps); // pdf
  182. BOOST_CHECK_CLOSE_FRACTION(
  183. cdf(w11, 0.1), static_cast<double>(0.0040761113207110162), 10 * tolfeweps); // cdf
  184. BOOST_CHECK_CLOSE_FRACTION( // small x
  185. pdf(w11, 0.01), static_cast<double>(2.0811768202028392e-19), tolfeweps); // pdf
  186. BOOST_CHECK_CLOSE_FRACTION(
  187. cdf(w11, 0.01), static_cast<double>(4.122313403318778e-23), 10 * tolfeweps); // cdf
  188. BOOST_CHECK_CLOSE_FRACTION( // smaller x
  189. pdf(w11, 0.001), static_cast<double>(2.4420044378793562e-213), tolfeweps); // pdf
  190. BOOST_CHECK_CLOSE_FRACTION(
  191. cdf(w11, 0.001), static_cast<double>(4.8791443010851493e-219), 1000 * tolfeweps); // cdf
  192. // 4.8791443010859224e-219 versus 4.8791443010851493e-219 so still 14 decimal digits.
  193. BOOST_CHECK_CLOSE_FRACTION(
  194. quantile(w11, 0.66810200122317065), static_cast<double>(1.), 1 * tolfeweps); // cdf
  195. BOOST_CHECK_CLOSE_FRACTION(
  196. quantile(w11, 0.0040761113207110162), static_cast<double>(0.1), 1 * tolfeweps); // cdf
  197. BOOST_CHECK_CLOSE_FRACTION(
  198. quantile(w11, 4.122313403318778e-23), 0.01, 1 * tolfeweps); // quantile
  199. BOOST_CHECK_CLOSE_FRACTION(
  200. quantile(w11, 2.4420044378793562e-213), 0.001, 0.03); // quantile
  201. // quantile 0.001026926242348481 compared to expected 0.001, so much less accurate,
  202. // but better than R that gives up completely!
  203. // R Error in SuppDists::qinverse_gaussian(4.87914430108515e-219, 1, 1) : Infinite value in NewtonRoot()
  204. BOOST_CHECK_CLOSE_FRACTION(
  205. pdf(w11, 0.5), static_cast<double>(0.87878257893544476), tolfeweps); // pdf
  206. BOOST_CHECK_CLOSE_FRACTION(
  207. cdf(w11, 0.5), static_cast<double>(0.3649755481729598), tolfeweps); // cdf
  208. BOOST_CHECK_CLOSE_FRACTION(
  209. pdf(w11, 2), static_cast<double>(0.10984782236693059), tolfeweps); // pdf
  210. BOOST_CHECK_CLOSE_FRACTION(
  211. cdf(w11, 2), static_cast<double>(.88547542598600637), tolfeweps); // cdf
  212. BOOST_CHECK_CLOSE_FRACTION(
  213. pdf(w11, 10), static_cast<double>(0.00021979480031862676), tolfeweps); // pdf
  214. BOOST_CHECK_CLOSE_FRACTION(
  215. cdf(w11, 10), static_cast<double>(0.99964958546279115), tolfeweps); // cdf
  216. BOOST_CHECK_CLOSE_FRACTION(
  217. pdf(w11, 100), static_cast<double>(2.0811768202028246e-25), tolfeweps); // pdf
  218. BOOST_CHECK_CLOSE_FRACTION(
  219. cdf(w11, 100), static_cast<double>(1), tolfeweps); // cdf
  220. BOOST_CHECK_CLOSE_FRACTION(
  221. pdf(w11, 1000), static_cast<double>(2.4420044378793564e-222), 10 * tolfeweps); // pdf
  222. BOOST_CHECK_CLOSE_FRACTION(
  223. cdf(w11, 1000), static_cast<double>(1.), tolfeweps); // cdf
  224. // A few more misc tests, probably not very useful.
  225. BOOST_CHECK_CLOSE_FRACTION(
  226. cdf(w11, 1.), static_cast<double>(0.66810200122317065), tolfeweps); // cdf
  227. BOOST_CHECK_CLOSE_FRACTION(
  228. cdf(w11, 0.1), static_cast<double>(0.0040761113207110162), tolfeweps * 5); // cdf
  229. // 0.0040761113207110162 0.0040761113207110362
  230. BOOST_CHECK_CLOSE_FRACTION(
  231. cdf(w11, 0.2), static_cast<double>(0.063753567519976254), tolfeweps * 5); // cdf
  232. BOOST_CHECK_CLOSE_FRACTION(
  233. cdf(w11, 0.5), static_cast<double>(0.3649755481729598), tolfeweps); // cdf
  234. BOOST_CHECK_CLOSE_FRACTION(
  235. cdf(w11, 0.9), static_cast<double>(0.62502320258649202), tolfeweps); // cdf
  236. BOOST_CHECK_CLOSE_FRACTION(
  237. cdf(w11, 0.99), static_cast<double>(0.66408247396139031), tolfeweps); // cdf
  238. BOOST_CHECK_CLOSE_FRACTION(
  239. cdf(w11, 0.999), static_cast<double>(0.66770275955311675), tolfeweps); // cdf
  240. BOOST_CHECK_CLOSE_FRACTION(
  241. cdf(w11, 10.), static_cast<double>(0.99964958546279115), tolfeweps); // cdf
  242. BOOST_CHECK_CLOSE_FRACTION(
  243. cdf(w11, 50.), static_cast<double>(0.99999999999992029), tolfeweps); // cdf
  244. BOOST_CHECK_CLOSE_FRACTION(
  245. quantile(w11, 0.3649755481729598), static_cast<double>(0.5), tolfeweps); // quantile
  246. BOOST_CHECK_CLOSE_FRACTION(
  247. quantile(w11, 0.62502320258649202), static_cast<double>(0.9), tolfeweps); // quantile
  248. BOOST_CHECK_CLOSE_FRACTION(
  249. quantile(w11, 0.0040761113207110162), static_cast<double>(0.1), tolfeweps); // quantile
  250. // Wald(2,3) tests
  251. // ===================
  252. BOOST_CHECK_CLOSE_FRACTION( // formatC(SuppDists::dinvGauss(1, 2, 3), digits=17) "0.47490884963330904"
  253. pdf(w23, 1.), static_cast<double>(0.47490884963330904), tolfeweps ); // pdf
  254. BOOST_CHECK_CLOSE_FRACTION(
  255. pdf(w23, 0.1), static_cast<double>(2.8854207087665401e-05), tolfeweps * 2); // pdf
  256. //2.8854207087665452e-005 2.8854207087665401e-005
  257. BOOST_CHECK_CLOSE_FRACTION(
  258. pdf(w23, 10.), static_cast<double>(0.0019822751498574636), tolfeweps); // pdf
  259. BOOST_CHECK_CLOSE_FRACTION(
  260. pdf(w23, 10.), static_cast<double>(0.0019822751498574636), tolfeweps); // pdf
  261. // Bigger changes in mean and scale.
  262. inverse_gaussian w012(0.1, 2);
  263. BOOST_CHECK_CLOSE_FRACTION(
  264. pdf(w012, 1.), static_cast<double>(3.7460367141230404e-36), tolfeweps ); // pdf
  265. BOOST_CHECK_CLOSE_FRACTION(
  266. cdf(w012, 1.), static_cast<double>(1), tolfeweps ); // pdf
  267. inverse_gaussian w0110(0.1, 10);
  268. BOOST_CHECK_CLOSE_FRACTION(
  269. pdf(w0110, 1.), static_cast<double>(1.6279643678071011e-176), 100 * tolfeweps ); // pdf
  270. BOOST_CHECK_CLOSE_FRACTION(
  271. cdf(w0110, 1.), static_cast<double>(1), tolfeweps ); // cdf
  272. BOOST_CHECK_CLOSE_FRACTION(
  273. cdf(complement(w0110, 1.)), static_cast<double>(3.2787685715328683e-179), 1e6 * tolfeweps ); // cdf complement
  274. // Differs because of loss of accuracy.
  275. BOOST_CHECK_CLOSE_FRACTION(
  276. pdf(w0110, 0.1), static_cast<double>(39.894228040143268), tolfeweps ); // pdf
  277. BOOST_CHECK_CLOSE_FRACTION(
  278. cdf(w0110, 0.1), static_cast<double>(0.51989761564832704), 10 * tolfeweps ); // cdf
  279. // Basic sanity-check spot values for all floating-point types..
  280. // (Parameter value, arbitrarily zero, only communicates the floating point type).
  281. test_spots(0.0F); // Test float. OK at decdigits = 0 tolerance = 0.0001 %
  282. test_spots(0.0); // Test double. OK at decdigits 7, tolerance = 1e07 %
  283. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  284. test_spots(0.0L); // Test long double.
  285. #ifndef BOOST_MATH_NO_REAL_CONCEPT_TESTS
  286. test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
  287. #endif
  288. #else
  289. std::cout << "<note>The long double tests have been disabled on this platform "
  290. "either because the long double overloads of the usual math functions are "
  291. "not available at all, or because they are too inaccurate for these tests "
  292. "to pass.</note>" << std::endl;
  293. #endif
  294. /* */
  295. } // BOOST_AUTO_TEST_CASE( test_main )
  296. /*
  297. Output:
  298. */