Skip to content

Commit

Permalink
enforcing naming consistency for all discrete univariate distributions
Browse files Browse the repository at this point in the history
  • Loading branch information
lindahua committed Aug 3, 2015
1 parent f2de979 commit 32ae52c
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 42 deletions.
8 changes: 3 additions & 5 deletions src/univariate/discrete/bernoulli.jl
Original file line number Diff line number Diff line change
@@ -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

Expand Down
6 changes: 4 additions & 2 deletions src/univariate/discrete/categorical.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions src/univariate/discrete/discreteuniform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions src/univariate/discrete/geometric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 6 additions & 7 deletions src/univariate/discrete/hypergeometric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 7 additions & 6 deletions src/univariate/discrete/negativebinomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions src/univariate/discrete/noncentralhypergeometric.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Noncentral hypergeometric distribution
# TODO: this distribution needs clean-up and testing

abstract NoncentralHypergeometric <: DiscreteUnivariateDistribution

Expand Down Expand Up @@ -104,6 +105,3 @@ function pdf(d::WalleniusNoncentralHypergeometric, k::Int)
end

logpdf(d::WalleniusNoncentralHypergeometric, k::Int) = log(pdf(d, k))



9 changes: 4 additions & 5 deletions src/univariate/discrete/poisson.jl
Original file line number Diff line number Diff line change
@@ -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

Expand Down
1 change: 1 addition & 0 deletions src/univariate/discrete/poissonbinomial.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# TODO: this distribution may need clean-up

# Computes the pdf of a poisson-binomial random variable using
# fast fourier transform
Expand Down
12 changes: 4 additions & 8 deletions src/univariate/discrete/skellam.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -55,4 +52,3 @@ end
### Sampling

rand(d::Skellam) = rand(Poisson(d.μ1)) - rand(Poisson(d.μ2))

0 comments on commit 32ae52c

Please sign in to comment.