beta.hpp 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621
  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. #ifndef BOOST_MATH_SPECIAL_BETA_HPP
  6. #define BOOST_MATH_SPECIAL_BETA_HPP
  7. #ifdef _MSC_VER
  8. #pragma once
  9. #endif
  10. #include <boost/math/special_functions/math_fwd.hpp>
  11. #include <boost/math/tools/config.hpp>
  12. #include <boost/math/special_functions/gamma.hpp>
  13. #include <boost/math/special_functions/binomial.hpp>
  14. #include <boost/math/special_functions/factorials.hpp>
  15. #include <boost/math/special_functions/erf.hpp>
  16. #include <boost/math/special_functions/log1p.hpp>
  17. #include <boost/math/special_functions/expm1.hpp>
  18. #include <boost/math/special_functions/trunc.hpp>
  19. #include <boost/math/tools/roots.hpp>
  20. #include <boost/math/tools/assert.hpp>
  21. #include <cmath>
  22. namespace boost{ namespace math{
  23. namespace detail{
  24. //
  25. // Implementation of Beta(a,b) using the Lanczos approximation:
  26. //
  27. template <class T, class Lanczos, class Policy>
  28. T beta_imp(T a, T b, const Lanczos&, const Policy& pol)
  29. {
  30. BOOST_MATH_STD_USING // for ADL of std names
  31. if(a <= 0)
  32. return policies::raise_domain_error<T>("boost::math::beta<%1%>(%1%,%1%)", "The arguments to the beta function must be greater than zero (got a=%1%).", a, pol);
  33. if(b <= 0)
  34. return policies::raise_domain_error<T>("boost::math::beta<%1%>(%1%,%1%)", "The arguments to the beta function must be greater than zero (got b=%1%).", b, pol);
  35. T result;
  36. T prefix = 1;
  37. T c = a + b;
  38. // Special cases:
  39. if((c == a) && (b < tools::epsilon<T>()))
  40. return 1 / b;
  41. else if((c == b) && (a < tools::epsilon<T>()))
  42. return 1 / a;
  43. if(b == 1)
  44. return 1/a;
  45. else if(a == 1)
  46. return 1/b;
  47. else if(c < tools::epsilon<T>())
  48. {
  49. result = c / a;
  50. result /= b;
  51. return result;
  52. }
  53. /*
  54. //
  55. // This code appears to be no longer necessary: it was
  56. // used to offset errors introduced from the Lanczos
  57. // approximation, but the current Lanczos approximations
  58. // are sufficiently accurate for all z that we can ditch
  59. // this. It remains in the file for future reference...
  60. //
  61. // If a or b are less than 1, shift to greater than 1:
  62. if(a < 1)
  63. {
  64. prefix *= c / a;
  65. c += 1;
  66. a += 1;
  67. }
  68. if(b < 1)
  69. {
  70. prefix *= c / b;
  71. c += 1;
  72. b += 1;
  73. }
  74. */
  75. if(a < b)
  76. std::swap(a, b);
  77. // Lanczos calculation:
  78. T agh = static_cast<T>(a + Lanczos::g() - 0.5f);
  79. T bgh = static_cast<T>(b + Lanczos::g() - 0.5f);
  80. T cgh = static_cast<T>(c + Lanczos::g() - 0.5f);
  81. result = Lanczos::lanczos_sum_expG_scaled(a) * (Lanczos::lanczos_sum_expG_scaled(b) / Lanczos::lanczos_sum_expG_scaled(c));
  82. T ambh = a - 0.5f - b;
  83. if((fabs(b * ambh) < (cgh * 100)) && (a > 100))
  84. {
  85. // Special case where the base of the power term is close to 1
  86. // compute (1+x)^y instead:
  87. result *= exp(ambh * boost::math::log1p(-b / cgh, pol));
  88. }
  89. else
  90. {
  91. result *= pow(agh / cgh, a - T(0.5) - b);
  92. }
  93. if(cgh > 1e10f)
  94. // this avoids possible overflow, but appears to be marginally less accurate:
  95. result *= pow((agh / cgh) * (bgh / cgh), b);
  96. else
  97. result *= pow((agh * bgh) / (cgh * cgh), b);
  98. result *= sqrt(boost::math::constants::e<T>() / bgh);
  99. // If a and b were originally less than 1 we need to scale the result:
  100. result *= prefix;
  101. return result;
  102. } // template <class T, class Lanczos> beta_imp(T a, T b, const Lanczos&)
  103. //
  104. // Generic implementation of Beta(a,b) without Lanczos approximation support
  105. // (Caution this is slow!!!):
  106. //
  107. template <class T, class Policy>
  108. T beta_imp(T a, T b, const lanczos::undefined_lanczos& l, const Policy& pol)
  109. {
  110. BOOST_MATH_STD_USING
  111. if(a <= 0)
  112. return policies::raise_domain_error<T>("boost::math::beta<%1%>(%1%,%1%)", "The arguments to the beta function must be greater than zero (got a=%1%).", a, pol);
  113. if(b <= 0)
  114. return policies::raise_domain_error<T>("boost::math::beta<%1%>(%1%,%1%)", "The arguments to the beta function must be greater than zero (got b=%1%).", b, pol);
  115. const T c = a + b;
  116. // Special cases:
  117. if ((c == a) && (b < tools::epsilon<T>()))
  118. return 1 / b;
  119. else if ((c == b) && (a < tools::epsilon<T>()))
  120. return 1 / a;
  121. if (b == 1)
  122. return 1 / a;
  123. else if (a == 1)
  124. return 1 / b;
  125. else if (c < tools::epsilon<T>())
  126. {
  127. T result = c / a;
  128. result /= b;
  129. return result;
  130. }
  131. // Regular cases start here:
  132. const T min_sterling = minimum_argument_for_bernoulli_recursion<T>();
  133. long shift_a = 0;
  134. long shift_b = 0;
  135. if(a < min_sterling)
  136. shift_a = 1 + ltrunc(min_sterling - a);
  137. if(b < min_sterling)
  138. shift_b = 1 + ltrunc(min_sterling - b);
  139. long shift_c = shift_a + shift_b;
  140. if ((shift_a == 0) && (shift_b == 0))
  141. {
  142. return pow(a / c, a) * pow(b / c, b) * scaled_tgamma_no_lanczos(a, pol) * scaled_tgamma_no_lanczos(b, pol) / scaled_tgamma_no_lanczos(c, pol);
  143. }
  144. else if ((a < 1) && (b < 1))
  145. {
  146. return boost::math::tgamma(a, pol) * (boost::math::tgamma(b, pol) / boost::math::tgamma(c));
  147. }
  148. else if(a < 1)
  149. return boost::math::tgamma(a, pol) * boost::math::tgamma_delta_ratio(b, a, pol);
  150. else if(b < 1)
  151. return boost::math::tgamma(b, pol) * boost::math::tgamma_delta_ratio(a, b, pol);
  152. else
  153. {
  154. T result = beta_imp(T(a + shift_a), T(b + shift_b), l, pol);
  155. //
  156. // Recursion:
  157. //
  158. for (long i = 0; i < shift_c; ++i)
  159. {
  160. result *= c + i;
  161. if (i < shift_a)
  162. result /= a + i;
  163. if (i < shift_b)
  164. result /= b + i;
  165. }
  166. return result;
  167. }
  168. } // template <class T>T beta_imp(T a, T b, const lanczos::undefined_lanczos& l)
  169. //
  170. // Compute the leading power terms in the incomplete Beta:
  171. //
  172. // (x^a)(y^b)/Beta(a,b) when normalised, and
  173. // (x^a)(y^b) otherwise.
  174. //
  175. // Almost all of the error in the incomplete beta comes from this
  176. // function: particularly when a and b are large. Computing large
  177. // powers are *hard* though, and using logarithms just leads to
  178. // horrendous cancellation errors.
  179. //
  180. template <class T, class Lanczos, class Policy>
  181. T ibeta_power_terms(T a,
  182. T b,
  183. T x,
  184. T y,
  185. const Lanczos&,
  186. bool normalised,
  187. const Policy& pol,
  188. T prefix = 1,
  189. const char* function = "boost::math::ibeta<%1%>(%1%, %1%, %1%)")
  190. {
  191. BOOST_MATH_STD_USING
  192. if(!normalised)
  193. {
  194. // can we do better here?
  195. return pow(x, a) * pow(y, b);
  196. }
  197. T result;
  198. T c = a + b;
  199. // combine power terms with Lanczos approximation:
  200. T agh = static_cast<T>(a + Lanczos::g() - 0.5f);
  201. T bgh = static_cast<T>(b + Lanczos::g() - 0.5f);
  202. T cgh = static_cast<T>(c + Lanczos::g() - 0.5f);
  203. result = Lanczos::lanczos_sum_expG_scaled(c) / (Lanczos::lanczos_sum_expG_scaled(a) * Lanczos::lanczos_sum_expG_scaled(b));
  204. result *= prefix;
  205. // combine with the leftover terms from the Lanczos approximation:
  206. result *= sqrt(bgh / boost::math::constants::e<T>());
  207. result *= sqrt(agh / cgh);
  208. // l1 and l2 are the base of the exponents minus one:
  209. T l1 = (x * b - y * agh) / agh;
  210. T l2 = (y * a - x * bgh) / bgh;
  211. if(((std::min)(fabs(l1), fabs(l2)) < 0.2))
  212. {
  213. // when the base of the exponent is very near 1 we get really
  214. // gross errors unless extra care is taken:
  215. if((l1 * l2 > 0) || ((std::min)(a, b) < 1))
  216. {
  217. //
  218. // This first branch handles the simple cases where either:
  219. //
  220. // * The two power terms both go in the same direction
  221. // (towards zero or towards infinity). In this case if either
  222. // term overflows or underflows, then the product of the two must
  223. // do so also.
  224. // *Alternatively if one exponent is less than one, then we
  225. // can't productively use it to eliminate overflow or underflow
  226. // from the other term. Problems with spurious overflow/underflow
  227. // can't be ruled out in this case, but it is *very* unlikely
  228. // since one of the power terms will evaluate to a number close to 1.
  229. //
  230. if(fabs(l1) < 0.1)
  231. {
  232. result *= exp(a * boost::math::log1p(l1, pol));
  233. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  234. }
  235. else
  236. {
  237. result *= pow((x * cgh) / agh, a);
  238. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  239. }
  240. if(fabs(l2) < 0.1)
  241. {
  242. result *= exp(b * boost::math::log1p(l2, pol));
  243. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  244. }
  245. else
  246. {
  247. result *= pow((y * cgh) / bgh, b);
  248. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  249. }
  250. }
  251. else if((std::max)(fabs(l1), fabs(l2)) < 0.5)
  252. {
  253. //
  254. // Both exponents are near one and both the exponents are
  255. // greater than one and further these two
  256. // power terms tend in opposite directions (one towards zero,
  257. // the other towards infinity), so we have to combine the terms
  258. // to avoid any risk of overflow or underflow.
  259. //
  260. // We do this by moving one power term inside the other, we have:
  261. //
  262. // (1 + l1)^a * (1 + l2)^b
  263. // = ((1 + l1)*(1 + l2)^(b/a))^a
  264. // = (1 + l1 + l3 + l1*l3)^a ; l3 = (1 + l2)^(b/a) - 1
  265. // = exp((b/a) * log(1 + l2)) - 1
  266. //
  267. // The tricky bit is deciding which term to move inside :-)
  268. // By preference we move the larger term inside, so that the
  269. // size of the largest exponent is reduced. However, that can
  270. // only be done as long as l3 (see above) is also small.
  271. //
  272. bool small_a = a < b;
  273. T ratio = b / a;
  274. if((small_a && (ratio * l2 < 0.1)) || (!small_a && (l1 / ratio > 0.1)))
  275. {
  276. T l3 = boost::math::expm1(ratio * boost::math::log1p(l2, pol), pol);
  277. l3 = l1 + l3 + l3 * l1;
  278. l3 = a * boost::math::log1p(l3, pol);
  279. result *= exp(l3);
  280. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  281. }
  282. else
  283. {
  284. T l3 = boost::math::expm1(boost::math::log1p(l1, pol) / ratio, pol);
  285. l3 = l2 + l3 + l3 * l2;
  286. l3 = b * boost::math::log1p(l3, pol);
  287. result *= exp(l3);
  288. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  289. }
  290. }
  291. else if(fabs(l1) < fabs(l2))
  292. {
  293. // First base near 1 only:
  294. T l = a * boost::math::log1p(l1, pol)
  295. + b * log((y * cgh) / bgh);
  296. if((l <= tools::log_min_value<T>()) || (l >= tools::log_max_value<T>()))
  297. {
  298. l += log(result);
  299. if(l >= tools::log_max_value<T>())
  300. return policies::raise_overflow_error<T>(function, nullptr, pol);
  301. result = exp(l);
  302. }
  303. else
  304. result *= exp(l);
  305. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  306. }
  307. else
  308. {
  309. // Second base near 1 only:
  310. T l = b * boost::math::log1p(l2, pol)
  311. + a * log((x * cgh) / agh);
  312. if((l <= tools::log_min_value<T>()) || (l >= tools::log_max_value<T>()))
  313. {
  314. l += log(result);
  315. if(l >= tools::log_max_value<T>())
  316. return policies::raise_overflow_error<T>(function, nullptr, pol);
  317. result = exp(l);
  318. }
  319. else
  320. result *= exp(l);
  321. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  322. }
  323. }
  324. else
  325. {
  326. // general case:
  327. T b1 = (x * cgh) / agh;
  328. T b2 = (y * cgh) / bgh;
  329. l1 = a * log(b1);
  330. l2 = b * log(b2);
  331. BOOST_MATH_INSTRUMENT_VARIABLE(b1);
  332. BOOST_MATH_INSTRUMENT_VARIABLE(b2);
  333. BOOST_MATH_INSTRUMENT_VARIABLE(l1);
  334. BOOST_MATH_INSTRUMENT_VARIABLE(l2);
  335. if((l1 >= tools::log_max_value<T>())
  336. || (l1 <= tools::log_min_value<T>())
  337. || (l2 >= tools::log_max_value<T>())
  338. || (l2 <= tools::log_min_value<T>())
  339. )
  340. {
  341. // Oops, under/overflow, sidestep if we can:
  342. if(a < b)
  343. {
  344. T p1 = pow(b2, b / a);
  345. T l3 = (b1 != 0) && (p1 != 0) ? (a * (log(b1) + log(p1))) : tools::max_value<T>(); // arbitrary large value if the logs would fail!
  346. if((l3 < tools::log_max_value<T>())
  347. && (l3 > tools::log_min_value<T>()))
  348. {
  349. result *= pow(p1 * b1, a);
  350. }
  351. else
  352. {
  353. l2 += l1 + log(result);
  354. if(l2 >= tools::log_max_value<T>())
  355. return policies::raise_overflow_error<T>(function, nullptr, pol);
  356. result = exp(l2);
  357. }
  358. }
  359. else
  360. {
  361. T p1 = pow(b1, a / b);
  362. T l3 = (p1 != 0) && (b2 != 0) ? (log(p1) + log(b2)) * b : tools::max_value<T>(); // arbitrary large value if the logs would fail!
  363. if((l3 < tools::log_max_value<T>())
  364. && (l3 > tools::log_min_value<T>()))
  365. {
  366. result *= pow(p1 * b2, b);
  367. }
  368. else
  369. {
  370. l2 += l1 + log(result);
  371. if(l2 >= tools::log_max_value<T>())
  372. return policies::raise_overflow_error<T>(function, nullptr, pol);
  373. result = exp(l2);
  374. }
  375. }
  376. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  377. }
  378. else
  379. {
  380. // finally the normal case:
  381. result *= pow(b1, a) * pow(b2, b);
  382. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  383. }
  384. }
  385. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  386. if (0 == result)
  387. {
  388. if ((a > 1) && (x == 0))
  389. return result; // true zero
  390. if ((b > 1) && (y == 0))
  391. return result; // true zero
  392. return boost::math::policies::raise_underflow_error<T>(function, nullptr, pol);
  393. }
  394. return result;
  395. }
  396. //
  397. // Compute the leading power terms in the incomplete Beta:
  398. //
  399. // (x^a)(y^b)/Beta(a,b) when normalised, and
  400. // (x^a)(y^b) otherwise.
  401. //
  402. // Almost all of the error in the incomplete beta comes from this
  403. // function: particularly when a and b are large. Computing large
  404. // powers are *hard* though, and using logarithms just leads to
  405. // horrendous cancellation errors.
  406. //
  407. // This version is generic, slow, and does not use the Lanczos approximation.
  408. //
  409. template <class T, class Policy>
  410. T ibeta_power_terms(T a,
  411. T b,
  412. T x,
  413. T y,
  414. const boost::math::lanczos::undefined_lanczos& l,
  415. bool normalised,
  416. const Policy& pol,
  417. T prefix = 1,
  418. const char* = "boost::math::ibeta<%1%>(%1%, %1%, %1%)")
  419. {
  420. BOOST_MATH_STD_USING
  421. if(!normalised)
  422. {
  423. return prefix * pow(x, a) * pow(y, b);
  424. }
  425. T c = a + b;
  426. const T min_sterling = minimum_argument_for_bernoulli_recursion<T>();
  427. long shift_a = 0;
  428. long shift_b = 0;
  429. if (a < min_sterling)
  430. shift_a = 1 + ltrunc(min_sterling - a);
  431. if (b < min_sterling)
  432. shift_b = 1 + ltrunc(min_sterling - b);
  433. if ((shift_a == 0) && (shift_b == 0))
  434. {
  435. T power1, power2;
  436. if (a < b)
  437. {
  438. power1 = pow((x * y * c * c) / (a * b), a);
  439. power2 = pow((y * c) / b, b - a);
  440. }
  441. else
  442. {
  443. power1 = pow((x * y * c * c) / (a * b), b);
  444. power2 = pow((x * c) / a, a - b);
  445. }
  446. if (!(boost::math::isnormal)(power1) || !(boost::math::isnormal)(power2))
  447. {
  448. // We have to use logs :(
  449. return prefix * exp(a * log(x * c / a) + b * log(y * c / b)) * scaled_tgamma_no_lanczos(c, pol) / (scaled_tgamma_no_lanczos(a, pol) * scaled_tgamma_no_lanczos(b, pol));
  450. }
  451. return prefix * power1 * power2 * scaled_tgamma_no_lanczos(c, pol) / (scaled_tgamma_no_lanczos(a, pol) * scaled_tgamma_no_lanczos(b, pol));
  452. }
  453. T power1 = pow(x, a);
  454. T power2 = pow(y, b);
  455. T bet = beta_imp(a, b, l, pol);
  456. if(!(boost::math::isnormal)(power1) || !(boost::math::isnormal)(power2) || !(boost::math::isnormal)(bet))
  457. {
  458. int shift_c = shift_a + shift_b;
  459. T result = ibeta_power_terms(T(a + shift_a), T(b + shift_b), x, y, l, normalised, pol, prefix);
  460. if ((boost::math::isnormal)(result))
  461. {
  462. for (int i = 0; i < shift_c; ++i)
  463. {
  464. result /= c + i;
  465. if (i < shift_a)
  466. {
  467. result *= a + i;
  468. result /= x;
  469. }
  470. if (i < shift_b)
  471. {
  472. result *= b + i;
  473. result /= y;
  474. }
  475. }
  476. return prefix * result;
  477. }
  478. else
  479. {
  480. T log_result = log(x) * a + log(y) * b + log(prefix);
  481. if ((boost::math::isnormal)(bet))
  482. log_result -= log(bet);
  483. else
  484. log_result += boost::math::lgamma(c, pol) - boost::math::lgamma(a, pol) - boost::math::lgamma(b, pol);
  485. return exp(log_result);
  486. }
  487. }
  488. return prefix * power1 * (power2 / bet);
  489. }
  490. //
  491. // Series approximation to the incomplete beta:
  492. //
  493. template <class T>
  494. struct ibeta_series_t
  495. {
  496. typedef T result_type;
  497. ibeta_series_t(T a_, T b_, T x_, T mult) : result(mult), x(x_), apn(a_), poch(1-b_), n(1) {}
  498. T operator()()
  499. {
  500. T r = result / apn;
  501. apn += 1;
  502. result *= poch * x / n;
  503. ++n;
  504. poch += 1;
  505. return r;
  506. }
  507. private:
  508. T result, x, apn, poch;
  509. int n;
  510. };
  511. template <class T, class Lanczos, class Policy>
  512. T ibeta_series(T a, T b, T x, T s0, const Lanczos&, bool normalised, T* p_derivative, T y, const Policy& pol)
  513. {
  514. BOOST_MATH_STD_USING
  515. T result;
  516. BOOST_MATH_ASSERT((p_derivative == 0) || normalised);
  517. if(normalised)
  518. {
  519. T c = a + b;
  520. // incomplete beta power term, combined with the Lanczos approximation:
  521. T agh = static_cast<T>(a + Lanczos::g() - 0.5f);
  522. T bgh = static_cast<T>(b + Lanczos::g() - 0.5f);
  523. T cgh = static_cast<T>(c + Lanczos::g() - 0.5f);
  524. result = Lanczos::lanczos_sum_expG_scaled(c) / (Lanczos::lanczos_sum_expG_scaled(a) * Lanczos::lanczos_sum_expG_scaled(b));
  525. if (!(boost::math::isfinite)(result))
  526. result = 0;
  527. T l1 = log(cgh / bgh) * (b - 0.5f);
  528. T l2 = log(x * cgh / agh) * a;
  529. //
  530. // Check for over/underflow in the power terms:
  531. //
  532. if((l1 > tools::log_min_value<T>())
  533. && (l1 < tools::log_max_value<T>())
  534. && (l2 > tools::log_min_value<T>())
  535. && (l2 < tools::log_max_value<T>()))
  536. {
  537. if(a * b < bgh * 10)
  538. result *= exp((b - 0.5f) * boost::math::log1p(a / bgh, pol));
  539. else
  540. result *= pow(cgh / bgh, b - 0.5f);
  541. result *= pow(x * cgh / agh, a);
  542. result *= sqrt(agh / boost::math::constants::e<T>());
  543. if(p_derivative)
  544. {
  545. *p_derivative = result * pow(y, b);
  546. BOOST_MATH_ASSERT(*p_derivative >= 0);
  547. }
  548. }
  549. else
  550. {
  551. //
  552. // Oh dear, we need logs, and this *will* cancel:
  553. //
  554. result = log(result) + l1 + l2 + (log(agh) - 1) / 2;
  555. if(p_derivative)
  556. *p_derivative = exp(result + b * log(y));
  557. result = exp(result);
  558. }
  559. }
  560. else
  561. {
  562. // Non-normalised, just compute the power:
  563. result = pow(x, a);
  564. }
  565. if(result < tools::min_value<T>())
  566. return s0; // Safeguard: series can't cope with denorms.
  567. ibeta_series_t<T> s(a, b, x, result);
  568. std::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
  569. result = boost::math::tools::sum_series(s, boost::math::policies::get_epsilon<T, Policy>(), max_iter, s0);
  570. policies::check_series_iterations<T>("boost::math::ibeta<%1%>(%1%, %1%, %1%) in ibeta_series (with lanczos)", max_iter, pol);
  571. return result;
  572. }
  573. //
  574. // Incomplete Beta series again, this time without Lanczos support:
  575. //
  576. template <class T, class Policy>
  577. T ibeta_series(T a, T b, T x, T s0, const boost::math::lanczos::undefined_lanczos& l, bool normalised, T* p_derivative, T y, const Policy& pol)
  578. {
  579. BOOST_MATH_STD_USING
  580. T result;
  581. BOOST_MATH_ASSERT((p_derivative == 0) || normalised);
  582. if(normalised)
  583. {
  584. const T min_sterling = minimum_argument_for_bernoulli_recursion<T>();
  585. long shift_a = 0;
  586. long shift_b = 0;
  587. if (a < min_sterling)
  588. shift_a = 1 + ltrunc(min_sterling - a);
  589. if (b < min_sterling)
  590. shift_b = 1 + ltrunc(min_sterling - b);
  591. T c = a + b;
  592. if ((shift_a == 0) && (shift_b == 0))
  593. {
  594. result = pow(x * c / a, a) * pow(c / b, b) * scaled_tgamma_no_lanczos(c, pol) / (scaled_tgamma_no_lanczos(a, pol) * scaled_tgamma_no_lanczos(b, pol));
  595. }
  596. else if ((a < 1) && (b > 1))
  597. result = pow(x, a) / (boost::math::tgamma(a, pol) * boost::math::tgamma_delta_ratio(b, a, pol));
  598. else
  599. {
  600. T power = pow(x, a);
  601. T bet = beta_imp(a, b, l, pol);
  602. if (!(boost::math::isnormal)(power) || !(boost::math::isnormal)(bet))
  603. {
  604. result = exp(a * log(x) + boost::math::lgamma(c, pol) - boost::math::lgamma(a, pol) - boost::math::lgamma(b, pol));
  605. }
  606. else
  607. result = power / bet;
  608. }
  609. if(p_derivative)
  610. {
  611. *p_derivative = result * pow(y, b);
  612. BOOST_MATH_ASSERT(*p_derivative >= 0);
  613. }
  614. }
  615. else
  616. {
  617. // Non-normalised, just compute the power:
  618. result = pow(x, a);
  619. }
  620. if(result < tools::min_value<T>())
  621. return s0; // Safeguard: series can't cope with denorms.
  622. ibeta_series_t<T> s(a, b, x, result);
  623. std::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
  624. result = boost::math::tools::sum_series(s, boost::math::policies::get_epsilon<T, Policy>(), max_iter, s0);
  625. policies::check_series_iterations<T>("boost::math::ibeta<%1%>(%1%, %1%, %1%) in ibeta_series (without lanczos)", max_iter, pol);
  626. return result;
  627. }
  628. //
  629. // Continued fraction for the incomplete beta:
  630. //
  631. template <class T>
  632. struct ibeta_fraction2_t
  633. {
  634. typedef std::pair<T, T> result_type;
  635. ibeta_fraction2_t(T a_, T b_, T x_, T y_) : a(a_), b(b_), x(x_), y(y_), m(0) {}
  636. result_type operator()()
  637. {
  638. T aN = (a + m - 1) * (a + b + m - 1) * m * (b - m) * x * x;
  639. T denom = (a + 2 * m - 1);
  640. aN /= denom * denom;
  641. T bN = static_cast<T>(m);
  642. bN += (m * (b - m) * x) / (a + 2*m - 1);
  643. bN += ((a + m) * (a * y - b * x + 1 + m *(2 - x))) / (a + 2*m + 1);
  644. ++m;
  645. return std::make_pair(aN, bN);
  646. }
  647. private:
  648. T a, b, x, y;
  649. int m;
  650. };
  651. //
  652. // Evaluate the incomplete beta via the continued fraction representation:
  653. //
  654. template <class T, class Policy>
  655. inline T ibeta_fraction2(T a, T b, T x, T y, const Policy& pol, bool normalised, T* p_derivative)
  656. {
  657. typedef typename lanczos::lanczos<T, Policy>::type lanczos_type;
  658. BOOST_MATH_STD_USING
  659. T result = ibeta_power_terms(a, b, x, y, lanczos_type(), normalised, pol);
  660. if(p_derivative)
  661. {
  662. *p_derivative = result;
  663. BOOST_MATH_ASSERT(*p_derivative >= 0);
  664. }
  665. if(result == 0)
  666. return result;
  667. ibeta_fraction2_t<T> f(a, b, x, y);
  668. T fract = boost::math::tools::continued_fraction_b(f, boost::math::policies::get_epsilon<T, Policy>());
  669. BOOST_MATH_INSTRUMENT_VARIABLE(fract);
  670. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  671. return result / fract;
  672. }
  673. //
  674. // Computes the difference between ibeta(a,b,x) and ibeta(a+k,b,x):
  675. //
  676. template <class T, class Policy>
  677. T ibeta_a_step(T a, T b, T x, T y, int k, const Policy& pol, bool normalised, T* p_derivative)
  678. {
  679. typedef typename lanczos::lanczos<T, Policy>::type lanczos_type;
  680. BOOST_MATH_INSTRUMENT_VARIABLE(k);
  681. T prefix = ibeta_power_terms(a, b, x, y, lanczos_type(), normalised, pol);
  682. if(p_derivative)
  683. {
  684. *p_derivative = prefix;
  685. BOOST_MATH_ASSERT(*p_derivative >= 0);
  686. }
  687. prefix /= a;
  688. if(prefix == 0)
  689. return prefix;
  690. T sum = 1;
  691. T term = 1;
  692. // series summation from 0 to k-1:
  693. for(int i = 0; i < k-1; ++i)
  694. {
  695. term *= (a+b+i) * x / (a+i+1);
  696. sum += term;
  697. }
  698. prefix *= sum;
  699. return prefix;
  700. }
  701. //
  702. // This function is only needed for the non-regular incomplete beta,
  703. // it computes the delta in:
  704. // beta(a,b,x) = prefix + delta * beta(a+k,b,x)
  705. // it is currently only called for small k.
  706. //
  707. template <class T>
  708. inline T rising_factorial_ratio(T a, T b, int k)
  709. {
  710. // calculate:
  711. // (a)(a+1)(a+2)...(a+k-1)
  712. // _______________________
  713. // (b)(b+1)(b+2)...(b+k-1)
  714. // This is only called with small k, for large k
  715. // it is grossly inefficient, do not use outside it's
  716. // intended purpose!!!
  717. BOOST_MATH_INSTRUMENT_VARIABLE(k);
  718. if(k == 0)
  719. return 1;
  720. T result = 1;
  721. for(int i = 0; i < k; ++i)
  722. result *= (a+i) / (b+i);
  723. return result;
  724. }
  725. //
  726. // Routine for a > 15, b < 1
  727. //
  728. // Begin by figuring out how large our table of Pn's should be,
  729. // quoted accuracies are "guesstimates" based on empirical observation.
  730. // Note that the table size should never exceed the size of our
  731. // tables of factorials.
  732. //
  733. template <class T>
  734. struct Pn_size
  735. {
  736. // This is likely to be enough for ~35-50 digit accuracy
  737. // but it's hard to quantify exactly:
  738. static constexpr unsigned value =
  739. ::boost::math::max_factorial<T>::value >= 100 ? 50
  740. : ::boost::math::max_factorial<T>::value >= ::boost::math::max_factorial<double>::value ? 30
  741. : ::boost::math::max_factorial<T>::value >= ::boost::math::max_factorial<float>::value ? 15 : 1;
  742. static_assert(::boost::math::max_factorial<T>::value >= ::boost::math::max_factorial<float>::value, "Type does not provide for 35-50 digits of accuracy.");
  743. };
  744. template <>
  745. struct Pn_size<float>
  746. {
  747. static constexpr unsigned value = 15; // ~8-15 digit accuracy
  748. static_assert(::boost::math::max_factorial<float>::value >= 30, "Type does not provide for 8-15 digits of accuracy.");
  749. };
  750. template <>
  751. struct Pn_size<double>
  752. {
  753. static constexpr unsigned value = 30; // 16-20 digit accuracy
  754. static_assert(::boost::math::max_factorial<double>::value >= 60, "Type does not provide for 16-20 digits of accuracy.");
  755. };
  756. template <>
  757. struct Pn_size<long double>
  758. {
  759. static constexpr unsigned value = 50; // ~35-50 digit accuracy
  760. static_assert(::boost::math::max_factorial<long double>::value >= 100, "Type does not provide for ~35-50 digits of accuracy");
  761. };
  762. template <class T, class Policy>
  763. T beta_small_b_large_a_series(T a, T b, T x, T y, T s0, T mult, const Policy& pol, bool normalised)
  764. {
  765. typedef typename lanczos::lanczos<T, Policy>::type lanczos_type;
  766. BOOST_MATH_STD_USING
  767. //
  768. // This is DiDonato and Morris's BGRAT routine, see Eq's 9 through 9.6.
  769. //
  770. // Some values we'll need later, these are Eq 9.1:
  771. //
  772. T bm1 = b - 1;
  773. T t = a + bm1 / 2;
  774. T lx, u;
  775. if(y < 0.35)
  776. lx = boost::math::log1p(-y, pol);
  777. else
  778. lx = log(x);
  779. u = -t * lx;
  780. // and from from 9.2:
  781. T prefix;
  782. T h = regularised_gamma_prefix(b, u, pol, lanczos_type());
  783. if(h <= tools::min_value<T>())
  784. return s0;
  785. if(normalised)
  786. {
  787. prefix = h / boost::math::tgamma_delta_ratio(a, b, pol);
  788. prefix /= pow(t, b);
  789. }
  790. else
  791. {
  792. prefix = full_igamma_prefix(b, u, pol) / pow(t, b);
  793. }
  794. prefix *= mult;
  795. //
  796. // now we need the quantity Pn, unfortunately this is computed
  797. // recursively, and requires a full history of all the previous values
  798. // so no choice but to declare a big table and hope it's big enough...
  799. //
  800. T p[ ::boost::math::detail::Pn_size<T>::value ] = { 1 }; // see 9.3.
  801. //
  802. // Now an initial value for J, see 9.6:
  803. //
  804. T j = boost::math::gamma_q(b, u, pol) / h;
  805. //
  806. // Now we can start to pull things together and evaluate the sum in Eq 9:
  807. //
  808. T sum = s0 + prefix * j; // Value at N = 0
  809. // some variables we'll need:
  810. unsigned tnp1 = 1; // 2*N+1
  811. T lx2 = lx / 2;
  812. lx2 *= lx2;
  813. T lxp = 1;
  814. T t4 = 4 * t * t;
  815. T b2n = b;
  816. for(unsigned n = 1; n < sizeof(p)/sizeof(p[0]); ++n)
  817. {
  818. /*
  819. // debugging code, enable this if you want to determine whether
  820. // the table of Pn's is large enough...
  821. //
  822. static int max_count = 2;
  823. if(n > max_count)
  824. {
  825. max_count = n;
  826. std::cerr << "Max iterations in BGRAT was " << n << std::endl;
  827. }
  828. */
  829. //
  830. // begin by evaluating the next Pn from Eq 9.4:
  831. //
  832. tnp1 += 2;
  833. p[n] = 0;
  834. T mbn = b - n;
  835. unsigned tmp1 = 3;
  836. for(unsigned m = 1; m < n; ++m)
  837. {
  838. mbn = m * b - n;
  839. p[n] += mbn * p[n-m] / boost::math::unchecked_factorial<T>(tmp1);
  840. tmp1 += 2;
  841. }
  842. p[n] /= n;
  843. p[n] += bm1 / boost::math::unchecked_factorial<T>(tnp1);
  844. //
  845. // Now we want Jn from Jn-1 using Eq 9.6:
  846. //
  847. j = (b2n * (b2n + 1) * j + (u + b2n + 1) * lxp) / t4;
  848. lxp *= lx2;
  849. b2n += 2;
  850. //
  851. // pull it together with Eq 9:
  852. //
  853. T r = prefix * p[n] * j;
  854. sum += r;
  855. if(r > 1)
  856. {
  857. if(fabs(r) < fabs(tools::epsilon<T>() * sum))
  858. break;
  859. }
  860. else
  861. {
  862. if(fabs(r / tools::epsilon<T>()) < fabs(sum))
  863. break;
  864. }
  865. }
  866. return sum;
  867. } // template <class T, class Lanczos>T beta_small_b_large_a_series(T a, T b, T x, T y, T s0, T mult, const Lanczos& l, bool normalised)
  868. //
  869. // For integer arguments we can relate the incomplete beta to the
  870. // complement of the binomial distribution cdf and use this finite sum.
  871. //
  872. template <class T, class Policy>
  873. T binomial_ccdf(T n, T k, T x, T y, const Policy& pol)
  874. {
  875. BOOST_MATH_STD_USING // ADL of std names
  876. T result = pow(x, n);
  877. if(result > tools::min_value<T>())
  878. {
  879. T term = result;
  880. for(unsigned i = itrunc(T(n - 1)); i > k; --i)
  881. {
  882. term *= ((i + 1) * y) / ((n - i) * x);
  883. result += term;
  884. }
  885. }
  886. else
  887. {
  888. // First term underflows so we need to start at the mode of the
  889. // distribution and work outwards:
  890. int start = itrunc(n * x);
  891. if(start <= k + 1)
  892. start = itrunc(k + 2);
  893. result = static_cast<T>(pow(x, start) * pow(y, n - start) * boost::math::binomial_coefficient<T>(itrunc(n), itrunc(start), pol));
  894. if(result == 0)
  895. {
  896. // OK, starting slightly above the mode didn't work,
  897. // we'll have to sum the terms the old fashioned way:
  898. for(unsigned i = start - 1; i > k; --i)
  899. {
  900. result += static_cast<T>(pow(x, static_cast<int>(i)) * pow(y, n - i) * boost::math::binomial_coefficient<T>(itrunc(n), itrunc(i), pol));
  901. }
  902. }
  903. else
  904. {
  905. T term = result;
  906. T start_term = result;
  907. for(unsigned i = start - 1; i > k; --i)
  908. {
  909. term *= ((i + 1) * y) / ((n - i) * x);
  910. result += term;
  911. }
  912. term = start_term;
  913. for(unsigned i = start + 1; i <= n; ++i)
  914. {
  915. term *= (n - i + 1) * x / (i * y);
  916. result += term;
  917. }
  918. }
  919. }
  920. return result;
  921. }
  922. //
  923. // The incomplete beta function implementation:
  924. // This is just a big bunch of spaghetti code to divide up the
  925. // input range and select the right implementation method for
  926. // each domain:
  927. //
  928. template <class T, class Policy>
  929. T ibeta_imp(T a, T b, T x, const Policy& pol, bool inv, bool normalised, T* p_derivative)
  930. {
  931. static const char* function = "boost::math::ibeta<%1%>(%1%, %1%, %1%)";
  932. typedef typename lanczos::lanczos<T, Policy>::type lanczos_type;
  933. BOOST_MATH_STD_USING // for ADL of std math functions.
  934. BOOST_MATH_INSTRUMENT_VARIABLE(a);
  935. BOOST_MATH_INSTRUMENT_VARIABLE(b);
  936. BOOST_MATH_INSTRUMENT_VARIABLE(x);
  937. BOOST_MATH_INSTRUMENT_VARIABLE(inv);
  938. BOOST_MATH_INSTRUMENT_VARIABLE(normalised);
  939. bool invert = inv;
  940. T fract;
  941. T y = 1 - x;
  942. BOOST_MATH_ASSERT((p_derivative == 0) || normalised);
  943. if(!(boost::math::isfinite)(a))
  944. return policies::raise_domain_error<T>(function, "The argument a to the incomplete beta function must be >= zero (got a=%1%).", a, pol);
  945. if(!(boost::math::isfinite)(b))
  946. return policies::raise_domain_error<T>(function, "The argument b to the incomplete beta function must be >= zero (got b=%1%).", b, pol);
  947. if(!(boost::math::isfinite)(x))
  948. return policies::raise_domain_error<T>(function, "The argument x to the incomplete beta function must be in [0,1] (got x=%1%).", x, pol);
  949. if(p_derivative)
  950. *p_derivative = -1; // value not set.
  951. if((x < 0) || (x > 1))
  952. return policies::raise_domain_error<T>(function, "Parameter x outside the range [0,1] in the incomplete beta function (got x=%1%).", x, pol);
  953. if(normalised)
  954. {
  955. if(a < 0)
  956. return policies::raise_domain_error<T>(function, "The argument a to the incomplete beta function must be >= zero (got a=%1%).", a, pol);
  957. if(b < 0)
  958. return policies::raise_domain_error<T>(function, "The argument b to the incomplete beta function must be >= zero (got b=%1%).", b, pol);
  959. // extend to a few very special cases:
  960. if(a == 0)
  961. {
  962. if(b == 0)
  963. return policies::raise_domain_error<T>(function, "The arguments a and b to the incomplete beta function cannot both be zero, with x=%1%.", x, pol);
  964. if(b > 0)
  965. return static_cast<T>(inv ? 0 : 1);
  966. }
  967. else if(b == 0)
  968. {
  969. if(a > 0)
  970. return static_cast<T>(inv ? 1 : 0);
  971. }
  972. }
  973. else
  974. {
  975. if(a <= 0)
  976. return policies::raise_domain_error<T>(function, "The argument a to the incomplete beta function must be greater than zero (got a=%1%).", a, pol);
  977. if(b <= 0)
  978. return policies::raise_domain_error<T>(function, "The argument b to the incomplete beta function must be greater than zero (got b=%1%).", b, pol);
  979. }
  980. if(x == 0)
  981. {
  982. if(p_derivative)
  983. {
  984. *p_derivative = (a == 1) ? (T)1 : (a < 1) ? T(tools::max_value<T>() / 2) : T(tools::min_value<T>() * 2);
  985. }
  986. return (invert ? (normalised ? T(1) : boost::math::beta(a, b, pol)) : T(0));
  987. }
  988. if(x == 1)
  989. {
  990. if(p_derivative)
  991. {
  992. *p_derivative = (b == 1) ? T(1) : (b < 1) ? T(tools::max_value<T>() / 2) : T(tools::min_value<T>() * 2);
  993. }
  994. return (invert == 0 ? (normalised ? 1 : boost::math::beta(a, b, pol)) : 0);
  995. }
  996. if((a == 0.5f) && (b == 0.5f))
  997. {
  998. // We have an arcsine distribution:
  999. if(p_derivative)
  1000. {
  1001. *p_derivative = 1 / constants::pi<T>() * sqrt(y * x);
  1002. }
  1003. T p = invert ? asin(sqrt(y)) / constants::half_pi<T>() : asin(sqrt(x)) / constants::half_pi<T>();
  1004. if(!normalised)
  1005. p *= constants::pi<T>();
  1006. return p;
  1007. }
  1008. if(a == 1)
  1009. {
  1010. std::swap(a, b);
  1011. std::swap(x, y);
  1012. invert = !invert;
  1013. }
  1014. if(b == 1)
  1015. {
  1016. //
  1017. // Special case see: http://functions.wolfram.com/GammaBetaErf/BetaRegularized/03/01/01/
  1018. //
  1019. if(a == 1)
  1020. {
  1021. if(p_derivative)
  1022. *p_derivative = 1;
  1023. return invert ? y : x;
  1024. }
  1025. if(p_derivative)
  1026. {
  1027. *p_derivative = a * pow(x, a - 1);
  1028. }
  1029. T p;
  1030. if(y < 0.5)
  1031. p = invert ? T(-boost::math::expm1(a * boost::math::log1p(-y, pol), pol)) : T(exp(a * boost::math::log1p(-y, pol)));
  1032. else
  1033. p = invert ? T(-boost::math::powm1(x, a, pol)) : T(pow(x, a));
  1034. if(!normalised)
  1035. p /= a;
  1036. return p;
  1037. }
  1038. if((std::min)(a, b) <= 1)
  1039. {
  1040. if(x > 0.5)
  1041. {
  1042. std::swap(a, b);
  1043. std::swap(x, y);
  1044. invert = !invert;
  1045. BOOST_MATH_INSTRUMENT_VARIABLE(invert);
  1046. }
  1047. if((std::max)(a, b) <= 1)
  1048. {
  1049. // Both a,b < 1:
  1050. if((a >= (std::min)(T(0.2), b)) || (pow(x, a) <= 0.9))
  1051. {
  1052. if(!invert)
  1053. {
  1054. fract = ibeta_series(a, b, x, T(0), lanczos_type(), normalised, p_derivative, y, pol);
  1055. BOOST_MATH_INSTRUMENT_VARIABLE(fract);
  1056. }
  1057. else
  1058. {
  1059. fract = -(normalised ? 1 : boost::math::beta(a, b, pol));
  1060. invert = false;
  1061. fract = -ibeta_series(a, b, x, fract, lanczos_type(), normalised, p_derivative, y, pol);
  1062. BOOST_MATH_INSTRUMENT_VARIABLE(fract);
  1063. }
  1064. }
  1065. else
  1066. {
  1067. std::swap(a, b);
  1068. std::swap(x, y);
  1069. invert = !invert;
  1070. if(y >= 0.3)
  1071. {
  1072. if(!invert)
  1073. {
  1074. fract = ibeta_series(a, b, x, T(0), lanczos_type(), normalised, p_derivative, y, pol);
  1075. BOOST_MATH_INSTRUMENT_VARIABLE(fract);
  1076. }
  1077. else
  1078. {
  1079. fract = -(normalised ? 1 : boost::math::beta(a, b, pol));
  1080. invert = false;
  1081. fract = -ibeta_series(a, b, x, fract, lanczos_type(), normalised, p_derivative, y, pol);
  1082. BOOST_MATH_INSTRUMENT_VARIABLE(fract);
  1083. }
  1084. }
  1085. else
  1086. {
  1087. // Sidestep on a, and then use the series representation:
  1088. T prefix;
  1089. if(!normalised)
  1090. {
  1091. prefix = rising_factorial_ratio(T(a+b), a, 20);
  1092. }
  1093. else
  1094. {
  1095. prefix = 1;
  1096. }
  1097. fract = ibeta_a_step(a, b, x, y, 20, pol, normalised, p_derivative);
  1098. if(!invert)
  1099. {
  1100. fract = beta_small_b_large_a_series(T(a + 20), b, x, y, fract, prefix, pol, normalised);
  1101. BOOST_MATH_INSTRUMENT_VARIABLE(fract);
  1102. }
  1103. else
  1104. {
  1105. fract -= (normalised ? 1 : boost::math::beta(a, b, pol));
  1106. invert = false;
  1107. fract = -beta_small_b_large_a_series(T(a + 20), b, x, y, fract, prefix, pol, normalised);
  1108. BOOST_MATH_INSTRUMENT_VARIABLE(fract);
  1109. }
  1110. }
  1111. }
  1112. }
  1113. else
  1114. {
  1115. // One of a, b < 1 only:
  1116. if((b <= 1) || ((x < 0.1) && (pow(b * x, a) <= 0.7)))
  1117. {
  1118. if(!invert)
  1119. {
  1120. fract = ibeta_series(a, b, x, T(0), lanczos_type(), normalised, p_derivative, y, pol);
  1121. BOOST_MATH_INSTRUMENT_VARIABLE(fract);
  1122. }
  1123. else
  1124. {
  1125. fract = -(normalised ? 1 : boost::math::beta(a, b, pol));
  1126. invert = false;
  1127. fract = -ibeta_series(a, b, x, fract, lanczos_type(), normalised, p_derivative, y, pol);
  1128. BOOST_MATH_INSTRUMENT_VARIABLE(fract);
  1129. }
  1130. }
  1131. else
  1132. {
  1133. std::swap(a, b);
  1134. std::swap(x, y);
  1135. invert = !invert;
  1136. if(y >= 0.3)
  1137. {
  1138. if(!invert)
  1139. {
  1140. fract = ibeta_series(a, b, x, T(0), lanczos_type(), normalised, p_derivative, y, pol);
  1141. BOOST_MATH_INSTRUMENT_VARIABLE(fract);
  1142. }
  1143. else
  1144. {
  1145. fract = -(normalised ? 1 : boost::math::beta(a, b, pol));
  1146. invert = false;
  1147. fract = -ibeta_series(a, b, x, fract, lanczos_type(), normalised, p_derivative, y, pol);
  1148. BOOST_MATH_INSTRUMENT_VARIABLE(fract);
  1149. }
  1150. }
  1151. else if(a >= 15)
  1152. {
  1153. if(!invert)
  1154. {
  1155. fract = beta_small_b_large_a_series(a, b, x, y, T(0), T(1), pol, normalised);
  1156. BOOST_MATH_INSTRUMENT_VARIABLE(fract);
  1157. }
  1158. else
  1159. {
  1160. fract = -(normalised ? 1 : boost::math::beta(a, b, pol));
  1161. invert = false;
  1162. fract = -beta_small_b_large_a_series(a, b, x, y, fract, T(1), pol, normalised);
  1163. BOOST_MATH_INSTRUMENT_VARIABLE(fract);
  1164. }
  1165. }
  1166. else
  1167. {
  1168. // Sidestep to improve errors:
  1169. T prefix;
  1170. if(!normalised)
  1171. {
  1172. prefix = rising_factorial_ratio(T(a+b), a, 20);
  1173. }
  1174. else
  1175. {
  1176. prefix = 1;
  1177. }
  1178. fract = ibeta_a_step(a, b, x, y, 20, pol, normalised, p_derivative);
  1179. BOOST_MATH_INSTRUMENT_VARIABLE(fract);
  1180. if(!invert)
  1181. {
  1182. fract = beta_small_b_large_a_series(T(a + 20), b, x, y, fract, prefix, pol, normalised);
  1183. BOOST_MATH_INSTRUMENT_VARIABLE(fract);
  1184. }
  1185. else
  1186. {
  1187. fract -= (normalised ? 1 : boost::math::beta(a, b, pol));
  1188. invert = false;
  1189. fract = -beta_small_b_large_a_series(T(a + 20), b, x, y, fract, prefix, pol, normalised);
  1190. BOOST_MATH_INSTRUMENT_VARIABLE(fract);
  1191. }
  1192. }
  1193. }
  1194. }
  1195. }
  1196. else
  1197. {
  1198. // Both a,b >= 1:
  1199. T lambda;
  1200. if(a < b)
  1201. {
  1202. lambda = a - (a + b) * x;
  1203. }
  1204. else
  1205. {
  1206. lambda = (a + b) * y - b;
  1207. }
  1208. if(lambda < 0)
  1209. {
  1210. std::swap(a, b);
  1211. std::swap(x, y);
  1212. invert = !invert;
  1213. BOOST_MATH_INSTRUMENT_VARIABLE(invert);
  1214. }
  1215. if(b < 40)
  1216. {
  1217. if((floor(a) == a) && (floor(b) == b) && (a < static_cast<T>((std::numeric_limits<int>::max)() - 100)) && (y != 1))
  1218. {
  1219. // relate to the binomial distribution and use a finite sum:
  1220. T k = a - 1;
  1221. T n = b + k;
  1222. fract = binomial_ccdf(n, k, x, y, pol);
  1223. if(!normalised)
  1224. fract *= boost::math::beta(a, b, pol);
  1225. BOOST_MATH_INSTRUMENT_VARIABLE(fract);
  1226. }
  1227. else if(b * x <= 0.7)
  1228. {
  1229. if(!invert)
  1230. {
  1231. fract = ibeta_series(a, b, x, T(0), lanczos_type(), normalised, p_derivative, y, pol);
  1232. BOOST_MATH_INSTRUMENT_VARIABLE(fract);
  1233. }
  1234. else
  1235. {
  1236. fract = -(normalised ? 1 : boost::math::beta(a, b, pol));
  1237. invert = false;
  1238. fract = -ibeta_series(a, b, x, fract, lanczos_type(), normalised, p_derivative, y, pol);
  1239. BOOST_MATH_INSTRUMENT_VARIABLE(fract);
  1240. }
  1241. }
  1242. else if(a > 15)
  1243. {
  1244. // sidestep so we can use the series representation:
  1245. int n = itrunc(T(floor(b)), pol);
  1246. if(n == b)
  1247. --n;
  1248. T bbar = b - n;
  1249. T prefix;
  1250. if(!normalised)
  1251. {
  1252. prefix = rising_factorial_ratio(T(a+bbar), bbar, n);
  1253. }
  1254. else
  1255. {
  1256. prefix = 1;
  1257. }
  1258. fract = ibeta_a_step(bbar, a, y, x, n, pol, normalised, static_cast<T*>(nullptr));
  1259. fract = beta_small_b_large_a_series(a, bbar, x, y, fract, T(1), pol, normalised);
  1260. fract /= prefix;
  1261. BOOST_MATH_INSTRUMENT_VARIABLE(fract);
  1262. }
  1263. else if(normalised)
  1264. {
  1265. // The formula here for the non-normalised case is tricky to figure
  1266. // out (for me!!), and requires two pochhammer calculations rather
  1267. // than one, so leave it for now and only use this in the normalized case....
  1268. int n = itrunc(T(floor(b)), pol);
  1269. T bbar = b - n;
  1270. if(bbar <= 0)
  1271. {
  1272. --n;
  1273. bbar += 1;
  1274. }
  1275. fract = ibeta_a_step(bbar, a, y, x, n, pol, normalised, static_cast<T*>(nullptr));
  1276. fract += ibeta_a_step(a, bbar, x, y, 20, pol, normalised, static_cast<T*>(nullptr));
  1277. if(invert)
  1278. fract -= 1; // Note this line would need changing if we ever enable this branch in non-normalized case
  1279. fract = beta_small_b_large_a_series(T(a+20), bbar, x, y, fract, T(1), pol, normalised);
  1280. if(invert)
  1281. {
  1282. fract = -fract;
  1283. invert = false;
  1284. }
  1285. BOOST_MATH_INSTRUMENT_VARIABLE(fract);
  1286. }
  1287. else
  1288. {
  1289. fract = ibeta_fraction2(a, b, x, y, pol, normalised, p_derivative);
  1290. BOOST_MATH_INSTRUMENT_VARIABLE(fract);
  1291. }
  1292. }
  1293. else
  1294. {
  1295. fract = ibeta_fraction2(a, b, x, y, pol, normalised, p_derivative);
  1296. BOOST_MATH_INSTRUMENT_VARIABLE(fract);
  1297. }
  1298. }
  1299. if(p_derivative)
  1300. {
  1301. if(*p_derivative < 0)
  1302. {
  1303. *p_derivative = ibeta_power_terms(a, b, x, y, lanczos_type(), true, pol);
  1304. }
  1305. T div = y * x;
  1306. if(*p_derivative != 0)
  1307. {
  1308. if((tools::max_value<T>() * div < *p_derivative))
  1309. {
  1310. // overflow, return an arbitrarily large value:
  1311. *p_derivative = tools::max_value<T>() / 2;
  1312. }
  1313. else
  1314. {
  1315. *p_derivative /= div;
  1316. }
  1317. }
  1318. }
  1319. return invert ? (normalised ? 1 : boost::math::beta(a, b, pol)) - fract : fract;
  1320. } // template <class T, class Lanczos>T ibeta_imp(T a, T b, T x, const Lanczos& l, bool inv, bool normalised)
  1321. template <class T, class Policy>
  1322. inline T ibeta_imp(T a, T b, T x, const Policy& pol, bool inv, bool normalised)
  1323. {
  1324. return ibeta_imp(a, b, x, pol, inv, normalised, static_cast<T*>(nullptr));
  1325. }
  1326. template <class T, class Policy>
  1327. T ibeta_derivative_imp(T a, T b, T x, const Policy& pol)
  1328. {
  1329. static const char* function = "ibeta_derivative<%1%>(%1%,%1%,%1%)";
  1330. //
  1331. // start with the usual error checks:
  1332. //
  1333. if (!(boost::math::isfinite)(a))
  1334. return policies::raise_domain_error<T>(function, "The argument a to the incomplete beta function must be >= zero (got a=%1%).", a, pol);
  1335. if (!(boost::math::isfinite)(b))
  1336. return policies::raise_domain_error<T>(function, "The argument b to the incomplete beta function must be >= zero (got b=%1%).", b, pol);
  1337. if (!(boost::math::isfinite)(x))
  1338. return policies::raise_domain_error<T>(function, "The argument x to the incomplete beta function must be in [0,1] (got x=%1%).", x, pol);
  1339. if(a <= 0)
  1340. return policies::raise_domain_error<T>(function, "The argument a to the incomplete beta function must be greater than zero (got a=%1%).", a, pol);
  1341. if(b <= 0)
  1342. return policies::raise_domain_error<T>(function, "The argument b to the incomplete beta function must be greater than zero (got b=%1%).", b, pol);
  1343. if((x < 0) || (x > 1))
  1344. return policies::raise_domain_error<T>(function, "Parameter x outside the range [0,1] in the incomplete beta function (got x=%1%).", x, pol);
  1345. //
  1346. // Now the corner cases:
  1347. //
  1348. if(x == 0)
  1349. {
  1350. return (a > 1) ? 0 :
  1351. (a == 1) ? 1 / boost::math::beta(a, b, pol) : policies::raise_overflow_error<T>(function, nullptr, pol);
  1352. }
  1353. else if(x == 1)
  1354. {
  1355. return (b > 1) ? 0 :
  1356. (b == 1) ? 1 / boost::math::beta(a, b, pol) : policies::raise_overflow_error<T>(function, nullptr, pol);
  1357. }
  1358. //
  1359. // Now the regular cases:
  1360. //
  1361. typedef typename lanczos::lanczos<T, Policy>::type lanczos_type;
  1362. T y = (1 - x) * x;
  1363. T f1 = ibeta_power_terms<T>(a, b, x, 1 - x, lanczos_type(), true, pol, 1 / y, function);
  1364. return f1;
  1365. }
  1366. //
  1367. // Some forwarding functions that disambiguate the third argument type:
  1368. //
  1369. template <class RT1, class RT2, class Policy>
  1370. inline typename tools::promote_args<RT1, RT2>::type
  1371. beta(RT1 a, RT2 b, const Policy&, const std::true_type*)
  1372. {
  1373. BOOST_FPU_EXCEPTION_GUARD
  1374. typedef typename tools::promote_args<RT1, RT2>::type result_type;
  1375. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  1376. typedef typename lanczos::lanczos<value_type, Policy>::type evaluation_type;
  1377. typedef typename policies::normalise<
  1378. Policy,
  1379. policies::promote_float<false>,
  1380. policies::promote_double<false>,
  1381. policies::discrete_quantile<>,
  1382. policies::assert_undefined<> >::type forwarding_policy;
  1383. return policies::checked_narrowing_cast<result_type, forwarding_policy>(detail::beta_imp(static_cast<value_type>(a), static_cast<value_type>(b), evaluation_type(), forwarding_policy()), "boost::math::beta<%1%>(%1%,%1%)");
  1384. }
  1385. template <class RT1, class RT2, class RT3>
  1386. inline typename tools::promote_args<RT1, RT2, RT3>::type
  1387. beta(RT1 a, RT2 b, RT3 x, const std::false_type*)
  1388. {
  1389. return boost::math::beta(a, b, x, policies::policy<>());
  1390. }
  1391. } // namespace detail
  1392. //
  1393. // The actual function entry-points now follow, these just figure out
  1394. // which Lanczos approximation to use
  1395. // and forward to the implementation functions:
  1396. //
  1397. template <class RT1, class RT2, class A>
  1398. inline typename tools::promote_args<RT1, RT2, A>::type
  1399. beta(RT1 a, RT2 b, A arg)
  1400. {
  1401. typedef typename policies::is_policy<A>::type tag;
  1402. return boost::math::detail::beta(a, b, arg, static_cast<tag*>(nullptr));
  1403. }
  1404. template <class RT1, class RT2>
  1405. inline typename tools::promote_args<RT1, RT2>::type
  1406. beta(RT1 a, RT2 b)
  1407. {
  1408. return boost::math::beta(a, b, policies::policy<>());
  1409. }
  1410. template <class RT1, class RT2, class RT3, class Policy>
  1411. inline typename tools::promote_args<RT1, RT2, RT3>::type
  1412. beta(RT1 a, RT2 b, RT3 x, const Policy&)
  1413. {
  1414. BOOST_FPU_EXCEPTION_GUARD
  1415. typedef typename tools::promote_args<RT1, RT2, RT3>::type result_type;
  1416. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  1417. typedef typename policies::normalise<
  1418. Policy,
  1419. policies::promote_float<false>,
  1420. policies::promote_double<false>,
  1421. policies::discrete_quantile<>,
  1422. policies::assert_undefined<> >::type forwarding_policy;
  1423. return policies::checked_narrowing_cast<result_type, forwarding_policy>(detail::ibeta_imp(static_cast<value_type>(a), static_cast<value_type>(b), static_cast<value_type>(x), forwarding_policy(), false, false), "boost::math::beta<%1%>(%1%,%1%,%1%)");
  1424. }
  1425. template <class RT1, class RT2, class RT3, class Policy>
  1426. inline typename tools::promote_args<RT1, RT2, RT3>::type
  1427. betac(RT1 a, RT2 b, RT3 x, const Policy&)
  1428. {
  1429. BOOST_FPU_EXCEPTION_GUARD
  1430. typedef typename tools::promote_args<RT1, RT2, RT3>::type result_type;
  1431. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  1432. typedef typename policies::normalise<
  1433. Policy,
  1434. policies::promote_float<false>,
  1435. policies::promote_double<false>,
  1436. policies::discrete_quantile<>,
  1437. policies::assert_undefined<> >::type forwarding_policy;
  1438. return policies::checked_narrowing_cast<result_type, forwarding_policy>(detail::ibeta_imp(static_cast<value_type>(a), static_cast<value_type>(b), static_cast<value_type>(x), forwarding_policy(), true, false), "boost::math::betac<%1%>(%1%,%1%,%1%)");
  1439. }
  1440. template <class RT1, class RT2, class RT3>
  1441. inline typename tools::promote_args<RT1, RT2, RT3>::type
  1442. betac(RT1 a, RT2 b, RT3 x)
  1443. {
  1444. return boost::math::betac(a, b, x, policies::policy<>());
  1445. }
  1446. template <class RT1, class RT2, class RT3, class Policy>
  1447. inline typename tools::promote_args<RT1, RT2, RT3>::type
  1448. ibeta(RT1 a, RT2 b, RT3 x, const Policy&)
  1449. {
  1450. BOOST_FPU_EXCEPTION_GUARD
  1451. typedef typename tools::promote_args<RT1, RT2, RT3>::type result_type;
  1452. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  1453. typedef typename policies::normalise<
  1454. Policy,
  1455. policies::promote_float<false>,
  1456. policies::promote_double<false>,
  1457. policies::discrete_quantile<>,
  1458. policies::assert_undefined<> >::type forwarding_policy;
  1459. return policies::checked_narrowing_cast<result_type, forwarding_policy>(detail::ibeta_imp(static_cast<value_type>(a), static_cast<value_type>(b), static_cast<value_type>(x), forwarding_policy(), false, true), "boost::math::ibeta<%1%>(%1%,%1%,%1%)");
  1460. }
  1461. template <class RT1, class RT2, class RT3>
  1462. inline typename tools::promote_args<RT1, RT2, RT3>::type
  1463. ibeta(RT1 a, RT2 b, RT3 x)
  1464. {
  1465. return boost::math::ibeta(a, b, x, policies::policy<>());
  1466. }
  1467. template <class RT1, class RT2, class RT3, class Policy>
  1468. inline typename tools::promote_args<RT1, RT2, RT3>::type
  1469. ibetac(RT1 a, RT2 b, RT3 x, const Policy&)
  1470. {
  1471. BOOST_FPU_EXCEPTION_GUARD
  1472. typedef typename tools::promote_args<RT1, RT2, RT3>::type result_type;
  1473. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  1474. typedef typename policies::normalise<
  1475. Policy,
  1476. policies::promote_float<false>,
  1477. policies::promote_double<false>,
  1478. policies::discrete_quantile<>,
  1479. policies::assert_undefined<> >::type forwarding_policy;
  1480. return policies::checked_narrowing_cast<result_type, forwarding_policy>(detail::ibeta_imp(static_cast<value_type>(a), static_cast<value_type>(b), static_cast<value_type>(x), forwarding_policy(), true, true), "boost::math::ibetac<%1%>(%1%,%1%,%1%)");
  1481. }
  1482. template <class RT1, class RT2, class RT3>
  1483. inline typename tools::promote_args<RT1, RT2, RT3>::type
  1484. ibetac(RT1 a, RT2 b, RT3 x)
  1485. {
  1486. return boost::math::ibetac(a, b, x, policies::policy<>());
  1487. }
  1488. template <class RT1, class RT2, class RT3, class Policy>
  1489. inline typename tools::promote_args<RT1, RT2, RT3>::type
  1490. ibeta_derivative(RT1 a, RT2 b, RT3 x, const Policy&)
  1491. {
  1492. BOOST_FPU_EXCEPTION_GUARD
  1493. typedef typename tools::promote_args<RT1, RT2, RT3>::type result_type;
  1494. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  1495. typedef typename policies::normalise<
  1496. Policy,
  1497. policies::promote_float<false>,
  1498. policies::promote_double<false>,
  1499. policies::discrete_quantile<>,
  1500. policies::assert_undefined<> >::type forwarding_policy;
  1501. return policies::checked_narrowing_cast<result_type, forwarding_policy>(detail::ibeta_derivative_imp(static_cast<value_type>(a), static_cast<value_type>(b), static_cast<value_type>(x), forwarding_policy()), "boost::math::ibeta_derivative<%1%>(%1%,%1%,%1%)");
  1502. }
  1503. template <class RT1, class RT2, class RT3>
  1504. inline typename tools::promote_args<RT1, RT2, RT3>::type
  1505. ibeta_derivative(RT1 a, RT2 b, RT3 x)
  1506. {
  1507. return boost::math::ibeta_derivative(a, b, x, policies::policy<>());
  1508. }
  1509. } // namespace math
  1510. } // namespace boost
  1511. #include <boost/math/special_functions/detail/ibeta_inverse.hpp>
  1512. #include <boost/math/special_functions/detail/ibeta_inv_ab.hpp>
  1513. #endif // BOOST_MATH_SPECIAL_BETA_HPP