From 194adf956290118b8cddc434474205abfb454ead Mon Sep 17 00:00:00 2001 From: Marcus P S Date: Tue, 17 Dec 2024 04:34:40 +0000 Subject: [PATCH 1/5] Returns smallest `mode` in `mode(Binomial)`, fix `modes` Fix #1927 --- src/univariate/discrete/binomial.jl | 31 ++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/univariate/discrete/binomial.jl b/src/univariate/discrete/binomial.jl index 5beb4137d..979a54c8b 100644 --- a/src/univariate/discrete/binomial.jl +++ b/src/univariate/discrete/binomial.jl @@ -67,11 +67,36 @@ params(d::Binomial) = (d.n, d.p) mean(d::Binomial) = ntrials(d) * succprob(d) var(d::Binomial) = ntrials(d) * succprob(d) * failprob(d) -function mode(d::Binomial{T}) where T<:Real + +function mode(d::Binomial) + (n, p) = params(d) + v = (n + 1) * p + quasi_mode = floor(Int, v) + if quasi_mode == v + if p == 1 + n + else + quasi_mode-1 + end + else + quasi_mode + end +end + +function modes(d::Binomial) (n, p) = params(d) - n > 0 ? floor(Int, (n + 1) * d.p) : zero(T) + v = (n + 1) * p + quasi_mode = floor(Int, v) + if quasi_mode == v + if p == 1 + Int[n] + else + Int[quasi_mode-1, quasi_mode] + end + else + Int[quasi_mode] + end end -modes(d::Binomial) = Int[mode(d)] function skewness(d::Binomial) n, p1 = params(d) From ea39724477c8f7162533673cdd41356bd9253150 Mon Sep 17 00:00:00 2001 From: Marcus P S Date: Tue, 17 Dec 2024 05:08:45 +0000 Subject: [PATCH 2/5] Update tests for mode(Binomial) and modes(Binomial) --- test/univariate/discrete/binomial.jl | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/test/univariate/discrete/binomial.jl b/test/univariate/discrete/binomial.jl index 5f95735bb..03c7924b9 100644 --- a/test/univariate/discrete/binomial.jl +++ b/test/univariate/discrete/binomial.jl @@ -32,11 +32,18 @@ end @test median(Binomial(45,3//10)) == 13 @test median(Binomial(65,3//10)) == 19 @test median(Binomial(85,3//10)) == 25 - + # Test mode -@test Distributions.mode(Binomial(100, 0.4)) == 40 -@test Distributions.mode(Binomial(1, 0.51)) == 1 -@test Distributions.mode(Binomial(1, 0.49)) == 0 +@test mode(Binomial(100, 0.4)) == 40 +@test mode(Binomial(1, 0.51)) == 1 +@test mode(Binomial(1, 0.49)) == 0 +@test mode(Binomial(4, 2//3)) == 3 +@test mode(Binomial(6, 2//7)) == 1 +@test mode(Binomial(7, 1//8)) == 0 + +@test modes(Binomial(4, 2//3)) == [3, 4] +@test modes(Binomial(6, 2//7)) == [1, 2] +@test modes(Binomial(7, 1//8)) == [0, 1] @test isplatykurtic(Bernoulli(0.5)) @test ismesokurtic(Normal(0.0, 1.0)) From 87df67f50c8d40ab34bfbdd36f548726993aad8f Mon Sep 17 00:00:00 2001 From: Marcus P S Date: Thu, 19 Dec 2024 03:24:58 +0000 Subject: [PATCH 3/5] Fix incorrect test Co-authored-by: David Widmann --- test/univariate/discrete/binomial.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/univariate/discrete/binomial.jl b/test/univariate/discrete/binomial.jl index 03c7924b9..170f4ae86 100644 --- a/test/univariate/discrete/binomial.jl +++ b/test/univariate/discrete/binomial.jl @@ -41,7 +41,7 @@ end @test mode(Binomial(6, 2//7)) == 1 @test mode(Binomial(7, 1//8)) == 0 -@test modes(Binomial(4, 2//3)) == [3, 4] +@test modes(Binomial(4, 2//3)) == [3] @test modes(Binomial(6, 2//7)) == [1, 2] @test modes(Binomial(7, 1//8)) == [0, 1] From 1116bf16e1fddb8eaafb81dabac8d4cde74f63e1 Mon Sep 17 00:00:00 2001 From: Marcus P S Date: Thu, 19 Dec 2024 03:42:45 +0000 Subject: [PATCH 4/5] Add a few more tests --- test/univariate/discrete/binomial.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/univariate/discrete/binomial.jl b/test/univariate/discrete/binomial.jl index 170f4ae86..2f810b9d2 100644 --- a/test/univariate/discrete/binomial.jl +++ b/test/univariate/discrete/binomial.jl @@ -42,7 +42,11 @@ end @test mode(Binomial(7, 1//8)) == 0 @test modes(Binomial(4, 2//3)) == [3] +@test modes(Binomial(5, 3//4)) == [4] +@test modes(Binomial(3, 2//4)) == [1, 2] +@test modes(Binomial(4, 2//5)) == [1, 2] @test modes(Binomial(6, 2//7)) == [1, 2] +@test modes(Binomial(6, 3//7)) == [2, 3] @test modes(Binomial(7, 1//8)) == [0, 1] @test isplatykurtic(Bernoulli(0.5)) From 0fac6c007af90ee83713cbd28001a19d20e56b5a Mon Sep 17 00:00:00 2001 From: Marcus P S Date: Thu, 19 Dec 2024 05:42:31 +0000 Subject: [PATCH 5/5] Fix bug with mode calculation condition --- src/univariate/discrete/binomial.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/univariate/discrete/binomial.jl b/src/univariate/discrete/binomial.jl index 979a54c8b..cb400b775 100644 --- a/src/univariate/discrete/binomial.jl +++ b/src/univariate/discrete/binomial.jl @@ -72,7 +72,7 @@ function mode(d::Binomial) (n, p) = params(d) v = (n + 1) * p quasi_mode = floor(Int, v) - if quasi_mode == v + if quasi_mode == v && p > 0 if p == 1 n else