-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Add lbinomial function based on lbeta #27419
Conversation
base/special/gamma.jl
Outdated
Natural logarithm of the [`binomial`](@ref) coefficient. | ||
""" | ||
function lbinomial(n::T, k::T) where {T<:Integer} | ||
const S = float(T) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
┌ Warning: Deprecated syntax ``const
declaration on local variable
around special/gamma.jl:165.
base/special/gamma.jl
Outdated
""" | ||
function lbinomial(n::T, k::T) where {T<:Integer} | ||
const S = float(T) | ||
(k < 0) && return typemin(S) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extra space between < 0
base/special/gamma.jl
Outdated
(k == 0 || k == n) && return zero(S) | ||
(k == 1) && return log(abs(n)) | ||
if k > (n>>1) | ||
k = n -k |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
space after -
(n - k
)
base/special/gamma.jl
Outdated
|
||
Natural logarithm of the [`binomial`](@ref) coefficient. | ||
""" | ||
function lbinomial(n::T, k::T) where {T<:Integer} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also define:
lbinomial(n::Integer, k::Integer) = lbinomial(promote(n, k)...)
?
test/math.jl
Outdated
@test lbinomial(10, 0) == 0.0 | ||
@test lbinomial(10, 10) == 0.0 | ||
@test lbinomial(10, 1) ≈ log(10) | ||
@test lbfixed(200).(0:200) ≈ blbfixed(200).(0:200) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe add a test for the behavior when n < 0
?
end | ||
k > n && return typemin(S) | ||
(k == 0 || k == n) && return zero(S) | ||
(k == 1) && return log(abs(n)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the code for binomial
, it looks like this is log(n < 0 && isodd(k) ? -n : n)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I guess this is equivalent, it just needs isodd(k) && throw(DomainError())
in the n < 0
branch?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, it's typical for lbinomial
to actually mean log(abs(binomial))
. I didn't note that in the docstr however.
I've rebased with:
|
Any interest in |
Maybe the Combinatorics.jl package would be a good place for this? |
This is a great function to have somewhere but we are generally trying to slim down Base to a minimum. I also think that |
I didn't know where |
Since #27473 has been merged, I'll just close this and open in |
This implementation of
lbinomial
uses the fact thatbinomial(n,k) = 1/( (n+1) * B(n - k + 1, k + 1) )
. I have added tests for some simple cases, and then also comparing for very largen
withbinomial(::BigInt, ::BigInt)
.The main motivation is evaluating
log(binomial(n,k))
for largen
withk
nearn/2
without approximating with Poisson:In R, this function is available as
lchoose(n, k)
and is a builtin.