Skip to content

Commit e2f4ec7

Browse files
authored
Merge pull request #392 from JuliaMath/an/fixbetaincwhenratioislarge
Underflow without erroring in beta_inc
2 parents 7a74eac + 13a0fe5 commit e2f4ec7

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/beta_inc.jl

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -371,19 +371,21 @@ function beta_inc_asymptotic_asymmetric(a::Float64, b::Float64, x::Float64, y::F
371371
end
372372
z = -nu*lnx
373373
if b*z == 0.0
374-
return error("expansion can't be computed")
374+
@debug("underflow: expansion can't be computed")
375+
return w
375376
end
376377

377378
# COMPUTATION OF THE EXPANSION
378379
#SET R = EXP(-Z)*Z**B/GAMMA(B)
379380
r = b*(1.0 + rgamma1pm1(b))*exp(b*log(z))
380381
r *= exp(a*lnx)*exp(0.5*bm1*lnx)
381-
u = loggammadiv(b,a) + b*log(nu)
382+
u = loggammadiv(b, a) + b*log(nu)
382383
u = r*exp(-u)
383384
if u == 0.0
384-
return error("expansion can't be computed")
385+
@debug("underflow: expansion can't be computed")
386+
return w
385387
end
386-
(p, q) = gamma_inc(b,z,0)
388+
p, q = gamma_inc(b, z, 0)
387389
v = inv(nu)^2/4
388390
t2 = lnx^2/4
389391
l = w/u
@@ -412,7 +414,8 @@ function beta_inc_asymptotic_asymmetric(a::Float64, b::Float64, x::Float64, y::F
412414
dj = d[n] * j
413415
sm += dj
414416
if sm <= 0.0
415-
return error("expansion can't be computed")
417+
@debug("underflow: expansion can't be computed")
418+
return w
416419
end
417420
if abs(dj) <= epps*(sm+l)
418421
break
@@ -837,7 +840,7 @@ function _beta_inc(a::Float64, b::Float64, x::Float64, y::Float64=1-x)
837840
elseif a0 < min(epps, epps*b0) && b0*x0 <= 1.0
838841
q = beta_inc_power_series1(a0, b0, x0, epps)
839842
p = 1.0 - q
840-
elseif max(a0,b0) > 1.0
843+
elseif max(a0, b0) > 1.0
841844
if b0 <= 1.0
842845
p = beta_inc_power_series(a0, b0, x0, epps)
843846
q = 1.0 - p

test/beta_inc.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@
230230
@test SpecialFunctions.loggammadiv(13.89, 21.0001) log(gamma(big(21.0001))/gamma(big(21.0001)+big(13.89)))
231231
@test SpecialFunctions.loggammadiv(big(13.89), big(21.0001)) log(gamma(big(21.0001))/gamma(big(21.0001)+big(13.89)))
232232
@test SpecialFunctions.stirling_corr(11.99, 100.1) SpecialFunctions.stirling_error(11.99) + SpecialFunctions.stirling_error(100.1) - SpecialFunctions.stirling_error(11.99 + 100.1)
233+
234+
@testset "Issue 334. Underflow without erroring" begin
235+
@test beta_inc(0.1, 4000, 0.2) == (1.0, 0.0)
236+
@test beta_inc(4000, 0.1, 0.2) == (0.0, 1.0)
237+
end
233238
end
234239

235240
@testset "inverse of incomplete beta" begin

0 commit comments

Comments
 (0)