Skip to content

Commit 8863a5f

Browse files
committed
Fix a bug in beta_inc
1 parent 5dd89c8 commit 8863a5f

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

src/beta_inc.jl

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ function beta_integrand(a::Float64, b::Float64, x::Float64, y::Float64, mu::Floa
105105
y0 = h/(1.0 + h)
106106
lambda = (a+b)*y - b
107107
else
108-
h = a/b
109-
x0 = h/(1.0 + h)
110-
y0 = 1.0/(1.0 + h)
111-
lambda = a - (a+b)*x
108+
h = a/b
109+
x0 = h/(1.0 + h)
110+
y0 = 1.0/(1.0 + h)
111+
lambda = a - (a+b)*x
112112
end
113113
e = -lambda/a
114114
u = abs(e) > 0.6 ? e - log(x/x0) : - LogExpFunctions.log1pmx(e)
@@ -155,23 +155,25 @@ function beta_integrand(a::Float64, b::Float64, x::Float64, y::Float64, mu::Floa
155155
t = 1.0 + rgamma1pm1(apb)
156156
end
157157
return a0*(esum(mu,z))*(1.0 + rgamma1pm1(b0))/t
158+
else
159+
ans = esum(mu, z)
160+
if ans == 0.0
161+
return 0.0
162+
end
163+
apb = a + b
164+
if apb > 1.0
165+
z = (1.0 + rgamma1pm1(apb - 1.0))/apb
166+
else
167+
z = 1.0 + rgamma1pm1(apb)
168+
end
169+
c = (1.0 + rgamma1pm1(a))*(1.0 + rgamma1pm1(b))/z
170+
return ans*(a0*c)/(1.0 + a0/b0)
158171
end
159172
else
160-
z -= logbeta(a,b)
161-
ans = esum(mu,z)
173+
z -= logbeta(a, b)
174+
ans = esum(mu, z)
162175
return ans
163176
end
164-
if ans == 0.0
165-
return 0.0
166-
end
167-
apb = a + b
168-
if apb > 1.0
169-
z = (1.0 + rgamma1pm1(apb - 1.0))/apb
170-
else
171-
z = 1.0 + rgamma1pm1(apb)
172-
end
173-
c = (1.0 + rgamma1pm1(a))*(1.0 + rgamma1pm1(b))/z
174-
return ans*(a0*c)/(1.0 + a0/b0)
175177
end
176178

177179
"""

test/beta_inc.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,9 @@
206206
3.186244273953916e-22,2.121983888512568e-22,1.063002842170579e-22,0])
207207
@test beta_inc(1e-20, 0.99, x, 1.0 - x)[2] val rtol=1e-14
208208
end
209+
209210
@test beta_inc(1.5, 200.5, 0.07,0.93)[1] 0.99999790408564
211+
@test beta_inc(1e-20, 0.000001, 0.2)[2] 1.0000013862929421e-14
210212

211213
@test SpecialFunctions.loggammadiv(13.89, 21.0001) log(gamma(big(21.0001))/gamma(big(21.0001)+big(13.89)))
212214
@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)

0 commit comments

Comments
 (0)