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

Fix logabsbeta for BigFloat #325

Merged
merged 4 commits into from
Jun 4, 2021
Merged

Conversation

devmotion
Copy link
Member

@devmotion devmotion commented Jun 2, 2021

This PR adds a fallback definition for _loggammadiv. This fixes test errors in TuringLang/DistributionsAD.jl#172.

Motivation:
Version 1.4.1, or more concretely #316 (@andreasnoack), caused some regressions of logbeta and logabsbeta with BigFloats.

On 1.4.0:

julia> using SpecialFunctions

julia> beta(big(nextfloat(0.0)), big(nextfloat(8.0)))
2.024022533073106183524953467189173070495566497641421183569013580274303395679953e+323

julia> logbeta(big(nextfloat(0.0)), big(nextfloat(8.0)))
744.4400719213812623141072984460816341130871443029141429256103301959047499954517

julia> logabsbeta(big(nextfloat(0.0)), big(nextfloat(8.0)))
(744.4400719213812623141072984460816341130871443029141429256103301959047499954517, 1)

On 1.4.1:

julia> beta(big(nextfloat(0.0)), big(nextfloat(8.0)))
2.024022533073106183524953467189173070495566497641421183569013580274303395679953e+323

julia> logbeta(big(nextfloat(0.0)), big(nextfloat(8.0)))
ERROR: MethodError: no method matching _loggammadiv(::BigFloat, ::BigFloat)
Stacktrace:
 [1] loggammadiv(a::BigFloat, b::BigFloat)
   @ SpecialFunctions ~/.julia/packages/SpecialFunctions/W3J2O/src/beta_inc.jl:12
 [2] logabsbeta(a::BigFloat, b::BigFloat)
   @ SpecialFunctions ~/.julia/packages/SpecialFunctions/W3J2O/src/gamma.jl:822
 [3] logbeta(a::BigFloat, b::BigFloat)
   @ SpecialFunctions ~/.julia/packages/SpecialFunctions/W3J2O/src/gamma.jl:788
 [4] top-level scope
   @ REPL[15]:1

julia> logabsbeta(big(nextfloat(0.0)), big(nextfloat(8.0)))
ERROR: MethodError: no method matching _loggammadiv(::BigFloat, ::BigFloat)
Stacktrace:
 [1] loggammadiv(a::BigFloat, b::BigFloat)
   @ SpecialFunctions ~/.julia/packages/SpecialFunctions/W3J2O/src/beta_inc.jl:12
 [2] logabsbeta(a::BigFloat, b::BigFloat)
   @ SpecialFunctions ~/.julia/packages/SpecialFunctions/W3J2O/src/gamma.jl:822
 [3] top-level scope
   @ REPL[16]:1

beta still works since it uses the definition in mpfr on my machine. Actually, the problem was present in SpecialFunctions 1.4.0 as well but it is easier to trigger it in 1.4.1 since the branch is chosen not only for inputs with large difference in magnitude anymore (on 1.4.0:

if abs(a) > 1e5*abs(b) && abs(a) > 1e5
, on 1.4.1:
if a > 0 && b > 8
).

On 1.4.0:

julia> logabsbeta(big(nextfloat(1e5)), big(1.0))
ERROR: MethodError: no method matching _loggammadiv(::BigFloat, ::BigFloat)
Stacktrace:
 [1] loggammadiv(a::BigFloat, b::BigFloat)
   @ SpecialFunctions ~/.julia/packages/SpecialFunctions/L13jJ/src/beta_inc.jl:12
 [2] logabsbeta(a::BigFloat, b::BigFloat)
   @ SpecialFunctions ~/.julia/packages/SpecialFunctions/L13jJ/src/gamma.jl:843
 [3] top-level scope
   @ REPL[14]:1

With this PR all examples work:

julia> beta(big(nextfloat(0.0)), big(nextfloat(8.0)))
2.024022533073106183524953467189173070495566497641421183569013580274303395679953e+323

julia> logbeta(big(nextfloat(0.0)), big(nextfloat(8.0)))
744.4400719213812623141072984460816341130871443029141429256103301959047499954517

julia> logabsbeta(big(nextfloat(0.0)), big(nextfloat(8.0)))
(744.4400719213812623141072984460816341130871443029141429256103301959047499954517, 1)

julia> logabsbeta(big(nextfloat(1e5)), big(1.0))
(-11.51292546497022856560910955709032851649991676439065368877238753498953004770512, 1)

@devmotion
Copy link
Member Author

Test failures are the same as on the master branch, it seems.

@giordano
Copy link
Member

giordano commented Jun 2, 2021

Test failures are the same as on the master branch, it seems.

I'm lost, are they due to #319? Is there anything we can do to have a usable state?

@devmotion
Copy link
Member Author

Hopefully #327 fixes it.

@codecov
Copy link

codecov bot commented Jun 2, 2021

Codecov Report

Merging #325 (9e7461f) into master (f67c0f6) will decrease coverage by 0.03%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #325      +/-   ##
==========================================
- Coverage   92.70%   92.66%   -0.04%     
==========================================
  Files          12       12              
  Lines        2645     2646       +1     
==========================================
  Hits         2452     2452              
- Misses        193      194       +1     
Flag Coverage Δ
unittests 92.66% <100.00%> (-0.04%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/beta_inc.jl 90.54% <100.00%> (-0.30%) ⬇️
src/gamma.jl 94.91% <0.00%> (+0.26%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f67c0f6...9e7461f. Read the comment docs.

src/beta_inc.jl Outdated

_loggammadiv(a::Number, b::Number) = loggamma(b) - loggamma(a + b)
Copy link
Member

Choose a reason for hiding this comment

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

I think it would be useful to add a comment here as a reminder that a proper implementation should be added at some point.

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 added a comment.

@andreasnoack andreasnoack merged commit 66eb6c7 into JuliaMath:master Jun 4, 2021
@devmotion devmotion deleted the dw/logabsbeta branch June 4, 2021 20:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants