test_nc_beta.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // test_nc_beta.cpp
  2. // Copyright John Maddock 2008.
  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. //
  8. // This must appear *before* any #includes, and precludes pch usage:
  9. //
  10. #define BOOST_MATH_ASSERT_UNDEFINED_POLICY false
  11. #ifdef _MSC_VER
  12. #pragma warning (disable:4127 4512)
  13. #endif
  14. #if !defined(TEST_FLOAT) && !defined(TEST_DOUBLE) && !defined(TEST_LDOUBLE) && !defined(TEST_REAL_CONCEPT)
  15. # define TEST_FLOAT
  16. # define TEST_DOUBLE
  17. # define TEST_LDOUBLE
  18. # define TEST_REAL_CONCEPT
  19. #endif
  20. #include <boost/math/concepts/real_concept.hpp> // for real_concept
  21. #include <boost/math/distributions/non_central_beta.hpp> // for chi_squared_distribution
  22. #include <boost/math/distributions/poisson.hpp> // for poisson_distribution
  23. #define BOOST_TEST_MAIN
  24. #include <boost/test/unit_test.hpp> // for test_main
  25. #include <boost/test/results_collector.hpp>
  26. #include <boost/test/unit_test.hpp>
  27. #include <boost/test/tools/floating_point_comparison.hpp> // for BOOST_CHECK_CLOSE
  28. #include "functor.hpp"
  29. #include "handle_test_result.hpp"
  30. #include "test_ncbeta_hooks.hpp"
  31. #include "table_type.hpp"
  32. #include "test_nc_beta.hpp"
  33. #include <iostream>
  34. using std::cout;
  35. using std::endl;
  36. #include <limits>
  37. using std::numeric_limits;
  38. void expected_results()
  39. {
  40. //
  41. // Define the max and mean errors expected for
  42. // various compilers and platforms.
  43. //
  44. const char* largest_type;
  45. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  46. if(boost::math::policies::digits<double, boost::math::policies::policy<> >() == boost::math::policies::digits<long double, boost::math::policies::policy<> >())
  47. {
  48. largest_type = "(long\\s+)?double|real_concept";
  49. }
  50. else
  51. {
  52. largest_type = "long double|real_concept";
  53. }
  54. #else
  55. largest_type = "(long\\s+)?double|real_concept";
  56. #endif
  57. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  58. if(boost::math::tools::digits<long double>() == 64)
  59. {
  60. //
  61. // Allow a small amount of error leakage from long double to double:
  62. //
  63. add_expected_result(
  64. "[^|]*", // compiler
  65. "[^|]*", // stdlib
  66. "[^|]*", // platform
  67. "double", // test type(s)
  68. "[^|]*large[^|]*", // test data group
  69. "[^|]*", 5, 5); // test function
  70. }
  71. if(boost::math::tools::digits<long double>() == 64)
  72. {
  73. add_expected_result(
  74. "[^|]*", // compiler
  75. "[^|]*", // stdlib
  76. "[^|]*", // platform
  77. largest_type, // test type(s)
  78. "[^|]*medium[^|]*", // test data group
  79. "[^|]*", 1200, 500); // test function
  80. add_expected_result(
  81. "[^|]*", // compiler
  82. "[^|]*", // stdlib
  83. "[^|]*", // platform
  84. largest_type, // test type(s)
  85. "[^|]*large[^|]*", // test data group
  86. "[^|]*", 40000, 6000); // test function
  87. }
  88. #endif
  89. //
  90. // Catch all cases come last:
  91. //
  92. add_expected_result(
  93. "[^|]*", // compiler
  94. "[^|]*", // stdlib
  95. "[^|]*", // platform
  96. largest_type, // test type(s)
  97. "[^|]*medium[^|]*", // test data group
  98. "[^|]*", 1500, 500); // test function
  99. add_expected_result(
  100. "[^|]*", // compiler
  101. "[^|]*", // stdlib
  102. "[^|]*", // platform
  103. "real_concept", // test type(s)
  104. "[^|]*large[^|]*", // test data group
  105. "[^|]*", 30000, 4000); // test function
  106. add_expected_result(
  107. "[^|]*", // compiler
  108. "[^|]*", // stdlib
  109. "[^|]*", // platform
  110. largest_type, // test type(s)
  111. "[^|]*large[^|]*", // test data group
  112. "[^|]*", 20000, 2000); // test function
  113. //
  114. // Finish off by printing out the compiler/stdlib/platform names,
  115. // we do this to make it easier to mark up expected error rates.
  116. //
  117. std::cout << "Tests run with " << BOOST_COMPILER << ", "
  118. << BOOST_STDLIB << ", " << BOOST_PLATFORM << std::endl;
  119. }
  120. template <class RealType>
  121. RealType naive_pdf(RealType a, RealType b, RealType lam, RealType x)
  122. {
  123. using namespace boost::math;
  124. RealType term = pdf(poisson_distribution<RealType>(lam/2), 0)
  125. * ibeta_derivative(a, b, x);
  126. RealType sum = term;
  127. int i = 1;
  128. while(term / sum > tools::epsilon<RealType>())
  129. {
  130. term = pdf(poisson_distribution<RealType>(lam/2), i)
  131. * ibeta_derivative(a + i, b, x);
  132. ++i;
  133. sum += term;
  134. }
  135. return sum;
  136. }
  137. template <class RealType>
  138. void test_spot(
  139. RealType a, // alpha
  140. RealType b, // beta
  141. RealType ncp, // non-centrality param
  142. RealType cs, // Chi Square statistic
  143. RealType P, // CDF
  144. RealType Q, // Complement of CDF
  145. RealType D, // PDF
  146. RealType tol) // Test tolerance
  147. {
  148. boost::math::non_central_beta_distribution<RealType> dist(a, b, ncp);
  149. BOOST_CHECK_CLOSE(
  150. cdf(dist, cs), P, tol);
  151. //
  152. // Sanity checking using the naive PDF calculation above fails at
  153. // float precision:
  154. //
  155. if(!boost::is_same<float, RealType>::value)
  156. {
  157. BOOST_CHECK_CLOSE(
  158. pdf(dist, cs), naive_pdf(dist.alpha(), dist.beta(), ncp, cs), tol);
  159. }
  160. BOOST_CHECK_CLOSE(
  161. pdf(dist, cs), D, tol);
  162. if((P < 0.99) && (Q < 0.99))
  163. {
  164. //
  165. // We can only check this if P is not too close to 1,
  166. // so that we can guarantee Q is reasonably free of error:
  167. //
  168. BOOST_CHECK_CLOSE(
  169. cdf(complement(dist, cs)), Q, tol);
  170. BOOST_CHECK_CLOSE(
  171. quantile(dist, P), cs, tol * 10);
  172. BOOST_CHECK_CLOSE(
  173. quantile(complement(dist, Q)), cs, tol * 10);
  174. }
  175. }
  176. template <class RealType> // Any floating-point type RealType.
  177. void test_spots(RealType)
  178. {
  179. RealType tolerance = (std::max)(
  180. boost::math::tools::epsilon<RealType>() * 100,
  181. (RealType)1e-6) * 100;
  182. RealType abs_tolerance = boost::math::tools::epsilon<RealType>() * 100;
  183. cout << "Tolerance = " << tolerance << "%." << endl;
  184. //
  185. // Spot tests use values computed by the R statistical
  186. // package and the pbeta and dbeta functions:
  187. //
  188. test_spot(
  189. RealType(2), // alpha
  190. RealType(5), // beta
  191. RealType(1), // non-centrality param
  192. RealType(0.25), // Chi Square statistic
  193. RealType(0.3658349), // CDF
  194. RealType(1-0.3658349), // Complement of CDF
  195. RealType(2.184465), // PDF
  196. RealType(tolerance));
  197. test_spot(
  198. RealType(20), // alpha
  199. RealType(15), // beta
  200. RealType(35), // non-centrality param
  201. RealType(0.75), // Chi Square statistic
  202. RealType(0.6994175), // CDF
  203. RealType(1-0.6994175), // Complement of CDF
  204. RealType(5.576146), // PDF
  205. RealType(tolerance));
  206. test_spot(
  207. RealType(100), // alpha
  208. RealType(3), // beta
  209. RealType(63), // non-centrality param
  210. RealType(0.95), // Chi Square statistic
  211. RealType(0.03529306), // CDF
  212. RealType(1-0.03529306), // Complement of CDF
  213. RealType(3.637894), // PDF
  214. RealType(tolerance));
  215. test_spot(
  216. RealType(0.25), // alpha
  217. RealType(0.75), // beta
  218. RealType(150), // non-centrality param
  219. RealType(0.975), // Chi Square statistic
  220. RealType(0.09752216), // CDF
  221. RealType(1-0.09752216), // Complement of CDF
  222. RealType(8.020935), // PDF
  223. RealType(tolerance));
  224. BOOST_MATH_STD_USING
  225. boost::math::non_central_beta_distribution<RealType> dist(100, 3, 63);
  226. BOOST_CHECK_CLOSE(mean(dist), RealType(4.82280451915522329944315287538684030781836554279474240490936e13L) * exp(-RealType(31.5)) * 100 / 103, tolerance);
  227. // Variance only guarantees small absolute error:
  228. BOOST_CHECK_SMALL(variance(dist)
  229. - static_cast<RealType>(RealType(4.85592267707818899235900237275021938334418424134218087127572e13L)
  230. * exp(RealType(-31.5)) * 100 * 101 / (103 * 104) -
  231. RealType(4.82280451915522329944315287538684030781836554279474240490936e13L) * RealType(4.82280451915522329944315287538684030781836554279474240490936e13L)
  232. * exp(RealType(-63)) * 10000 / (103 * 103)), abs_tolerance);
  233. BOOST_MATH_CHECK_THROW(skewness(dist), boost::math::evaluation_error);
  234. BOOST_MATH_CHECK_THROW(kurtosis(dist), boost::math::evaluation_error);
  235. BOOST_MATH_CHECK_THROW(kurtosis_excess(dist), boost::math::evaluation_error);
  236. } // template <class RealType>void test_spots(RealType)
  237. BOOST_AUTO_TEST_CASE( test_main )
  238. {
  239. BOOST_MATH_CONTROL_FP;
  240. // Basic sanity-check spot values.
  241. expected_results();
  242. // (Parameter value, arbitrarily zero, only communicates the floating point type).
  243. #ifdef TEST_FLOAT
  244. test_spots(0.0F); // Test float.
  245. #endif
  246. #ifdef TEST_DOUBLE
  247. test_spots(0.0); // Test double.
  248. #endif
  249. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  250. #ifdef TEST_LDOUBLE
  251. test_spots(0.0L); // Test long double.
  252. #endif
  253. #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
  254. #ifdef TEST_REAL_CONCEPT
  255. test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
  256. #endif
  257. #endif
  258. #endif
  259. #ifdef TEST_FLOAT
  260. test_accuracy(0.0F, "float"); // Test float.
  261. #endif
  262. #ifdef TEST_DOUBLE
  263. test_accuracy(0.0, "double"); // Test double.
  264. #endif
  265. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  266. #ifdef TEST_LDOUBLE
  267. test_accuracy(0.0L, "long double"); // Test long double.
  268. #endif
  269. #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
  270. #ifdef TEST_REAL_CONCEPT
  271. test_accuracy(boost::math::concepts::real_concept(0.), "real_concept"); // Test real concept.
  272. #endif
  273. #endif
  274. #endif
  275. } // BOOST_AUTO_TEST_CASE( test_main )