gsl_cdf__betainv.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* cdf/betainv.c
  2. *
  3. * Copyright (C) 2004 Free Software Foundation, Inc.
  4. * Copyright (C) 2006, 2007 Brian Gough
  5. * Written by Jason H. Stover.
  6. * Modified for GSL by Brian Gough
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. /*
  23. * Invert the Beta distribution.
  24. *
  25. * References:
  26. *
  27. * Roger W. Abernathy and Robert P. Smith. "Applying Series Expansion
  28. * to the Inverse Beta Distribution to Find Percentiles of the
  29. * F-Distribution," ACM Transactions on Mathematical Software, volume
  30. * 19, number 4, December 1993, pages 474-480.
  31. *
  32. * G.W. Hill and A.W. Davis. "Generalized asymptotic expansions of a
  33. * Cornish-Fisher type," Annals of Mathematical Statistics, volume 39,
  34. * number 8, August 1968, pages 1264-1273.
  35. */
  36. #include "gsl__config.h"
  37. #include <math.h>
  38. #include "gsl_math.h"
  39. #include "gsl_errno.h"
  40. #include "gsl_sf_gamma.h"
  41. #include "gsl_cdf.h"
  42. #include "gsl_randist.h"
  43. #include "gsl_cdf__error.h"
  44. static double
  45. bisect (double x, double P, double a, double b, double xtol, double Ptol)
  46. {
  47. double x0 = 0, x1 = 1, Px;
  48. while (fabs(x1 - x0) > xtol) {
  49. Px = gsl_cdf_beta_P (x, a, b);
  50. if (fabs(Px - P) < Ptol) {
  51. /* return as soon as approximation is good enough, including on
  52. the first iteration */
  53. return x;
  54. } else if (Px < P) {
  55. x0 = x;
  56. } else if (Px > P) {
  57. x1 = x;
  58. }
  59. x = 0.5 * (x0 + x1);
  60. }
  61. return x;
  62. }
  63. double
  64. gsl_cdf_beta_Pinv (const double P, const double a, const double b)
  65. {
  66. double x, mean;
  67. if (P < 0.0 || P > 1.0)
  68. {
  69. CDF_ERROR ("P must be in range 0 < P < 1", GSL_EDOM);
  70. }
  71. if (a < 0.0)
  72. {
  73. CDF_ERROR ("a < 0", GSL_EDOM);
  74. }
  75. if (b < 0.0)
  76. {
  77. CDF_ERROR ("b < 0", GSL_EDOM);
  78. }
  79. if (P == 0.0)
  80. {
  81. return 0.0;
  82. }
  83. if (P == 1.0)
  84. {
  85. return 1.0;
  86. }
  87. if (P > 0.5)
  88. {
  89. return gsl_cdf_beta_Qinv (1 - P, a, b);
  90. }
  91. mean = a / (a + b);
  92. if (P < 0.1)
  93. {
  94. /* small x */
  95. double lg_ab = gsl_sf_lngamma (a + b);
  96. double lg_a = gsl_sf_lngamma (a);
  97. double lg_b = gsl_sf_lngamma (b);
  98. double lx = (log (a) + lg_a + lg_b - lg_ab + log (P)) / a;
  99. if (lx <= 0) {
  100. x = exp (lx); /* first approximation */
  101. x *= pow (1 - x, -(b - 1) / a); /* second approximation */
  102. } else {
  103. x = mean;
  104. }
  105. if (x > mean)
  106. x = mean;
  107. }
  108. else
  109. {
  110. /* Use expected value as first guess */
  111. x = mean;
  112. }
  113. /* Do bisection to get closer */
  114. x = bisect (x, P, a, b, 0.01, 0.01);
  115. {
  116. double lambda, dP, phi;
  117. unsigned int n = 0;
  118. start:
  119. dP = P - gsl_cdf_beta_P (x, a, b);
  120. phi = gsl_ran_beta_pdf (x, a, b);
  121. if (dP == 0.0 || n++ > 64)
  122. goto end;
  123. lambda = dP / GSL_MAX (2 * fabs (dP / x), phi);
  124. {
  125. double step0 = lambda;
  126. double step1 = -((a - 1) / x - (b - 1) / (1 - x)) * lambda * lambda / 2;
  127. double step = step0;
  128. if (fabs (step1) < fabs (step0))
  129. {
  130. step += step1;
  131. }
  132. else
  133. {
  134. /* scale back step to a reasonable size when too large */
  135. step *= 2 * fabs (step0 / step1);
  136. };
  137. if (x + step > 0 && x + step < 1)
  138. {
  139. x += step;
  140. }
  141. else
  142. {
  143. x = sqrt (x) * sqrt (mean); /* try a new starting point */
  144. }
  145. if (fabs (step0) > 1e-10 * x)
  146. goto start;
  147. }
  148. end:
  149. if (fabs(dP) > GSL_SQRT_DBL_EPSILON * P)
  150. {
  151. GSL_ERROR_VAL("inverse failed to converge", GSL_EFAILED, GSL_NAN);
  152. }
  153. return x;
  154. }
  155. }
  156. double
  157. gsl_cdf_beta_Qinv (const double Q, const double a, const double b)
  158. {
  159. if (Q < 0.0 || Q > 1.0)
  160. {
  161. CDF_ERROR ("Q must be inside range 0 < Q < 1", GSL_EDOM);
  162. }
  163. if (a < 0.0)
  164. {
  165. CDF_ERROR ("a < 0", GSL_EDOM);
  166. }
  167. if (b < 0.0)
  168. {
  169. CDF_ERROR ("b < 0", GSL_EDOM);
  170. }
  171. if (Q == 0.0)
  172. {
  173. return 1.0;
  174. }
  175. if (Q == 1.0)
  176. {
  177. return 0.0;
  178. }
  179. if (Q > 0.5)
  180. {
  181. return gsl_cdf_beta_Pinv (1 - Q, a, b);
  182. }
  183. else
  184. {
  185. return 1 - gsl_cdf_beta_Pinv (Q, b, a);
  186. };
  187. }