diff --git a/src/univariate/discrete/bernoulli.jl b/src/univariate/discrete/bernoulli.jl index f0d812e5b..8d613fe45 100644 --- a/src/univariate/discrete/bernoulli.jl +++ b/src/univariate/discrete/bernoulli.jl @@ -1,13 +1,11 @@ immutable Bernoulli <: DiscreteUnivariateDistribution p::Float64 - function Bernoulli(p::Float64) - 0.0 <= p <= 1.0 || + function Bernoulli(p::Real) + zero(p) <= p <= one(p) || throw(ArgumentError("Bernoulli: p must be in [0, 1].")) - new(p) + @compat new(Float64(p)) end - - Bernoulli(p::Real) = @compat Bernoulli(Float64(p)) Bernoulli() = new(0.5) end diff --git a/src/univariate/discrete/categorical.jl b/src/univariate/discrete/categorical.jl index 6731aac28..2512812c5 100644 --- a/src/univariate/discrete/categorical.jl +++ b/src/univariate/discrete/categorical.jl @@ -5,12 +5,14 @@ immutable Categorical <: DiscreteUnivariateDistribution Categorical(p::Vector{Float64}, ::NoArgCheck) = new(length(p), p) function Categorical(p::Vector{Float64}) - isprobvec(p) || error("p is not a valid probability vector.") + isprobvec(p) || + throw(ArgumentError("Categorical: p is not a valid probability vector.")) new(length(p), p) end function Categorical(k::Integer) - k >= 1 || error("k must be a positive integer.") + k >= 1 || + throw(ArgumentError("k must be a positive integer.")) new(k, fill(1.0/k, k)) end end diff --git a/src/univariate/discrete/discreteuniform.jl b/src/univariate/discrete/discreteuniform.jl index 110c7e0c4..90f5f2ce3 100644 --- a/src/univariate/discrete/discreteuniform.jl +++ b/src/univariate/discrete/discreteuniform.jl @@ -3,13 +3,15 @@ immutable DiscreteUniform <: DiscreteUnivariateDistribution b::Int pv::Float64 - function DiscreteUniform(a::Int, b::Int) - a <= b || error("a and b must satisfy a <= b") + function DiscreteUniform(a::Real, b::Real) + a <= b || + throw(ArgumentError("DiscreteUniform: a and b must satisfy a <= b.")) + @compat a_ = Int(a) + @compat b_ = Int(b) new(a, b, 1.0 / (b - a + 1)) end - @compat DiscreteUniform(a::Real, b::Real) = DiscreteUniform(round(Int, a), round(Int, b)) - DiscreteUniform(b::Real) = DiscreteUniform(0, round(Int, b)) + DiscreteUniform(b::Real) = DiscreteUniform(0, b) DiscreteUniform() = new(0, 1, 0.5) end diff --git a/src/univariate/discrete/geometric.jl b/src/univariate/discrete/geometric.jl index d77901ac7..f58483279 100644 --- a/src/univariate/discrete/geometric.jl +++ b/src/univariate/discrete/geometric.jl @@ -2,11 +2,12 @@ immutable Geometric <: DiscreteUnivariateDistribution p::Float64 function Geometric(p::Real) - zero(p) < p < one(p) || error("prob must be in (0, 1)") + zero(p) < p < one(p) || + throw(ArgumentError("Geometric: p must be in (0, 1).")) @compat new(Float64(p)) end - Geometric() = Geometric(0.5) # Flips of a fair coin + Geometric() = new(0.5) # Flips of a fair coin end @distr_support Geometric 0 Inf diff --git a/src/univariate/discrete/hypergeometric.jl b/src/univariate/discrete/hypergeometric.jl index 8845eb54f..ea9e2339c 100644 --- a/src/univariate/discrete/hypergeometric.jl +++ b/src/univariate/discrete/hypergeometric.jl @@ -11,14 +11,13 @@ immutable Hypergeometric <: DiscreteUnivariateDistribution nf::Int # number of failures in population n::Int # sample size - function Hypergeometric(ns::Int, nf::Int, n::Int) - ns >= 0 || error("ns must be non-negative.") - nf >= 0 || error("nf must be non-negative.") - 0 < n < ns + nf || error("n must have 0 < n < ns + nf") - new(ns, nf, n) + function Hypergeometric(ns::Real, nf::Real, n::Real) + (ns >= 0 && nf >= 0) || + throw(ArgumentError("Hypergeometric: ns must be non-negative.")) + 0 < n < ns + nf || + throw(ArgumentError("Hypergeometric: n must have 0 < n < ns + nf.")) + @compat new(Int(ns), Int(nf), Int(n)) end - - @compat Hypergeometric(ns::Real, nf::Real, n::Real) = Hypergeometric(round(Int, ns), round(Int, nf), round(Int, n)) end @distr_support Hypergeometric max(d.n - d.nf, 0) min(d.ns, d.n) diff --git a/src/univariate/discrete/negativebinomial.jl b/src/univariate/discrete/negativebinomial.jl index 5cbde211a..5ca9674f0 100644 --- a/src/univariate/discrete/negativebinomial.jl +++ b/src/univariate/discrete/negativebinomial.jl @@ -8,15 +8,16 @@ immutable NegativeBinomial <: DiscreteUnivariateDistribution r::Int p::Float64 - function NegativeBinomial(r::Int, p::Float64) - r > 0 || error("r must be positive.") - 0.0 < p <= 1.0 || error("prob must be in (0, 1].") - new(r, p) + function NegativeBinomial(r::Real, p::Real) + r > zero(r) || + throw(ArgumentError("NegativeBinomial: r must be positive.")) + zero(p) < p <= one(p) || + throw(ArgumentError("NegativeBinomial: prob must be in (0, 1].")) + @compat new(Int(r), Float64(p)) end - @compat NegativeBinomial(r::Real, p::Real) = NegativeBinomial(round(Int, r), Float64(p)) NegativeBinomial(r::Real) = NegativeBinomial(r, 0.5) - NegativeBinomial() = new(1.0, 0.5) + NegativeBinomial() = new(1, 0.5) end @distr_support NegativeBinomial 0 Inf diff --git a/src/univariate/discrete/noncentralhypergeometric.jl b/src/univariate/discrete/noncentralhypergeometric.jl index c04cdf863..3766897c8 100644 --- a/src/univariate/discrete/noncentralhypergeometric.jl +++ b/src/univariate/discrete/noncentralhypergeometric.jl @@ -1,4 +1,5 @@ # Noncentral hypergeometric distribution +# TODO: this distribution needs clean-up and testing abstract NoncentralHypergeometric <: DiscreteUnivariateDistribution @@ -104,6 +105,3 @@ function pdf(d::WalleniusNoncentralHypergeometric, k::Int) end logpdf(d::WalleniusNoncentralHypergeometric, k::Int) = log(pdf(d, k)) - - - diff --git a/src/univariate/discrete/poisson.jl b/src/univariate/discrete/poisson.jl index c0edc5867..1726dced1 100644 --- a/src/univariate/discrete/poisson.jl +++ b/src/univariate/discrete/poisson.jl @@ -1,12 +1,11 @@ immutable Poisson <: DiscreteUnivariateDistribution λ::Float64 - function Poisson(λ::Float64) - λ > 0.0 || error("λ must be positive.") - new(λ) + function Poisson(λ::Real) + λ > zero(λ) || + throw(ArgumentError("Poisson: λ must be positive.")) + @compat new(Float64(λ)) end - - @compat Poisson(λ::Real) = Poisson(Float64(λ)) Poisson() = new(1.0) end diff --git a/src/univariate/discrete/poissonbinomial.jl b/src/univariate/discrete/poissonbinomial.jl index 1c8e7d73d..afa19394f 100644 --- a/src/univariate/discrete/poissonbinomial.jl +++ b/src/univariate/discrete/poissonbinomial.jl @@ -1,3 +1,4 @@ +# TODO: this distribution may need clean-up # Computes the pdf of a poisson-binomial random variable using # fast fourier transform diff --git a/src/univariate/discrete/skellam.jl b/src/univariate/discrete/skellam.jl index d5fdd3a5c..4522faac1 100644 --- a/src/univariate/discrete/skellam.jl +++ b/src/univariate/discrete/skellam.jl @@ -2,21 +2,18 @@ immutable Skellam <: DiscreteUnivariateDistribution μ1::Float64 μ2::Float64 - function Skellam(μ1::Float64, μ2::Float64) - μ1 > 0.0 && μ2 > 0.0 || error("μ1 and μ2 must be positive.") - new(μ1, μ2) + function Skellam(μ1::Real, μ2::Real) + μ1 > zero(μ1) && μ2 > zero(μ2) || + throw(ArgumentError("Skellam: μ1 and μ2 must be positive.")) + @compat new(Float64(μ1), Float64(μ2)) end - @compat Skellam(μ1::Real, μ2::Real) = Skellam(Float64(μ1), Float64(μ2)) - Skellam(μ::Real) = Skellam(μ, μ) - Skellam() = new(1.0, 1.0) end @distr_support Skellam -Inf Inf - ### Parameters params(d::Skellam) = (d.μ1, d.μ2) @@ -55,4 +52,3 @@ end ### Sampling rand(d::Skellam) = rand(Poisson(d.μ1)) - rand(Poisson(d.μ2)) -