igamma.qbk 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. [section:igamma Incomplete Gamma Functions]
  2. [h4 Synopsis]
  3. ``
  4. #include <boost/math/special_functions/gamma.hpp>
  5. ``
  6. namespace boost{ namespace math{
  7. template <class T1, class T2>
  8. ``__sf_result`` gamma_p(T1 a, T2 z);
  9. template <class T1, class T2, class ``__Policy``>
  10. ``__sf_result`` gamma_p(T1 a, T2 z, const ``__Policy``&);
  11. template <class T1, class T2>
  12. ``__sf_result`` gamma_q(T1 a, T2 z);
  13. template <class T1, class T2, class ``__Policy``>
  14. ``__sf_result`` gamma_q(T1 a, T2 z, const ``__Policy``&);
  15. template <class T1, class T2>
  16. ``__sf_result`` tgamma_lower(T1 a, T2 z);
  17. template <class T1, class T2, class ``__Policy``>
  18. ``__sf_result`` tgamma_lower(T1 a, T2 z, const ``__Policy``&);
  19. template <class T1, class T2>
  20. ``__sf_result`` tgamma(T1 a, T2 z);
  21. template <class T1, class T2, class ``__Policy``>
  22. ``__sf_result`` tgamma(T1 a, T2 z, const ``__Policy``&);
  23. }} // namespaces
  24. [h4 Description]
  25. There are four [@http://mathworld.wolfram.com/IncompleteGammaFunction.html
  26. incomplete gamma functions]:
  27. two are normalised versions (also known as /regularized/ incomplete gamma functions)
  28. that return values in the range [0, 1], and two are non-normalised and
  29. return values in the range [0, [Gamma](a)]. Users interested in statistical
  30. applications should use the
  31. [@http://mathworld.wolfram.com/RegularizedGammaFunction.html normalised versions (`gamma_p` and `gamma_q`)].
  32. All of these functions require /a > 0/ and /z >= 0/, otherwise they return
  33. the result of __domain_error.
  34. [optional_policy]
  35. The return type of these functions is computed using the __arg_promotion_rules
  36. when T1 and T2 are different types, otherwise the return type is simply T1.
  37. template <class T1, class T2>
  38. ``__sf_result`` gamma_p(T1 a, T2 z);
  39. template <class T1, class T2, class Policy>
  40. ``__sf_result`` gamma_p(T1 a, T2 z, const ``__Policy``&);
  41. Returns the normalised lower incomplete gamma function of a and z:
  42. [equation igamma4]
  43. This function changes rapidly from 0 to 1 around the point z == a:
  44. [graph gamma_p]
  45. template <class T1, class T2>
  46. ``__sf_result`` gamma_q(T1 a, T2 z);
  47. template <class T1, class T2, class ``__Policy``>
  48. ``__sf_result`` gamma_q(T1 a, T2 z, const ``__Policy``&);
  49. Returns the normalised upper incomplete gamma function of a and z:
  50. [equation igamma3]
  51. This function changes rapidly from 1 to 0 around the point z == a:
  52. [graph gamma_q]
  53. template <class T1, class T2>
  54. ``__sf_result`` tgamma_lower(T1 a, T2 z);
  55. template <class T1, class T2, class ``__Policy``>
  56. ``__sf_result`` tgamma_lower(T1 a, T2 z, const ``__Policy``&);
  57. Returns the full (non-normalised) lower incomplete gamma function of a and z:
  58. [equation igamma2]
  59. template <class T1, class T2>
  60. ``__sf_result`` tgamma(T1 a, T2 z);
  61. template <class T1, class T2, class ``__Policy``>
  62. ``__sf_result`` tgamma(T1 a, T2 z, const ``__Policy``&);
  63. Returns the full (non-normalised) upper incomplete gamma function of a and z:
  64. [equation igamma1]
  65. [h4 Accuracy]
  66. The following tables give peak and mean relative errors in over various domains of
  67. a and z, along with comparisons to the __gsl and __cephes libraries.
  68. Note that only results for the widest floating-point type on the system are given as
  69. narrower types have __zero_error.
  70. Note that errors grow as /a/ grows larger.
  71. Note also that the higher error rates for the 80 and 128 bit
  72. long double results are somewhat misleading: expected results that are
  73. zero at 64-bit double precision may be non-zero - but exceptionally small -
  74. with the larger exponent range of a long double. These results therefore
  75. reflect the more extreme nature of the tests conducted for these types.
  76. All values are in units of epsilon.
  77. [table_gamma_p]
  78. [table_gamma_q]
  79. [table_tgamma_lower]
  80. [table_tgamma_incomplete_]
  81. [h4 Testing]
  82. There are two sets of tests: spot tests compare values taken from
  83. [@http://functions.wolfram.com/GammaBetaErf/ Mathworld's online evaluator]
  84. with this implementation to perform a basic "sanity check".
  85. Accuracy tests use data generated at very high precision
  86. (using NTL's RR class set at 1000-bit precision) using this implementation
  87. with a very high precision 60-term __lanczos, and some but not all of the special
  88. case handling disabled.
  89. This is less than satisfactory: an independent method should really be used,
  90. but apparently a complete lack of such methods are available. We can't even use a deliberately
  91. naive implementation without special case handling since Legendre's continued fraction
  92. (see below) is unstable for small a and z.
  93. [h4 Implementation]
  94. These four functions share a common implementation since
  95. they are all related via:
  96. 1) [equation igamma5]
  97. 2) [equation igamma6]
  98. 3) [equation igamma7]
  99. The lower incomplete gamma is computed from its series representation:
  100. 4) [equation igamma8]
  101. Or by subtraction of the upper integral from either [Gamma](a) or 1
  102. when /x - (1/(3x)) > a and x > 1.1/.
  103. The upper integral is computed from Legendre's continued fraction representation:
  104. 5) [equation igamma9]
  105. When /(x > 1.1)/ or by subtraction of the lower integral from either [Gamma](a) or 1
  106. when /x - (1/(3x)) < a/.
  107. For /x < 1.1/ computation of the upper integral is more complex as the continued
  108. fraction representation is unstable in this area. However there is another
  109. series representation for the lower integral:
  110. 6) [equation igamma10]
  111. That lends itself to calculation of the upper integral via rearrangement
  112. to:
  113. 7) [equation igamma11]
  114. Refer to the documentation for __powm1 and __tgamma1pm1 for details
  115. of their implementation.
  116. For /x < 1.1/ the crossover point where the result is ~0.5 no longer
  117. occurs for /x ~ y/. Using /x * 0.75 < a/ as the crossover criterion
  118. for /0.5 < x <= 1.1/ keeps the maximum value computed (whether
  119. it's the upper or lower interval) to around 0.75. Likewise for
  120. /x <= 0.5/ then using /-0.4 / log(x) < a/ as the crossover criterion
  121. keeps the maximum value computed to around 0.7
  122. (whether it's the upper or lower interval).
  123. There are two special cases used when a is an integer or half integer,
  124. and the crossover conditions listed above indicate that we should compute
  125. the upper integral Q.
  126. If a is an integer in the range /1 <= a < 30/ then the following
  127. finite sum is used:
  128. 9) [equation igamma1f]
  129. While for half-integers in the range /0.5 <= a < 30/ then the
  130. following finite sum is used:
  131. 10) [equation igamma2f]
  132. These are both more stable and more efficient than the continued fraction
  133. alternative.
  134. When the argument /a/ is large, and /x ~ a/ then the series (4) and continued
  135. fraction (5) above are very slow to converge. In this area an expansion due to
  136. Temme is used:
  137. 11) [equation igamma16]
  138. 12) [equation igamma17]
  139. 13) [equation igamma18]
  140. 14) [equation igamma19]
  141. The double sum is truncated to a fixed number of terms - to give a specific
  142. target precision - and evaluated as a polynomial-of-polynomials. There are
  143. versions for up to 128-bit long double precision: types requiring
  144. greater precision than that do not use these expansions. The
  145. coefficients C[sub k][super n] are computed in advance using the recurrence
  146. relations given by Temme. The zone where these expansions are used is
  147. (a > 20) && (a < 200) && fabs(x-a)/a < 0.4
  148. And:
  149. (a > 200) && (fabs(x-a)/a < 4.5/sqrt(a))
  150. The latter range is valid for all types up to 128-bit long doubles, and
  151. is designed to ensure that the result is larger than 10[super -6], the
  152. first range is used only for types up to 80-bit long doubles. These
  153. domains are narrower than the ones recommended by either Temme or Didonato
  154. and Morris. However, using a wider range results in large and inexact
  155. (i.e. computed) values being passed to the `exp` and `erfc` functions
  156. resulting in significantly larger error rates. In other words there is a
  157. fine trade off here between efficiency and error. The current limits should
  158. keep the number of terms required by (4) and (5) to no more than ~20
  159. at double precision.
  160. For the normalised incomplete gamma functions, calculation of the
  161. leading power terms is central to the accuracy of the function.
  162. For smallish a and x combining
  163. the power terms with the __lanczos gives the greatest accuracy:
  164. 15) [equation igamma12]
  165. In the event that this causes underflow/overflow then the exponent can
  166. be reduced by a factor of /a/ and brought inside the power term.
  167. When a and x are large, we end up with a very large exponent with a base
  168. near one: this will not be computed accurately via the pow function,
  169. and taking logs simply leads to cancellation errors. The worst of the
  170. errors can be avoided by using:
  171. 16) [equation igamma13]
  172. when /a-x/ is small and a and x are large. There is still a subtraction
  173. and therefore some cancellation errors - but the terms are small so the absolute
  174. error will be small - and it is absolute rather than relative error that
  175. counts in the argument to the /exp/ function. Note that for sufficiently
  176. large a and x the errors will still get you eventually, although this does
  177. delay the inevitable much longer than other methods. Use of /log(1+x)-x/ here
  178. is inspired by Temme (see references below).
  179. [h4 References]
  180. * N. M. Temme, A Set of Algorithms for the Incomplete Gamma Functions,
  181. Probability in the Engineering and Informational Sciences, 8, 1994.
  182. * N. M. Temme, The Asymptotic Expansion of the Incomplete Gamma Functions,
  183. Siam J. Math Anal. Vol 10 No 4, July 1979, p757.
  184. * A. R. Didonato and A. H. Morris, Computation of the Incomplete Gamma
  185. Function Ratios and their Inverse. ACM TOMS, Vol 12, No 4, Dec 1986, p377.
  186. * W. Gautschi, The Incomplete Gamma Functions Since Tricomi, In Tricomi's Ideas
  187. and Contemporary Applied Mathematics, Atti dei Convegni Lincei, n. 147,
  188. Accademia Nazionale dei Lincei, Roma, 1998, pp. 203--237.
  189. [@http://citeseer.ist.psu.edu/gautschi98incomplete.html http://citeseer.ist.psu.edu/gautschi98incomplete.html]
  190. [endsect] [/section:igamma The Incomplete Gamma Function]
  191. [/
  192. Copyright 2006 John Maddock and Paul A. Bristow.
  193. Distributed under the Boost Software License, Version 1.0.
  194. (See accompanying file LICENSE_1_0.txt or copy at
  195. http://www.boost.org/LICENSE_1_0.txt).
  196. ]