non_central_beta.hpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. // boost\math\distributions\non_central_beta.hpp
  2. // Copyright John Maddock 2008.
  3. // Copyright Matt Borland 2024.
  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. #ifndef BOOST_MATH_SPECIAL_NON_CENTRAL_BETA_HPP
  9. #define BOOST_MATH_SPECIAL_NON_CENTRAL_BETA_HPP
  10. #include <boost/math/tools/config.hpp>
  11. #include <boost/math/tools/tuple.hpp>
  12. #include <boost/math/tools/cstdint.hpp>
  13. #include <boost/math/tools/numeric_limits.hpp>
  14. #include <boost/math/distributions/fwd.hpp>
  15. #include <boost/math/special_functions/beta.hpp> // for incomplete gamma. gamma_q
  16. #include <boost/math/distributions/complement.hpp> // complements
  17. #include <boost/math/distributions/beta.hpp> // central distribution
  18. #include <boost/math/distributions/detail/generic_mode.hpp>
  19. #include <boost/math/distributions/detail/common_error_handling.hpp> // error checks
  20. #include <boost/math/special_functions/fpclassify.hpp> // isnan.
  21. #include <boost/math/special_functions/trunc.hpp>
  22. #include <boost/math/tools/roots.hpp> // for root finding.
  23. #include <boost/math/tools/series.hpp>
  24. #include <boost/math/policies/error_handling.hpp>
  25. namespace boost
  26. {
  27. namespace math
  28. {
  29. template <class RealType, class Policy>
  30. class non_central_beta_distribution;
  31. namespace detail{
  32. template <class T, class Policy>
  33. BOOST_MATH_GPU_ENABLED T non_central_beta_p(T a, T b, T lam, T x, T y, const Policy& pol, T init_val = 0)
  34. {
  35. BOOST_MATH_STD_USING
  36. using namespace boost::math;
  37. //
  38. // Variables come first:
  39. //
  40. boost::math::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
  41. T errtol = boost::math::policies::get_epsilon<T, Policy>();
  42. T l2 = lam / 2;
  43. //
  44. // k is the starting point for iteration, and is the
  45. // maximum of the poisson weighting term,
  46. // note that unlike other similar code, we do not set
  47. // k to zero, when l2 is small, as forward iteration
  48. // is unstable:
  49. //
  50. long long k = lltrunc(l2);
  51. if(k == 0)
  52. k = 1;
  53. // Starting Poisson weight:
  54. T pois = gamma_p_derivative(T(k+1), l2, pol);
  55. if(pois == 0)
  56. return init_val;
  57. // recurance term:
  58. T xterm;
  59. // Starting beta term:
  60. T beta = x < y
  61. ? detail::ibeta_imp(T(a + k), b, x, pol, false, true, &xterm)
  62. : detail::ibeta_imp(b, T(a + k), y, pol, true, true, &xterm);
  63. while (fabs(beta * pois) < tools::min_value<T>())
  64. {
  65. if ((k == 0) || (pois == 0))
  66. return init_val;
  67. k /= 2;
  68. pois = gamma_p_derivative(T(k + 1), l2, pol);
  69. beta = x < y
  70. ? detail::ibeta_imp(T(a + k), b, x, pol, false, true, &xterm)
  71. : detail::ibeta_imp(b, T(a + k), y, pol, true, true, &xterm);
  72. }
  73. xterm *= y / (a + b + k - 1);
  74. T poisf(pois), betaf(beta), xtermf(xterm);
  75. T sum = init_val;
  76. if((beta == 0) && (xterm == 0))
  77. return init_val;
  78. //
  79. // Backwards recursion first, this is the stable
  80. // direction for recursion:
  81. //
  82. T last_term = 0;
  83. boost::math::uintmax_t count = k;
  84. for(auto i = k; i >= 0; --i)
  85. {
  86. T term = beta * pois;
  87. sum += term;
  88. if(((fabs(term/sum) < errtol) && (fabs(last_term) >= fabs(term))) || (term == 0))
  89. {
  90. count = k - i;
  91. break;
  92. }
  93. pois *= i / l2;
  94. beta += xterm;
  95. if (a + b + i != 2)
  96. {
  97. xterm *= (a + i - 1) / (x * (a + b + i - 2));
  98. }
  99. last_term = term;
  100. }
  101. last_term = 0;
  102. T betaf_lim = betaf * tools::epsilon<T>() * 4;
  103. for(auto i = k + 1; ; ++i)
  104. {
  105. poisf *= l2 / i;
  106. xtermf *= (x * (a + b + i - 2)) / (a + i - 1);
  107. betaf -= xtermf;
  108. if (betaf < betaf_lim)
  109. break; // nothing but garbage bits in betaf!!
  110. T term = poisf * betaf;
  111. sum += term;
  112. if(((fabs(term/sum) < errtol) && (fabs(last_term) >= fabs(term))) || (term == 0))
  113. {
  114. break;
  115. }
  116. last_term = term;
  117. if(static_cast<boost::math::uintmax_t>(count + i - k) > max_iter)
  118. {
  119. return policies::raise_evaluation_error("cdf(non_central_beta_distribution<%1%>, %1%)", "Series did not converge, closest value was %1%", sum, pol); // LCOV_EXCL_LINE
  120. }
  121. }
  122. return sum;
  123. }
  124. template <class T, class Policy>
  125. BOOST_MATH_GPU_ENABLED T non_central_beta_q(T a, T b, T lam, T x, T y, const Policy& pol, T init_val = 0)
  126. {
  127. BOOST_MATH_STD_USING
  128. using namespace boost::math;
  129. //
  130. // Variables come first:
  131. //
  132. boost::math::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
  133. T errtol = boost::math::policies::get_epsilon<T, Policy>();
  134. T l2 = lam / 2;
  135. //
  136. // k is the starting point for iteration, and is the
  137. // maximum of the poisson weighting term:
  138. //
  139. long long k = lltrunc(l2);
  140. T pois;
  141. if(k <= 30)
  142. {
  143. //
  144. // Might as well start at 0 since we'll likely have this number of terms anyway:
  145. //
  146. if(a + b > 1)
  147. k = 0;
  148. else if(k == 0)
  149. k = 1;
  150. }
  151. if(k == 0)
  152. {
  153. // Starting Poisson weight:
  154. pois = exp(-l2);
  155. }
  156. else
  157. {
  158. // Starting Poisson weight:
  159. pois = gamma_p_derivative(T(k+1), l2, pol);
  160. }
  161. if(pois == 0)
  162. return init_val;
  163. // recurance term:
  164. T xterm;
  165. // Starting beta term:
  166. T beta = x < y
  167. ? detail::ibeta_imp(T(a + k), b, x, pol, true, true, &xterm)
  168. : detail::ibeta_imp(b, T(a + k), y, pol, false, true, &xterm);
  169. xterm *= y / (a + b + k - 1);
  170. T poisf(pois), betaf(beta), xtermf(xterm);
  171. T sum = init_val;
  172. if((beta == 0) && (xterm == 0))
  173. return init_val;
  174. //
  175. // Forwards recursion first, this is the stable
  176. // direction for recursion, and the location
  177. // of the bulk of the sum:
  178. //
  179. T last_term = 0;
  180. boost::math::uintmax_t count = 0;
  181. for(long long i = k + 1; ; ++i)
  182. {
  183. poisf *= l2 / i;
  184. xtermf *= (x * (a + b + (i - 2))) / (a + (i - 1));
  185. betaf += xtermf;
  186. T term = poisf * betaf;
  187. sum += term;
  188. if((fabs(term/sum) < errtol) && (last_term >= term))
  189. {
  190. count = i - k;
  191. break;
  192. }
  193. if(static_cast<boost::math::uintmax_t>(i - k) > max_iter)
  194. {
  195. return policies::raise_evaluation_error("cdf(non_central_beta_distribution<%1%>, %1%)", "Series did not converge, closest value was %1%", sum, pol); // LCOV_EXCL_LINE
  196. }
  197. last_term = term;
  198. }
  199. for(auto i = k; i >= 0; --i)
  200. {
  201. T term = beta * pois;
  202. sum += term;
  203. if(fabs(term/sum) < errtol)
  204. {
  205. break;
  206. }
  207. if(static_cast<boost::math::uintmax_t>(count + k - i) > max_iter)
  208. {
  209. return policies::raise_evaluation_error("cdf(non_central_beta_distribution<%1%>, %1%)", "Series did not converge, closest value was %1%", sum, pol); // LCOV_EXCL_LINE
  210. }
  211. pois *= i / l2;
  212. beta -= xterm;
  213. if (a + b + i - 2 != 0)
  214. {
  215. xterm *= (a + i - 1) / (x * (a + b + i - 2));
  216. }
  217. }
  218. return sum;
  219. }
  220. template <class RealType, class Policy>
  221. BOOST_MATH_GPU_ENABLED inline RealType non_central_beta_cdf(RealType x, RealType y, RealType a, RealType b, RealType l, bool invert, const Policy&)
  222. {
  223. typedef typename policies::evaluation<RealType, Policy>::type value_type;
  224. typedef typename policies::normalise<
  225. Policy,
  226. policies::promote_float<false>,
  227. policies::promote_double<false>,
  228. policies::discrete_quantile<>,
  229. policies::assert_undefined<> >::type forwarding_policy;
  230. BOOST_MATH_STD_USING
  231. if(x == 0)
  232. return invert ? 1.0f : 0.0f;
  233. if(y == 0)
  234. return invert ? 0.0f : 1.0f;
  235. value_type result;
  236. value_type c = a + b + l / 2;
  237. value_type cross = 1 - (b / c) * (1 + l / (2 * c * c));
  238. if(l == 0)
  239. result = cdf(boost::math::beta_distribution<RealType, Policy>(a, b), x);
  240. else if(x > cross)
  241. {
  242. // Complement is the smaller of the two:
  243. result = detail::non_central_beta_q(
  244. static_cast<value_type>(a),
  245. static_cast<value_type>(b),
  246. static_cast<value_type>(l),
  247. static_cast<value_type>(x),
  248. static_cast<value_type>(y),
  249. forwarding_policy(),
  250. static_cast<value_type>(invert ? 0 : -1));
  251. invert = !invert;
  252. }
  253. else
  254. {
  255. result = detail::non_central_beta_p(
  256. static_cast<value_type>(a),
  257. static_cast<value_type>(b),
  258. static_cast<value_type>(l),
  259. static_cast<value_type>(x),
  260. static_cast<value_type>(y),
  261. forwarding_policy(),
  262. static_cast<value_type>(invert ? -1 : 0));
  263. }
  264. if(invert)
  265. result = -result;
  266. return policies::checked_narrowing_cast<RealType, forwarding_policy>(
  267. result,
  268. "boost::math::non_central_beta_cdf<%1%>(%1%, %1%, %1%)");
  269. }
  270. template <class T, class Policy>
  271. struct nc_beta_quantile_functor
  272. {
  273. BOOST_MATH_GPU_ENABLED nc_beta_quantile_functor(const non_central_beta_distribution<T,Policy>& d, T t, bool c)
  274. : dist(d), target(t), comp(c) {}
  275. BOOST_MATH_GPU_ENABLED T operator()(const T& x)
  276. {
  277. return comp ?
  278. T(target - cdf(complement(dist, x)))
  279. : T(cdf(dist, x) - target);
  280. }
  281. private:
  282. non_central_beta_distribution<T,Policy> dist;
  283. T target;
  284. bool comp;
  285. };
  286. //
  287. // This is more or less a copy of bracket_and_solve_root, but
  288. // modified to search only the interval [0,1] using similar
  289. // heuristics.
  290. //
  291. template <class F, class T, class Tol, class Policy>
  292. BOOST_MATH_GPU_ENABLED boost::math::pair<T, T> bracket_and_solve_root_01(F f, const T& guess, T factor, bool rising, Tol tol, boost::math::uintmax_t& max_iter, const Policy& pol)
  293. {
  294. BOOST_MATH_STD_USING
  295. constexpr auto function = "boost::math::tools::bracket_and_solve_root_01<%1%>";
  296. //
  297. // Set up initial brackets:
  298. //
  299. T a = guess;
  300. T b = a;
  301. T fa = f(a);
  302. T fb = fa;
  303. //
  304. // Set up invocation count:
  305. //
  306. boost::math::uintmax_t count = max_iter - 1;
  307. if((fa < 0) == (guess < 0 ? !rising : rising))
  308. {
  309. //
  310. // Zero is to the right of b, so walk upwards
  311. // until we find it:
  312. //
  313. while((boost::math::sign)(fb) == (boost::math::sign)(fa))
  314. {
  315. if(count == 0)
  316. {
  317. b = policies::raise_evaluation_error(function, "Unable to bracket root, last nearest value was %1%", b, pol); // LCOV_EXCL_LINE
  318. return boost::math::make_pair(a, b);
  319. }
  320. //
  321. // Heuristic: every 20 iterations we double the growth factor in case the
  322. // initial guess was *really* bad !
  323. //
  324. if((max_iter - count) % 20 == 0)
  325. factor *= 2;
  326. //
  327. // Now go ahead and move are guess by "factor",
  328. // we do this by reducing 1-guess by factor:
  329. //
  330. a = b;
  331. fa = fb;
  332. b = 1 - ((1 - b) / factor);
  333. fb = f(b);
  334. --count;
  335. BOOST_MATH_INSTRUMENT_CODE("a = " << a << " b = " << b << " fa = " << fa << " fb = " << fb << " count = " << count);
  336. }
  337. }
  338. else
  339. {
  340. //
  341. // Zero is to the left of a, so walk downwards
  342. // until we find it:
  343. //
  344. while((boost::math::sign)(fb) == (boost::math::sign)(fa))
  345. {
  346. if(fabs(a) < tools::min_value<T>())
  347. {
  348. // Escape route just in case the answer is zero!
  349. max_iter -= count;
  350. max_iter += 1;
  351. return a > 0 ? boost::math::make_pair(T(0), T(a)) : boost::math::make_pair(T(a), T(0));
  352. }
  353. if(count == 0)
  354. {
  355. a = policies::raise_evaluation_error(function, "Unable to bracket root, last nearest value was %1%", a, pol); // LCOV_EXCL_LINE
  356. return boost::math::make_pair(a, b);
  357. }
  358. //
  359. // Heuristic: every 20 iterations we double the growth factor in case the
  360. // initial guess was *really* bad !
  361. //
  362. if((max_iter - count) % 20 == 0)
  363. factor *= 2;
  364. //
  365. // Now go ahead and move are guess by "factor":
  366. //
  367. b = a;
  368. fb = fa;
  369. a /= factor;
  370. fa = f(a);
  371. --count;
  372. BOOST_MATH_INSTRUMENT_CODE("a = " << a << " b = " << b << " fa = " << fa << " fb = " << fb << " count = " << count);
  373. }
  374. }
  375. max_iter -= count;
  376. max_iter += 1;
  377. boost::math::pair<T, T> r = toms748_solve(
  378. f,
  379. (a < 0 ? b : a),
  380. (a < 0 ? a : b),
  381. (a < 0 ? fb : fa),
  382. (a < 0 ? fa : fb),
  383. tol,
  384. count,
  385. pol);
  386. max_iter += count;
  387. BOOST_MATH_INSTRUMENT_CODE("max_iter = " << max_iter << " count = " << count);
  388. return r;
  389. }
  390. template <class RealType, class Policy>
  391. BOOST_MATH_GPU_ENABLED RealType nc_beta_quantile(const non_central_beta_distribution<RealType, Policy>& dist, const RealType& p, bool comp)
  392. {
  393. constexpr auto function = "quantile(non_central_beta_distribution<%1%>, %1%)";
  394. typedef typename policies::evaluation<RealType, Policy>::type value_type;
  395. typedef typename policies::normalise<
  396. Policy,
  397. policies::promote_float<false>,
  398. policies::promote_double<false>,
  399. policies::discrete_quantile<>,
  400. policies::assert_undefined<> >::type forwarding_policy;
  401. value_type a = dist.alpha();
  402. value_type b = dist.beta();
  403. value_type l = dist.non_centrality();
  404. value_type r;
  405. if(!beta_detail::check_alpha(
  406. function,
  407. a, &r, Policy())
  408. ||
  409. !beta_detail::check_beta(
  410. function,
  411. b, &r, Policy())
  412. ||
  413. !detail::check_non_centrality(
  414. function,
  415. l,
  416. &r,
  417. Policy())
  418. ||
  419. !detail::check_probability(
  420. function,
  421. static_cast<value_type>(p),
  422. &r,
  423. Policy()))
  424. return static_cast<RealType>(r);
  425. //
  426. // Special cases first:
  427. //
  428. if(p == 0)
  429. return comp
  430. ? 1.0f
  431. : 0.0f;
  432. if(p == 1)
  433. return !comp
  434. ? 1.0f
  435. : 0.0f;
  436. value_type c = a + b + l / 2;
  437. value_type mean = 1 - (b / c) * (1 + l / (2 * c * c));
  438. /*
  439. //
  440. // Calculate a normal approximation to the quantile,
  441. // uses mean and variance approximations from:
  442. // Algorithm AS 310:
  443. // Computing the Non-Central Beta Distribution Function
  444. // R. Chattamvelli; R. Shanmugam
  445. // Applied Statistics, Vol. 46, No. 1. (1997), pp. 146-156.
  446. //
  447. // Unfortunately, when this is wrong it tends to be *very*
  448. // wrong, so it's disabled for now, even though it often
  449. // gets the initial guess quite close. Probably we could
  450. // do much better by factoring in the skewness if only
  451. // we could calculate it....
  452. //
  453. value_type delta = l / 2;
  454. value_type delta2 = delta * delta;
  455. value_type delta3 = delta * delta2;
  456. value_type delta4 = delta2 * delta2;
  457. value_type G = c * (c + 1) + delta;
  458. value_type alpha = a + b;
  459. value_type alpha2 = alpha * alpha;
  460. value_type eta = (2 * alpha + 1) * (2 * alpha + 1) + 1;
  461. value_type H = 3 * alpha2 + 5 * alpha + 2;
  462. value_type F = alpha2 * (alpha + 1) + H * delta
  463. + (2 * alpha + 4) * delta2 + delta3;
  464. value_type P = (3 * alpha + 1) * (9 * alpha + 17)
  465. + 2 * alpha * (3 * alpha + 2) * (3 * alpha + 4) + 15;
  466. value_type Q = 54 * alpha2 + 162 * alpha + 130;
  467. value_type R = 6 * (6 * alpha + 11);
  468. value_type D = delta
  469. * (H * H + 2 * P * delta + Q * delta2 + R * delta3 + 9 * delta4);
  470. value_type variance = (b / G)
  471. * (1 + delta * (l * l + 3 * l + eta) / (G * G))
  472. - (b * b / F) * (1 + D / (F * F));
  473. value_type sd = sqrt(variance);
  474. value_type guess = comp
  475. ? quantile(complement(normal_distribution<RealType, Policy>(static_cast<RealType>(mean), static_cast<RealType>(sd)), p))
  476. : quantile(normal_distribution<RealType, Policy>(static_cast<RealType>(mean), static_cast<RealType>(sd)), p);
  477. if(guess >= 1)
  478. guess = mean;
  479. if(guess <= tools::min_value<value_type>())
  480. guess = mean;
  481. */
  482. value_type guess = mean;
  483. detail::nc_beta_quantile_functor<value_type, Policy>
  484. f(non_central_beta_distribution<value_type, Policy>(a, b, l), p, comp);
  485. tools::eps_tolerance<value_type> tol(policies::digits<RealType, Policy>());
  486. boost::math::uintmax_t max_iter = policies::get_max_root_iterations<Policy>();
  487. boost::math::pair<value_type, value_type> ir
  488. = bracket_and_solve_root_01(
  489. f, guess, value_type(2.5), true, tol,
  490. max_iter, Policy());
  491. value_type result = ir.first + (ir.second - ir.first) / 2;
  492. if(max_iter >= policies::get_max_root_iterations<Policy>())
  493. {
  494. // LCOV_EXCL_START
  495. return policies::raise_evaluation_error<RealType>(function, "Unable to locate solution in a reasonable time:"
  496. " either there is no answer to quantile of the non central beta distribution"
  497. " or the answer is infinite. Current best guess is %1%",
  498. policies::checked_narrowing_cast<RealType, forwarding_policy>(
  499. result,
  500. function), Policy());
  501. // LCOV_EXCL_STOP
  502. }
  503. return policies::checked_narrowing_cast<RealType, forwarding_policy>(
  504. result,
  505. function);
  506. }
  507. template <class T, class Policy>
  508. BOOST_MATH_GPU_ENABLED T non_central_beta_pdf(T a, T b, T lam, T x, T y, const Policy& pol)
  509. {
  510. BOOST_MATH_STD_USING
  511. //
  512. // Special cases:
  513. //
  514. if((x == 0) || (y == 0))
  515. return 0;
  516. //
  517. // Variables come first:
  518. //
  519. boost::math::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
  520. T errtol = boost::math::policies::get_epsilon<T, Policy>();
  521. T l2 = lam / 2;
  522. //
  523. // k is the starting point for iteration, and is the
  524. // maximum of the poisson weighting term:
  525. //
  526. long long k = lltrunc(l2);
  527. // Starting Poisson weight:
  528. T pois = gamma_p_derivative(T(k+1), l2, pol);
  529. // Starting beta term:
  530. T beta = x < y ?
  531. ibeta_derivative(a + k, b, x, pol)
  532. : ibeta_derivative(b, a + k, y, pol);
  533. while (fabs(beta * pois) < tools::min_value<T>())
  534. {
  535. if ((k == 0) || (pois == 0))
  536. return 0; // Nothing else we can do!
  537. //
  538. // We only get here when a+k and b are large and x is small,
  539. // in that case reduce k (bisect) until both terms are finite:
  540. //
  541. k /= 2;
  542. pois = gamma_p_derivative(T(k + 1), l2, pol);
  543. // Starting beta term:
  544. beta = x < y ?
  545. ibeta_derivative(a + k, b, x, pol)
  546. : ibeta_derivative(b, a + k, y, pol);
  547. }
  548. T sum = 0;
  549. T poisf(pois);
  550. T betaf(beta);
  551. //
  552. // Stable backwards recursion first:
  553. //
  554. boost::math::uintmax_t count = k;
  555. T ratio = 0;
  556. T old_ratio = 0;
  557. for(auto i = k; i >= 0; --i)
  558. {
  559. T term = beta * pois;
  560. sum += term;
  561. ratio = fabs(term / sum);
  562. if(((ratio < errtol) && (ratio < old_ratio)) || (term == 0))
  563. {
  564. count = k - i;
  565. break;
  566. }
  567. ratio = old_ratio;
  568. pois *= i / l2;
  569. if (a + b + i != 1)
  570. {
  571. beta *= (a + i - 1) / (x * (a + i + b - 1));
  572. }
  573. }
  574. old_ratio = 0;
  575. for(auto i = k + 1; ; ++i)
  576. {
  577. poisf *= l2 / i;
  578. betaf *= x * (a + b + i - 1) / (a + i - 1);
  579. T term = poisf * betaf;
  580. sum += term;
  581. ratio = fabs(term / sum);
  582. if(((ratio < errtol) && (ratio < old_ratio)) || (term == 0))
  583. {
  584. break;
  585. }
  586. old_ratio = ratio;
  587. if(static_cast<boost::math::uintmax_t>(count + i - k) > max_iter)
  588. {
  589. return policies::raise_evaluation_error("pdf(non_central_beta_distribution<%1%>, %1%)", "Series did not converge, closest value was %1%", sum, pol); // LCOV_EXCL_LINE
  590. }
  591. }
  592. return sum;
  593. }
  594. template <class RealType, class Policy>
  595. BOOST_MATH_GPU_ENABLED RealType nc_beta_pdf(const non_central_beta_distribution<RealType, Policy>& dist, const RealType& x)
  596. {
  597. BOOST_MATH_STD_USING
  598. constexpr auto function = "pdf(non_central_beta_distribution<%1%>, %1%)";
  599. typedef typename policies::evaluation<RealType, Policy>::type value_type;
  600. typedef typename policies::normalise<
  601. Policy,
  602. policies::promote_float<false>,
  603. policies::promote_double<false>,
  604. policies::discrete_quantile<>,
  605. policies::assert_undefined<> >::type forwarding_policy;
  606. value_type a = dist.alpha();
  607. value_type b = dist.beta();
  608. value_type l = dist.non_centrality();
  609. value_type r;
  610. if(!beta_detail::check_alpha(
  611. function,
  612. a, &r, Policy())
  613. ||
  614. !beta_detail::check_beta(
  615. function,
  616. b, &r, Policy())
  617. ||
  618. !detail::check_non_centrality(
  619. function,
  620. l,
  621. &r,
  622. Policy())
  623. ||
  624. !beta_detail::check_x(
  625. function,
  626. static_cast<value_type>(x),
  627. &r,
  628. Policy()))
  629. return static_cast<RealType>(r);
  630. if(l == 0)
  631. return pdf(boost::math::beta_distribution<RealType, Policy>(dist.alpha(), dist.beta()), x);
  632. return policies::checked_narrowing_cast<RealType, forwarding_policy>(
  633. non_central_beta_pdf(a, b, l, static_cast<value_type>(x), value_type(1 - static_cast<value_type>(x)), forwarding_policy()),
  634. "function");
  635. }
  636. template <class T>
  637. struct hypergeometric_2F2_sum
  638. {
  639. typedef T result_type;
  640. BOOST_MATH_GPU_ENABLED hypergeometric_2F2_sum(T a1_, T a2_, T b1_, T b2_, T z_) : a1(a1_), a2(a2_), b1(b1_), b2(b2_), z(z_), term(1), k(0) {}
  641. BOOST_MATH_GPU_ENABLED T operator()()
  642. {
  643. T result = term;
  644. term *= a1 * a2 / (b1 * b2);
  645. a1 += 1;
  646. a2 += 1;
  647. b1 += 1;
  648. b2 += 1;
  649. k += 1;
  650. term /= k;
  651. term *= z;
  652. return result;
  653. }
  654. T a1, a2, b1, b2, z, term, k;
  655. };
  656. template <class T, class Policy>
  657. BOOST_MATH_GPU_ENABLED T hypergeometric_2F2(T a1, T a2, T b1, T b2, T z, const Policy& pol)
  658. {
  659. typedef typename policies::evaluation<T, Policy>::type value_type;
  660. const char* function = "boost::math::detail::hypergeometric_2F2<%1%>(%1%,%1%,%1%,%1%,%1%)";
  661. hypergeometric_2F2_sum<value_type> s(a1, a2, b1, b2, z);
  662. boost::math::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
  663. value_type result = boost::math::tools::sum_series(s, boost::math::policies::get_epsilon<value_type, Policy>(), max_iter);
  664. policies::check_series_iterations<T>(function, max_iter, pol);
  665. return policies::checked_narrowing_cast<T, Policy>(result, function);
  666. }
  667. } // namespace detail
  668. template <class RealType = double, class Policy = policies::policy<> >
  669. class non_central_beta_distribution
  670. {
  671. public:
  672. typedef RealType value_type;
  673. typedef Policy policy_type;
  674. BOOST_MATH_GPU_ENABLED non_central_beta_distribution(RealType a_, RealType b_, RealType lambda) : a(a_), b(b_), ncp(lambda)
  675. {
  676. const char* function = "boost::math::non_central_beta_distribution<%1%>::non_central_beta_distribution(%1%,%1%)";
  677. RealType r;
  678. beta_detail::check_alpha(
  679. function,
  680. a, &r, Policy());
  681. beta_detail::check_beta(
  682. function,
  683. b, &r, Policy());
  684. detail::check_non_centrality(
  685. function,
  686. lambda,
  687. &r,
  688. Policy());
  689. } // non_central_beta_distribution constructor.
  690. BOOST_MATH_GPU_ENABLED RealType alpha() const
  691. { // Private data getter function.
  692. return a;
  693. }
  694. BOOST_MATH_GPU_ENABLED RealType beta() const
  695. { // Private data getter function.
  696. return b;
  697. }
  698. BOOST_MATH_GPU_ENABLED RealType non_centrality() const
  699. { // Private data getter function.
  700. return ncp;
  701. }
  702. private:
  703. // Data member, initialized by constructor.
  704. RealType a; // alpha.
  705. RealType b; // beta.
  706. RealType ncp; // non-centrality parameter
  707. }; // template <class RealType, class Policy> class non_central_beta_distribution
  708. typedef non_central_beta_distribution<double> non_central_beta; // Reserved name of type double.
  709. #ifdef __cpp_deduction_guides
  710. template <class RealType>
  711. non_central_beta_distribution(RealType,RealType,RealType)->non_central_beta_distribution<typename boost::math::tools::promote_args<RealType>::type>;
  712. #endif
  713. // Non-member functions to give properties of the distribution.
  714. template <class RealType, class Policy>
  715. BOOST_MATH_GPU_ENABLED inline const boost::math::pair<RealType, RealType> range(const non_central_beta_distribution<RealType, Policy>& /* dist */)
  716. { // Range of permissible values for random variable k.
  717. using boost::math::tools::max_value;
  718. return boost::math::pair<RealType, RealType>(static_cast<RealType>(0), static_cast<RealType>(1));
  719. }
  720. template <class RealType, class Policy>
  721. BOOST_MATH_GPU_ENABLED inline const boost::math::pair<RealType, RealType> support(const non_central_beta_distribution<RealType, Policy>& /* dist */)
  722. { // Range of supported values for random variable k.
  723. // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
  724. using boost::math::tools::max_value;
  725. return boost::math::pair<RealType, RealType>(static_cast<RealType>(0), static_cast<RealType>(1));
  726. }
  727. template <class RealType, class Policy>
  728. BOOST_MATH_GPU_ENABLED inline RealType mode(const non_central_beta_distribution<RealType, Policy>& dist)
  729. { // mode.
  730. constexpr auto function = "mode(non_central_beta_distribution<%1%> const&)";
  731. RealType a = dist.alpha();
  732. RealType b = dist.beta();
  733. RealType l = dist.non_centrality();
  734. RealType r;
  735. if(!beta_detail::check_alpha(
  736. function,
  737. a, &r, Policy())
  738. ||
  739. !beta_detail::check_beta(
  740. function,
  741. b, &r, Policy())
  742. ||
  743. !detail::check_non_centrality(
  744. function,
  745. l,
  746. &r,
  747. Policy()))
  748. return static_cast<RealType>(r);
  749. RealType c = a + b + l / 2;
  750. RealType mean = 1 - (b / c) * (1 + l / (2 * c * c));
  751. return detail::generic_find_mode_01(
  752. dist,
  753. mean,
  754. function);
  755. }
  756. //
  757. // We don't have the necessary information to implement
  758. // these at present. These are just disabled for now,
  759. // prototypes retained so we can fill in the blanks
  760. // later:
  761. //
  762. template <class RealType, class Policy>
  763. BOOST_MATH_GPU_ENABLED inline RealType mean(const non_central_beta_distribution<RealType, Policy>& dist)
  764. {
  765. BOOST_MATH_STD_USING
  766. RealType a = dist.alpha();
  767. RealType b = dist.beta();
  768. RealType d = dist.non_centrality();
  769. RealType apb = a + b;
  770. return exp(-d / 2) * a * detail::hypergeometric_2F2<RealType, Policy>(1 + a, apb, a, 1 + apb, d / 2, Policy()) / apb;
  771. } // mean
  772. template <class RealType, class Policy>
  773. BOOST_MATH_GPU_ENABLED inline RealType variance(const non_central_beta_distribution<RealType, Policy>& dist)
  774. {
  775. //
  776. // Relative error of this function may be arbitrarily large... absolute
  777. // error will be small however... that's the best we can do for now.
  778. //
  779. BOOST_MATH_STD_USING
  780. RealType a = dist.alpha();
  781. RealType b = dist.beta();
  782. RealType d = dist.non_centrality();
  783. RealType apb = a + b;
  784. RealType result = detail::hypergeometric_2F2(RealType(1 + a), apb, a, RealType(1 + apb), RealType(d / 2), Policy());
  785. result *= result * -exp(-d) * a * a / (apb * apb);
  786. result += exp(-d / 2) * a * (1 + a) * detail::hypergeometric_2F2(RealType(2 + a), apb, a, RealType(2 + apb), RealType(d / 2), Policy()) / (apb * (1 + apb));
  787. return result;
  788. }
  789. // RealType standard_deviation(const non_central_beta_distribution<RealType, Policy>& dist)
  790. // standard_deviation provided by derived accessors.
  791. template <class RealType, class Policy>
  792. BOOST_MATH_GPU_ENABLED inline RealType skewness(const non_central_beta_distribution<RealType, Policy>& /*dist*/)
  793. { // skewness = sqrt(l).
  794. const char* function = "boost::math::non_central_beta_distribution<%1%>::skewness()";
  795. typedef typename Policy::assert_undefined_type assert_type;
  796. static_assert(assert_type::value == 0, "The Non Central Beta Distribution has no skewness.");
  797. return policies::raise_evaluation_error<RealType>(function, "This function is not yet implemented, the only sensible result is %1%.", // LCOV_EXCL_LINE
  798. boost::math::numeric_limits<RealType>::quiet_NaN(), Policy()); // infinity? LCOV_EXCL_LINE
  799. }
  800. template <class RealType, class Policy>
  801. BOOST_MATH_GPU_ENABLED inline RealType kurtosis_excess(const non_central_beta_distribution<RealType, Policy>& /*dist*/)
  802. {
  803. const char* function = "boost::math::non_central_beta_distribution<%1%>::kurtosis_excess()";
  804. typedef typename Policy::assert_undefined_type assert_type;
  805. static_assert(assert_type::value == 0, "The Non Central Beta Distribution has no kurtosis excess.");
  806. return policies::raise_evaluation_error<RealType>(function, "This function is not yet implemented, the only sensible result is %1%.", // LCOV_EXCL_LINE
  807. boost::math::numeric_limits<RealType>::quiet_NaN(), Policy()); // infinity? LCOV_EXCL_LINE
  808. } // kurtosis_excess
  809. template <class RealType, class Policy>
  810. BOOST_MATH_GPU_ENABLED inline RealType kurtosis(const non_central_beta_distribution<RealType, Policy>& dist)
  811. {
  812. return kurtosis_excess(dist) + 3;
  813. }
  814. template <class RealType, class Policy>
  815. BOOST_MATH_GPU_ENABLED inline RealType pdf(const non_central_beta_distribution<RealType, Policy>& dist, const RealType& x)
  816. { // Probability Density/Mass Function.
  817. return detail::nc_beta_pdf(dist, x);
  818. } // pdf
  819. template <class RealType, class Policy>
  820. BOOST_MATH_GPU_ENABLED RealType cdf(const non_central_beta_distribution<RealType, Policy>& dist, const RealType& x)
  821. {
  822. const char* function = "boost::math::non_central_beta_distribution<%1%>::cdf(%1%)";
  823. RealType a = dist.alpha();
  824. RealType b = dist.beta();
  825. RealType l = dist.non_centrality();
  826. RealType r;
  827. if(!beta_detail::check_alpha(
  828. function,
  829. a, &r, Policy())
  830. ||
  831. !beta_detail::check_beta(
  832. function,
  833. b, &r, Policy())
  834. ||
  835. !detail::check_non_centrality(
  836. function,
  837. l,
  838. &r,
  839. Policy())
  840. ||
  841. !beta_detail::check_x(
  842. function,
  843. x,
  844. &r,
  845. Policy()))
  846. return static_cast<RealType>(r);
  847. if(l == 0)
  848. return cdf(beta_distribution<RealType, Policy>(a, b), x);
  849. return detail::non_central_beta_cdf(x, RealType(1 - x), a, b, l, false, Policy());
  850. } // cdf
  851. template <class RealType, class Policy>
  852. BOOST_MATH_GPU_ENABLED RealType cdf(const complemented2_type<non_central_beta_distribution<RealType, Policy>, RealType>& c)
  853. { // Complemented Cumulative Distribution Function
  854. const char* function = "boost::math::non_central_beta_distribution<%1%>::cdf(%1%)";
  855. non_central_beta_distribution<RealType, Policy> const& dist = c.dist;
  856. RealType a = dist.alpha();
  857. RealType b = dist.beta();
  858. RealType l = dist.non_centrality();
  859. RealType x = c.param;
  860. RealType r;
  861. if(!beta_detail::check_alpha(
  862. function,
  863. a, &r, Policy())
  864. ||
  865. !beta_detail::check_beta(
  866. function,
  867. b, &r, Policy())
  868. ||
  869. !detail::check_non_centrality(
  870. function,
  871. l,
  872. &r,
  873. Policy())
  874. ||
  875. !beta_detail::check_x(
  876. function,
  877. x,
  878. &r,
  879. Policy()))
  880. return static_cast<RealType>(r);
  881. if(l == 0)
  882. return cdf(complement(beta_distribution<RealType, Policy>(a, b), x));
  883. return detail::non_central_beta_cdf(x, RealType(1 - x), a, b, l, true, Policy());
  884. } // ccdf
  885. template <class RealType, class Policy>
  886. BOOST_MATH_GPU_ENABLED inline RealType quantile(const non_central_beta_distribution<RealType, Policy>& dist, const RealType& p)
  887. { // Quantile (or Percent Point) function.
  888. return detail::nc_beta_quantile(dist, p, false);
  889. } // quantile
  890. template <class RealType, class Policy>
  891. BOOST_MATH_GPU_ENABLED inline RealType quantile(const complemented2_type<non_central_beta_distribution<RealType, Policy>, RealType>& c)
  892. { // Quantile (or Percent Point) function.
  893. return detail::nc_beta_quantile(c.dist, c.param, true);
  894. } // quantile complement.
  895. } // namespace math
  896. } // namespace boost
  897. // This include must be at the end, *after* the accessors
  898. // for this distribution have been defined, in order to
  899. // keep compilers that support two-phase lookup happy.
  900. #include <boost/math/distributions/detail/derived_accessors.hpp>
  901. #endif // BOOST_MATH_SPECIAL_NON_CENTRAL_BETA_HPP