beta.hpp 59 KB

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