1 /** 2 * Implementation of the gamma and beta functions, and their integrals. 3 * 4 * Copyright: 5 * Based on the CEPHES math library, which is 6 * Copyright (C) 1994 Stephen L. Moshier (moshier@world.std.com). 7 * Some parts copyright (c) 2009-2016 dunnhumby Germany GmbH. 8 * All rights reserved. 9 * 10 * License: 11 * Tango Dual License: 3-Clause BSD License / Academic Free License v3.0. 12 * See LICENSE_TANGO.txt for details. 13 * 14 * Authors: Stephen L. Moshier (original C code). Conversion to D by Don Clugston 15 * 16 * Macros: 17 * TABLE_SV = <table border=1 cellpadding=4 cellspacing=0> 18 * <caption>Special Values</caption> 19 * $0</table> 20 * SVH = $(TR $(TH $1) $(TH $2)) 21 * SV = $(TR $(TD $1) $(TD $2)) 22 * GAMMA = Γ 23 * INTEGRATE = $(BIG ∫<sub>$(SMALL $1)</sub><sup>$2</sup>) 24 * POWER = $1<sup>$2</sup> 25 * NAN = $(RED NAN) 26 */ 27 module ocean.math.GammaFunction; 28 import ocean.meta.types.Qualifiers; 29 import ocean.math.Math; 30 import ocean.math.IEEE; 31 import ocean.math.ErrorFunction; 32 import ocean.core.Verify; 33 34 version (unittest) import ocean.core.Test; 35 36 //------------------------------------------------------------------ 37 38 /// The maximum value of x for which gamma(x) < real.infinity. 39 static immutable real MAXGAMMA = 1755.5483429L; 40 41 private { 42 43 static immutable real SQRT2PI = 2.50662827463100050242E0L; // sqrt(2pi) 44 45 // Polynomial approximations for gamma and loggamma. 46 47 static immutable real[] GammaNumeratorCoeffs = [ 1.0, 48 0x1.acf42d903366539ep-1, 0x1.73a991c8475f1aeap-2, 0x1.c7e918751d6b2a92p-4, 49 0x1.86d162cca32cfe86p-6, 0x1.0c378e2e6eaf7cd8p-8, 0x1.dc5c66b7d05feb54p-12, 50 0x1.616457b47e448694p-15 51 ]; 52 53 static immutable real[] GammaDenominatorCoeffs = [ 1.0, 54 0x1.a8f9faae5d8fc8bp-2, -0x1.cb7895a6756eebdep-3, -0x1.7b9bab006d30652ap-5, 55 0x1.c671af78f312082ep-6, -0x1.a11ebbfaf96252dcp-11, -0x1.447b4d2230a77ddap-10, 56 0x1.ec1d45bb85e06696p-13,-0x1.d4ce24d05bd0a8e6p-17 57 ]; 58 59 static immutable real[] GammaSmallCoeffs = [ 1.0, 60 0x1.2788cfc6fb618f52p-1, -0x1.4fcf4026afa2f7ecp-1, -0x1.5815e8fa24d7e306p-5, 61 0x1.5512320aea2ad71ap-3, -0x1.59af0fb9d82e216p-5, -0x1.3b4b61d3bfdf244ap-7, 62 0x1.d9358e9d9d69fd34p-8, -0x1.38fc4bcbada775d6p-10 63 ]; 64 65 static immutable real[] GammaSmallNegCoeffs = [ -1.0, 66 0x1.2788cfc6fb618f54p-1, 0x1.4fcf4026afa2bc4cp-1, -0x1.5815e8fa2468fec8p-5, 67 -0x1.5512320baedaf4b6p-3, -0x1.59af0fa283baf07ep-5, 0x1.3b4a70de31e05942p-7, 68 0x1.d9398be3bad13136p-8, 0x1.291b73ee05bcbba2p-10 69 ]; 70 71 static immutable real[] logGammaStirlingCoeffs = [ 72 0x1.5555555555553f98p-4, -0x1.6c16c16c07509b1p-9, 0x1.a01a012461cbf1e4p-11, 73 -0x1.3813089d3f9d164p-11, 0x1.b911a92555a277b8p-11, -0x1.ed0a7b4206087b22p-10, 74 0x1.402523859811b308p-8 75 ]; 76 77 static immutable real[] logGammaNumerator = [ 78 -0x1.0edd25913aaa40a2p+23, -0x1.31c6ce2e58842d1ep+24, -0x1.f015814039477c3p+23, 79 -0x1.74ffe40c4b184b34p+22, -0x1.0d9c6d08f9eab55p+20, -0x1.54c6b71935f1fc88p+16, 80 -0x1.0e761b42932b2aaep+11 81 ]; 82 83 static immutable real[] logGammaDenominator = [ 84 -0x1.4055572d75d08c56p+24, -0x1.deeb6013998e4d76p+24, -0x1.106f7cded5dcc79ep+24, 85 -0x1.25e17184848c66d2p+22, -0x1.301303b99a614a0ap+19, -0x1.09e76ab41ae965p+15, 86 -0x1.00f95ced9e5f54eep+9, 1.0 87 ]; 88 89 /* 90 * Helper function: Gamma function computed by Stirling's formula. 91 * 92 * Stirling's formula for the gamma function is: 93 * 94 * $(GAMMA)(x) = sqrt(2 π) x<sup>x-0.5</sup> exp(-x) (1 + 1/x P(1/x)) 95 * 96 */ 97 real gammaStirling(real x) 98 { 99 // CEPHES code Copyright 1994 by Stephen L. Moshier 100 101 static immutable real[] SmallStirlingCoeffs = [ 102 0x1.55555555555543aap-4, 0x1.c71c71c720dd8792p-9, -0x1.5f7268f0b5907438p-9, 103 -0x1.e13cd410e0477de6p-13, 0x1.9b0f31643442616ep-11, 0x1.2527623a3472ae08p-14, 104 -0x1.37f6bc8ef8b374dep-11,-0x1.8c968886052b872ap-16, 0x1.76baa9c6d3eeddbcp-11 105 ]; 106 107 static immutable real[] LargeStirlingCoeffs = [ 1.0L, 108 8.33333333333333333333E-2L, 3.47222222222222222222E-3L, 109 -2.68132716049382716049E-3L, -2.29472093621399176955E-4L, 110 7.84039221720066627474E-4L, 6.97281375836585777429E-5L 111 ]; 112 113 real w = 1.0L/x; 114 real y = exp(x); 115 if ( x > 1024.0L ) { 116 // For large x, use rational coefficients from the analytical expansion. 117 w = poly(w, LargeStirlingCoeffs); 118 // Avoid overflow in pow() 119 real v = pow( x, 0.5L * x - 0.25L ); 120 y = v * (v / y); 121 } 122 else { 123 w = 1.0L + w * poly( w, SmallStirlingCoeffs); 124 y = pow( x, x - 0.5L ) / y; 125 } 126 y = SQRT2PI * y * w; 127 return y; 128 } 129 130 } // private 131 132 /**************** 133 * The sign of $(GAMMA)(x). 134 * 135 * Returns -1 if $(GAMMA)(x) < 0, +1 if $(GAMMA)(x) > 0, 136 * $(NAN) if sign is indeterminate. 137 */ 138 real sgnGamma(real x) 139 { 140 /* Author: Don Clugston. */ 141 if (isNaN(x)) return x; 142 if (x > 0) return 1.0; 143 if (x < -1/real.epsilon) { 144 // Large negatives lose all precision 145 return NaN(TANGO_NAN.SGNGAMMA); 146 } 147 // if (remquo(x, -1.0, n) == 0) { 148 long n = rndlong(x); 149 if (x == n) { 150 return x == 0 ? copysign(1, x) : NaN(TANGO_NAN.SGNGAMMA); 151 } 152 return n & 1 ? 1.0 : -1.0; 153 } 154 155 unittest { 156 test(sgnGamma(5.0) == 1.0); 157 test(isNaN(sgnGamma(-3.0))); 158 test(sgnGamma(-0.1) == -1.0); 159 test(sgnGamma(-55.1) == 1.0); 160 test(isNaN(sgnGamma(-real.infinity))); 161 test(isIdentical(sgnGamma(NaN(0xABC)), NaN(0xABC))); 162 } 163 164 /***************************************************** 165 * The Gamma function, $(GAMMA)(x) 166 * 167 * $(GAMMA)(x) is a generalisation of the factorial function 168 * to real and complex numbers. 169 * Like x!, $(GAMMA)(x+1) = x*$(GAMMA)(x). 170 * 171 * Mathematically, if z.re > 0 then 172 * $(GAMMA)(z) = $(INTEGRATE 0, ∞) $(POWER t, z-1)$(POWER e, -t) dt 173 * 174 * $(TABLE_SV 175 * $(SVH x, $(GAMMA)(x) ) 176 * $(SV $(NAN), $(NAN) ) 177 * $(SV ±0.0, ±∞) 178 * $(SV integer > 0, (x-1)! ) 179 * $(SV integer < 0, $(NAN) ) 180 * $(SV +∞, +∞ ) 181 * $(SV -∞, $(NAN) ) 182 * ) 183 */ 184 real gamma(real x) 185 { 186 /* Based on code from the CEPHES library. 187 * CEPHES code Copyright 1994 by Stephen L. Moshier 188 * 189 * Arguments |x| <= 13 are reduced by recurrence and the function 190 * approximated by a rational function of degree 7/8 in the 191 * interval (2,3). Large arguments are handled by Stirling's 192 * formula. Large negative arguments are made positive using 193 * a reflection formula. 194 */ 195 196 real q, z; 197 if (isNaN(x)) return x; 198 if (x == -x.infinity) return NaN(TANGO_NAN.GAMMA_DOMAIN); 199 if ( fabs(x) > MAXGAMMA ) return real.infinity; 200 if (x==0) return 1.0/x; // +- infinity depending on sign of x, create an exception. 201 202 q = fabs(x); 203 204 if ( q > 13.0L ) { 205 // Large arguments are handled by Stirling's 206 // formula. Large negative arguments are made positive using 207 // the reflection formula. 208 209 if ( x < 0.0L ) { 210 int sgngam = 1; // sign of gamma. 211 real p = floor(q); 212 if (p == q) 213 return NaN(TANGO_NAN.GAMMA_DOMAIN); // poles for all integers <0. 214 int intpart = cast(int)(p); 215 if ( (intpart & 1) == 0 ) 216 sgngam = -1; 217 z = q - p; 218 if ( z > 0.5L ) { 219 p += 1.0L; 220 z = q - p; 221 } 222 z = q * sin( PI * z ); 223 z = fabs(z) * gammaStirling(q); 224 if ( z <= PI/real.max ) return sgngam * real.infinity; 225 return sgngam * PI/z; 226 } else { 227 return gammaStirling(x); 228 } 229 } 230 231 // Arguments |x| <= 13 are reduced by recurrence and the function 232 // approximated by a rational function of degree 7/8 in the 233 // interval (2,3). 234 235 z = 1.0L; 236 while ( x >= 3.0L ) { 237 x -= 1.0L; 238 z *= x; 239 } 240 241 while ( x < -0.03125L ) { 242 z /= x; 243 x += 1.0L; 244 } 245 246 if ( x <= 0.03125L ) { 247 if ( x == 0.0L ) 248 return NaN(TANGO_NAN.GAMMA_POLE); 249 else { 250 if ( x < 0.0L ) { 251 x = -x; 252 return z / (x * poly( x, GammaSmallNegCoeffs )); 253 } else { 254 return z / (x * poly( x, GammaSmallCoeffs )); 255 } 256 } 257 } 258 259 while ( x < 2.0L ) { 260 z /= x; 261 x += 1.0L; 262 } 263 if ( x == 2.0L ) return z; 264 265 x -= 2.0L; 266 return z * poly( x, GammaNumeratorCoeffs ) / poly( x, GammaDenominatorCoeffs ); 267 } 268 269 unittest { 270 // gamma(n) = factorial(n-1) if n is an integer. 271 real fact = 1.0L; 272 for (int i=1; fact<real.max; ++i) { 273 // Require exact equality for small factorials 274 if (i<14) test(gamma(i*1.0L) == fact); 275 version(FailsOnLinux) test(feqrel(gamma(i*1.0L), fact) > real.mant_dig-15); 276 fact *= (i*1.0L); 277 } 278 test(gamma(0.0) == real.infinity); 279 test(gamma(-0.0) == -real.infinity); 280 test(isNaN(gamma(-1.0))); 281 test(isNaN(gamma(-15.0))); 282 test(isIdentical(gamma(NaN(0xABC)), NaN(0xABC))); 283 test(gamma(real.infinity) == real.infinity); 284 test(gamma(real.max) == real.infinity); 285 test(isNaN(gamma(-real.infinity))); 286 test(gamma(real.min_normal*real.epsilon) == real.infinity); 287 test(gamma(MAXGAMMA)< real.infinity); 288 test(gamma(MAXGAMMA*2) == real.infinity); 289 290 // Test some high-precision values (50 decimal digits) 291 static immutable real SQRT_PI = 1.77245385090551602729816748334114518279754945612238L; 292 293 version(FailsOnLinux) test(feqrel(gamma(0.5L), SQRT_PI) == real.mant_dig); 294 295 test(feqrel(gamma(1.0/3.0L), 2.67893853470774763365569294097467764412868937795730L) >= real.mant_dig-2); 296 test(feqrel(gamma(0.25L), 297 3.62560990822190831193068515586767200299516768288006L) >= real.mant_dig-1); 298 test(feqrel(gamma(1.0/5.0L), 299 4.59084371199880305320475827592915200343410999829340L) >= real.mant_dig-1); 300 } 301 302 /***************************************************** 303 * Natural logarithm of gamma function. 304 * 305 * Returns the base e (2.718...) logarithm of the absolute 306 * value of the gamma function of the argument. 307 * 308 * For reals, logGamma is equivalent to log(fabs(gamma(x))). 309 * 310 * $(TABLE_SV 311 * $(SVH x, logGamma(x) ) 312 * $(SV $(NAN), $(NAN) ) 313 * $(SV integer <= 0, +∞ ) 314 * $(SV ±∞, +∞ ) 315 * ) 316 */ 317 real logGamma(real x) 318 { 319 /* Based on code from the CEPHES library. 320 * CEPHES code Copyright 1994 by Stephen L. Moshier 321 * 322 * For arguments greater than 33, the logarithm of the gamma 323 * function is approximated by the logarithmic version of 324 * Stirling's formula using a polynomial approximation of 325 * degree 4. Arguments between -33 and +33 are reduced by 326 * recurrence to the interval [2,3] of a rational approximation. 327 * The cosecant reflection formula is employed for arguments 328 * less than -33. 329 */ 330 real q, w, z, f, nx; 331 332 if (isNaN(x)) return x; 333 if (fabs(x) == x.infinity) return x.infinity; 334 335 if( x < -34.0L ) { 336 q = -x; 337 w = logGamma(q); 338 real p = floor(q); 339 if ( p == q ) return real.infinity; 340 int intpart = cast(int)(p); 341 real sgngam = 1; 342 if ( (intpart & 1) == 0 ) 343 sgngam = -1; 344 z = q - p; 345 if ( z > 0.5L ) { 346 p += 1.0L; 347 z = p - q; 348 } 349 z = q * sin( PI * z ); 350 if ( z == 0.0L ) return sgngam * real.infinity; 351 /* z = LOGPI - logl( z ) - w; */ 352 z = log( PI/z ) - w; 353 return z; 354 } 355 356 if( x < 13.0L ) { 357 z = 1.0L; 358 nx = floor( x + 0.5L ); 359 f = x - nx; 360 while ( x >= 3.0L ) { 361 nx -= 1.0L; 362 x = nx + f; 363 z *= x; 364 } 365 while ( x < 2.0L ) { 366 if( fabs(x) <= 0.03125 ) { 367 if ( x == 0.0L ) return real.infinity; 368 if ( x < 0.0L ) { 369 x = -x; 370 q = z / (x * poly( x, GammaSmallNegCoeffs)); 371 } else 372 q = z / (x * poly( x, GammaSmallCoeffs)); 373 return log( fabs(q) ); 374 } 375 z /= nx + f; 376 nx += 1.0L; 377 x = nx + f; 378 } 379 z = fabs(z); 380 if ( x == 2.0L ) 381 return log(z); 382 x = (nx - 2.0L) + f; 383 real p = x * rationalPoly( x, logGammaNumerator, logGammaDenominator); 384 return log(z) + p; 385 } 386 387 // const real MAXLGM = 1.04848146839019521116e+4928L; 388 // if( x > MAXLGM ) return sgngaml * real.infinity; 389 390 static immutable real LOGSQRT2PI = 0.91893853320467274178L; // log( sqrt( 2*pi ) ) 391 392 q = ( x - 0.5L ) * log(x) - x + LOGSQRT2PI; 393 if (x > 1.0e10L) return q; 394 real p = 1.0L / (x*x); 395 q += poly( p, logGammaStirlingCoeffs ) / x; 396 return q ; 397 } 398 399 unittest { 400 test(isIdentical(logGamma(NaN(0xDEF)), NaN(0xDEF))); 401 test(logGamma(real.infinity) == real.infinity); 402 test(logGamma(-1.0) == real.infinity); 403 test(logGamma(0.0) == real.infinity); 404 test(logGamma(-50.0) == real.infinity); 405 test(isIdentical(0.0L, logGamma(1.0L))); 406 test(isIdentical(0.0L, logGamma(2.0L))); 407 test(logGamma(real.min_normal*real.epsilon) == real.infinity); 408 test(logGamma(-real.min_normal*real.epsilon) == real.infinity); 409 410 // x, correct loggamma(x), correct d/dx loggamma(x). 411 static real[] testpoints = [ 412 8.0L, 8.525146484375L + 1.48766904143001655310E-5, 2.01564147795560999654E0L, 413 8.99993896484375e-1L, 6.6375732421875e-2L + 5.11505711292524166220E-6L, -7.54938684259372234258E-1, 414 7.31597900390625e-1L, 2.2369384765625e-1 + 5.21506341809849792422E-6L, -1.13355566660398608343E0L, 415 2.31639862060546875e-1L, 1.3686676025390625L + 1.12609441752996145670E-5L, -4.56670961813812679012E0, 416 1.73162841796875L, -8.88214111328125e-2L + 3.36207740803753034508E-6L, 2.33339034686200586920E-1L, 417 1.23162841796875L, -9.3902587890625e-2L + 1.28765089229009648104E-5L, -2.49677345775751390414E-1L, 418 7.3786976294838206464e19L, 3.301798506038663053312e21L - 1.656137564136932662487046269677E5L, 419 4.57477139169563904215E1L, 420 1.08420217248550443401E-19L, 4.36682586669921875e1L + 1.37082843669932230418E-5L, 421 -9.22337203685477580858E18L, 422 1.0L, 0.0L, -5.77215664901532860607E-1L, 423 2.0L, 0.0L, 4.22784335098467139393E-1L, 424 -0.5L, 1.2655029296875L + 9.19379714539648894580E-6L, 3.64899739785765205590E-2L, 425 -1.5L, 8.6004638671875e-1L + 6.28657731014510932682E-7L, 7.03156640645243187226E-1L, 426 -2.5L, -5.6243896484375E-2L + 1.79986700949327405470E-7, 1.10315664064524318723E0L, 427 -3.5L, -1.30902099609375L + 1.43111007079536392848E-5L, 1.38887092635952890151E0L 428 ]; 429 // TODO: test derivatives as well. 430 for (int i=0; i<testpoints.length; i+=3) { 431 test( feqrel(logGamma(testpoints[i]), testpoints[i+1]) > real.mant_dig-5); 432 if (testpoints[i]<MAXGAMMA) { 433 test( feqrel(log(fabs(gamma(testpoints[i]))), testpoints[i+1]) > real.mant_dig-5); 434 } 435 } 436 test(logGamma(-50.2) == log(fabs(gamma(-50.2)))); 437 test(logGamma(-0.008) == log(fabs(gamma(-0.008)))); 438 test(feqrel(logGamma(-38.8),log(fabs(gamma(-38.8)))) > real.mant_dig-4); 439 test(feqrel(logGamma(1500.0L),log(gamma(1500.0L))) > real.mant_dig-2); 440 } 441 442 private { 443 static immutable real MAXLOG = 0x1.62e42fefa39ef358p+13L; // log(real.max) 444 static immutable real MINLOG = -0x1.6436716d5406e6d8p+13L; // log(real.min*real.epsilon) = log(smallest denormal) 445 static immutable real BETA_BIG = 9.223372036854775808e18L; 446 static immutable real BETA_BIGINV = 1.084202172485504434007e-19L; 447 } 448 449 /** Beta function 450 * 451 * The beta function is defined as 452 * 453 * beta(x, y) = (Γ(x) Γ(y))/Γ(x + y) 454 */ 455 real beta(real x, real y) 456 { 457 if ((x+y)> MAXGAMMA) { 458 return exp(logGamma(x) + logGamma(y) - logGamma(x+y)); 459 } else return gamma(x)*gamma(y)/gamma(x+y); 460 } 461 462 unittest { 463 test(isIdentical(beta(NaN(0xABC), 4), NaN(0xABC))); 464 test(isIdentical(beta(2, NaN(0xABC)), NaN(0xABC))); 465 } 466 467 /** Incomplete beta integral 468 * 469 * Returns incomplete beta integral of the arguments, evaluated 470 * from zero to x. The regularized incomplete beta function is defined as 471 * 472 * betaIncomplete(a, b, x) = Γ(a+b)/(Γ(a) Γ(b)) * 473 * $(INTEGRATE 0, x) $(POWER t, a-1)$(POWER (1-t),b-1) dt 474 * 475 * and is the same as the the cumulative distribution function. 476 * 477 * The domain of definition is 0 <= x <= 1. In this 478 * implementation a and b are restricted to positive values. 479 * The integral from x to 1 may be obtained by the symmetry 480 * relation 481 * 482 * betaIncompleteCompl(a, b, x ) = betaIncomplete( b, a, 1-x ) 483 * 484 * The integral is evaluated by a continued fraction expansion 485 * or, when b*x is small, by a power series. 486 */ 487 real betaIncomplete(real aa, real bb, real xx ) 488 { 489 if (!(aa>0 && bb>0)) { 490 if (isNaN(aa)) return aa; 491 if (isNaN(bb)) return bb; 492 return NaN(TANGO_NAN.BETA_DOMAIN); // domain error 493 } 494 if (!(xx>0 && xx<1.0)) { 495 if (isNaN(xx)) return xx; 496 if ( xx == 0.0L ) return 0.0; 497 if ( xx == 1.0L ) return 1.0; 498 return NaN(TANGO_NAN.BETA_DOMAIN); // domain error 499 } 500 if ( (bb * xx) <= 1.0L && xx <= 0.95L) { 501 return betaDistPowerSeries(aa, bb, xx); 502 } 503 real x; 504 real xc; // = 1 - x 505 506 real a, b; 507 int flag = 0; 508 509 /* Reverse a and b if x is greater than the mean. */ 510 if( xx > (aa/(aa+bb)) ) { 511 // here x > aa/(aa+bb) and (bb*x>1 or x>0.95) 512 flag = 1; 513 a = bb; 514 b = aa; 515 xc = xx; 516 x = 1.0L - xx; 517 } else { 518 a = aa; 519 b = bb; 520 xc = 1.0L - xx; 521 x = xx; 522 } 523 524 if( flag == 1 && (b * x) <= 1.0L && x <= 0.95L) { 525 // here xx > aa/(aa+bb) and ((bb*xx>1) or xx>0.95) and (aa*(1-xx)<=1) and xx > 0.05 526 return 1.0 - betaDistPowerSeries(a, b, x); // note loss of precision 527 } 528 529 real w; 530 // Choose expansion for optimal convergence 531 // One is for x * (a+b+2) < (a+1), 532 // the other is for x * (a+b+2) > (a+1). 533 real y = x * (a+b-2.0L) - (a-1.0L); 534 if( y < 0.0L ) { 535 w = betaDistExpansion1( a, b, x ); 536 } else { 537 w = betaDistExpansion2( a, b, x ) / xc; 538 } 539 540 /* Multiply w by the factor 541 a b 542 x (1-x) Gamma(a+b) / ( a Gamma(a) Gamma(b) ) . */ 543 544 y = a * log(x); 545 real t = b * log(xc); 546 if ( (a+b) < MAXGAMMA && fabs(y) < MAXLOG && fabs(t) < MAXLOG ) { 547 t = pow(xc,b); 548 t *= pow(x,a); 549 t /= a; 550 t *= w; 551 t *= gamma(a+b) / (gamma(a) * gamma(b)); 552 } else { 553 /* Resort to logarithms. */ 554 y += t + logGamma(a+b) - logGamma(a) - logGamma(b); 555 y += log(w/a); 556 557 t = exp(y); 558 /+ 559 // There seems to be a bug in Cephes at this point. 560 // Problems occur for y > MAXLOG, not y < MINLOG. 561 if( y < MINLOG ) { 562 t = 0.0L; 563 } else { 564 t = exp(y); 565 } 566 +/ 567 } 568 if( flag == 1 ) { 569 /+ // CEPHES includes this code, but I think it is erroneous. 570 if( t <= real.epsilon ) { 571 t = 1.0L - real.epsilon; 572 } else 573 +/ 574 t = 1.0L - t; 575 } 576 return t; 577 } 578 579 /** Inverse of incomplete beta integral 580 * 581 * Given y, the function finds x such that 582 * 583 * betaIncomplete(a, b, x) == y 584 * 585 * Newton iterations or interval halving is used. 586 */ 587 real betaIncompleteInv(real aa, real bb, real yy0 ) 588 { 589 real a, b, y0, d, y, x, x0, x1, lgm, yp, di, dithresh, yl, yh, xt; 590 int i, rflg, dir, nflg; 591 592 if (isNaN(yy0)) return yy0; 593 if (isNaN(aa)) return aa; 594 if (isNaN(bb)) return bb; 595 if( yy0 <= 0.0L ) 596 return 0.0L; 597 if( yy0 >= 1.0L ) 598 return 1.0L; 599 x0 = 0.0L; 600 yl = 0.0L; 601 x1 = 1.0L; 602 yh = 1.0L; 603 if( aa <= 1.0L || bb <= 1.0L ) { 604 dithresh = 1.0e-7L; 605 rflg = 0; 606 a = aa; 607 b = bb; 608 y0 = yy0; 609 x = a/(a+b); 610 y = betaIncomplete( a, b, x ); 611 nflg = 0; 612 goto ihalve; 613 } else { 614 nflg = 0; 615 dithresh = 1.0e-4L; 616 } 617 618 /* approximation to inverse function */ 619 620 yp = -normalDistributionInvImpl( yy0 ); 621 622 if( yy0 > 0.5L ) { 623 rflg = 1; 624 a = bb; 625 b = aa; 626 y0 = 1.0L - yy0; 627 yp = -yp; 628 } else { 629 rflg = 0; 630 a = aa; 631 b = bb; 632 y0 = yy0; 633 } 634 635 lgm = (yp * yp - 3.0L)/6.0L; 636 x = 2.0L/( 1.0L/(2.0L * a-1.0L) + 1.0L/(2.0L * b - 1.0L) ); 637 d = yp * sqrt( x + lgm ) / x 638 - ( 1.0L/(2.0L * b - 1.0L) - 1.0L/(2.0L * a - 1.0L) ) 639 * (lgm + (5.0L/6.0L) - 2.0L/(3.0L * x)); 640 d = 2.0L * d; 641 if( d < MINLOG ) { 642 x = 1.0L; 643 goto under; 644 } 645 x = a/( a + b * exp(d) ); 646 y = betaIncomplete( a, b, x ); 647 yp = (y - y0)/y0; 648 if( fabs(yp) < 0.2 ) 649 goto newt; 650 651 /* Resort to interval halving if not close enough. */ 652 ihalve: 653 654 dir = 0; 655 di = 0.5L; 656 for( i=0; i<400; i++ ) { 657 if( i != 0 ) { 658 x = x0 + di * (x1 - x0); 659 if( x == 1.0L ) { 660 x = 1.0L - real.epsilon; 661 } 662 if( x == 0.0L ) { 663 di = 0.5; 664 x = x0 + di * (x1 - x0); 665 if( x == 0.0 ) 666 goto under; 667 } 668 y = betaIncomplete( a, b, x ); 669 yp = (x1 - x0)/(x1 + x0); 670 if( fabs(yp) < dithresh ) 671 goto newt; 672 yp = (y-y0)/y0; 673 if( fabs(yp) < dithresh ) 674 goto newt; 675 } 676 if( y < y0 ) { 677 x0 = x; 678 yl = y; 679 if( dir < 0 ) { 680 dir = 0; 681 di = 0.5L; 682 } else if( dir > 3 ) 683 di = 1.0L - (1.0L - di) * (1.0L - di); 684 else if( dir > 1 ) 685 di = 0.5L * di + 0.5L; 686 else 687 di = (y0 - y)/(yh - yl); 688 dir += 1; 689 if( x0 > 0.95L ) { 690 if( rflg == 1 ) { 691 rflg = 0; 692 a = aa; 693 b = bb; 694 y0 = yy0; 695 } else { 696 rflg = 1; 697 a = bb; 698 b = aa; 699 y0 = 1.0 - yy0; 700 } 701 x = 1.0L - x; 702 y = betaIncomplete( a, b, x ); 703 x0 = 0.0; 704 yl = 0.0; 705 x1 = 1.0; 706 yh = 1.0; 707 goto ihalve; 708 } 709 } else { 710 x1 = x; 711 if( rflg == 1 && x1 < real.epsilon ) { 712 x = 0.0L; 713 goto done; 714 } 715 yh = y; 716 if( dir > 0 ) { 717 dir = 0; 718 di = 0.5L; 719 } 720 else if( dir < -3 ) 721 di = di * di; 722 else if( dir < -1 ) 723 di = 0.5L * di; 724 else 725 di = (y - y0)/(yh - yl); 726 dir -= 1; 727 } 728 } 729 // loss of precision has occurred 730 731 //mtherr( "incbil", PLOSS ); 732 if( x0 >= 1.0L ) { 733 x = 1.0L - real.epsilon; 734 goto done; 735 } 736 if( x <= 0.0L ) { 737 under: 738 // underflow has occurred 739 //mtherr( "incbil", UNDERFLOW ); 740 x = 0.0L; 741 goto done; 742 } 743 744 newt: 745 746 if ( nflg ) { 747 goto done; 748 } 749 nflg = 1; 750 lgm = logGamma(a+b) - logGamma(a) - logGamma(b); 751 752 for( i=0; i<15; i++ ) { 753 /* Compute the function at this point. */ 754 if ( i != 0 ) 755 y = betaIncomplete(a,b,x); 756 if ( y < yl ) { 757 x = x0; 758 y = yl; 759 } else if( y > yh ) { 760 x = x1; 761 y = yh; 762 } else if( y < y0 ) { 763 x0 = x; 764 yl = y; 765 } else { 766 x1 = x; 767 yh = y; 768 } 769 if( x == 1.0L || x == 0.0L ) 770 break; 771 /* Compute the derivative of the function at this point. */ 772 d = (a - 1.0L) * log(x) + (b - 1.0L) * log(1.0L - x) + lgm; 773 if ( d < MINLOG ) { 774 goto done; 775 } 776 if ( d > MAXLOG ) { 777 break; 778 } 779 d = exp(d); 780 /* Compute the step to the next approximation of x. */ 781 d = (y - y0)/d; 782 xt = x - d; 783 if ( xt <= x0 ) { 784 y = (x - x0) / (x1 - x0); 785 xt = x0 + 0.5L * y * (x - x0); 786 if( xt <= 0.0L ) 787 break; 788 } 789 if ( xt >= x1 ) { 790 y = (x1 - x) / (x1 - x0); 791 xt = x1 - 0.5L * y * (x1 - x); 792 if ( xt >= 1.0L ) 793 break; 794 } 795 x = xt; 796 if ( fabs(d/x) < (128.0L * real.epsilon) ) 797 goto done; 798 } 799 /* Did not converge. */ 800 dithresh = 256.0L * real.epsilon; 801 goto ihalve; 802 803 done: 804 if ( rflg ) { 805 if( x <= real.epsilon ) 806 x = 1.0L - real.epsilon; 807 else 808 x = 1.0L - x; 809 } 810 return x; 811 } 812 813 unittest { // also tested by the normal distribution 814 // check NaN propagation 815 test(isIdentical(betaIncomplete(NaN(0xABC),2,3), NaN(0xABC))); 816 test(isIdentical(betaIncomplete(7,NaN(0xABC),3), NaN(0xABC))); 817 test(isIdentical(betaIncomplete(7,15,NaN(0xABC)), NaN(0xABC))); 818 test(isIdentical(betaIncompleteInv(NaN(0xABC),1,17), NaN(0xABC))); 819 test(isIdentical(betaIncompleteInv(2,NaN(0xABC),8), NaN(0xABC))); 820 test(isIdentical(betaIncompleteInv(2,3, NaN(0xABC)), NaN(0xABC))); 821 822 test(isNaN(betaIncomplete(-1, 2, 3))); 823 824 test(betaIncomplete(1, 2, 0)==0); 825 test(betaIncomplete(1, 2, 1)==1); 826 test(isNaN(betaIncomplete(1, 2, 3))); 827 test(betaIncompleteInv(1, 1, 0)==0); 828 test(betaIncompleteInv(1, 1, 1)==1); 829 830 // Test some values against Microsoft Excel 2003. 831 832 test(fabs(betaIncomplete(8, 10, 0.2) - 0.010_934_315_236_957_2L) < 0.000_000_000_5); 833 test(fabs(betaIncomplete(2, 2.5, 0.9) - 0.989_722_597_604_107L) < 0.000_000_000_000_5); 834 test(fabs(betaIncomplete(1000, 800, 0.5) - 1.17914088832798E-06L) < 0.000_000_05e-6); 835 836 test(fabs(betaIncomplete(0.0001, 10000, 0.0001) - 0.999978059369989L) < 0.000_000_000_05); 837 838 test(fabs(betaIncompleteInv(5, 10, 0.2) - 0.229121208190918L) < 0.000_000_5L); 839 test(fabs(betaIncompleteInv(4, 7, 0.8) - 0.483657360076904L) < 0.000_000_5L); 840 841 // Coverage tests. I don't have correct values for these tests, but 842 // these values cover most of the code, so they are useful for 843 // regression testing. 844 // Extensive testing failed to increase the coverage. It seems likely that about 845 // half the code in this function is unnecessary; there is potential for 846 // significant improvement over the original CEPHES code. 847 848 // Excel 2003 gives clearly erroneous results (betadist>1) when a and x are tiny and b is huge. 849 // The correct results are for these next tests are unknown. 850 851 // real testpoint1 = betaIncomplete(1e-10, 5e20, 8e-21); 852 // assert(testpoint1 == 0x1.ffff_ffff_c906_404cp-1L); 853 854 test(betaIncomplete(0.01, 327726.7, 0.545113) == 1.0); 855 test(betaIncompleteInv(0.01, 8e-48, 5.45464e-20)==1-real.epsilon); 856 test(betaIncompleteInv(0.01, 8e-48, 9e-26)==1-real.epsilon); 857 858 test(betaIncomplete(0.01, 498.437, 0.0121433) == 0x1.ffff_8f72_19197402p-1); 859 test(1- betaIncomplete(0.01, 328222, 4.0375e-5) == 0x1.5f62926b4p-30); 860 version(FailsOnLinux) test(betaIncompleteInv(0x1.b3d151fbba0eb18p+1, 1.2265e-19, 2.44859e-18)==0x1.c0110c8531d0952cp-1); 861 version(FailsOnLinux) test(betaIncompleteInv(0x1.ff1275ae5b939bcap-41, 4.6713e18, 0.0813601)==0x1.f97749d90c7adba8p-63); 862 real a1; 863 a1 = 3.40483; 864 version(FailsOnLinux) test(betaIncompleteInv(a1, 4.0640301659679627772e19L, 0.545113)== 0x1.ba8c08108aaf5d14p-109); 865 real b1; 866 b1= 2.82847e-25; 867 version(FailsOnLinux) test(betaIncompleteInv(0.01, b1, 9e-26) == 0x1.549696104490aa9p-830); 868 869 // --- Problematic cases --- 870 // This is a situation where the series expansion fails to converge 871 test( isNaN(betaIncompleteInv(0.12167, 4.0640301659679627772e19L, 0.0813601))); 872 // This next result is almost certainly erroneous. 873 test(betaIncomplete(1.16251e20, 2.18e39, 5.45e-20)==-real.infinity); 874 } 875 876 private { 877 // Implementation functions 878 879 // Continued fraction expansion #1 for incomplete beta integral 880 // Use when x < (a+1)/(a+b+2) 881 real betaDistExpansion1(real a, real b, real x ) 882 { 883 real xk, pk, pkm1, pkm2, qk, qkm1, qkm2; 884 real k1, k2, k3, k4, k5, k6, k7, k8; 885 real r, t, ans; 886 int n; 887 888 k1 = a; 889 k2 = a + b; 890 k3 = a; 891 k4 = a + 1.0L; 892 k5 = 1.0L; 893 k6 = b - 1.0L; 894 k7 = k4; 895 k8 = a + 2.0L; 896 897 pkm2 = 0.0L; 898 qkm2 = 1.0L; 899 pkm1 = 1.0L; 900 qkm1 = 1.0L; 901 ans = 1.0L; 902 r = 1.0L; 903 n = 0; 904 static immutable real thresh = 3.0L * real.epsilon; 905 do { 906 xk = -( x * k1 * k2 )/( k3 * k4 ); 907 pk = pkm1 + pkm2 * xk; 908 qk = qkm1 + qkm2 * xk; 909 pkm2 = pkm1; 910 pkm1 = pk; 911 qkm2 = qkm1; 912 qkm1 = qk; 913 914 xk = ( x * k5 * k6 )/( k7 * k8 ); 915 pk = pkm1 + pkm2 * xk; 916 qk = qkm1 + qkm2 * xk; 917 pkm2 = pkm1; 918 pkm1 = pk; 919 qkm2 = qkm1; 920 qkm1 = qk; 921 922 if( qk != 0.0L ) 923 r = pk/qk; 924 if( r != 0.0L ) { 925 t = fabs( (ans - r)/r ); 926 ans = r; 927 } else { 928 t = 1.0L; 929 } 930 931 if( t < thresh ) 932 return ans; 933 934 k1 += 1.0L; 935 k2 += 1.0L; 936 k3 += 2.0L; 937 k4 += 2.0L; 938 k5 += 1.0L; 939 k6 -= 1.0L; 940 k7 += 2.0L; 941 k8 += 2.0L; 942 943 if( (fabs(qk) + fabs(pk)) > BETA_BIG ) { 944 pkm2 *= BETA_BIGINV; 945 pkm1 *= BETA_BIGINV; 946 qkm2 *= BETA_BIGINV; 947 qkm1 *= BETA_BIGINV; 948 } 949 if( (fabs(qk) < BETA_BIGINV) || (fabs(pk) < BETA_BIGINV) ) { 950 pkm2 *= BETA_BIG; 951 pkm1 *= BETA_BIG; 952 qkm2 *= BETA_BIG; 953 qkm1 *= BETA_BIG; 954 } 955 } 956 while( ++n < 400 ); 957 // loss of precision has occurred 958 // mtherr( "incbetl", PLOSS ); 959 return ans; 960 } 961 962 // Continued fraction expansion #2 for incomplete beta integral 963 // Use when x > (a+1)/(a+b+2) 964 real betaDistExpansion2(real a, real b, real x ) 965 { 966 real xk, pk, pkm1, pkm2, qk, qkm1, qkm2; 967 real k1, k2, k3, k4, k5, k6, k7, k8; 968 real r, t, ans, z; 969 970 k1 = a; 971 k2 = b - 1.0L; 972 k3 = a; 973 k4 = a + 1.0L; 974 k5 = 1.0L; 975 k6 = a + b; 976 k7 = a + 1.0L; 977 k8 = a + 2.0L; 978 979 pkm2 = 0.0L; 980 qkm2 = 1.0L; 981 pkm1 = 1.0L; 982 qkm1 = 1.0L; 983 z = x / (1.0L-x); 984 ans = 1.0L; 985 r = 1.0L; 986 int n = 0; 987 static immutable real thresh = 3.0L * real.epsilon; 988 do { 989 990 xk = -( z * k1 * k2 )/( k3 * k4 ); 991 pk = pkm1 + pkm2 * xk; 992 qk = qkm1 + qkm2 * xk; 993 pkm2 = pkm1; 994 pkm1 = pk; 995 qkm2 = qkm1; 996 qkm1 = qk; 997 998 xk = ( z * k5 * k6 )/( k7 * k8 ); 999 pk = pkm1 + pkm2 * xk; 1000 qk = qkm1 + qkm2 * xk; 1001 pkm2 = pkm1; 1002 pkm1 = pk; 1003 qkm2 = qkm1; 1004 qkm1 = qk; 1005 1006 if( qk != 0.0L ) 1007 r = pk/qk; 1008 if( r != 0.0L ) { 1009 t = fabs( (ans - r)/r ); 1010 ans = r; 1011 } else 1012 t = 1.0L; 1013 1014 if( t < thresh ) 1015 return ans; 1016 k1 += 1.0L; 1017 k2 -= 1.0L; 1018 k3 += 2.0L; 1019 k4 += 2.0L; 1020 k5 += 1.0L; 1021 k6 += 1.0L; 1022 k7 += 2.0L; 1023 k8 += 2.0L; 1024 1025 if( (fabs(qk) + fabs(pk)) > BETA_BIG ) { 1026 pkm2 *= BETA_BIGINV; 1027 pkm1 *= BETA_BIGINV; 1028 qkm2 *= BETA_BIGINV; 1029 qkm1 *= BETA_BIGINV; 1030 } 1031 if( (fabs(qk) < BETA_BIGINV) || (fabs(pk) < BETA_BIGINV) ) { 1032 pkm2 *= BETA_BIG; 1033 pkm1 *= BETA_BIG; 1034 qkm2 *= BETA_BIG; 1035 qkm1 *= BETA_BIG; 1036 } 1037 } while( ++n < 400 ); 1038 // loss of precision has occurred 1039 //mtherr( "incbetl", PLOSS ); 1040 return ans; 1041 } 1042 1043 /* Power series for incomplete gamma integral. 1044 Use when b*x is small. */ 1045 real betaDistPowerSeries(real a, real b, real x ) 1046 { 1047 real ai = 1.0L / a; 1048 real u = (1.0L - b) * x; 1049 real v = u / (a + 1.0L); 1050 real t1 = v; 1051 real t = u; 1052 real n = 2.0L; 1053 real s = 0.0L; 1054 real z = real.epsilon * ai; 1055 while( fabs(v) > z ) { 1056 u = (n - b) * x / n; 1057 t *= u; 1058 v = t / (a + n); 1059 s += v; 1060 n += 1.0L; 1061 } 1062 s += t1; 1063 s += ai; 1064 1065 u = a * log(x); 1066 if ( (a+b) < MAXGAMMA && fabs(u) < MAXLOG ) { 1067 t = gamma(a+b)/(gamma(a)*gamma(b)); 1068 s = s * t * pow(x,a); 1069 } else { 1070 t = logGamma(a+b) - logGamma(a) - logGamma(b) + u + log(s); 1071 1072 if( t < MINLOG ) { 1073 s = 0.0L; 1074 } else 1075 s = exp(t); 1076 } 1077 return s; 1078 } 1079 1080 } 1081 1082 /*************************************** 1083 * Incomplete gamma integral and its complement 1084 * 1085 * These functions are defined by 1086 * 1087 * gammaIncomplete = ( $(INTEGRATE 0, x) $(POWER e, -t) $(POWER t, a-1) dt )/ $(GAMMA)(a) 1088 * 1089 * gammaIncompleteCompl(a,x) = 1 - gammaIncomplete(a,x) 1090 * = ($(INTEGRATE x, ∞) $(POWER e, -t) $(POWER t, a-1) dt )/ $(GAMMA)(a) 1091 * 1092 * In this implementation both arguments must be positive. 1093 * The integral is evaluated by either a power series or 1094 * continued fraction expansion, depending on the relative 1095 * values of a and x. 1096 */ 1097 real gammaIncomplete(real a, real x ) 1098 { 1099 verify(x >= 0); 1100 verify(a > 0); 1101 1102 /* left tail of incomplete gamma function: 1103 * 1104 * inf. k 1105 * a -x - x 1106 * x e > ---------- 1107 * - - 1108 * k=0 | (a+k+1) 1109 * 1110 */ 1111 if (x==0) 1112 return 0.0L; 1113 1114 if ( (x > 1.0L) && (x > a ) ) 1115 return 1.0L - gammaIncompleteCompl(a,x); 1116 1117 real ax = a * log(x) - x - logGamma(a); 1118 /+ 1119 if( ax < MINLOGL ) return 0; // underflow 1120 // { mtherr( "igaml", UNDERFLOW ); return( 0.0L ); } 1121 +/ 1122 ax = exp(ax); 1123 1124 /* power series */ 1125 real r = a; 1126 real c = 1.0L; 1127 real ans = 1.0L; 1128 1129 do { 1130 r += 1.0L; 1131 c *= x/r; 1132 ans += c; 1133 } while( c/ans > real.epsilon ); 1134 1135 return ans * ax/a; 1136 } 1137 1138 /** ditto */ 1139 real gammaIncompleteCompl(real a, real x ) 1140 { 1141 verify(x >= 0); 1142 verify(a > 0); 1143 1144 if (x==0) 1145 return 1.0L; 1146 if ( (x < 1.0L) || (x < a) ) 1147 return 1.0L - gammaIncomplete(a,x); 1148 1149 // DAC (Cephes bug fix): This is necessary to avoid 1150 // spurious nans, eg 1151 // log(x)-x = NaN when x = real.infinity 1152 static immutable real MAXLOGL = 1.1356523406294143949492E4L; 1153 if (x > MAXLOGL) return 0; // underflow 1154 1155 real ax = a * log(x) - x - logGamma(a); 1156 //const real MINLOGL = -1.1355137111933024058873E4L; 1157 // if ( ax < MINLOGL ) return 0; // underflow; 1158 ax = exp(ax); 1159 1160 1161 /* continued fraction */ 1162 real y = 1.0L - a; 1163 real z = x + y + 1.0L; 1164 real c = 0.0L; 1165 1166 real pk, qk, t; 1167 1168 real pkm2 = 1.0L; 1169 real qkm2 = x; 1170 real pkm1 = x + 1.0L; 1171 real qkm1 = z * x; 1172 real ans = pkm1/qkm1; 1173 1174 do { 1175 c += 1.0L; 1176 y += 1.0L; 1177 z += 2.0L; 1178 real yc = y * c; 1179 pk = pkm1 * z - pkm2 * yc; 1180 qk = qkm1 * z - qkm2 * yc; 1181 if( qk != 0.0L ) { 1182 real r = pk/qk; 1183 t = fabs( (ans - r)/r ); 1184 ans = r; 1185 } else { 1186 t = 1.0L; 1187 } 1188 pkm2 = pkm1; 1189 pkm1 = pk; 1190 qkm2 = qkm1; 1191 qkm1 = qk; 1192 1193 static immutable real BIG = 9.223372036854775808e18L; 1194 1195 if ( fabs(pk) > BIG ) { 1196 pkm2 /= BIG; 1197 pkm1 /= BIG; 1198 qkm2 /= BIG; 1199 qkm1 /= BIG; 1200 } 1201 } while ( t > real.epsilon ); 1202 1203 return ans * ax; 1204 } 1205 1206 /** Inverse of complemented incomplete gamma integral 1207 * 1208 * Given a and y, the function finds x such that 1209 * 1210 * gammaIncompleteCompl( a, x ) = p. 1211 * 1212 * Starting with the approximate value x = a $(POWER t, 3), where 1213 * t = 1 - d - normalDistributionInv(p) sqrt(d), 1214 * and d = 1/9a, 1215 * the routine performs up to 10 Newton iterations to find the 1216 * root of incompleteGammaCompl(a,x) - p = 0. 1217 */ 1218 real gammaIncompleteComplInv(real a, real p) 1219 { 1220 verify(p>=0 && p<= 1); 1221 verify(a>0); 1222 1223 if (p==0) return real.infinity; 1224 1225 real y0 = p; 1226 static immutable real MAXLOGL = 1.1356523406294143949492E4L; 1227 real x0, x1, x, yl, yh, y, d, lgm, dithresh; 1228 int i, dir; 1229 1230 /* bound the solution */ 1231 x0 = real.max; 1232 yl = 0.0L; 1233 x1 = 0.0L; 1234 yh = 1.0L; 1235 dithresh = 4.0 * real.epsilon; 1236 1237 /* approximation to inverse function */ 1238 d = 1.0L/(9.0L*a); 1239 y = 1.0L - d - normalDistributionInvImpl(y0) * sqrt(d); 1240 x = a * y * y * y; 1241 1242 lgm = logGamma(a); 1243 1244 for( i=0; i<10; i++ ) { 1245 if( x > x0 || x < x1 ) 1246 goto ihalve; 1247 y = gammaIncompleteCompl(a,x); 1248 if ( y < yl || y > yh ) 1249 goto ihalve; 1250 if ( y < y0 ) { 1251 x0 = x; 1252 yl = y; 1253 } else { 1254 x1 = x; 1255 yh = y; 1256 } 1257 /* compute the derivative of the function at this point */ 1258 d = (a - 1.0L) * log(x0) - x0 - lgm; 1259 if ( d < -MAXLOGL ) 1260 goto ihalve; 1261 d = -exp(d); 1262 /* compute the step to the next approximation of x */ 1263 d = (y - y0)/d; 1264 x = x - d; 1265 if ( i < 3 ) continue; 1266 if ( fabs(d/x) < dithresh ) return x; 1267 } 1268 1269 /* Resort to interval halving if Newton iteration did not converge. */ 1270 ihalve: 1271 d = 0.0625L; 1272 if ( x0 == real.max ) { 1273 if( x <= 0.0L ) 1274 x = 1.0L; 1275 while( x0 == real.max ) { 1276 x = (1.0L + d) * x; 1277 y = gammaIncompleteCompl( a, x ); 1278 if ( y < y0 ) { 1279 x0 = x; 1280 yl = y; 1281 break; 1282 } 1283 d = d + d; 1284 } 1285 } 1286 d = 0.5L; 1287 dir = 0; 1288 1289 for( i=0; i<400; i++ ) { 1290 x = x1 + d * (x0 - x1); 1291 y = gammaIncompleteCompl( a, x ); 1292 lgm = (x0 - x1)/(x1 + x0); 1293 if ( fabs(lgm) < dithresh ) 1294 break; 1295 lgm = (y - y0)/y0; 1296 if ( fabs(lgm) < dithresh ) 1297 break; 1298 if ( x <= 0.0L ) 1299 break; 1300 if ( y > y0 ) { 1301 x1 = x; 1302 yh = y; 1303 if ( dir < 0 ) { 1304 dir = 0; 1305 d = 0.5L; 1306 } else if ( dir > 1 ) 1307 d = 0.5L * d + 0.5L; 1308 else 1309 d = (y0 - yl)/(yh - yl); 1310 dir += 1; 1311 } else { 1312 x0 = x; 1313 yl = y; 1314 if ( dir > 0 ) { 1315 dir = 0; 1316 d = 0.5L; 1317 } else if ( dir < -1 ) 1318 d = 0.5L * d; 1319 else 1320 d = (y0 - yl)/(yh - yl); 1321 dir -= 1; 1322 } 1323 } 1324 /+ 1325 if( x == 0.0L ) 1326 mtherr( "igamil", UNDERFLOW ); 1327 +/ 1328 return x; 1329 } 1330 1331 unittest { 1332 //Values from Excel's GammaInv(1-p, x, 1) 1333 test(fabs(gammaIncompleteComplInv(1, 0.5) - 0.693147188044814) < 0.00000005); 1334 test(fabs(gammaIncompleteComplInv(12, 0.99) - 5.42818075054289) < 0.00000005); 1335 test(fabs(gammaIncompleteComplInv(100, 0.8) - 91.5013985848288L) < 0.000005); 1336 1337 test(gammaIncomplete(1, 0)==0); 1338 test(gammaIncompleteCompl(1, 0)==1); 1339 test(gammaIncomplete(4545, real.infinity)==1); 1340 1341 // Values from Excel's (1-GammaDist(x, alpha, 1, TRUE)) 1342 1343 test(fabs(1.0L-gammaIncompleteCompl(0.5, 2) - 0.954499729507309L) < 0.00000005); 1344 test(fabs(gammaIncomplete(0.5, 2) - 0.954499729507309L) < 0.00000005); 1345 // Fixed Cephes bug: 1346 test(gammaIncompleteCompl(384, real.infinity)==0); 1347 test(gammaIncompleteComplInv(3, 0)==real.infinity); 1348 } 1349 1350 /** Digamma function 1351 * 1352 * The digamma function is the logarithmic derivative of the gamma function. 1353 * 1354 * digamma(x) = d/dx logGamma(x) 1355 * 1356 */ 1357 real digamma(real x) 1358 { 1359 // Based on CEPHES, Stephen L. Moshier. 1360 1361 // DAC: These values are Bn / n for n=2,4,6,8,10,12,14. 1362 static immutable real [] Bn_n = [ 1363 1.0L/(6*2), -1.0L/(30*4), 1.0L/(42*6), -1.0L/(30*8), 1364 5.0L/(66*10), -691.0L/(2730*12), 7.0L/(6*14) ]; 1365 1366 real p, q, nz, s, w, y, z; 1367 int i, n, negative; 1368 1369 negative = 0; 1370 nz = 0.0; 1371 1372 if ( x <= 0.0 ) { 1373 negative = 1; 1374 q = x; 1375 p = floor(q); 1376 if( p == q ) { 1377 return NaN(TANGO_NAN.GAMMA_POLE); // singularity. 1378 } 1379 /* Remove the zeros of tan(PI x) 1380 * by subtracting the nearest integer from x 1381 */ 1382 nz = q - p; 1383 if ( nz != 0.5 ) { 1384 if ( nz > 0.5 ) { 1385 p += 1.0; 1386 nz = q - p; 1387 } 1388 nz = PI/tan(PI*nz); 1389 } else { 1390 nz = 0.0; 1391 } 1392 x = 1.0 - x; 1393 } 1394 1395 // check for small positive integer 1396 if ((x <= 13.0) && (x == floor(x)) ) { 1397 y = 0.0; 1398 n = rndint(x); 1399 // DAC: CEPHES bugfix. Cephes did this in reverse order, which 1400 // created a larger roundoff error. 1401 for (i=n-1; i>0; --i) { 1402 y+=1.0L/i; 1403 } 1404 y -= EULERGAMMA; 1405 goto done; 1406 } 1407 1408 s = x; 1409 w = 0.0; 1410 while ( s < 10.0 ) { 1411 w += 1.0/s; 1412 s += 1.0; 1413 } 1414 1415 if ( s < 1.0e17 ) { 1416 z = 1.0/(s * s); 1417 y = z * poly(z, Bn_n); 1418 } else 1419 y = 0.0; 1420 1421 y = log(s) - 0.5L/s - y - w; 1422 1423 done: 1424 if ( negative ) { 1425 y -= nz; 1426 } 1427 return y; 1428 } 1429 1430 unittest { 1431 // Exact values 1432 test(digamma(1)== -EULERGAMMA); 1433 test(feqrel(digamma(0.25), -PI/2 - 3* LN2 - EULERGAMMA)>=real.mant_dig-7); 1434 test(feqrel(digamma(1.0L/6), -PI/2 *sqrt(3.0L) - 2* LN2 -1.5*log(3.0L) - EULERGAMMA)>=real.mant_dig-7); 1435 test(isNaN(digamma(-5))); 1436 test(feqrel(digamma(2.5), -EULERGAMMA - 2*LN2 + 2.0 + 2.0L/3)>=real.mant_dig-9); 1437 test(isIdentical(digamma(NaN(0xABC)), NaN(0xABC))); 1438 1439 for (int k=1; k<40; ++k) { 1440 real y=0; 1441 for (int u=k; u>=1; --u) { 1442 y+= 1.0L/u; 1443 } 1444 test(feqrel(digamma(k+1),-EULERGAMMA + y) >=real.mant_dig-2); 1445 } 1446 1447 // printf("%d %La %La %d %d\n", k+1, digamma(k+1), -EULERGAMMA + x, feqrel(digamma(k+1),-EULERGAMMA + y), feqrel(digamma(k+1), -EULERGAMMA + x)); 1448 }