ibeta.qbk 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. [section:ibeta_function Incomplete Beta Functions]
  2. [h4 Synopsis]
  3. ``
  4. #include <boost/math/special_functions/beta.hpp>
  5. ``
  6. namespace boost{ namespace math{
  7. template <class T1, class T2, class T3>
  8. ``__sf_result`` ibeta(T1 a, T2 b, T3 x);
  9. template <class T1, class T2, class T3, class ``__Policy``>
  10. ``__sf_result`` ibeta(T1 a, T2 b, T3 x, const ``__Policy``&);
  11. template <class T1, class T2, class T3>
  12. ``__sf_result`` ibetac(T1 a, T2 b, T3 x);
  13. template <class T1, class T2, class T3, class ``__Policy``>
  14. ``__sf_result`` ibetac(T1 a, T2 b, T3 x, const ``__Policy``&);
  15. template <class T1, class T2, class T3>
  16. ``__sf_result`` beta(T1 a, T2 b, T3 x);
  17. template <class T1, class T2, class T3, class ``__Policy``>
  18. ``__sf_result`` beta(T1 a, T2 b, T3 x, const ``__Policy``&);
  19. template <class T1, class T2, class T3>
  20. ``__sf_result`` betac(T1 a, T2 b, T3 x);
  21. template <class T1, class T2, class T3, class ``__Policy``>
  22. ``__sf_result`` betac(T1 a, T2 b, T3 x, const ``__Policy``&);
  23. }} // namespaces
  24. [h4 Description]
  25. There are four [@http://en.wikipedia.org/wiki/Incomplete_beta_function incomplete beta functions]
  26. : two are normalised versions (also known as ['regularized] beta functions)
  27. that return values in the range [0, 1], and two are non-normalised and
  28. return values in the range [0, __beta(a, b)]. Users interested in statistical
  29. applications should use the normalised (or
  30. [@http://mathworld.wolfram.com/RegularizedBetaFunction.html regularized]
  31. ) versions (ibeta and ibetac).
  32. All of these functions require /0 <= x <= 1/.
  33. The normalized functions __ibeta and __ibetac require /a,b >= 0/, and in addition that
  34. not both /a/ and /b/ are zero.
  35. The functions __beta and __betac require /a,b > 0/.
  36. The return type of these functions is computed using the __arg_promotion_rules
  37. when T1, T2 and T3 are different types.
  38. [optional_policy]
  39. template <class T1, class T2, class T3>
  40. ``__sf_result`` ibeta(T1 a, T2 b, T3 x);
  41. template <class T1, class T2, class T3, class ``__Policy``>
  42. ``__sf_result`` ibeta(T1 a, T2 b, T3 x, const ``__Policy``&);
  43. Returns the normalised incomplete beta function of a, b and x:
  44. [equation ibeta3]
  45. [graph ibeta]
  46. template <class T1, class T2, class T3>
  47. ``__sf_result`` ibetac(T1 a, T2 b, T3 x);
  48. template <class T1, class T2, class T3, class ``__Policy``>
  49. ``__sf_result`` ibetac(T1 a, T2 b, T3 x, const ``__Policy``&);
  50. Returns the normalised complement of the incomplete beta function of a, b and x:
  51. [equation ibeta4]
  52. template <class T1, class T2, class T3>
  53. ``__sf_result`` beta(T1 a, T2 b, T3 x);
  54. template <class T1, class T2, class T3, class ``__Policy``>
  55. ``__sf_result`` beta(T1 a, T2 b, T3 x, const ``__Policy``&);
  56. Returns the full (non-normalised) incomplete beta function of a, b and x:
  57. [equation ibeta1]
  58. template <class T1, class T2, class T3>
  59. ``__sf_result`` betac(T1 a, T2 b, T3 x);
  60. template <class T1, class T2, class T3, class ``__Policy``>
  61. ``__sf_result`` betac(T1 a, T2 b, T3 x, const ``__Policy``&);
  62. Returns the full (non-normalised) complement of the incomplete beta function of a, b and x:
  63. [equation ibeta2]
  64. [h4 Accuracy]
  65. The following tables give peak and mean relative errors in over various domains of
  66. a, b and x, along with comparisons to the __gsl and __cephes libraries.
  67. Note that only results for the widest floating-point type on the system are given as
  68. narrower types have __zero_error.
  69. Note that the results for 80 and 128-bit long doubles are noticeably higher than
  70. for doubles: this is because the wider exponent range of these types allow
  71. more extreme test cases to be tested. For example expected results that
  72. are zero at double precision, may be finite but exceptionally small with
  73. the wider exponent range of the long double types.
  74. [table_ibeta]
  75. [table_ibetac]
  76. [table_beta_incomplete_]
  77. [table_betac]
  78. [h4 Testing]
  79. There are two sets of tests: spot tests compare values taken from
  80. [@http://functions.wolfram.com/webMathematica/FunctionEvaluation.jsp?name=BetaRegularized Mathworld's online function evaluator]
  81. with this implementation: they provide a basic "sanity check"
  82. for the implementation, with one spot-test in each implementation-domain
  83. (see implementation notes below).
  84. Accuracy tests use data generated at very high precision
  85. (with [@http://shoup.net/ntl/doc/RR.txt NTL RR class] set at 1000-bit precision),
  86. using the "textbook" continued fraction representation (refer to the first continued
  87. fraction in the implementation discussion below).
  88. Note that this continued fraction is /not/ used in the implementation,
  89. and therefore we have test data that is fully independent of the code.
  90. [h4 Implementation]
  91. This implementation is closely based upon
  92. [@http://portal.acm.org/citation.cfm?doid=131766.131776 "Algorithm 708; Significant digit computation of the incomplete beta function ratios", DiDonato and Morris, ACM, 1992.]
  93. All four of these functions share a common implementation: this is passed both
  94. x and y, and can return either p or q where these are related by:
  95. [equation ibeta_inv5]
  96. so at any point we can swap a for b, x for y and p for q if this results in
  97. a more favourable position. Generally such swaps are performed so that we always
  98. compute a value less than 0.9: when required this can then be subtracted from 1
  99. without undue cancellation error.
  100. The following continued fraction representation is found in many textbooks
  101. but is not used in this implementation - it's both slower and less accurate than
  102. the alternatives - however it is used to generate test data:
  103. [equation ibeta5]
  104. The following continued fraction is due to [@http://portal.acm.org/citation.cfm?doid=131766.131776 Didonato and Morris],
  105. and is used in this implementation when a and b are both greater than 1:
  106. [equation ibeta6]
  107. For smallish b and x then a series representation can be used:
  108. [equation ibeta7]
  109. When b << a then the transition from 0 to 1 occurs very close to x = 1
  110. and some care has to be taken over the method of computation, in that case
  111. the following series representation is used:
  112. [equation ibeta8]
  113. [/[equation ibeta9]]
  114. Where Q(a,x) is an [@http://functions.wolfram.com/GammaBetaErf/Gamma2/ incomplete gamma function].
  115. Note that this method relies
  116. on keeping a table of all the p[sub n ] previously computed, which does limit
  117. the precision of the method, depending upon the size of the table used.
  118. When /a/ and /b/ are both small integers, then we can relate the incomplete
  119. beta to the binomial distribution and use the following finite sum:
  120. [equation ibeta12]
  121. Finally we can sidestep difficult areas, or move to an area with a more
  122. efficient means of computation, by using the duplication formulae:
  123. [equation ibeta10]
  124. [equation ibeta11]
  125. The domains of a, b and x for which the various methods are used are identical
  126. to those described in the
  127. [@http://portal.acm.org/citation.cfm?doid=131766.131776 Didonato and Morris TOMS 708 paper].
  128. [endsect][/section:ibeta_function The Incomplete Beta Function]
  129. [/
  130. Copyright 2006 John Maddock and Paul A. Bristow.
  131. Distributed under the Boost Software License, Version 1.0.
  132. (See accompanying file LICENSE_1_0.txt or copy at
  133. http://www.boost.org/LICENSE_1_0.txt).
  134. ]