Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions src/univariate/discrete/binomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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 && p > 0
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 median(dist::Binomial)
# The median is floor(Int, mean) or ceil(Int, mean)
Expand Down
17 changes: 14 additions & 3 deletions test/univariate/discrete/binomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,20 @@ end
@test all(median(Binomial(7, p)) == quantile(Binomial(7, p), 1//2) for p in 0:0.1:1)

# 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]
@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))
@test ismesokurtic(Normal(0.0, 1.0))
Expand Down
Loading