test_inverse_gamma_distribution.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. // test_inverse_gamma.cpp
  2. // Copyright Paul A. Bristow 2010.
  3. // Copyright John Maddock 2010.
  4. // Use, modification and distribution are subject to the
  5. // Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt
  7. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. #ifdef _MSC_VER
  9. # pragma warning (disable : 4224) // nonstandard extension used : formal parameter 'type' was previously defined as a type
  10. // in Boost.test and lexical_cast
  11. # pragma warning (disable : 4310) // cast truncates constant value
  12. #endif
  13. #include <boost/math/tools/test.hpp>
  14. #include <boost/math/concepts/real_concept.hpp> // for real_concept
  15. using ::boost::math::concepts::real_concept;
  16. //#include <boost/math/tools/test.hpp>
  17. #define BOOST_TEST_MAIN
  18. #include <boost/test/unit_test.hpp> // for test_main
  19. #include <boost/test/tools/floating_point_comparison.hpp> // for BOOST_CHECK_CLOSE_FRACTION
  20. #include "test_out_of_range.hpp"
  21. #include <boost/math/distributions/inverse_gamma.hpp> // for inverse_gamma_distribution
  22. using boost::math::inverse_gamma_distribution;
  23. using ::boost::math::inverse_gamma;
  24. // using ::boost::math::cdf;
  25. // using ::boost::math::pdf;
  26. #include <boost/math/special_functions/gamma.hpp>
  27. using boost::math::tgamma; // for naive pdf.
  28. #include <iostream>
  29. using std::cout;
  30. using std::endl;
  31. #include <limits>
  32. using std::numeric_limits;
  33. template <class RealType>
  34. RealType naive_pdf(RealType shape, RealType scale, RealType x)
  35. { // Formula from Wikipedia
  36. using namespace std; // For ADL of std functions.
  37. using boost::math::tgamma;
  38. RealType result = (pow(scale, shape) * pow(x, (-shape -1)) * exp(-scale/x) ) / tgamma(shape);
  39. return result;
  40. }
  41. // Test using a spot value from some other reference source,
  42. // in this case test values from output from R provided by Thomas Mang.
  43. template <class RealType>
  44. void test_spot(
  45. RealType shape, // shape,
  46. RealType scale, // scale,
  47. RealType x, // random variate x,
  48. RealType pd, // expected pdf,
  49. RealType P, // expected CDF,
  50. RealType Q, // expected complement of CDF,
  51. RealType tol) // test tolerance.
  52. {
  53. boost::math::inverse_gamma_distribution<RealType> dist(shape, scale);
  54. BOOST_CHECK_CLOSE_FRACTION
  55. ( // Compare to expected PDF.
  56. pdf(dist, x), // calculated.
  57. pd, // expected
  58. tol);
  59. BOOST_CHECK_CLOSE_FRACTION( // Compare to naive formula (might be less accurate).
  60. pdf(dist, x), naive_pdf(dist.shape(), dist.scale(), x), tol);
  61. BOOST_CHECK_CLOSE_FRACTION( // Compare to expected CDF.
  62. cdf(dist, x), P, tol);
  63. if((P < 0.999) && (Q < 0.999))
  64. { // We can only check this if P is not too close to 1,
  65. // so that we can guarantee Q is accurate:
  66. BOOST_CHECK_CLOSE_FRACTION(
  67. cdf(complement(dist, x)), Q, tol);
  68. BOOST_CHECK_CLOSE_FRACTION(
  69. quantile(dist, P), x, tol); // quantile(pdf) = x
  70. BOOST_CHECK_CLOSE_FRACTION(
  71. quantile(complement(dist, Q)), x, tol);
  72. }
  73. } // test_spot
  74. // Test using a spot value from some other reference source.
  75. template <class RealType> // Any floating-point type RealType.
  76. void test_spots(RealType)
  77. {
  78. // Basic sanity checks, test data is to six decimal places only
  79. // so set tolerance to 0.000001 expressed as a percentage = 0.0001%.
  80. RealType tolerance = 0.000001f; // as fraction.
  81. cout << "Tolerance = " << tolerance * 100 << "%." << endl;
  82. // This test values from output from R provided by Thomas Mang.
  83. test_spot(static_cast<RealType>(2), static_cast<RealType>(1), // shape, scale
  84. static_cast<RealType>(2.L), // x
  85. static_cast<RealType>(0.075816332464079136L), // pdf
  86. static_cast<RealType>(0.90979598956895047L), // cdf
  87. static_cast<RealType>(1 - 0.90979598956895047L), // cdf complement
  88. tolerance // tol
  89. );
  90. test_spot(static_cast<RealType>(1.593), static_cast<RealType>( 0.5), // shape, scale
  91. static_cast<RealType>( 0.5), // x
  92. static_cast<RealType>(0.82415241749687074L), // pdf
  93. static_cast<RealType>(0.60648042700409865L), // cdf
  94. static_cast<RealType>(1 - 0.60648042700409865L), // cdf complement
  95. tolerance // tol
  96. );
  97. test_spot(static_cast<RealType>(13.319), static_cast<RealType>(0.5), // shape, scale
  98. static_cast<RealType>(0.5), // x
  99. static_cast<RealType>(0.00000000068343206235379223), // pdf
  100. static_cast<RealType>(0.99999999997242739L), // cdf
  101. static_cast<RealType>(1 - 0.99999999997242739L), // cdf complement
  102. tolerance // tol
  103. );
  104. test_spot(static_cast<RealType>(1.593), static_cast<RealType>(1), // shape, scale
  105. static_cast<RealType>(1.977), // x
  106. static_cast<RealType>(0.11535946773398653L), // pdf
  107. static_cast<RealType>(0.82449794420341549L), // cdf
  108. static_cast<RealType>(1 - 0.82449794420341549L), // cdf complement
  109. tolerance // tol
  110. );
  111. test_spot(static_cast<RealType>(6.666), static_cast<RealType>(1.411), // shape, scale
  112. static_cast<RealType>(5), // x
  113. static_cast<RealType>(0.000000084415758206386872), // pdf
  114. static_cast<RealType>(0.99999993427280998L), // cdf
  115. static_cast<RealType>(1 - 0.99999993427280998L), // cdf complement
  116. tolerance // tol
  117. );
  118. // Check some bad parameters to the distribution,
  119. #ifndef BOOST_NO_EXCEPTIONS
  120. BOOST_MATH_CHECK_THROW(boost::math::inverse_gamma_distribution<RealType> igbad1(-1, 0), std::domain_error); // negative shape.
  121. BOOST_MATH_CHECK_THROW(boost::math::inverse_gamma_distribution<RealType> igbad2(0, -1), std::domain_error); // negative scale.
  122. BOOST_MATH_CHECK_THROW(boost::math::inverse_gamma_distribution<RealType> igbad2(-1, -1), std::domain_error); // negative scale and shape.
  123. #else
  124. BOOST_MATH_CHECK_THROW(boost::math::inverse_gamma_distribution<RealType>(-1, 0), std::domain_error); // negative shape.
  125. BOOST_MATH_CHECK_THROW(boost::math::inverse_gamma_distribution<RealType>(0, -1), std::domain_error); // negative scale.
  126. BOOST_MATH_CHECK_THROW(boost::math::inverse_gamma_distribution<RealType>(-1, -1), std::domain_error); // negative scale and shape.
  127. #endif
  128. inverse_gamma_distribution<RealType> ig21(2, 1);
  129. if(std::numeric_limits<RealType>::has_infinity)
  130. {
  131. BOOST_MATH_CHECK_THROW(pdf(ig21, +std::numeric_limits<RealType>::infinity()), std::domain_error); // x = + infinity, pdf = 0
  132. BOOST_MATH_CHECK_THROW(pdf(ig21, -std::numeric_limits<RealType>::infinity()), std::domain_error); // x = - infinity, pdf = 0
  133. BOOST_MATH_CHECK_THROW(cdf(ig21, +std::numeric_limits<RealType>::infinity()),std::domain_error ); // x = + infinity, cdf = 1
  134. BOOST_MATH_CHECK_THROW(cdf(ig21, -std::numeric_limits<RealType>::infinity()), std::domain_error); // x = - infinity, cdf = 0
  135. BOOST_MATH_CHECK_THROW(cdf(complement(ig21, +std::numeric_limits<RealType>::infinity())), std::domain_error); // x = + infinity, c cdf = 0
  136. BOOST_MATH_CHECK_THROW(cdf(complement(ig21, -std::numeric_limits<RealType>::infinity())), std::domain_error); // x = - infinity, c cdf = 1
  137. #ifndef BOOST_NO_EXCEPTIONS
  138. BOOST_MATH_CHECK_THROW(boost::math::inverse_gamma_distribution<RealType> nbad1(std::numeric_limits<RealType>::infinity(), static_cast<RealType>(1)), std::domain_error); // +infinite mean
  139. BOOST_MATH_CHECK_THROW(boost::math::inverse_gamma_distribution<RealType> nbad1(-std::numeric_limits<RealType>::infinity(), static_cast<RealType>(1)), std::domain_error); // -infinite mean
  140. BOOST_MATH_CHECK_THROW(boost::math::inverse_gamma_distribution<RealType> nbad1(static_cast<RealType>(0), std::numeric_limits<RealType>::infinity()), std::domain_error); // infinite sd
  141. #else
  142. BOOST_MATH_CHECK_THROW(boost::math::inverse_gamma_distribution<RealType>(std::numeric_limits<RealType>::infinity(), static_cast<RealType>(1)), std::domain_error); // +infinite mean
  143. BOOST_MATH_CHECK_THROW(boost::math::inverse_gamma_distribution<RealType>(-std::numeric_limits<RealType>::infinity(), static_cast<RealType>(1)), std::domain_error); // -infinite mean
  144. BOOST_MATH_CHECK_THROW(boost::math::inverse_gamma_distribution<RealType>(static_cast<RealType>(0), std::numeric_limits<RealType>::infinity()), std::domain_error); // infinite sd
  145. #endif
  146. }
  147. if (std::numeric_limits<RealType>::has_quiet_NaN)
  148. {
  149. // No longer allow x to be NaN, then these tests should throw.
  150. BOOST_MATH_CHECK_THROW(pdf(ig21, +std::numeric_limits<RealType>::quiet_NaN()), std::domain_error); // x = NaN
  151. BOOST_MATH_CHECK_THROW(cdf(ig21, +std::numeric_limits<RealType>::quiet_NaN()), std::domain_error); // x = NaN
  152. BOOST_MATH_CHECK_THROW(cdf(complement(ig21, +std::numeric_limits<RealType>::quiet_NaN())), std::domain_error); // x = + infinity
  153. BOOST_MATH_CHECK_THROW(quantile(ig21, +std::numeric_limits<RealType>::quiet_NaN()), std::domain_error); // p = + infinity
  154. BOOST_MATH_CHECK_THROW(quantile(complement(ig21, +std::numeric_limits<RealType>::quiet_NaN())), std::domain_error); // p = + infinity
  155. }
  156. // Spot check for pdf using 'naive pdf' function
  157. for(RealType x = 0.5; x < 5; x += 0.5)
  158. {
  159. BOOST_CHECK_CLOSE_FRACTION(
  160. pdf(inverse_gamma_distribution<RealType>(5, 6), x),
  161. naive_pdf(RealType(5), RealType(6), x),
  162. tolerance);
  163. } // Spot checks for parameters:
  164. RealType tol_few_eps = boost::math::tools::epsilon<RealType>() * 5; // 5 eps as a fraction.
  165. inverse_gamma_distribution<RealType> dist51(5, 1);
  166. inverse_gamma_distribution<RealType> dist52(5, 2);
  167. inverse_gamma_distribution<RealType> dist31(3, 1);
  168. inverse_gamma_distribution<RealType> dist111(11, 1);
  169. // 11 mean 0.10000000000000001, variance 0.0011111111111111111, sd 0.033333333333333333
  170. RealType x = static_cast<RealType>(0.125);
  171. using namespace std; // ADL of std names.
  172. using namespace boost::math;
  173. // mean, variance etc
  174. BOOST_CHECK_CLOSE_FRACTION(mean(dist52), static_cast<RealType>(0.5), tol_few_eps);
  175. BOOST_CHECK_CLOSE_FRACTION(mean(dist111), static_cast<RealType>(0.1L), tol_few_eps);
  176. inverse_gamma_distribution<RealType> igamma41(static_cast<RealType>(4.), static_cast<RealType>(1.) );
  177. BOOST_CHECK_CLOSE_FRACTION(mean(igamma41), static_cast<RealType>(0.3333333333333333333333333333333333333333333333333333333L), tol_few_eps);
  178. // variance:
  179. BOOST_CHECK_CLOSE_FRACTION(variance(dist51), static_cast<RealType>(0.0208333333333333333333333333333333333333333333333333L), tol_few_eps);
  180. BOOST_CHECK_CLOSE_FRACTION(variance(dist31), static_cast<RealType>(0.25), tol_few_eps);
  181. BOOST_CHECK_CLOSE_FRACTION(variance(dist111), static_cast<RealType>(0.001111111111111111111111111111111111111111111111111L), tol_few_eps);
  182. // std deviation:
  183. BOOST_CHECK_CLOSE_FRACTION(standard_deviation(dist31), static_cast<RealType>(0.5), tol_few_eps);
  184. BOOST_CHECK_CLOSE_FRACTION(standard_deviation(dist111), static_cast<RealType>(0.0333333333333333333333333333333333333333333333333L), tol_few_eps);
  185. // hazard:
  186. BOOST_CHECK_CLOSE_FRACTION(hazard(dist51, x), pdf(dist51, x) / cdf(complement(dist51, x)), tol_few_eps);
  187. // cumulative hazard:
  188. BOOST_CHECK_CLOSE_FRACTION(chf(dist51, x), -log(cdf(complement(dist51, x))), tol_few_eps);
  189. // coefficient_of_variation:
  190. BOOST_CHECK_CLOSE_FRACTION(coefficient_of_variation(dist51), standard_deviation(dist51) / mean(dist51), tol_few_eps);
  191. // mode:
  192. BOOST_CHECK_CLOSE_FRACTION(mode(dist51), static_cast<RealType>(0.166666666666666666666666666666666666666666666666666L), tol_few_eps);
  193. // median
  194. //BOOST_CHECK_CLOSE_FRACTION(median(dist52), static_cast<RealType>(0), tol_few_eps);
  195. // Useful to have an exact median? Failing that use a loop back test.
  196. BOOST_CHECK_CLOSE_FRACTION(cdf(dist111, median(dist111)), 0.5, tol_few_eps);
  197. // skewness:
  198. BOOST_CHECK_CLOSE_FRACTION(skewness(dist111), static_cast<RealType>(1.5), tol_few_eps);
  199. //kurtosis:
  200. BOOST_CHECK_CLOSE_FRACTION(kurtosis(dist51), static_cast<RealType>(42 + 3), tol_few_eps);
  201. // kurtosis excess:
  202. BOOST_CHECK_CLOSE_FRACTION(kurtosis_excess(dist51), static_cast<RealType>(42), tol_few_eps);
  203. tol_few_eps = boost::math::tools::epsilon<RealType>() * 3; // 3 eps as a percentage.
  204. // Special and limit cases:
  205. if(std::numeric_limits<RealType>::is_specialized)
  206. {
  207. RealType mx = (std::numeric_limits<RealType>::max)();
  208. RealType mi = (std::numeric_limits<RealType>::min)();
  209. BOOST_CHECK_EQUAL(
  210. pdf(inverse_gamma_distribution<RealType>(1),
  211. static_cast<RealType>(mx)), // max()
  212. static_cast<RealType>(0)
  213. );
  214. BOOST_CHECK_EQUAL(
  215. pdf(inverse_gamma_distribution<RealType>(1),
  216. static_cast<RealType>(mi)), // min()
  217. static_cast<RealType>(0)
  218. );
  219. }
  220. BOOST_CHECK_EQUAL(
  221. pdf(inverse_gamma_distribution<RealType>(1), static_cast<RealType>(0)), static_cast<RealType>(0));
  222. BOOST_CHECK_EQUAL(
  223. pdf(inverse_gamma_distribution<RealType>(3), static_cast<RealType>(0))
  224. , static_cast<RealType>(0.0f));
  225. BOOST_CHECK_EQUAL(
  226. cdf(inverse_gamma_distribution<RealType>(1), static_cast<RealType>(0))
  227. , static_cast<RealType>(0.0f));
  228. BOOST_CHECK_EQUAL(
  229. cdf(inverse_gamma_distribution<RealType>(2), static_cast<RealType>(0))
  230. , static_cast<RealType>(0.0f));
  231. BOOST_CHECK_EQUAL(
  232. cdf(inverse_gamma_distribution<RealType>(3), static_cast<RealType>(0))
  233. , static_cast<RealType>(0.0f));
  234. BOOST_CHECK_EQUAL(
  235. cdf(complement(inverse_gamma_distribution<RealType>(1), static_cast<RealType>(0)))
  236. , static_cast<RealType>(1));
  237. BOOST_CHECK_EQUAL(
  238. cdf(complement(inverse_gamma_distribution<RealType>(2), static_cast<RealType>(0)))
  239. , static_cast<RealType>(1));
  240. BOOST_CHECK_EQUAL(
  241. cdf(complement(inverse_gamma_distribution<RealType>(3), static_cast<RealType>(0)))
  242. , static_cast<RealType>(1));
  243. BOOST_MATH_CHECK_THROW(
  244. pdf(
  245. inverse_gamma_distribution<RealType>(static_cast<RealType>(-1)), // shape negative.
  246. static_cast<RealType>(1)), std::domain_error
  247. );
  248. BOOST_MATH_CHECK_THROW(
  249. pdf(
  250. inverse_gamma_distribution<RealType>(static_cast<RealType>(8)),
  251. static_cast<RealType>(-1)), std::domain_error
  252. );
  253. BOOST_MATH_CHECK_THROW(
  254. cdf(
  255. inverse_gamma_distribution<RealType>(static_cast<RealType>(-1)),
  256. static_cast<RealType>(1)), std::domain_error
  257. );
  258. BOOST_MATH_CHECK_THROW(
  259. cdf(
  260. inverse_gamma_distribution<RealType>(static_cast<RealType>(8)),
  261. static_cast<RealType>(-1)), std::domain_error
  262. );
  263. BOOST_MATH_CHECK_THROW(
  264. cdf(complement(
  265. inverse_gamma_distribution<RealType>(static_cast<RealType>(-1)),
  266. static_cast<RealType>(1))), std::domain_error
  267. );
  268. BOOST_MATH_CHECK_THROW(
  269. cdf(complement(
  270. inverse_gamma_distribution<RealType>(static_cast<RealType>(8)),
  271. static_cast<RealType>(-1))), std::domain_error
  272. );
  273. BOOST_MATH_CHECK_THROW(
  274. quantile(
  275. inverse_gamma_distribution<RealType>(static_cast<RealType>(-1)),
  276. static_cast<RealType>(0.5)), std::domain_error
  277. );
  278. BOOST_MATH_CHECK_THROW(
  279. quantile(
  280. inverse_gamma_distribution<RealType>(static_cast<RealType>(8)),
  281. static_cast<RealType>(-1)), std::domain_error
  282. );
  283. BOOST_MATH_CHECK_THROW(
  284. quantile(
  285. inverse_gamma_distribution<RealType>(static_cast<RealType>(8)),
  286. static_cast<RealType>(1.1)), std::domain_error
  287. );
  288. BOOST_MATH_CHECK_THROW(
  289. quantile(complement(
  290. inverse_gamma_distribution<RealType>(static_cast<RealType>(-1)),
  291. static_cast<RealType>(0.5))), std::domain_error
  292. );
  293. BOOST_MATH_CHECK_THROW(
  294. quantile(complement(
  295. inverse_gamma_distribution<RealType>(static_cast<RealType>(8)),
  296. static_cast<RealType>(-1))), std::domain_error
  297. );
  298. BOOST_MATH_CHECK_THROW(
  299. quantile(complement(
  300. inverse_gamma_distribution<RealType>(static_cast<RealType>(8)),
  301. static_cast<RealType>(1.1))), std::domain_error
  302. );
  303. check_out_of_range<inverse_gamma_distribution<RealType> >(1, 1);
  304. } // template <class RealType>void test_spots(RealType)
  305. BOOST_AUTO_TEST_CASE( test_main )
  306. {
  307. BOOST_MATH_CONTROL_FP;
  308. // Check that can generate inverse_gamma distribution using the two convenience methods:
  309. // inverse_gamma_distribution; // with default parameters, shape = 1, scale - 1
  310. using boost::math::inverse_gamma;
  311. inverse_gamma ig2(2.); // Using typedef and shape parameter (and default scale = 1).
  312. BOOST_CHECK_EQUAL(ig2.shape(), 2.); // scale == 2.
  313. BOOST_CHECK_EQUAL(ig2.scale(), 1.); // scale == 1 (default).
  314. inverse_gamma ig; // Using typedef, type double and default values, shape = 1 and scale = 1
  315. // check default is (1, 1)
  316. BOOST_CHECK_EQUAL(ig.shape(), 1.); // shape == 1
  317. BOOST_CHECK_EQUAL(ig.scale(), 1.); // scale == 1
  318. BOOST_CHECK_EQUAL(mode(ig), 0.5); // mode = 1/2
  319. // Used to find some 'exact' values for testing mean, variance ...
  320. //for (int shape = 4; shape < 30; shape++)
  321. // {
  322. // inverse_gamma ig(shape, 1);
  323. // cout.precision(17);
  324. // cout << shape << ' ' << mean(ig) << ' ' << variance(ig) << ' ' << standard_deviation(ig)
  325. // << ' ' << median(ig) << endl;
  326. // }
  327. // and "using boost::math::inverse_gamma_distribution;".
  328. inverse_gamma_distribution<> ig23(2., 3.); // Using default RealType double.
  329. BOOST_CHECK_EQUAL(ig23.shape(), 2.); //
  330. BOOST_CHECK_EQUAL(ig23.scale(), 3.); //
  331. inverse_gamma_distribution<float> igf23(1.f, 2.f); // Using explicit RealType float.
  332. BOOST_CHECK_EQUAL(igf23.shape(), 1.f); //
  333. BOOST_CHECK_EQUAL(igf23.scale(), 2.f); //
  334. // Some tests using default double.
  335. double tol5eps = boost::math::tools::epsilon<double>() * 5; // 5 eps as a fraction.
  336. inverse_gamma_distribution<double> ig102(10., 2.); //
  337. BOOST_CHECK_EQUAL(ig102.shape(), 10.); //
  338. BOOST_CHECK_EQUAL(ig102.scale(), 2.); //
  339. // formatC(SuppDists::dinvGauss(10, 1, 0.5), digits=17)[1] "0.0011774669940754754"
  340. BOOST_CHECK_CLOSE_FRACTION(pdf(ig102, 0.5), 0.1058495335284024, tol5eps);
  341. // formatC(SuppDists::pinvGauss(10, 1, 0.5), digits=17) [1] "0.99681494462166653"
  342. BOOST_CHECK_CLOSE_FRACTION(cdf(ig102, 0.5), 0.99186775720306608, tol5eps);
  343. BOOST_CHECK_CLOSE_FRACTION(quantile(ig102, 0.05), 0.12734622346137681, tol5eps);
  344. BOOST_CHECK_CLOSE_FRACTION(quantile(ig102, 0.5), 0.20685272858879727, tol5eps);
  345. BOOST_CHECK_CLOSE_FRACTION(quantile(ig102, 0.95), 0.36863602680851204, tol5eps);
  346. // Check mean, etc spot values.
  347. inverse_gamma_distribution<double> ig51(5., 1.); // shape = 5, scale = 1
  348. BOOST_CHECK_CLOSE_FRACTION(mean(ig51), 0.25, tol5eps);
  349. BOOST_CHECK_CLOSE_FRACTION(variance(ig51), 0.0208333333333333333333333333333333333333333, tol5eps);
  350. BOOST_CHECK_CLOSE_FRACTION(skewness(ig51), 2 * std::sqrt(3.), tol5eps);
  351. BOOST_CHECK_CLOSE_FRACTION(kurtosis_excess(ig51), 42, tol5eps);
  352. // mode and median
  353. inverse_gamma_distribution<double> ig21(1., 2.);
  354. BOOST_CHECK_CLOSE_FRACTION(mode(ig21), 1, tol5eps);
  355. BOOST_CHECK_CLOSE_FRACTION(median(ig21), 2.8853900817779268, tol5eps);
  356. BOOST_CHECK_CLOSE_FRACTION(quantile(ig21, 0.5), 2.8853900817779268, tol5eps);
  357. BOOST_CHECK_CLOSE_FRACTION(cdf(ig21, median(ig21)), 0.5, tol5eps);
  358. // Check throws from bad parameters.
  359. inverse_gamma ig051(0.5, 1.); // shape < 1, so wrong for mean.
  360. BOOST_MATH_CHECK_THROW(mean(ig051), std::domain_error);
  361. inverse_gamma ig191(1.9999, 1.); // shape < 2, so wrong for variance.
  362. BOOST_MATH_CHECK_THROW(variance(ig191), std::domain_error);
  363. inverse_gamma ig291(2.9999, 1.); // shape < 3, so wrong for skewness.
  364. BOOST_MATH_CHECK_THROW(skewness(ig291), std::domain_error);
  365. inverse_gamma ig391(3.9999, 1.); // shape < 1, so wrong for kurtosis and kurtosis_excess.
  366. BOOST_MATH_CHECK_THROW(kurtosis(ig391), std::domain_error);
  367. BOOST_MATH_CHECK_THROW(kurtosis_excess(ig391), std::domain_error);
  368. // Basic sanity-check spot values.
  369. // (Parameter value, arbitrarily zero, only communicates the floating point type).
  370. test_spots(0.0F); // Test float. OK at decdigits = 0 tolerance = 0.0001 %
  371. test_spots(0.0); // Test double. OK at decdigits 7, tolerance = 1e07 %
  372. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  373. test_spots(0.0L); // Test long double.
  374. #ifndef BOOST_MATH_NO_REAL_CONCEPT_TESTS
  375. test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
  376. #endif
  377. #else
  378. std::cout << "<note>The long double tests have been disabled on this platform "
  379. "either because the long double overloads of the usual math functions are "
  380. "not available at all, or because they are too inaccurate for these tests "
  381. "to pass.</note>" << std::endl;
  382. #endif
  383. } // BOOST_AUTO_TEST_CASE( test_main )
  384. /*
  385. Output:
  386. ------ Build started: Project: test_inverse_gamma_distribution, Configuration: Release Win32 ------
  387. test_inverse_gamma_distribution.cpp
  388. Generating code
  389. Finished generating code
  390. test_inverse_gamma_distribution.vcxproj -> J:\Cpp\MathToolkit\test\Math_test\Release\test_inverse_gamma_distribution.exe
  391. Running 1 test case...
  392. Tolerance = 0.0001%.
  393. Tolerance = 0.0001%.
  394. Tolerance = 0.0001%.
  395. Tolerance = 0.0001%.
  396. *** No errors detected
  397. ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
  398. */