Skip to content

Commit

Permalink
Enforce naming consistency for Bernoulli & Binomial
Browse files Browse the repository at this point in the history
  • Loading branch information
lindahua committed Aug 2, 2015
1 parent 2dcb2ee commit f2de979
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
16 changes: 5 additions & 11 deletions src/univariate/discrete/bernoulli.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
# Bernoulli distribution


#### Type and Constructor

immutable Bernoulli <: DiscreteUnivariateDistribution
p::Float64

function Bernoulli(p::Float64)
0.0 <= p <= 1.0 || error("p must be in [0, 1].")
0.0 <= p <= 1.0 ||
throw(ArgumentError("Bernoulli: p must be in [0, 1]."))
new(p)
end

@compat Bernoulli(p::Real) = Bernoulli(Float64(p))
Bernoulli(p::Real) = @compat Bernoulli(Float64(p))
Bernoulli() = new(0.5)
end

Expand Down Expand Up @@ -48,7 +44,7 @@ function median(d::Bernoulli)
p > 0.5 ? 1.0 : 0.5
end

function entropy(d::Bernoulli)
function entropy(d::Bernoulli)
p0 = failprob(d)
p1 = succprob(d)
(p0 == 0.0 || p0 == 1.0) ? 0.0 : -(p0 * log(p0) + p1 * log(p1))
Expand All @@ -57,7 +53,7 @@ end
#### Evaluation

pdf(d::Bernoulli, x::Bool) = x ? succprob(d) : failprob(d)
pdf(d::Bernoulli, x::Int) = x == 0 ? failprob(d) :
pdf(d::Bernoulli, x::Int) = x == 0 ? failprob(d) :
x == 1 ? succprob(d) : 0.0

pdf(d::Bernoulli) = Float64[failprob(d), succprob(d)]
Expand Down Expand Up @@ -126,5 +122,3 @@ function suffstats{T<:Integer}(::Type{Bernoulli}, x::AbstractArray{T}, w::Abstra
end
BernoulliStats(c0, c1)
end


14 changes: 7 additions & 7 deletions src/univariate/discrete/binomial.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@

immutable Binomial <: DiscreteUnivariateDistribution
n::Int
p::Float64

function Binomial(n::Int, p::Float64)
n >= 0 || error("n must be non-negative but is $n.")
0.0 <= p <= 1.0 || error("p must be in [0, 1] but is $p")
new(n, p)
function Binomial(n::Int, p::Real)
n >= zero(n) ||
throw(ArgumentError("Binomial: n must be non-negative but is $n."))
zero(p) <= p <= one(p) ||
throw(ArgumentError("Binomial: p must be in [0, 1] but is $p"))
@compat new(Int(n), Float64(p))
end

@compat Binomial(n::Integer, p::Real) = Binomial(round(Int, n), Float64(p))
@compat Binomial(n::Integer) = Binomial(round(Int, n), 0.5)
Binomial(n::Integer) = Binomial(n, 0.5)
Binomial() = new(1, 0.5)
end

Expand Down

0 comments on commit f2de979

Please sign in to comment.