Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit number of Newton iterations in beta_inc_inv #396

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/beta_inc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,9 @@ function beta_inc_inv(a::Real, b::Real, p::Real, q::Real)
end

function _beta_inc_inv(a::Float64, b::Float64, p::Float64, q::Float64=1-p)

maxiter = 30

#change tail if necessary
if p > 0.5
y, x = _beta_inc_inv(b, a, q, p)
Expand Down Expand Up @@ -968,8 +971,8 @@ function _beta_inc_inv(a::Float64, b::Float64, p::Float64, q::Float64=1-p)
sq = 1.0
prev = 1.0

if x < 0.0001
x = 0.0001
if x < 1e-200
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any specific reason for this choice?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementation-wise, it seems the section here could be simplified to

    x = clamp(x, 1e-200, 0.9999)

x = 1e-200
end
if x > .9999
x = .9999
Expand Down Expand Up @@ -1001,7 +1004,7 @@ function _beta_inc_inv(a::Float64, b::Float64, p::Float64, q::Float64=1-p)
acu = exp10(iex)

#iterate
while true
for i in 1:maxiter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe

Suggested change
for i in 1:maxiter
for _ in 1:maxiter

since we don't use i?

p_approx = beta_inc(a, b, x)[1]
xin = x
p_approx = (p_approx - p)*min(
Expand All @@ -1026,7 +1029,7 @@ function _beta_inc_inv(a::Float64, b::Float64, p::Float64, q::Float64=1-p)
end

#check if current estimate is acceptable

prev, acu, p_approx, x, tx
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this was for debugging?

Suggested change
prev, acu, p_approx, x, tx

if prev <= acu || p_approx^2 <= acu
x = tx
return (x, 1.0 - x)
Expand All @@ -1039,6 +1042,9 @@ function _beta_inc_inv(a::Float64, b::Float64, p::Float64, q::Float64=1-p)
x = tx
p_approx_prev = p_approx
end

@debug "Newton iterations didn't converge in $maxiter iterations. The result might have reduced precision."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably deserves a higher logging level?

Suggested change
@debug "Newton iterations didn't converge in $maxiter iterations. The result might have reduced precision."
@warn "Newton iterations didn't converge in $maxiter iterations. The result might have reduced precision."

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I deliberately chose @debug over @warn to avoid that we raise warnings in normal usage. These functions sometimes lose precision in corner cases and it's not something we generally warn about. Eventually, we should have a better and more granular way of providing this information. E.g. via error codes and that's why I added the @debug statement.

return (x, 1.0 - x)
end

function _beta_inc_inv(a::T, b::T, p::T) where {T<:Union{Float16, Float32}}
Expand Down
6 changes: 6 additions & 0 deletions test/beta_inc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,10 @@ end
@test first(beta_inc_inv(1.0, 1.0, 1e-21)) ≈ 1e-21
@test beta_inc_inv(1.0e30, 1.0, 0.49) == (1.0, 0.0)
end

@testset "Avoid infinite loops" begin
# See https://github.com/JuliaStats/StatsFuns.jl/issues/133#issuecomment-1069602721
y = 2.0e-280
@test beta_inc(2.0, 1.0, beta_inc_inv(2.0, 1.0, y, 1.0)[1])[1] ≈ y
end
end