beta.hpp 51 KB

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