# \`mode\` and \`modes\` fail in Distributions.jl

**URL:** <https://discourse.julialang.org/t/mode-and-modes-fail-in-distributions-jl/103634>\
**Category:** Statistics\
**Tags:** package\
**Created:** [September 7, 2023, 10:57pm UTC](https://discourse.julialang.org/t/mode-and-modes-fail-in-distributions-jl/103634 "2023-09-07T22:57:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aerdely](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aerdely/32/37506_2.png) [@aerdely](https://discourse.julialang.org/u/aerdely)\
**Post date:** [September 7, 2023, 10:57pm UTC](https://discourse.julialang.org/t/mode-and-modes-fail-in-distributions-jl/103634/1 "2023-09-07T22:57:30Z")

</div>

The `Distributions.jl` package does not calculate correctly the theoretical mode and/or modes under models like `Binomial`, `Poisson` and `NegativeBinomial` here some examples:

```julia
using Distributions

## Example 1
B = Binomial(3, 0.5)
v = collect(0:3);
hcat(v, pdf.(B, v)) # theoretical modes are 1 and 2
pdf(B, 1) == pdf(B, 2) # true, as expected
modes(B) # should return 1 and 2 not just 2
mode(B) # should return 1 not 2
# But surprisingly:
D = DiscreteNonParametric(v, pdf.(B, v))
pdf(D, 1) == pdf(D, 2) # true, as expected
modes(D) # 1 and 2, as expected
mode(D) # 1, as expected

## Example 2
P = Poisson(2.0)
x = collect(0:4);
hcat(x, pdf.(P, x)) # theoretical modes are 1 and 2
pdf(P, 1) == pdf(P, 2) # true, as expected
modes(P) # 1 and 2, as expected
mode(P) # should return 1 not 2

## Example 3
N = NegativeBinomial(3, 0.5)
y = collect(0:3);
hcat(y, pdf.(N, y)) # theoretical modes are 1 and 2
pdf(N, 1) == pdf(N, 2) # should be true
pdf(N, 1) - pdf(N, 2) # very small positive difference
pdf(N, 1) > pdf(N, 2) # so numerically the mode should be 1 but:
mode(N) # oops! 2 instead of 1
modes(N) # should return 1 and 2 not just 2

```

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [September 7, 2023, 11:47pm UTC](https://discourse.julialang.org/t/mode-and-modes-fail-in-distributions-jl/103634/2 "2023-09-07T23:47:24Z")

</div>

This is indeed an error. It falls back to a [generic](https://github.com/JuliaStats/Distributions.jl/blob/2cd6c678a1f237462d71496c39178dab1e265c61/src/univariates.jl#L195) method because no method for `modes` is defined for `NegativeBinomial`. I can submit an issue if you do not have a github account.

---

<div class="post-metadata">

**Author:** ![aerdely](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aerdely/32/37506_2.png) [@aerdely](https://discourse.julialang.org/u/aerdely)\
**Post date:** [September 8, 2023, 12:14am UTC](https://discourse.julialang.org/t/mode-and-modes-fail-in-distributions-jl/103634/3 "2023-09-08T00:14:17Z")

</div>

> [@aerdely](#):
>
> ```julia
> using Distributions
> 
> ## Example 1
> B = Binomial(3, 0.5)
> v = collect(0:3);
> hcat(v, pdf.(B, v)) # theoretical modes are 1 and 2
> pdf(B, 1) == pdf(B, 2) # true, as expected
> modes(B) # should return 1 and 2 not just 2
> mode(B) # should return 1 not 2
> # But surprisingly:
> D = DiscreteNonParametric(v, pdf.(B, v))
> pdf(D, 1) == pdf(D, 2) # true, as expected
> modes(D) # 1 and 2, as expected
> mode(D) # 1, as expected
> 
> ## Example 2
> P = Poisson(2.0)
> x = collect(0:4);
> hcat(x, pdf.(P, x)) # theoretical modes are 1 and 2
> pdf(P, 1) == pdf(P, 2) # true, as expected
> modes(P) # 1 and 2, as expected
> mode(P) # should return 1 not 2
> 
> ## Example 3
> N = NegativeBinomial(3, 0.5)
> y = collect(0:3);
> hcat(y, pdf.(N, y)) # theoretical modes are 1 and 2
> pdf(N, 1) == pdf(N, 2) # should be true
> pdf(N, 1) - pdf(N, 2) # very small positive difference
> pdf(N, 1) > pdf(N, 2) # so numerically the mode should be 1 but:
> mode(N) # oops! 2 instead of 1
> modes(N) # should return 1 and 2 not just 2
> 
> ```

Ok then, I have just submitted the issue. Thanks.

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [September 8, 2023, 9:41am UTC](https://discourse.julialang.org/t/mode-and-modes-fail-in-distributions-jl/103634/4 "2023-09-08T09:41:29Z")

</div>

I am cross-referencing the [issue](https://github.com/JuliaStats/Distributions.jl/issues/1772) in case someone comes across this thread.
