-
Notifications
You must be signed in to change notification settings - Fork 40
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
Native binompdf and binomlogpdf #33
Closed
Closed
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
f678962
Native binompdf and binomlogpdf
Ellipse0934 07ca55d
Added tests for bino
Ellipse0934 0fc437c
Test fix for 32 bit machines
Ellipse0934 4493ba0
32 bit fix + sfe fix
Ellipse0934 fc8a130
binomlogpdf fix
Ellipse0934 3343a17
binompdf test
Ellipse0934 cc5689f
binompdf test check
Ellipse0934 a0b72a4
binomlogpdf test check
Ellipse0934 d4f572c
New testing
Ellipse0934 2ad0331
32 bit machine fix
Ellipse0934 8a3c998
retest
Ellipse0934 bcec450
Test fix
Ellipse0934 f922d4f
32 machine type assert fix
Ellipse0934 e6d96e7
Changes based on review
Ellipse0934 990d5c3
typo fix
Ellipse0934 541a94c
sfe(x) change and inv
Ellipse0934 d533cf7
Changed stirlerr argument
Ellipse0934 0b3ca4b
small fix sfe
Ellipse0934 0daf0b4
typo fix
Ellipse0934 a9cdcf1
updated stirlerr
Ellipse0934 1052ffc
type care, stirlerr removed
Ellipse0934 84eaed4
Argument errors and lstirling_asym changes
Ellipse0934 05dd41f
Small changes
Ellipse0934 5062ff0
small fix
Ellipse0934 dea75b8
Merge remote-tracking branch 'origin/master' into binomial
Ellipse0934 5a8789d
Auto stash before merge of "binomial" and "origin/master"
Ellipse0934 79f35e7
bug fix in test
Ellipse0934 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# Computation of binomial probability distribution function using Catherine Loader's Saddle point algorithm (http://octave.1599824.n4.nabble.com/attachment/3829107/0/loader2000Fast.pdf) | ||
# binompdf(n, p, x) | ||
# log(binompdf(n, p, x)) = log(binompdf(n, x/n, x)) - Dev(n, p, x) | ||
# Deviance term 'Dev': Dev(n, p, x) = xlog(x/(np)) + (n-x)log((n - x)/(n*(1 - p))) | ||
# Stirling's approximation: n! = √(2*π*n) * (n/e)^n + δ(n), where δ(n) is the error in the stirling approximation | ||
# δ(n) ≈ lstirling_asym(n), the Asymptotic stirling series expansion error, https://dlmf.nist.gov/5.11, lstirling_asym defined in misc.jl | ||
# p(n, x/n, x) = √(n/(2πx(n - x)))*e^(δ(n) - δ(x) - δ(n - x)), log(p(n, x/n, x)) = 0.5*log(n/(2*pi*x*(n - x))) + δ(n) - δ(x) - δ(n - x) | ||
""" | ||
binompdf(n::Integer, p::Float, x::Integer) | ||
|
||
Computes the binomial probability distibution function. | ||
Given probability *`p`* of for success of each trial, returns the probability that *`x`* out of *`n`* trials are successful. | ||
|
||
# Examples | ||
```julia-repl | ||
julia> binompdf(13, 0.58, 7) | ||
0.20797396939077062 | ||
``` | ||
# Arguments | ||
- `n::Integer`: total number of trials. | ||
- `p::Float`: probability of success of each trial. | ||
- `x::Integer`: number of successful trials. | ||
... | ||
""" | ||
function binompdf{ T <: Union{Float16, Float32, Float64} }(n::Integer, p::T, x::Integer) | ||
n > typemax(Int64) && error("n is too large.") | ||
n < 0 && throw(ArgumentError("n = $n must be a non-zero positive integer")) | ||
( x > n || x < 0 ) && throw(ArgumentError("x = $x must ∈ [0, n]")) | ||
(p < 0.0 || p > 1.0) && throw(ArgumentError("p = $p must ∈ [0, 1]")) | ||
n == 0 && return one(T) | ||
p == 0.0 && return ( (x == 0) ? one(T): zero(T) ) | ||
p == 1.0 && return ( (x == n) ? zero(T) : zero(T) ) | ||
x == 0 && return exp(n*log1p(-p)) | ||
x == n && return p^n | ||
p = Float64(p) | ||
lc = lstirling_asym(n) - lstirling_asym(x) - lstirling_asym(n - x) -D(x, n*p) - D(n - x, n*(1 - p)) | ||
return T(exp(lc)*sqrt(n/(2π*x*(n - x)))) | ||
end | ||
|
||
# Deviance term: D(x, np) = x*log(x/np) + np - x | ||
D(x, np) = -x*logmxp1(np/x) | ||
|
||
# binomlogpdf(n, p, x) = log(binompdf(n, p, x)) | ||
# We use the same strategy as above but do not exponentiate the final result | ||
""" | ||
binomlogpdf(n::Integer, p::Float, x::Integer) | ||
|
||
Computes the log of the binomial probability distibution function. | ||
|
||
# Examples | ||
```julia-repl | ||
julia> binomlogpdf(13, 0.58, 7) | ||
-1.5703423542721349 | ||
``` | ||
# Arguments | ||
- `n::Integer`: total number of trials. | ||
- `p::Float`: probability of success of each trial. | ||
- `x::Integer`: number of successful trials. | ||
... | ||
""" | ||
function binomlogpdf{ T <: Union{Float16, Float32, Float64} }(n::Integer, p::T, x::Integer) | ||
n > typemax(Int64) && error("n is too large") | ||
n < 0 && throw(ArgumentError("n = $n must be a non-zero positive integer")) | ||
( x > n || x < 0 ) && throw(ArgumentError("x = $x must ∈ [0, n]")) | ||
(p < 0.0 || p > 1.0) && throw(ArgumentError("p = $p must ∈ [0, 1]")) | ||
n == 0 && return zero(T) | ||
p == 0.0 && return ( (x == 0) ? zero(T) : T(-Inf) ) | ||
p == 1.0 && return ( (x == n) ? zero(T) : T(-Inf) ) | ||
x == 0 && return n*log1p(-p) | ||
x == n && return n*log(p) | ||
p = Float64(p) | ||
(n, x) = (Int64(n), Int64(x)) | ||
lc = lstirling_asym(n) - lstirling_asym(x) - lstirling_asym(n - x) - D(x, n*p) - D(n - x, n*(1.0 - p)) | ||
return T(lc + 0.5*log(n/(2π*x*(n - x)))) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using StatsFuns | ||
using Base.Test | ||
|
||
#Test Compares the difference in values w.r.t Rmath implementation | ||
|
||
N = Int64.( (0, 1, 1, 8, 20, 20, 20, 150, 700, 900, 1000) ); | ||
P = (0.0, 0.5, 0.7, 0.6, 0.34, 0.89, 0.53, 0.77, 0.98, 0.5, 0.29); | ||
|
||
@testset "binompdf" begin | ||
for i = 1:length(N) | ||
for x = Int64(0):N[i] | ||
if isfinite(binompdf(N[i], P[i], x)) | ||
@test abs(binompdf(N[i], P[i], x) - Rmath.dbinom(x, N[i], P[i])) < 1e-15 | ||
end | ||
end | ||
end | ||
end | ||
|
||
@testset "binomlogpdf" begin | ||
for i = 1:length(N) | ||
for x = Int64(0):N[i] | ||
val = abs(binomlogpdf(N[i], P[i], x) - Rmath.dbinom(x, N[i], P[i], true))/binomlogpdf(N[i], P[i], x) | ||
if isfinite(val) && binomlogpdf(N[i], P[i], x) != Rmath.dbinom(x, N[i], P[i], true) | ||
@test val < 2.24e-14 | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Isn’t this inferred to be
Int64
by default?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.
Yep, 2 year old PR, back when I had no idea how Julia worked, will fix this tomorrow.