beta.hpp 51 KB

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