Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
18 changes: 18 additions & 0 deletions src/univariate/discrete/binomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ function mode(d::Binomial{T}) where T<:Real
end
modes(d::Binomial) = Int[mode(d)]

function median(dist::Binomial)
bound = min(dist.p, 1-dist.p) # from http://dx.doi.org/10.1111/j.1467-9574.1980.tb00681.x
dist_mean = mean(dist)

floor_mean = floor(Int, dist_mean)
difference = dist_mean - floor_mean

if difference <= bound
floor_mean
elseif difference >= 1 - bound
floor_mean + 1
elseif cdf(dist, floor_mean) >= 0.5
floor_mean
else
floor_mean + 1
end
end

function skewness(d::Binomial)
n, p1 = params(d)
p0 = 1 - p1
Expand Down
6 changes: 5 additions & 1 deletion test/univariate/discrete/binomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ end
@test median(Binomial(45,3//10)) == 13
@test median(Binomial(65,3//10)) == 19
@test median(Binomial(85,3//10)) == 25


@test median(Binomial(25,0.756)) == 19
@test median(Binomial(25,1//2)) == 12
@test median(Binomial(25,3//5)) == 15

# Test mode
@test Distributions.mode(Binomial(100, 0.4)) == 40
@test Distributions.mode(Binomial(1, 0.51)) == 1
Expand Down
Loading