Skip to content

Commit

Permalink
BUG: Fisher test corner case/gh #276 (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
kshedden authored Oct 11, 2022
1 parent be980f3 commit 0493ff3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/fisher.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ end

function pvalue_both_minlike(x::FisherExactTest, ω::Float64=1.0)
a, b, c, d = reorder(x.a, x.b, x.c, x.d)

if a == c == 0 || b == d == 0
return 1.0
end
dist = FisherNoncentralHypergeometric(a+b, c+d, a+c, ω)

p = pdf(dist, a)
Expand Down Expand Up @@ -179,6 +181,9 @@ Fisher's non-central hypergeometric distribution. For `tail = :both`, the only
"""
function StatsBase.confint(x::FisherExactTest; level::Float64=0.95, tail=:both, method=:central)
check_level(level)
if x.a == x.c == 0 || x.b == x.d == 0
return (0.0, Inf)
end
dist(ω) = FisherNoncentralHypergeometric(x.a+x.b, x.c+x.d, x.a+x.c, ω)
obj(ω) = pvalue(dist(ω), x.a, tail=tail) - (1-level)

Expand Down Expand Up @@ -245,6 +250,9 @@ end
# since the mode and mean of Fisher's Noncentral Hypergeometric distribution
# coincide, this is equivalent to find ω, s.t., mean(dist(ω)) = a
function cond_mle_odds_ratio(a::Int, b::Int, c::Int, d::Int)
if a == c == 0 || b == d == 0
return 0.0
end
dist(ω) = FisherNoncentralHypergeometric(a+b, c+d, a+c, ω)

if (a == minimum(dist(1.0)))
Expand Down
19 changes: 19 additions & 0 deletions test/fisher.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,25 @@ t = HypothesisTests.FisherExactTest(4, 1, 20, 1)
#@test_approx_eq [confint(t; method=:minlike)...] [0.005, 9.5943]
show(IOBuffer(), t)

# Corner cases gh #276
t = HypothesisTests.FisherExactTest(5, 0, 5, 0)
@test pvalue(t; tail=:left) 1
@test pvalue(t; tail=:right) 1
@test pvalue(t; method=:central) 1
@test pvalue(t; method=:minlike) 1
@test_ci_approx confint(t; tail=:left) (0.0, Inf)
@test_ci_approx confint(t; tail=:right) (0.0, Inf)
@test_ci_approx confint(t; method=:central) (0.0, Inf)

t = HypothesisTests.FisherExactTest(0, 5, 0, 5)
@test pvalue(t; tail=:left) 1
@test pvalue(t; tail=:right) 1
@test pvalue(t; method=:central) 1
@test pvalue(t; method=:minlike) 1
@test_ci_approx confint(t; tail=:left) (0.0, Inf)
@test_ci_approx confint(t; tail=:right) (0.0, Inf)
@test_ci_approx confint(t; method=:central) (0.0, Inf)

t = HypothesisTests.FisherExactTest(1, 1, 1, 1)
@test HypothesisTests.pvalue(t, tail=:both) <= 1
show(IOBuffer(), t)
Expand Down

0 comments on commit 0493ff3

Please sign in to comment.