Skip to content

Commit

Permalink
Bound binomial CI (#296)
Browse files Browse the repository at this point in the history
* bound binomial CIs on [0, 1]

* test

* patch bump

* break it down by method

* Apply suggestions from code review

Co-authored-by: Alex Arslan <[email protected]>

* test for upper bound clamping

---------

Co-authored-by: Alex Arslan <[email protected]>
  • Loading branch information
palday and ararslan authored May 7, 2023
1 parent d19a8e1 commit a1713cf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "HypothesisTests"
uuid = "09f84164-cd44-5f33-b23f-e6b0d136a0d5"
version = "0.10.12"
version = "0.10.13"

[deps]
Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"
Expand Down
10 changes: 7 additions & 3 deletions src/binomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ end
function ci_wald(x::BinomialTest, alpha::Float64=0.05)
μ = x.x / x.n
σ = sqrt*(1-μ)/x.n)
(quantile(Normal(μ, σ), alpha/2), quantile(Normal(μ, σ), 1-alpha/2))
lower, upper = (quantile(Normal(μ, σ), alpha/2), quantile(Normal(μ, σ), 1-alpha/2))
# make sure we stay in [0, 1]
(max(lower, 0), min(upper, 1))
end

# Jeffreys interval
Expand All @@ -154,7 +156,8 @@ function ci_agresti_coull(x::BinomialTest, alpha::Float64=0.05)
n = x.n + q^2
μ = (x.x + q^2/2)/n
σ = sqrt*(1-μ)/n)
-q*σ, μ+q*σ)
# make sure we stay in [0, 1]
(max-q*σ, 0), min+q*σ, 1))
end

# Wilson score interval
Expand All @@ -166,7 +169,8 @@ function ci_wilson(x::BinomialTest, alpha::Float64=0.05)
μ /= denominator
σ = sqrt(p*(1-p)/x.n + q^2/(4x.n^2))
σ /= denominator
-q*σ, μ+q*σ)
# make sure we stay in [0, 1]
(max-q*σ, 0), min+q*σ, 1))
end

# Arcsine transformation interval as based on Cohen's H: https://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Arcsine_transformation
Expand Down
14 changes: 14 additions & 0 deletions test/binomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ using HypothesisTests: default_tail
t = BinomialTest(100, 100, 0.99)
@test pvalue(t) 0.7320646825464584
show(IOBuffer(), t)

# from issue #295
# without clamping: (-0.05457239484968546, 0.4890548596328611)
@test_ci_approx confint(BinomialTest(0, 5), method=:agresti_coull) (0.0, 0.4890548596328611)
# without clamping: (0.5109451403671388, 1.0545723948496855)
@test_ci_approx confint(BinomialTest(5, 5), method=:agresti_coull) (0.5109451403671388, 1.0)
# without clamping: (-0.15060901623063327, 0.5506090162306333)
@test_ci_approx confint(BinomialTest(1, 5), method=:wald) (0.0, 0.5506090162306333)
# without clamping: (0.44939098376936687, 1.1506090162306333)
@test_ci_approx confint(BinomialTest(4, 5), method=:wald) (0.44939098376936687, 1.0)
# without clamping: (-2.7755575615628914e-17, 0.2775327998628899)
@test_ci_approx confint(BinomialTest(0, 10), method=:wilson) (0.0, 0.2775327998628899)
# without clamping: (0.7575059933447587, 1.0000000000000002)
@test_ci_approx confint(BinomialTest(12, 12), method=:wilson) (0.7575059933447587, 1.0)
end

@testset "SignTest" begin
Expand Down

2 comments on commit a1713cf

@palday
Copy link
Member Author

@palday palday commented on a1713cf May 7, 2023

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/83071

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.10.13 -m "<description of version>" a1713cfec37abd10f010961964f8f6bb11a3cc5f
git push origin v0.10.13

Please sign in to comment.