From f2de979d6086ec87f6008c74c7ae17723bf78299 Mon Sep 17 00:00:00 2001 From: Dahua Lin Date: Sun, 2 Aug 2015 21:22:20 +0800 Subject: [PATCH] Enforce naming consistency for Bernoulli & Binomial --- src/univariate/discrete/bernoulli.jl | 16 +++++----------- src/univariate/discrete/binomial.jl | 14 +++++++------- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/src/univariate/discrete/bernoulli.jl b/src/univariate/discrete/bernoulli.jl index ed89b2e35..f0d812e5b 100644 --- a/src/univariate/discrete/bernoulli.jl +++ b/src/univariate/discrete/bernoulli.jl @@ -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 @@ -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)) @@ -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)] @@ -126,5 +122,3 @@ function suffstats{T<:Integer}(::Type{Bernoulli}, x::AbstractArray{T}, w::Abstra end BernoulliStats(c0, c1) end - - diff --git a/src/univariate/discrete/binomial.jl b/src/univariate/discrete/binomial.jl index 2ed142c52..57ad3bf7a 100644 --- a/src/univariate/discrete/binomial.jl +++ b/src/univariate/discrete/binomial.jl @@ -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