test_ibeta_inv.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // Copyright John Maddock 2006.
  2. // Copyright Paul A. Bristow 2007, 2009
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/math/concepts/real_concept.hpp>
  7. #define BOOST_TEST_MAIN
  8. #include <boost/test/unit_test.hpp>
  9. #include <boost/test/tools/floating_point_comparison.hpp>
  10. #include <boost/math/special_functions/math_fwd.hpp>
  11. #include <boost/math/tools/stats.hpp>
  12. #include <boost/math/tools/test.hpp>
  13. #include <boost/math/constants/constants.hpp>
  14. #include <boost/type_traits/is_floating_point.hpp>
  15. #include <boost/array.hpp>
  16. #include "functor.hpp"
  17. #include "handle_test_result.hpp"
  18. #include "table_type.hpp"
  19. #ifndef SC_
  20. #define SC_(x) static_cast<typename table_type<T>::type>(BOOST_JOIN(x, L))
  21. #endif
  22. template <class Real, class T>
  23. void test_inverses(const T& data)
  24. {
  25. using namespace std;
  26. //typedef typename T::value_type row_type;
  27. typedef Real value_type;
  28. value_type precision = static_cast<value_type>(ldexp(1.0, 1-boost::math::policies::digits<value_type, boost::math::policies::policy<> >()/2)) * 100;
  29. if(boost::math::policies::digits<value_type, boost::math::policies::policy<> >() < 50)
  30. precision = 1; // 1% or two decimal digits, all we can hope for when the input is truncated
  31. for(unsigned i = 0; i < data.size(); ++i)
  32. {
  33. //
  34. // These inverse tests are thrown off if the output of the
  35. // incomplete beta is too close to 1: basically there is insuffient
  36. // information left in the value we're using as input to the inverse
  37. // to be able to get back to the original value.
  38. //
  39. if(Real(data[i][5]) == 0)
  40. BOOST_CHECK_EQUAL(boost::math::ibeta_inv(Real(data[i][0]), Real(data[i][1]), Real(data[i][5])), value_type(0));
  41. else if((1 - Real(data[i][5]) > 0.001)
  42. && (fabs(Real(data[i][5])) > 2 * boost::math::tools::min_value<value_type>())
  43. && (fabs(Real(data[i][5])) > 2 * boost::math::tools::min_value<double>()))
  44. {
  45. value_type inv = boost::math::ibeta_inv(Real(data[i][0]), Real(data[i][1]), Real(data[i][5]));
  46. BOOST_CHECK_CLOSE(Real(data[i][2]), inv, precision);
  47. }
  48. else if(1 == Real(data[i][5]))
  49. BOOST_CHECK_EQUAL(boost::math::ibeta_inv(Real(data[i][0]), Real(data[i][1]), Real(data[i][5])), value_type(1));
  50. if(Real(data[i][6]) == 0)
  51. BOOST_CHECK_EQUAL(boost::math::ibetac_inv(Real(data[i][0]), Real(data[i][1]), Real(data[i][6])), value_type(1));
  52. else if((1 - Real(data[i][6]) > 0.001)
  53. && (fabs(Real(data[i][6])) > 2 * boost::math::tools::min_value<value_type>())
  54. && (fabs(Real(data[i][6])) > 2 * boost::math::tools::min_value<double>()))
  55. {
  56. value_type inv = boost::math::ibetac_inv(Real(data[i][0]), Real(data[i][1]), Real(data[i][6]));
  57. BOOST_CHECK_CLOSE(Real(data[i][2]), inv, precision);
  58. }
  59. else if(Real(data[i][6]) == 1)
  60. BOOST_CHECK_EQUAL(boost::math::ibetac_inv(Real(data[i][0]), Real(data[i][1]), Real(data[i][6])), value_type(0));
  61. }
  62. }
  63. template <class Real, class T>
  64. void test_inverses2(const T& data, const char* type_name, const char* test_name)
  65. {
  66. #if !(defined(ERROR_REPORTING_MODE) && !defined(IBETA_INV_FUNCTION_TO_TEST))
  67. //typedef typename T::value_type row_type;
  68. typedef Real value_type;
  69. typedef value_type (*pg)(value_type, value_type, value_type);
  70. #ifdef IBETA_INV_FUNCTION_TO_TEST
  71. pg funcp = IBETA_INV_FUNCTION_TO_TEST;
  72. #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  73. pg funcp = boost::math::ibeta_inv<value_type, value_type, value_type>;
  74. #else
  75. pg funcp = boost::math::ibeta_inv;
  76. #endif
  77. boost::math::tools::test_result<value_type> result;
  78. std::cout << "Testing " << test_name << " with type " << type_name
  79. << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
  80. //
  81. // test ibeta_inv(T, T, T) against data:
  82. //
  83. result = boost::math::tools::test_hetero<Real>(
  84. data,
  85. bind_func<Real>(funcp, 0, 1, 2),
  86. extract_result<Real>(3));
  87. handle_test_result(result, data[result.worst()], result.worst(), type_name, "ibeta_inv", test_name);
  88. //
  89. // test ibetac_inv(T, T, T) against data:
  90. //
  91. #ifdef IBETAC_INV_FUNCTION_TO_TEST
  92. funcp = IBETAC_INV_FUNCTION_TO_TEST;
  93. #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  94. funcp = boost::math::ibetac_inv<value_type, value_type, value_type>;
  95. #else
  96. funcp = boost::math::ibetac_inv;
  97. #endif
  98. result = boost::math::tools::test_hetero<Real>(
  99. data,
  100. bind_func<Real>(funcp, 0, 1, 2),
  101. extract_result<Real>(4));
  102. handle_test_result(result, data[result.worst()], result.worst(), type_name, "ibetac_inv", test_name);
  103. #endif
  104. }
  105. template <class T>
  106. void test_beta(T, const char* name)
  107. {
  108. #if !defined(ERROR_REPORTING_MODE)
  109. (void)name;
  110. //
  111. // The actual test data is rather verbose, so it's in a separate file
  112. //
  113. // The contents are as follows, each row of data contains
  114. // five items, input value a, input value b, integration limits x, beta(a, b, x) and ibeta(a, b, x):
  115. //
  116. #if !defined(TEST_DATA) || (TEST_DATA == 1)
  117. # include "ibeta_small_data.ipp"
  118. test_inverses<T>(ibeta_small_data);
  119. #endif
  120. #if !defined(TEST_DATA) || (TEST_DATA == 2)
  121. # include "ibeta_data.ipp"
  122. test_inverses<T>(ibeta_data);
  123. #endif
  124. #if !defined(TEST_DATA) || (TEST_DATA == 3)
  125. # include "ibeta_large_data.ipp"
  126. test_inverses<T>(ibeta_large_data);
  127. #endif
  128. #endif
  129. #if !defined(TEST_DATA) || (TEST_DATA == 4)
  130. # include "ibeta_inv_data.ipp"
  131. test_inverses2<T>(ibeta_inv_data, name, "Inverse incomplete beta");
  132. #endif
  133. }
  134. template <class T>
  135. void test_spots(T)
  136. {
  137. BOOST_MATH_STD_USING
  138. //
  139. // basic sanity checks, tolerance is 100 epsilon expressed as a percentage:
  140. //
  141. T tolerance = boost::math::tools::epsilon<T>() * 10000;
  142. BOOST_CHECK_CLOSE(
  143. ::boost::math::ibeta_inv(
  144. static_cast<T>(1),
  145. static_cast<T>(2),
  146. static_cast<T>(0.5)),
  147. static_cast<T>(0.29289321881345247559915563789515096071516406231153L), tolerance);
  148. BOOST_CHECK_CLOSE(
  149. ::boost::math::ibeta_inv(
  150. static_cast<T>(3),
  151. static_cast<T>(0.5),
  152. static_cast<T>(0.5)),
  153. static_cast<T>(0.92096723292382700385142816696980724853063433975470L), tolerance);
  154. BOOST_CHECK_CLOSE(
  155. ::boost::math::ibeta_inv(
  156. static_cast<T>(20.125),
  157. static_cast<T>(0.5),
  158. static_cast<T>(0.5)),
  159. static_cast<T>(0.98862133312917003480022776106012775747685870929920L), tolerance);
  160. BOOST_CHECK_CLOSE(
  161. ::boost::math::ibeta_inv(
  162. static_cast<T>(40),
  163. static_cast<T>(80),
  164. static_cast<T>(0.5)),
  165. static_cast<T>(0.33240456430025026300937492802591128972548660643778L), tolerance);
  166. BOOST_CHECK_CLOSE(
  167. ::boost::math::ibeta_inv(
  168. static_cast<T>(40),
  169. static_cast<T>(0.5),
  170. ldexp(T(1), -30)),
  171. static_cast<T>(0.624305407878048788716096298053941618358257550305573588792717L), tolerance);
  172. BOOST_CHECK_CLOSE(
  173. ::boost::math::ibeta_inv(
  174. static_cast<T>(40),
  175. static_cast<T>(0.5),
  176. static_cast<T>(1 - ldexp(T(1), -30))),
  177. static_cast<T>(0.99999999999999999998286262026583217516676792408012252456039L), tolerance);
  178. BOOST_CHECK_CLOSE(
  179. ::boost::math::ibeta_inv(
  180. static_cast<T>(0.5),
  181. static_cast<T>(40),
  182. static_cast<T>(ldexp(T(1), -30))),
  183. static_cast<T>(1.713737973416782483323207591987747543960774485649459249e-20L), tolerance);
  184. BOOST_CHECK_CLOSE(
  185. ::boost::math::ibeta_inv(
  186. static_cast<T>(0.5),
  187. static_cast<T>(0.75),
  188. static_cast<T>(ldexp(T(1), -30))),
  189. static_cast<T>(1.245132488513853853809715434621955746959615015005382639e-18L), tolerance);
  190. BOOST_CHECK_CLOSE(
  191. ::boost::math::ibeta_inv(
  192. static_cast<T>(0.5),
  193. static_cast<T>(0.5),
  194. static_cast<T>(0.25)),
  195. static_cast<T>(0.1464466094067262377995778189475754803575820311557629L), tolerance);
  196. BOOST_CHECK_CLOSE(
  197. ::boost::math::ibeta_inv(
  198. static_cast<T>(0.5),
  199. static_cast<T>(0.5),
  200. static_cast<T>(0.75)),
  201. static_cast<T>(0.853553390593273762200422181052424519642417968844237018294169L), tolerance);
  202. BOOST_CHECK_CLOSE(
  203. ::boost::math::ibeta_inv(
  204. static_cast<T>(1),
  205. static_cast<T>(5),
  206. static_cast<T>(0.125)),
  207. static_cast<T>(0.026352819384831863473794894078665766580641189002729204514544L), tolerance);
  208. BOOST_CHECK_CLOSE(
  209. ::boost::math::ibeta_inv(
  210. static_cast<T>(5),
  211. static_cast<T>(1),
  212. static_cast<T>(0.125)),
  213. static_cast<T>(0.659753955386447129687000985614820066516734506596709340752903L), tolerance);
  214. BOOST_CHECK_CLOSE(
  215. ::boost::math::ibeta_inv(
  216. static_cast<T>(1),
  217. static_cast<T>(0.125),
  218. static_cast<T>(0.125)),
  219. static_cast<T>(0.656391084194183349609374999999999999999999999999999999999999L), tolerance);
  220. BOOST_CHECK_CLOSE(
  221. ::boost::math::ibeta_inv(
  222. static_cast<T>(0.125),
  223. static_cast<T>(1),
  224. static_cast<T>(0.125)),
  225. static_cast<T>(5.960464477539062500000e-8), tolerance);
  226. BOOST_CHECK_CLOSE(
  227. ::boost::math::ibetac_inv(
  228. static_cast<T>(5),
  229. static_cast<T>(1),
  230. static_cast<T>(0.125)),
  231. static_cast<T>(0.973647180615168136526205105921334233419358810997270795485455L), tolerance);
  232. BOOST_CHECK_CLOSE(
  233. ::boost::math::ibetac_inv(
  234. static_cast<T>(1),
  235. static_cast<T>(5),
  236. static_cast<T>(0.125)),
  237. static_cast<T>(0.340246044613552870312999014385179933483265493403290659247096L), tolerance);
  238. BOOST_CHECK_CLOSE(
  239. ::boost::math::ibetac_inv(
  240. static_cast<T>(0.125),
  241. static_cast<T>(1),
  242. static_cast<T>(0.125)),
  243. static_cast<T>(0.343608915805816650390625000000000000000000000000000000000000L), tolerance);
  244. BOOST_CHECK_CLOSE(
  245. ::boost::math::ibetac_inv(
  246. static_cast<T>(1),
  247. static_cast<T>(0.125),
  248. static_cast<T>(0.125)),
  249. static_cast<T>(0.99999994039535522460937500000000000000000000000L), tolerance);
  250. }