test_inverse_chi_squared_distribution.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. // test_inverse_chi_squared.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 : 4310) // cast truncates constant value.
  10. #endif
  11. // http://www.wolframalpha.com/input/?i=inverse+chisquare+distribution
  12. #include <boost/math/tools/test.hpp>
  13. #include <boost/math/concepts/real_concept.hpp> // for real_concept
  14. using ::boost::math::concepts::real_concept;
  15. //#include <boost/math/tools/test.hpp>
  16. #define BOOST_TEST_MAIN
  17. #include <boost/test/unit_test.hpp> // for test_main
  18. #include <boost/test/tools/floating_point_comparison.hpp> // for BOOST_CHECK_CLOSE_FRACTION
  19. #include "test_out_of_range.hpp"
  20. #include <boost/math/distributions/inverse_chi_squared.hpp> // for inverse_chisquared_distribution
  21. using boost::math::inverse_chi_squared_distribution;
  22. using boost::math::cdf;
  23. using boost::math::pdf;
  24. // Use Inverse Gamma distribution to check their relationship:
  25. // inverse_chi_squared<>(v) == inverse_gamma<>(v / 2., 0.5)
  26. #include <boost/math/distributions/inverse_gamma.hpp> // for inverse_gamma_distribution
  27. using boost::math::inverse_gamma_distribution;
  28. using boost::math::inverse_gamma;
  29. // using ::boost::math::cdf;
  30. // using ::boost::math::pdf;
  31. #include <boost/math/special_functions/gamma.hpp>
  32. using boost::math::tgamma; // for naive pdf.
  33. #include <iostream>
  34. using std::cout;
  35. using std::endl;
  36. #include <limits>
  37. using std::numeric_limits; // for epsilon.
  38. template <class RealType>
  39. RealType naive_pdf(RealType df, RealType scale, RealType x)
  40. { // Formula from Wikipedia
  41. using namespace std; // For ADL of std functions.
  42. using boost::math::tgamma;
  43. RealType result = pow(scale * df/2, df/2) * exp(-df * scale/(2 * x));
  44. result /= tgamma(df/2) * pow(x, 1 + df/2);
  45. return result;
  46. }
  47. // Test using a spot value from some other reference source,
  48. // in this case test values from output from R provided by Thomas Mang,
  49. // and Wolfram Mathematica by Mark Coleman.
  50. template <class RealType>
  51. void test_spot(
  52. RealType degrees_of_freedom, // degrees_of_freedom,
  53. RealType scale, // scale,
  54. RealType x, // random variate x,
  55. RealType pd, // expected pdf,
  56. RealType P, // expected CDF,
  57. RealType Q, // expected complement of CDF,
  58. RealType tol) // test tolerance.
  59. {
  60. boost::math::inverse_chi_squared_distribution<RealType> dist(degrees_of_freedom, scale);
  61. BOOST_CHECK_CLOSE_FRACTION
  62. ( // Compare to expected PDF.
  63. pdf(dist, x), // calculated.
  64. pd, // expected
  65. tol);
  66. BOOST_CHECK_CLOSE_FRACTION( // Compare to naive pdf formula (probably less accurate).
  67. pdf(dist, x), naive_pdf(dist.degrees_of_freedom(), dist.scale(), x), tol);
  68. BOOST_CHECK_CLOSE_FRACTION( // Compare to expected CDF.
  69. cdf(dist, x), P, tol);
  70. if((P < 0.999) && (Q < 0.999))
  71. { // We can only check this if P is not too close to 1,
  72. // so that we can guarantee Q is accurate:
  73. BOOST_CHECK_CLOSE_FRACTION(
  74. cdf(complement(dist, x)), Q, tol); // 1 - cdf
  75. BOOST_CHECK_CLOSE_FRACTION(
  76. quantile(dist, P), x, tol); // quantile(cdf) = x
  77. BOOST_CHECK_CLOSE_FRACTION(
  78. quantile(complement(dist, Q)), x, tol); // quantile(complement(1 - cdf)) = x
  79. }
  80. } // test_spot
  81. template <class RealType> // Any floating-point type RealType.
  82. void test_spots(RealType)
  83. {
  84. // Basic sanity checks, some test data is to six decimal places only,
  85. // so set tolerance to 0.000001 (expressed as a percentage = 0.0001%).
  86. RealType tolerance = 0.000001f;
  87. cout << "Tolerance = " << tolerance * 100 << "%." << endl;
  88. // This test values from output from geoR (17 decimal digits) guided by Thomas Mang.
  89. test_spot(static_cast<RealType>(2), static_cast<RealType>(1./2.),
  90. // degrees_of_freedom, default scale = 1/df.
  91. static_cast<RealType>(1.L), // x.
  92. static_cast<RealType>(0.30326532985631671L), // pdf.
  93. static_cast<RealType>(0.60653065971263365L), // cdf.
  94. static_cast<RealType>(1 - 0.606530659712633657L), // cdf complement.
  95. tolerance // tol
  96. );
  97. // Tests from Mark Coleman & Georgi Boshnakov using Wolfram Mathematica.
  98. test_spot(static_cast<RealType>(10), static_cast<RealType>(0.1L), // degrees_of_freedom, scale
  99. static_cast<RealType>(0.2), // x
  100. static_cast<RealType>(1.6700235722635659824529759616528281217001163943570L), // pdf
  101. static_cast<RealType>(0.89117801891415124234834646836872197623907651175353L), // cdf
  102. static_cast<RealType>(1 - 0.89117801891415127L), // cdf complement
  103. tolerance // tol
  104. );
  105. test_spot(static_cast<RealType>(10), static_cast<RealType>(0.1L), // degrees_of_freedom, scale
  106. static_cast<RealType>(0.5), // x
  107. static_cast<RealType>(0.03065662009762021L), // pdf
  108. static_cast<RealType>(0.99634015317265628765454354418728984933240514654437L), // cdf
  109. static_cast<RealType>(1 - 0.99634015317265628765454354418728984933240514654437L), // cdf complement
  110. tolerance // tol
  111. );
  112. test_spot(static_cast<RealType>(10), static_cast<RealType>(2), // degrees_of_freedom, scale
  113. static_cast<RealType>(0.5), // x
  114. static_cast<RealType>(0.00054964096598361569L), // pdf
  115. static_cast<RealType>(0.000016944743930067383903707995865261004246785511612700L), // cdf
  116. static_cast<RealType>(1 - 0.000016944743930067383903707995865261004246785511612700L), // cdf complement
  117. tolerance // tol
  118. );
  119. // Check some bad parameters to the distribution cause expected exception to be thrown.
  120. #ifndef BOOST_NO_EXCEPTIONS
  121. BOOST_MATH_CHECK_THROW(boost::math::inverse_chi_squared_distribution<RealType> ichsqbad1(-1), std::domain_error); // negative degrees_of_freedom.
  122. BOOST_MATH_CHECK_THROW(boost::math::inverse_chi_squared_distribution<RealType> ichsqbad2(1, -1), std::domain_error); // negative scale.
  123. BOOST_MATH_CHECK_THROW(boost::math::inverse_chi_squared_distribution<RealType> ichsqbad3(-1, -1), std::domain_error); // negative scale and degrees_of_freedom.
  124. #else
  125. BOOST_MATH_CHECK_THROW(boost::math::inverse_chi_squared_distribution<RealType>(-1), std::domain_error); // negative degrees_of_freedom.
  126. BOOST_MATH_CHECK_THROW(boost::math::inverse_chi_squared_distribution<RealType>(1, -1), std::domain_error); // negative scale.
  127. BOOST_MATH_CHECK_THROW(boost::math::inverse_chi_squared_distribution<RealType>(-1, -1), std::domain_error); // negative scale and degrees_of_freedom.
  128. #endif
  129. check_out_of_range<boost::math::inverse_chi_squared_distribution<RealType> >(1, 1);
  130. inverse_chi_squared_distribution<RealType> ichsq;
  131. if(std::numeric_limits<RealType>::has_infinity)
  132. {
  133. BOOST_MATH_CHECK_THROW(pdf(ichsq, +std::numeric_limits<RealType>::infinity()), std::domain_error); // x = + infinity, pdf = 0
  134. BOOST_MATH_CHECK_THROW(pdf(ichsq, -std::numeric_limits<RealType>::infinity()), std::domain_error); // x = - infinity, pdf = 0
  135. BOOST_MATH_CHECK_THROW(cdf(ichsq, +std::numeric_limits<RealType>::infinity()),std::domain_error ); // x = + infinity, cdf = 1
  136. BOOST_MATH_CHECK_THROW(cdf(ichsq, -std::numeric_limits<RealType>::infinity()), std::domain_error); // x = - infinity, cdf = 0
  137. BOOST_MATH_CHECK_THROW(cdf(complement(ichsq, +std::numeric_limits<RealType>::infinity())), std::domain_error); // x = + infinity, c cdf = 0
  138. BOOST_MATH_CHECK_THROW(cdf(complement(ichsq, -std::numeric_limits<RealType>::infinity())), std::domain_error); // x = - infinity, c cdf = 1
  139. #ifndef BOOST_NO_EXCEPTIONS
  140. BOOST_MATH_CHECK_THROW(boost::math::inverse_chi_squared_distribution<RealType> nbad1(std::numeric_limits<RealType>::infinity(), static_cast<RealType>(1)), std::domain_error); // +infinite mean
  141. BOOST_MATH_CHECK_THROW(boost::math::inverse_chi_squared_distribution<RealType> nbad1(-std::numeric_limits<RealType>::infinity(), static_cast<RealType>(1)), std::domain_error); // -infinite mean
  142. BOOST_MATH_CHECK_THROW(boost::math::inverse_chi_squared_distribution<RealType> nbad1(static_cast<RealType>(0), std::numeric_limits<RealType>::infinity()), std::domain_error); // infinite sd
  143. #else
  144. BOOST_MATH_CHECK_THROW(boost::math::inverse_chi_squared_distribution<RealType>(std::numeric_limits<RealType>::infinity(), static_cast<RealType>(1)), std::domain_error); // +infinite mean
  145. BOOST_MATH_CHECK_THROW(boost::math::inverse_chi_squared_distribution<RealType>(-std::numeric_limits<RealType>::infinity(), static_cast<RealType>(1)), std::domain_error); // -infinite mean
  146. BOOST_MATH_CHECK_THROW(boost::math::inverse_chi_squared_distribution<RealType>(static_cast<RealType>(0), std::numeric_limits<RealType>::infinity()), std::domain_error); // infinite sd
  147. #endif
  148. }
  149. if (std::numeric_limits<RealType>::has_quiet_NaN)
  150. { // If no longer allow x or p to be NaN, then these tests should throw.
  151. BOOST_MATH_CHECK_THROW(pdf(ichsq, +std::numeric_limits<RealType>::quiet_NaN()), std::domain_error); // x = NaN
  152. BOOST_MATH_CHECK_THROW(cdf(ichsq, +std::numeric_limits<RealType>::quiet_NaN()), std::domain_error); // x = NaN
  153. BOOST_MATH_CHECK_THROW(cdf(complement(ichsq, +std::numeric_limits<RealType>::quiet_NaN())), std::domain_error); // x = + infinity
  154. BOOST_MATH_CHECK_THROW(quantile(ichsq, std::numeric_limits<RealType>::quiet_NaN()), std::domain_error); // p = + quiet_NaN
  155. BOOST_MATH_CHECK_THROW(quantile(complement(ichsq, std::numeric_limits<RealType>::quiet_NaN())), std::domain_error); // p = + quiet_NaN
  156. }
  157. // Spot check for pdf using 'naive pdf' function
  158. for(RealType x = 0.5; x < 5; x += 0.5)
  159. {
  160. BOOST_CHECK_CLOSE_FRACTION(
  161. pdf(inverse_chi_squared_distribution<RealType>(5, 6), x),
  162. naive_pdf(RealType(5), RealType(6), x),
  163. tolerance);
  164. } // Spot checks for parameters:
  165. RealType tol_2eps = boost::math::tools::epsilon<RealType>() * 2; // 2 eps as a fraction.
  166. inverse_chi_squared_distribution<RealType> dist51(5, 1);
  167. inverse_chi_squared_distribution<RealType> dist52(5, 2);
  168. inverse_chi_squared_distribution<RealType> dist31(3, 1);
  169. inverse_chi_squared_distribution<RealType> dist111(11, 1);
  170. // 11 mean 0.10000000000000001, variance 0.0011111111111111111, sd 0.033333333333333333
  171. using namespace std; // ADL of std names.
  172. using namespace boost::math;
  173. inverse_chi_squared_distribution<RealType> dist10(10);
  174. // mean, variance etc
  175. BOOST_CHECK_CLOSE_FRACTION(mean(dist10), static_cast<RealType>(0.125), tol_2eps);
  176. BOOST_CHECK_CLOSE_FRACTION(variance(dist10), static_cast<RealType>(0.0052083333333333333333333333333333333333333333333333L), tol_2eps);
  177. BOOST_CHECK_CLOSE_FRACTION(mode(dist10), static_cast<RealType>(0.08333333333333333333333333333333333333333333333L), tol_2eps);
  178. BOOST_CHECK_CLOSE_FRACTION(median(dist10), static_cast<RealType>(0.10704554778227709530244586234274024205738435512468L), tol_2eps);
  179. BOOST_CHECK_CLOSE_FRACTION(cdf(dist10, median(dist10)), static_cast<RealType>(0.5L), 4 * tol_2eps);
  180. BOOST_CHECK_CLOSE_FRACTION(skewness(dist10), static_cast<RealType>(3.4641016151377545870548926830117447338856105076208L), tol_2eps);
  181. BOOST_CHECK_CLOSE_FRACTION(kurtosis(dist10), static_cast<RealType>(45), tol_2eps);
  182. BOOST_CHECK_CLOSE_FRACTION(kurtosis_excess(dist10), static_cast<RealType>(45-3), tol_2eps);
  183. tol_2eps = boost::math::tools::epsilon<RealType>() * 2; // 2 eps as a percentage.
  184. // Special and limit cases:
  185. RealType mx = (std::numeric_limits<RealType>::max)();
  186. RealType mi = (std::numeric_limits<RealType>::min)();
  187. BOOST_CHECK_EQUAL(
  188. pdf(inverse_chi_squared_distribution<RealType>(1),
  189. static_cast<RealType>(mx)), // max()
  190. static_cast<RealType>(0)
  191. );
  192. BOOST_CHECK_EQUAL(
  193. pdf(inverse_chi_squared_distribution<RealType>(1),
  194. static_cast<RealType>(mi)), // min()
  195. static_cast<RealType>(0)
  196. );
  197. BOOST_CHECK_EQUAL(
  198. pdf(inverse_chi_squared_distribution<RealType>(1), static_cast<RealType>(0)), static_cast<RealType>(0));
  199. BOOST_CHECK_EQUAL(
  200. pdf(inverse_chi_squared_distribution<RealType>(3), static_cast<RealType>(0))
  201. , static_cast<RealType>(0.0f));
  202. BOOST_CHECK_EQUAL(
  203. cdf(inverse_chi_squared_distribution<RealType>(1), static_cast<RealType>(0))
  204. , static_cast<RealType>(0.0f));
  205. BOOST_CHECK_EQUAL(
  206. cdf(inverse_chi_squared_distribution<RealType>(2), static_cast<RealType>(0))
  207. , static_cast<RealType>(0.0f));
  208. BOOST_CHECK_EQUAL(
  209. cdf(inverse_chi_squared_distribution<RealType>(3L), static_cast<RealType>(0L))
  210. , static_cast<RealType>(0));
  211. BOOST_CHECK_EQUAL(
  212. cdf(complement(inverse_chi_squared_distribution<RealType>(1), static_cast<RealType>(0)))
  213. , static_cast<RealType>(1));
  214. BOOST_CHECK_EQUAL(
  215. cdf(complement(inverse_chi_squared_distribution<RealType>(2), static_cast<RealType>(0)))
  216. , static_cast<RealType>(1));
  217. BOOST_CHECK_EQUAL(
  218. cdf(complement(inverse_chi_squared_distribution<RealType>(3), static_cast<RealType>(0)))
  219. , static_cast<RealType>(1));
  220. BOOST_MATH_CHECK_THROW(
  221. pdf(
  222. inverse_chi_squared_distribution<RealType>(static_cast<RealType>(-1)), // degrees_of_freedom negative.
  223. static_cast<RealType>(1)), std::domain_error
  224. );
  225. BOOST_MATH_CHECK_THROW(
  226. pdf(
  227. inverse_chi_squared_distribution<RealType>(static_cast<RealType>(8)),
  228. static_cast<RealType>(-1)), std::domain_error
  229. );
  230. BOOST_MATH_CHECK_THROW(
  231. cdf(
  232. inverse_chi_squared_distribution<RealType>(static_cast<RealType>(-1)),
  233. static_cast<RealType>(1)), std::domain_error
  234. );
  235. BOOST_MATH_CHECK_THROW(
  236. cdf(
  237. inverse_chi_squared_distribution<RealType>(static_cast<RealType>(8)),
  238. static_cast<RealType>(-1)), std::domain_error
  239. );
  240. BOOST_MATH_CHECK_THROW(
  241. cdf(complement(
  242. inverse_chi_squared_distribution<RealType>(static_cast<RealType>(-1)),
  243. static_cast<RealType>(1))), std::domain_error
  244. );
  245. BOOST_MATH_CHECK_THROW(
  246. cdf(complement(
  247. inverse_chi_squared_distribution<RealType>(static_cast<RealType>(8)),
  248. static_cast<RealType>(-1))), std::domain_error
  249. );
  250. BOOST_MATH_CHECK_THROW(
  251. quantile(
  252. inverse_chi_squared_distribution<RealType>(static_cast<RealType>(-1)),
  253. static_cast<RealType>(0.5)), std::domain_error
  254. );
  255. BOOST_MATH_CHECK_THROW(
  256. quantile(
  257. inverse_chi_squared_distribution<RealType>(static_cast<RealType>(8)),
  258. static_cast<RealType>(-1)), std::domain_error
  259. );
  260. BOOST_MATH_CHECK_THROW(
  261. quantile(
  262. inverse_chi_squared_distribution<RealType>(static_cast<RealType>(8)),
  263. static_cast<RealType>(1.1)), std::domain_error
  264. );
  265. BOOST_MATH_CHECK_THROW(
  266. quantile(complement(
  267. inverse_chi_squared_distribution<RealType>(static_cast<RealType>(-1)),
  268. static_cast<RealType>(0.5))), std::domain_error
  269. );
  270. BOOST_MATH_CHECK_THROW(
  271. quantile(complement(
  272. inverse_chi_squared_distribution<RealType>(static_cast<RealType>(8)),
  273. static_cast<RealType>(-1))), std::domain_error
  274. );
  275. BOOST_MATH_CHECK_THROW(
  276. quantile(complement(
  277. inverse_chi_squared_distribution<RealType>(static_cast<RealType>(8)),
  278. static_cast<RealType>(1.1))), std::domain_error
  279. );
  280. } // template <class RealType>void test_spots(RealType)
  281. BOOST_AUTO_TEST_CASE( test_main )
  282. {
  283. BOOST_MATH_CONTROL_FP;
  284. double tol_few_eps = numeric_limits<double>::epsilon() * 4;
  285. // Check that can generate inverse_chi_squared distribution using the two convenience methods:
  286. // inverse_chi_squared_distribution; // with default parameters, degrees_of_freedom = 1, scale - 1
  287. using boost::math::inverse_chi_squared;
  288. // Some constructor tests using default double.
  289. double tol4eps = boost::math::tools::epsilon<double>() * 4; // 4 eps as a fraction.
  290. inverse_chi_squared ichsqdef; // Using typedef and both default parameters.
  291. BOOST_CHECK_EQUAL(ichsqdef.degrees_of_freedom(), 1.); // df == 1
  292. BOOST_CHECK_EQUAL(ichsqdef.scale(), 1); // scale == 1./df
  293. BOOST_CHECK_CLOSE_FRACTION(pdf(ichsqdef, 1), 0.24197072451914330, tol4eps);
  294. BOOST_CHECK_CLOSE_FRACTION(pdf(ichsqdef, 9), 0.013977156581221969, tol4eps);
  295. inverse_chi_squared_distribution<double> ichisq102(10., 2); // Both parameters specified.
  296. BOOST_CHECK_EQUAL(ichisq102.degrees_of_freedom(), 10.); // Check both parameters stored OK.
  297. BOOST_CHECK_EQUAL(ichisq102.scale(), 2.); // Check both parameters stored OK.
  298. inverse_chi_squared_distribution<double> ichisq10(10.); // Only df parameter specified (unscaled).
  299. BOOST_CHECK_EQUAL(ichisq10.degrees_of_freedom(), 10.); // Check parameter stored.
  300. BOOST_CHECK_EQUAL(ichisq10.scale(), 0.1); // Check default scale = 1/df = 1/10 = 0.1
  301. BOOST_CHECK_CLOSE_FRACTION(pdf(ichisq10, 1), 0.00078975346316749169, tol4eps);
  302. BOOST_CHECK_CLOSE_FRACTION(pdf(ichisq10, 10), 0.0000000012385799798186384, tol4eps);
  303. BOOST_CHECK_CLOSE_FRACTION(mode(ichisq10), 0.0833333333333333333333333333333333333333, tol4eps);
  304. // nu * xi / nu + 2 = 10 * 0.1 / (10 + 2) = 1/12 = 0.0833333...
  305. // mode is not defined in Mathematica.
  306. // See Discussion section http://en.wikipedia.org/wiki/Talk:Scaled-inverse-chi-square_distribution
  307. // for origin of this formula.
  308. inverse_chi_squared_distribution<double> ichisq5(5.); // // Only df parameter specified.
  309. BOOST_CHECK_EQUAL(ichisq5.degrees_of_freedom(), 5.); // check parameter stored.
  310. BOOST_CHECK_EQUAL(ichisq5.scale(), 1./5.); // check default is 1/df
  311. BOOST_CHECK_CLOSE_FRACTION(pdf(ichisq5, 0.2), 3.0510380337346841, tol4eps);
  312. BOOST_CHECK_CLOSE_FRACTION(cdf(ichisq5, 0.5), 0.84914503608460956, tol4eps);
  313. BOOST_CHECK_CLOSE_FRACTION(cdf(complement(ichisq5, 0.5)), 1 - 0.84914503608460956, tol4eps);
  314. BOOST_CHECK_CLOSE_FRACTION(quantile(ichisq5, 0.84914503608460956), 0.5, tol4eps*100);
  315. BOOST_CHECK_CLOSE_FRACTION(quantile(complement(ichisq5, 1. - 0.84914503608460956)), 0.5, tol4eps*100);
  316. // Check mean, etc spot values.
  317. inverse_chi_squared_distribution<double> ichisq81(8., 1.); // degrees_of_freedom = 5, scale = 1
  318. BOOST_CHECK_CLOSE_FRACTION(mean(ichisq81),1.33333333333333333333333333333333333333333, tol4eps);
  319. BOOST_CHECK_CLOSE_FRACTION(variance(ichisq81), 0.888888888888888888888888888888888888888888888, tol4eps);
  320. BOOST_CHECK_CLOSE_FRACTION(skewness(ichisq81), 2 * std::sqrt(8.), tol4eps);
  321. inverse_chi_squared_distribution<double> ichisq21(2., 1.);
  322. BOOST_CHECK_CLOSE_FRACTION(mode(ichisq21), 0.5, tol4eps);
  323. BOOST_CHECK_CLOSE_FRACTION(median(ichisq21), 1.4426950408889634, tol4eps);
  324. inverse_chi_squared ichsq4(4.); // Using typedef and degrees_of_freedom parameter (and default scale = 1/df).
  325. BOOST_CHECK_EQUAL(ichsq4.degrees_of_freedom(), 4.); // df == 4.
  326. BOOST_CHECK_EQUAL(ichsq4.scale(), 0.25); // scale == 1 /df == 1/4.
  327. inverse_chi_squared ichsq32(3, 2);
  328. BOOST_CHECK_EQUAL(ichsq32.degrees_of_freedom(), 3.); // df == 3.
  329. BOOST_CHECK_EQUAL(ichsq32.scale(), 2); // scale == 2
  330. inverse_chi_squared ichsq11(1, 1); // Using explicit degrees_of_freedom parameter, and default scale = 1).
  331. BOOST_CHECK_CLOSE_FRACTION(mode(ichsq11), 0.3333333333333333333333333333333333333333, tol4eps);
  332. // (1 * 1)/ (1 + 2) = 1/3 using Wikipedia nu * xi /(nu + 2)
  333. BOOST_CHECK_EQUAL(ichsq11.degrees_of_freedom(), 1.); // df == 1 (default).
  334. BOOST_CHECK_EQUAL(ichsq11.scale(), 1.); // scale == 1.
  335. /*
  336. // Used to find some 'exact' values for testing mean, variance ...
  337. // First with scale fixed at unity (Wikipedia definition 1)
  338. cout << "df scale mean variance sd median" << endl;
  339. for (int degrees_of_freedom = 8; degrees_of_freedom < 30; degrees_of_freedom++)
  340. {
  341. inverse_chi_squared ichisq(degrees_of_freedom, 1);
  342. cout.precision(17);
  343. cout << degrees_of_freedom << " " << 1 << " " << mean(ichisq) << ' '
  344. << variance(ichisq) << ' ' << standard_deviation(ichisq)
  345. << ' ' << median(ichisq) << endl;
  346. }
  347. // Default scale = 1 / df
  348. cout << "|\n" << "df scale mean variance sd median" << endl;
  349. for (int degrees_of_freedom = 8; degrees_of_freedom < 30; degrees_of_freedom++)
  350. {
  351. inverse_chi_squared ichisq(degrees_of_freedom);
  352. cout.precision(17);
  353. cout << degrees_of_freedom << " " << 1./degrees_of_freedom << " " << mean(ichisq) << ' '
  354. << variance(ichisq) << ' ' << standard_deviation(ichisq)
  355. << ' ' << median(ichisq) << endl;
  356. }
  357. */
  358. inverse_chi_squared_distribution<> ichisq14(14, 1); // Using default RealType double.
  359. BOOST_CHECK_CLOSE_FRACTION(mean(ichisq14), 1.166666666666666666666666666666666666666666666, tol4eps);
  360. BOOST_CHECK_CLOSE_FRACTION(variance(ichisq14), 0.272222222222222222222222222222222222222222222, tol4eps);
  361. inverse_chi_squared_distribution<> ichisq121(12); // Using default RealType double.
  362. BOOST_CHECK_CLOSE_FRACTION(mean(ichisq121), 0.1, tol4eps);
  363. BOOST_CHECK_CLOSE_FRACTION(variance(ichisq121), 0.0025, tol4eps);
  364. BOOST_CHECK_CLOSE_FRACTION(standard_deviation(ichisq121), 0.05, tol4eps);
  365. // and "using boost::math::inverse_chi_squared_distribution;".
  366. inverse_chi_squared_distribution<> ichsq23(2., 3.); // Using default RealType double.
  367. BOOST_CHECK_EQUAL(ichsq23.degrees_of_freedom(), 2.); //
  368. BOOST_CHECK_EQUAL(ichsq23.scale(), 3.); //
  369. BOOST_MATH_CHECK_THROW(mean(ichsq23), std::domain_error); // Degrees of freedom (nu) must be > 2
  370. BOOST_MATH_CHECK_THROW(variance(ichsq23), std::domain_error); // Degrees of freedom (nu) must be > 4
  371. BOOST_MATH_CHECK_THROW(skewness(ichsq23), std::domain_error); // Degrees of freedom (nu) must be > 6
  372. BOOST_MATH_CHECK_THROW(kurtosis_excess(ichsq23), std::domain_error); // Degrees of freedom (nu) must be > 8
  373. { // Check relationship between inverse gamma and inverse chi_squared distributions.
  374. using boost::math::inverse_gamma_distribution;
  375. double df = 2.;
  376. double scale = 1.;
  377. double alpha = df/2; // aka inv_gamma shape
  378. double beta = scale /2; // inv_gamma scale.
  379. inverse_gamma_distribution<> ig(alpha, beta);
  380. inverse_chi_squared_distribution<> ichsq(df, 1./df); // == default scale.
  381. BOOST_CHECK_EQUAL(pdf(ichsq, 0), 0); // Special case of zero x.
  382. double x = 0.5;
  383. BOOST_CHECK_EQUAL(pdf(ig, x), pdf(ichsq, x)); // inv_gamma compared to inv_chisq
  384. BOOST_CHECK_EQUAL(cdf(ichsq, 0), 0); // Special case of zero.
  385. BOOST_CHECK_EQUAL(cdf(ig, x), cdf(ichsq, x)); // invgamma == invchisq
  386. // Test pdf by comparing using naive_pdf with relation to inverse gamma distribution
  387. // wikipedia http://en.wikipedia.org/wiki/Scaled-inverse-chi-square_distribution related distributions.
  388. // So if naive_pdf is correct, inverse_chi_squared_distribution should agree.
  389. df = 1.; scale = 1.;
  390. BOOST_CHECK_CLOSE_FRACTION(naive_pdf(df, scale, x), pdf(ichsq11, x), tol_few_eps);
  391. //inverse_gamma_distribution<> igd(df/2, (df * scale)/2);
  392. inverse_gamma_distribution<> igd11(df/2, df * scale/2);
  393. BOOST_CHECK_CLOSE_FRACTION(naive_pdf(df, scale, x), pdf(igd11, x), tol_few_eps);
  394. BOOST_CHECK_CLOSE_FRACTION(naive_pdf(df, scale, x), pdf(ichsq11, x), tol_few_eps);
  395. df = 2; scale = 1;
  396. inverse_gamma_distribution<> igd21(df/2, df * scale/2);
  397. inverse_chi_squared_distribution<> ichsq21(df, scale);
  398. BOOST_CHECK_CLOSE_FRACTION(naive_pdf(df, scale, x), pdf(igd21, x), tol_few_eps); // 0.54134113294645081 OK
  399. BOOST_CHECK_CLOSE_FRACTION(naive_pdf(df, scale, x), pdf(ichsq21, x), tol_few_eps);
  400. df = 2; scale = 2;
  401. inverse_gamma_distribution<> igd22(df/2, df * scale/2);
  402. inverse_chi_squared_distribution<> ichsq22(df, scale);
  403. BOOST_CHECK_CLOSE_FRACTION(naive_pdf(df, scale, x), pdf(igd22, x), tol_few_eps);
  404. BOOST_CHECK_CLOSE_FRACTION(naive_pdf(df, scale, x), pdf(ichsq22, x), tol_few_eps);
  405. }
  406. // Check using float.
  407. inverse_chi_squared_distribution<float> igf23(1.f, 2.f); // Using explicit RealType float.
  408. BOOST_CHECK_EQUAL(igf23.degrees_of_freedom(), 1.f); //
  409. BOOST_CHECK_EQUAL(igf23.scale(), 2.f); //
  410. // Check throws from bad parameters.
  411. inverse_chi_squared ig051(0.5, 1.); // degrees_of_freedom < 1, so wrong for mean.
  412. BOOST_MATH_CHECK_THROW(mean(ig051), std::domain_error);
  413. inverse_chi_squared ig191(1.9999, 1.); // degrees_of_freedom < 2, so wrong for variance.
  414. BOOST_MATH_CHECK_THROW(variance(ig191), std::domain_error);
  415. inverse_chi_squared ig291(2.9999, 1.); // degrees_of_freedom < 3, so wrong for skewness.
  416. BOOST_MATH_CHECK_THROW(skewness(ig291), std::domain_error);
  417. inverse_chi_squared ig391(3.9999, 1.); // degrees_of_freedom < 1, so wrong for kurtosis and kurtosis_excess.
  418. BOOST_MATH_CHECK_THROW(kurtosis(ig391), std::domain_error);
  419. BOOST_MATH_CHECK_THROW(kurtosis_excess(ig391), std::domain_error);
  420. inverse_chi_squared ig102(10, 2); // Wolfram.com/ page 2, quantile = 2.96859.
  421. //http://reference.wolfram.com/mathematica/ref/InverseChiSquareDistribution.html
  422. BOOST_CHECK_CLOSE_FRACTION(quantile(ig102, 0.75), 2.96859, 0.000001);
  423. BOOST_CHECK_CLOSE_FRACTION(cdf(ig102, 2.96859), 0.75 , 0.000001);
  424. BOOST_CHECK_CLOSE_FRACTION(cdf(complement(ig102, 2.96859)), 1 - 0.75 , 0.00001);
  425. BOOST_CHECK_CLOSE_FRACTION(quantile(complement(ig102, 1 - 0.75)), 2.96859, 0.000001);
  426. // Basic sanity-check spot values.
  427. // (Parameter value, arbitrarily zero, only communicates the floating point type).
  428. test_spots(0.0F); // Test float. OK at decdigits = 0 tolerance = 0.0001 %
  429. test_spots(0.0); // Test double. OK at decdigits 7, tolerance = 1e07 %
  430. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  431. test_spots(0.0L); // Test long double.
  432. #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0582))
  433. test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
  434. #endif
  435. #else
  436. std::cout << "<note>The long double tests have been disabled on this platform "
  437. "either because the long double overloads of the usual math functions are "
  438. "not available at all, or because they are too inaccurate for these tests "
  439. "to pass.</note>" << std::endl;
  440. #endif
  441. /* */
  442. } // BOOST_AUTO_TEST_CASE( test_main )
  443. /*
  444. Output:
  445. */