ibeta_inv_ab.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // (C) Copyright John Maddock 2006.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. //
  6. // This is not a complete header file, it is included by beta.hpp
  7. // after it has defined it's definitions. This inverts the incomplete
  8. // beta functions ibeta and ibetac on the first parameters "a"
  9. // and "b" using a generic root finding algorithm (TOMS Algorithm 748).
  10. //
  11. #ifndef BOOST_MATH_SP_DETAIL_BETA_INV_AB
  12. #define BOOST_MATH_SP_DETAIL_BETA_INV_AB
  13. #ifdef _MSC_VER
  14. #pragma once
  15. #endif
  16. #include <boost/math/tools/toms748_solve.hpp>
  17. #include <boost/cstdint.hpp>
  18. namespace boost{ namespace math{ namespace detail{
  19. template <class T, class Policy>
  20. struct beta_inv_ab_t
  21. {
  22. beta_inv_ab_t(T b_, T z_, T p_, bool invert_, bool swap_ab_) : b(b_), z(z_), p(p_), invert(invert_), swap_ab(swap_ab_) {}
  23. T operator()(T a)
  24. {
  25. return invert ?
  26. p - boost::math::ibetac(swap_ab ? b : a, swap_ab ? a : b, z, Policy())
  27. : boost::math::ibeta(swap_ab ? b : a, swap_ab ? a : b, z, Policy()) - p;
  28. }
  29. private:
  30. T b, z, p;
  31. bool invert, swap_ab;
  32. };
  33. template <class T, class Policy>
  34. T inverse_negative_binomial_cornish_fisher(T n, T sf, T sfc, T p, T q, const Policy& pol)
  35. {
  36. BOOST_MATH_STD_USING
  37. // mean:
  38. T m = n * (sfc) / sf;
  39. T t = sqrt(n * (sfc));
  40. // standard deviation:
  41. T sigma = t / sf;
  42. // skewness
  43. T sk = (1 + sfc) / t;
  44. // kurtosis:
  45. T k = (6 - sf * (5+sfc)) / (n * (sfc));
  46. // Get the inverse of a std normal distribution:
  47. T x = boost::math::erfc_inv(p > q ? 2 * q : 2 * p, pol) * constants::root_two<T>();
  48. // Set the sign:
  49. if(p < 0.5)
  50. x = -x;
  51. T x2 = x * x;
  52. // w is correction term due to skewness
  53. T w = x + sk * (x2 - 1) / 6;
  54. //
  55. // Add on correction due to kurtosis.
  56. //
  57. if(n >= 10)
  58. w += k * x * (x2 - 3) / 24 + sk * sk * x * (2 * x2 - 5) / -36;
  59. w = m + sigma * w;
  60. if(w < tools::min_value<T>())
  61. return tools::min_value<T>();
  62. return w;
  63. }
  64. template <class T, class Policy>
  65. T ibeta_inv_ab_imp(const T& b, const T& z, const T& p, const T& q, bool swap_ab, const Policy& pol)
  66. {
  67. BOOST_MATH_STD_USING // for ADL of std lib math functions
  68. //
  69. // Special cases first:
  70. //
  71. BOOST_MATH_INSTRUMENT_CODE("b = " << b << " z = " << z << " p = " << p << " q = " << " swap = " << swap_ab);
  72. if(p == 0)
  73. {
  74. return swap_ab ? tools::min_value<T>() : tools::max_value<T>();
  75. }
  76. if(q == 0)
  77. {
  78. return swap_ab ? tools::max_value<T>() : tools::min_value<T>();
  79. }
  80. //
  81. // Function object, this is the functor whose root
  82. // we have to solve:
  83. //
  84. beta_inv_ab_t<T, Policy> f(b, z, (p < q) ? p : q, (p < q) ? false : true, swap_ab);
  85. //
  86. // Tolerance: full precision.
  87. //
  88. tools::eps_tolerance<T> tol(policies::digits<T, Policy>());
  89. //
  90. // Now figure out a starting guess for what a may be,
  91. // we'll start out with a value that'll put p or q
  92. // right bang in the middle of their range, the functions
  93. // are quite sensitive so we should need too many steps
  94. // to bracket the root from there:
  95. //
  96. T guess = 0;
  97. T factor = 5;
  98. //
  99. // Convert variables to parameters of a negative binomial distribution:
  100. //
  101. T n = b;
  102. T sf = swap_ab ? z : 1-z;
  103. T sfc = swap_ab ? 1-z : z;
  104. T u = swap_ab ? p : q;
  105. T v = swap_ab ? q : p;
  106. if(u <= pow(sf, n))
  107. {
  108. //
  109. // Result is less than 1, negative binomial approximation
  110. // is useless....
  111. //
  112. if((p < q) != swap_ab)
  113. {
  114. guess = (std::min)(T(b * 2), T(1));
  115. }
  116. else
  117. {
  118. guess = (std::min)(T(b / 2), T(1));
  119. }
  120. }
  121. if(n * n * n * u * sf > 0.005)
  122. guess = 1 + inverse_negative_binomial_cornish_fisher(n, sf, sfc, u, v, pol);
  123. if(guess < 10)
  124. {
  125. //
  126. // Negative binomial approximation not accurate in this area:
  127. //
  128. if((p < q) != swap_ab)
  129. {
  130. guess = (std::min)(T(b * 2), T(10));
  131. }
  132. else
  133. {
  134. guess = (std::min)(T(b / 2), T(10));
  135. }
  136. }
  137. else
  138. factor = (v < sqrt(tools::epsilon<T>())) ? 2 : (guess < 20 ? 1.2f : 1.1f);
  139. BOOST_MATH_INSTRUMENT_CODE("guess = " << guess);
  140. //
  141. // Max iterations permitted:
  142. //
  143. boost::uintmax_t max_iter = policies::get_max_root_iterations<Policy>();
  144. std::pair<T, T> r = bracket_and_solve_root(f, guess, factor, swap_ab ? true : false, tol, max_iter, pol);
  145. if(max_iter >= policies::get_max_root_iterations<Policy>())
  146. return policies::raise_evaluation_error<T>("boost::math::ibeta_invab_imp<%1%>(%1%,%1%,%1%)", "Unable to locate the root within a reasonable number of iterations, closest approximation so far was %1%", r.first, pol);
  147. return (r.first + r.second) / 2;
  148. }
  149. } // namespace detail
  150. template <class RT1, class RT2, class RT3, class Policy>
  151. typename tools::promote_args<RT1, RT2, RT3>::type
  152. ibeta_inva(RT1 b, RT2 x, RT3 p, const Policy& pol)
  153. {
  154. typedef typename tools::promote_args<RT1, RT2, RT3>::type result_type;
  155. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  156. typedef typename policies::normalise<
  157. Policy,
  158. policies::promote_float<false>,
  159. policies::promote_double<false>,
  160. policies::discrete_quantile<>,
  161. policies::assert_undefined<> >::type forwarding_policy;
  162. static const char* function = "boost::math::ibeta_inva<%1%>(%1%,%1%,%1%)";
  163. if(p == 0)
  164. {
  165. return policies::raise_overflow_error<result_type>(function, 0, Policy());
  166. }
  167. if(p == 1)
  168. {
  169. return tools::min_value<result_type>();
  170. }
  171. return policies::checked_narrowing_cast<result_type, forwarding_policy>(
  172. detail::ibeta_inv_ab_imp(
  173. static_cast<value_type>(b),
  174. static_cast<value_type>(x),
  175. static_cast<value_type>(p),
  176. static_cast<value_type>(1 - static_cast<value_type>(p)),
  177. false, pol),
  178. function);
  179. }
  180. template <class RT1, class RT2, class RT3, class Policy>
  181. typename tools::promote_args<RT1, RT2, RT3>::type
  182. ibetac_inva(RT1 b, RT2 x, RT3 q, const Policy& pol)
  183. {
  184. typedef typename tools::promote_args<RT1, RT2, RT3>::type result_type;
  185. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  186. typedef typename policies::normalise<
  187. Policy,
  188. policies::promote_float<false>,
  189. policies::promote_double<false>,
  190. policies::discrete_quantile<>,
  191. policies::assert_undefined<> >::type forwarding_policy;
  192. static const char* function = "boost::math::ibetac_inva<%1%>(%1%,%1%,%1%)";
  193. if(q == 1)
  194. {
  195. return policies::raise_overflow_error<result_type>(function, 0, Policy());
  196. }
  197. if(q == 0)
  198. {
  199. return tools::min_value<result_type>();
  200. }
  201. return policies::checked_narrowing_cast<result_type, forwarding_policy>(
  202. detail::ibeta_inv_ab_imp(
  203. static_cast<value_type>(b),
  204. static_cast<value_type>(x),
  205. static_cast<value_type>(1 - static_cast<value_type>(q)),
  206. static_cast<value_type>(q),
  207. false, pol),
  208. function);
  209. }
  210. template <class RT1, class RT2, class RT3, class Policy>
  211. typename tools::promote_args<RT1, RT2, RT3>::type
  212. ibeta_invb(RT1 a, RT2 x, RT3 p, const Policy& pol)
  213. {
  214. typedef typename tools::promote_args<RT1, RT2, RT3>::type result_type;
  215. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  216. typedef typename policies::normalise<
  217. Policy,
  218. policies::promote_float<false>,
  219. policies::promote_double<false>,
  220. policies::discrete_quantile<>,
  221. policies::assert_undefined<> >::type forwarding_policy;
  222. static const char* function = "boost::math::ibeta_invb<%1%>(%1%,%1%,%1%)";
  223. if(p == 0)
  224. {
  225. return tools::min_value<result_type>();
  226. }
  227. if(p == 1)
  228. {
  229. return policies::raise_overflow_error<result_type>(function, 0, Policy());
  230. }
  231. return policies::checked_narrowing_cast<result_type, forwarding_policy>(
  232. detail::ibeta_inv_ab_imp(
  233. static_cast<value_type>(a),
  234. static_cast<value_type>(x),
  235. static_cast<value_type>(p),
  236. static_cast<value_type>(1 - static_cast<value_type>(p)),
  237. true, pol),
  238. function);
  239. }
  240. template <class RT1, class RT2, class RT3, class Policy>
  241. typename tools::promote_args<RT1, RT2, RT3>::type
  242. ibetac_invb(RT1 a, RT2 x, RT3 q, const Policy& pol)
  243. {
  244. static const char* function = "boost::math::ibeta_invb<%1%>(%1%, %1%, %1%)";
  245. typedef typename tools::promote_args<RT1, RT2, RT3>::type result_type;
  246. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  247. typedef typename policies::normalise<
  248. Policy,
  249. policies::promote_float<false>,
  250. policies::promote_double<false>,
  251. policies::discrete_quantile<>,
  252. policies::assert_undefined<> >::type forwarding_policy;
  253. if(q == 1)
  254. {
  255. return tools::min_value<result_type>();
  256. }
  257. if(q == 0)
  258. {
  259. return policies::raise_overflow_error<result_type>(function, 0, Policy());
  260. }
  261. return policies::checked_narrowing_cast<result_type, forwarding_policy>(
  262. detail::ibeta_inv_ab_imp(
  263. static_cast<value_type>(a),
  264. static_cast<value_type>(x),
  265. static_cast<value_type>(1 - static_cast<value_type>(q)),
  266. static_cast<value_type>(q),
  267. true, pol),
  268. function);
  269. }
  270. template <class RT1, class RT2, class RT3>
  271. inline typename tools::promote_args<RT1, RT2, RT3>::type
  272. ibeta_inva(RT1 b, RT2 x, RT3 p)
  273. {
  274. return boost::math::ibeta_inva(b, x, p, policies::policy<>());
  275. }
  276. template <class RT1, class RT2, class RT3>
  277. inline typename tools::promote_args<RT1, RT2, RT3>::type
  278. ibetac_inva(RT1 b, RT2 x, RT3 q)
  279. {
  280. return boost::math::ibetac_inva(b, x, q, policies::policy<>());
  281. }
  282. template <class RT1, class RT2, class RT3>
  283. inline typename tools::promote_args<RT1, RT2, RT3>::type
  284. ibeta_invb(RT1 a, RT2 x, RT3 p)
  285. {
  286. return boost::math::ibeta_invb(a, x, p, policies::policy<>());
  287. }
  288. template <class RT1, class RT2, class RT3>
  289. inline typename tools::promote_args<RT1, RT2, RT3>::type
  290. ibetac_invb(RT1 a, RT2 x, RT3 q)
  291. {
  292. return boost::math::ibetac_invb(a, x, q, policies::policy<>());
  293. }
  294. } // namespace math
  295. } // namespace boost
  296. #endif // BOOST_MATH_SP_DETAIL_BETA_INV_AB