igamma_inv.qbk 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. [section:igamma_inv Incomplete Gamma Function Inverses]
  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_q_inv(T1 a, T2 q);
  9. template <class T1, class T2, class ``__Policy``>
  10. ``__sf_result`` gamma_q_inv(T1 a, T2 q, const ``__Policy``&);
  11. template <class T1, class T2>
  12. ``__sf_result`` gamma_p_inv(T1 a, T2 p);
  13. template <class T1, class T2, class ``__Policy``>
  14. ``__sf_result`` gamma_p_inv(T1 a, T2 p, const ``__Policy``&);
  15. template <class T1, class T2>
  16. ``__sf_result`` gamma_q_inva(T1 x, T2 q);
  17. template <class T1, class T2, class ``__Policy``>
  18. ``__sf_result`` gamma_q_inva(T1 x, T2 q, const ``__Policy``&);
  19. template <class T1, class T2>
  20. ``__sf_result`` gamma_p_inva(T1 x, T2 p);
  21. template <class T1, class T2, class ``__Policy``>
  22. ``__sf_result`` gamma_p_inva(T1 x, T2 p, const ``__Policy``&);
  23. }} // namespaces
  24. [h4 Description]
  25. There are four [@http://mathworld.wolfram.com/IncompleteGammaFunction.html incomplete gamma function]
  26. inverses which either compute
  27. /x/ given /a/ and /p/ or /q/,
  28. or else compute /a/ given /x/ and either /p/ or /q/.
  29. The return type of these functions is computed using the __arg_promotion_rules
  30. when T1 and T2 are different types, otherwise the return type is simply T1.
  31. [optional_policy]
  32. [tip When people normally talk about the inverse of the incomplete
  33. gamma function, they are talking about inverting on parameter /x/.
  34. These are implemented here as `gamma_p_inv` and `gamma_q_inv`, and are by
  35. far the most efficient of the inverses presented here.
  36. The inverse on the /a/ parameter finds use in some statistical
  37. applications but has to be computed by rather brute force numerical
  38. techniques and is consequently several times slower.
  39. These are implemented here as `gamma_p_inva` and `gamma_q_inva`.]
  40. template <class T1, class T2>
  41. ``__sf_result`` gamma_q_inv(T1 a, T2 q);
  42. template <class T1, class T2, class ``__Policy``>
  43. ``__sf_result`` gamma_q_inv(T1 a, T2 q, const ``__Policy``&);
  44. Returns a value x such that: `q = gamma_q(a, x);`
  45. Requires: /a > 0/ and /1 >= p,q >= 0/.
  46. template <class T1, class T2>
  47. ``__sf_result`` gamma_p_inv(T1 a, T2 p);
  48. template <class T1, class T2, class ``__Policy``>
  49. ``__sf_result`` gamma_p_inv(T1 a, T2 p, const ``__Policy``&);
  50. Returns a value x such that: `p = gamma_p(a, x);`
  51. Requires: /a > 0/ and /1 >= p,q >= 0/.
  52. template <class T1, class T2>
  53. ``__sf_result`` gamma_q_inva(T1 x, T2 q);
  54. template <class T1, class T2, class ``__Policy``>
  55. ``__sf_result`` gamma_q_inva(T1 x, T2 q, const ``__Policy``&);
  56. Returns a value a such that: `q = gamma_q(a, x);`
  57. Requires: /x > 0/ and /1 >= p,q >= 0/.
  58. template <class T1, class T2>
  59. ``__sf_result`` gamma_p_inva(T1 x, T2 p);
  60. template <class T1, class T2, class ``__Policy``>
  61. ``__sf_result`` gamma_p_inva(T1 x, T2 p, const ``__Policy``&);
  62. Returns a value a such that: `p = gamma_p(a, x);`
  63. Requires: /x > 0/ and /1 >= p,q >= 0/.
  64. [h4 Accuracy]
  65. The accuracy of these functions doesn't vary much by platform or by
  66. the type T. Given that these functions are computed by iterative methods,
  67. they are deliberately "detuned" so as not to be too accurate: it is in
  68. any case impossible for these function to be more accurate than the
  69. regular forward incomplete gamma functions. In practice, the accuracy
  70. of these functions is very similar to that of __gamma_p and __gamma_q
  71. functions:
  72. [table_gamma_p_inv]
  73. [table_gamma_q_inv]
  74. [table_gamma_p_inva]
  75. [table_gamma_q_inva]
  76. [h4 Testing]
  77. There are two sets of tests:
  78. * Basic sanity checks attempt to "round-trip" from
  79. /a/ and /x/ to /p/ or /q/ and back again. These tests have quite
  80. generous tolerances: in general both the incomplete gamma, and its
  81. inverses, change so rapidly that round tripping to more than a couple
  82. of significant digits isn't possible. This is especially true when
  83. /p/ or /q/ is very near one: in this case there isn't enough
  84. "information content" in the input to the inverse function to get
  85. back where you started.
  86. * Accuracy checks using high precision test values. These measure
  87. the accuracy of the result, given exact input values.
  88. [h4 Implementation]
  89. The functions `gamma_p_inv` and [@http://functions.wolfram.com/GammaBetaErf/InverseGammaRegularized/ `gamma_q_inv`]
  90. share a common implementation.
  91. First an initial approximation is computed using the methodology described
  92. in:
  93. [@http://portal.acm.org/citation.cfm?id=23109&coll=portal&dl=ACM
  94. A. R. Didonato and A. H. Morris, Computation of the Incomplete Gamma
  95. Function Ratios and their Inverse, ACM Trans. Math. Software 12 (1986), 377-393.]
  96. Finally, the last few bits are cleaned up using Halley iteration, the iteration
  97. limit is set to 2/3 of the number of bits in T, which by experiment is
  98. sufficient to ensure that the inverses are at least as accurate as the normal
  99. incomplete gamma functions. In testing, no more than 3 iterations are required
  100. to produce a result as accurate as the forward incomplete gamma function, and
  101. in many cases only one iteration is required.
  102. The functions `gamma_p_inva` and `gamma_q_inva` also share a common implementation
  103. but are handled separately from `gamma_p_inv` and `gamma_q_inv`.
  104. An initial approximation for /a/ is computed very crudely so that
  105. /gamma_p(a, x) ~ 0.5/, this value is then used as a starting point
  106. for a generic derivative-free root finding algorithm. As a consequence,
  107. these two functions are rather more expensive to compute than the
  108. `gamma_p_inv` or `gamma_q_inv` functions. Even so, the root is usually found
  109. in fewer than 10 iterations.
  110. [endsect] [/section The Incomplete Gamma Function Inverses]
  111. [/
  112. Copyright 2006 John Maddock and Paul A. Bristow.
  113. Distributed under the Boost Software License, Version 1.0.
  114. (See accompanying file LICENSE_1_0.txt or copy at
  115. http://www.boost.org/LICENSE_1_0.txt).
  116. ]