Skip to content

Commit

Permalink
Clean up distribution constructors.
Browse files Browse the repository at this point in the history
1. remove `@compat` in many constructors. The new function can automatically convert values, one need not write something explicitly as `@compat new(Float64(x))`.
2. use the macro `@check_args` to provide a uniform way for argument checking during construction.
  • Loading branch information
lindahua committed Aug 3, 2015
1 parent 32ae52c commit fe4cba0
Show file tree
Hide file tree
Showing 52 changed files with 178 additions and 305 deletions.
11 changes: 5 additions & 6 deletions src/edgeworth.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ kurtosis(d::EdgeworthAbstract) = kurtosis(d.dist) / d.n
immutable EdgeworthZ{D<:UnivariateDistribution} <: EdgeworthAbstract
dist::D
n::Float64

function EdgeworthZ{T<:UnivariateDistribution}(d::T, n::Real)
n > zero(n) ||
error("n must be positive")
@compat new(d, Float64(n))
@check_args(EdgeworthZ, n > zero(n))
new(d, n)
end
end
EdgeworthZ(d::UnivariateDistribution,n::Real) = EdgeworthZ{typeof(d)}(d,n)
Expand Down Expand Up @@ -78,9 +78,8 @@ immutable EdgeworthSum{D<:UnivariateDistribution} <: EdgeworthAbstract
dist::D
n::Float64
function EdgeworthSum{T<:UnivariateDistribution}(d::T, n::Real)
n > zero(n) ||
error("n must be positive")
@compat new(d, Float64(n))
@check_args(EdgeworthSum, n > zero(n))
new(d, n)
end
end
EdgeworthSum(d::UnivariateDistribution, n::Real) = EdgeworthSum{typeof(d)}(d,n)
Expand Down
2 changes: 1 addition & 1 deletion src/matrix/inversewishart.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function _logpdf(d::InverseWishart, X::DenseMatrix{Float64})
-0.5 * ((df + p + 1) * logdet(Xcf) + trace(Xcf \ Ψ)) - d.c0
end

@compat _logpdf{T<:Real}(d::InverseWishart, X::DenseMatrix{T}) = _logpdf(d, Float64(X))
_logpdf{T<:Real}(d::InverseWishart, X::DenseMatrix{T}) = _logpdf(d, convert(Matrix{Float64}, X))


#### Sampling
Expand Down
4 changes: 2 additions & 2 deletions src/matrix/wishart.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function mode(d::Wishart)
end
end

function meanlogdet(d::Wishart)
function meanlogdet(d::Wishart)
p = dim(d)
df = d.df
v = logdet(d.S) + p * logtwo
Expand All @@ -81,7 +81,7 @@ function _logpdf(d::Wishart, X::DenseMatrix{Float64})
0.5 * ((df - (p + 1)) * logdet(Xcf) - trace(d.S \ X)) - d.c0
end

@compat _logpdf{T<:Real}(d::Wishart, X::DenseMatrix{T}) = _logpdf(d, Float64(X))
_logpdf{T<:Real}(d::Wishart, X::DenseMatrix{T}) = _logpdf(d, convert(Matrix{Float64}, X))

#### Sampling

Expand Down
2 changes: 1 addition & 1 deletion src/multivariate/dirichlet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ immutable Dirichlet <: ContinuousMultivariateDistribution
lmnB::Float64 = 0.0
for i in 1:length(alpha)
ai = alpha[i]
ai > 0 || throw(ArgumentError("alpha must be a positive vector."))
ai > 0 || throw(ArgumentError("Dirichlet: alpha must be a positive vector."))
alpha0 += ai
lmnB += lgamma(ai)
end
Expand Down
2 changes: 1 addition & 1 deletion src/testutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ end
function test_stats(d::DiscreteUnivariateDistribution, vs::AbstractVector)
# using definition (or an approximation)

@compat vf = Float64[Float64(v) for v in vs]
vf = Float64[v for v in vs]
p = pdf(d, vf)
xmean = dot(p, vf)
xvar = dot(p, abs2(vf .- xmean))
Expand Down
9 changes: 2 additions & 7 deletions src/univariate/continuous/arcsine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@ immutable Arcsine <: ContinuousUnivariateDistribution
a::Float64
b::Float64

function Arcsine(a::Float64, b::Float64)
a < b || throw(ArgumentError("Arcsine: a must be less than b."))
new(a, b)
end

@compat Arcsine(a::Real, b::Real) = Arcsine(Float64(a), Float64(b))
@compat Arcsine(b::Real) = Arcsine(0.0, Float64(b))
Arcsine(a::Real, b::Real) = (@check_args(Arcsine, a < b); new(a, b))
Arcsine(b::Real) = (@check_args(Arcsine, b > zero(b)); new(0.0, b))
Arcsine() = new(0.0, 1.0)
end

Expand Down
6 changes: 2 additions & 4 deletions src/univariate/continuous/beta.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ immutable Beta <: ContinuousUnivariateDistribution
β::Float64

function Beta::Real, β::Real)
> zero(α) && β > zero(β)) ||
throw(ArgumentError("Beta: α and β must be positive"))
@compat new(Float64(α), Float64(β))
@check_args(Beta, α > zero(α) && β > zero(β))
new(α, β)
end

Beta::Real) = Beta(α, α)
Beta() = new(1.0, 1.0)
end
Expand Down
10 changes: 4 additions & 6 deletions src/univariate/continuous/betaprime.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ immutable BetaPrime <: ContinuousUnivariateDistribution
α::Float64
β::Float64

function BetaPrime::Float64, β::Float64)
> zero(α) && β > zero(β)) ||
throw(ArgumentError("BetaPrime: α and β must be positive."))
@compat new(Float64(α), Float64(β))
function BetaPrime::Real, β::Real)
@check_args(BetaPrime, α > zero(α) && β > zero(β))
new(α, β)
end

BetaPrime::Float64) = BetaPrime(α)
BetaPrime::Real) = BetaPrime(α)
BetaPrime() = new(1.0, 1.0)
end

Expand Down
7 changes: 1 addition & 6 deletions src/univariate/continuous/biweight.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ immutable Biweight <: ContinuousUnivariateDistribution
μ::Float64
σ::Float64

function Biweight::Real, σ::Real)
σ > zero(σ) ||
throw(ArgumentError("Biweight: σ must be positive."))
@compat new(Float64(μ), Float64(σ))
end

Biweight::Real, σ::Real) = (@check_args(Biweight, σ > zero(σ)); new(μ, σ))
Biweight::Real) = new(μ, 1.0)
Biweight() = new(0.0, 1.0)
end
Expand Down
9 changes: 2 additions & 7 deletions src/univariate/continuous/cauchy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@ immutable Cauchy <: ContinuousUnivariateDistribution
μ::Float64
σ::Float64

function Cauchy::Real, σ::Real)
σ > zero(σ) ||
throw(ArgumentError("Cauchy: σ must be positive."))
@compat new(Float64(μ), Float64(σ))
end

@compat Cauchy::Real) = new(Float64(μ), 1.0)
Cauchy::Real, σ::Real) = (@check_args(Cauchy, σ > zero(σ)); new(μ, σ))
Cauchy::Real) = new(μ, 1.0)
Cauchy() = new(0.0, 1.0)
end

Expand Down
6 changes: 1 addition & 5 deletions src/univariate/continuous/chi.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
immutable Chi <: ContinuousUnivariateDistribution
ν::Float64

function Chi::Real)
ν > zero(ν) || throw(ArgumentError("Chi: ν must be positive."))
@compat new(Float64(ν))
end
Chi::Real) = (@check_args(Chi, ν > zero(ν)); new(ν))
end

@distr_support Chi 0.0 Inf


#### Parameters

dof(d::Chi) = d.ν
Expand Down
6 changes: 1 addition & 5 deletions src/univariate/continuous/chisq.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
immutable Chisq <: ContinuousUnivariateDistribution
ν::Float64

function Chisq::Real)
ν > zero(ν) ||
throw(ArgumentError("Chisq: ν must be positive."))
@compat new(Float64(ν))
end
Chisq::Real) = (@check_args(Chisq, ν > zero(ν)); new(ν))
end

@distr_support Chisq 0.0 Inf
Expand Down
8 changes: 2 additions & 6 deletions src/univariate/continuous/cosine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ immutable Cosine <: ContinuousUnivariateDistribution
μ::Float64
σ::Float64

function Cosine::Real, σ::Real)
σ > 0.0 || error("Cosine: σ must be positive.")
@compat new(Float64(μ), Float64(σ))
end

Cosine::Real) = @compat new(Float64(μ), 1.0)
Cosine::Real, σ::Real) = (@check_args(Cosine, σ > zero(σ)); new(μ, σ))
Cosine::Real) = new(μ, 1.0)
Cosine() = new(0.0, 1.0)
end

Expand Down
8 changes: 2 additions & 6 deletions src/univariate/continuous/epanechnikov.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
immutable Epanechnikov <: ContinuousUnivariateDistribution
μ::Float64
σ::Float64
function Epanechnikov::Real, σ::Real)
σ > zero(σ) ||
throw(ArgumentError("Epanechnikov: σ must be positive."))
@compat new(Float64(μ), Float64(σ))
end

Epanechnikov::Real) = @compat new(Float64(μ), 1.0)
Epanechnikov::Real, σ::Real) = (@check_args(Epanechnikov, σ > zero(σ)); new(μ, σ))
Epanechnikov::Real) = new(μ, 1.0)
Epanechnikov() = new(0.0, 1.0)
end

Expand Down
4 changes: 1 addition & 3 deletions src/univariate/continuous/erlang.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ immutable Erlang <: ContinuousUnivariateDistribution
gammad::Gamma

function Erlang::Real, θ::Real)
isinteger(α) || error("Erlang shape parameter must be an integer")
@check_args(Erlang, isinteger(α) && α >= zero(α))
new(Gamma(α, θ))
end

Erlang::Real) = Erlang(α, 1.0)
Erlang() = new(Gamma())
end
Expand Down Expand Up @@ -56,4 +55,3 @@ cf(d::Erlang, t::Real) = cf(d.gammad, t)
#### Sampling

rand(d::Erlang) = rand(d.gammad)

8 changes: 2 additions & 6 deletions src/univariate/continuous/exponential.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
immutable Exponential <: ContinuousUnivariateDistribution
θ::Float64 # note: scale not rate

function Exponential::Real)
θ > zero(θ) ||
throw(ArgumentError("Exponential: scale must be positive"))
@compat new(Float64(θ))
end
Exponential::Real) = (@check_args(Exponential, θ > zero(θ)); new(θ))
Exponential() = new(1.0)
end

Expand Down Expand Up @@ -72,7 +68,7 @@ immutable ExponentialStats <: SufficientStats
sx::Float64 # (weighted) sum of x
sw::Float64 # sum of sample weights

@compat ExponentialStats(sx::Real, sw::Real) = new(Float64(sx), Float64(sw))
ExponentialStats(sx::Real, sw::Real) = new(sx, sw)
end

suffstats{T<:Real}(::Type{Exponential}, x::AbstractArray{T}) = ExponentialStats(sum(x), length(x))
Expand Down
6 changes: 3 additions & 3 deletions src/univariate/continuous/fdist.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ immutable FDist <: ContinuousUnivariateDistribution
ν2::Float64

function FDist(ν1::Real, ν2::Real)
ν1 > zero(ν1) && ν2 > zero(ν2) ||
throw(ArgumentError("FDist: ν1 and ν2 must be positive."))
@compat new(Float64(ν1), Float64(ν2))
@check_args(FDist, ν1 > zero(ν1) && ν2 > zero(ν2))
new(ν1, ν2)
end
end

@distr_support FDist 0.0 Inf


#### Parameters

params(d::FDist) = (d.ν1, d.ν2)
Expand Down
6 changes: 2 additions & 4 deletions src/univariate/continuous/frechet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ immutable Frechet <: ContinuousUnivariateDistribution
θ::Float64

function Frechet::Real, θ::Real)
α > zero(α) && θ > zero(θ) ||
throw(ArgumentError("Frechet: both α and θ must be positive."))
@compat new(Float64(α), Float64(θ))
@check_args(Frechet, α > zero(α) && θ > zero(θ))
new(α, θ)
end

Frechet::Real) = Frechet(α, 1.0)
Frechet() = new(1.0, 1.0)
end
Expand Down
13 changes: 7 additions & 6 deletions src/univariate/continuous/gamma.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ immutable Gamma <: ContinuousUnivariateDistribution
θ::Float64

function Gamma::Real, θ::Real)
α > zero(α) && θ > zero(θ) ||
throw(ArgumentError("Gamma: both α and Θ must be positive"))
@compat new(Float64(α), Float64(θ))
@check_args(Gamma, α > zero(α) && θ > zero(θ))
new(α, θ)
end
function Gamma::Real)
@check_args(Gamma, α > zero(α))
new(α, 1.0)
end

Gamma::Real) = @compat Gamma(Float64(α), 1.0)
Gamma() = new(1.0, 1.0)
end

Expand Down Expand Up @@ -66,7 +67,7 @@ immutable GammaStats <: SufficientStats
slogx::Float64 # (weighted) sum of log(x)
tw::Float64 # total sample weight

@compat GammaStats(sx::Real, slogx::Real, tw::Real) = new(Float64(sx), Float64(slogx), Float64(tw))
GammaStats(sx::Real, slogx::Real, tw::Real) = new(sx, slogx, tw)
end

function suffstats{T<:Real}(::Type{Gamma}, x::AbstractArray{T})
Expand Down
9 changes: 2 additions & 7 deletions src/univariate/continuous/gumbel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@ immutable Gumbel <: ContinuousUnivariateDistribution
μ::Float64 # location
θ::Float64 # scale

function Gumbel::Real, θ::Real)
θ > zero(θ) ||
throw(ArgumentError("Gumbel: Θ must be positive."))
@compat new(Float64(μ), Float64(θ))
end

Gumbel::Real) = @compat Gumbel(Float64(μ), 1.0)
Gumbel::Real, θ::Real) = (@check_args(Gumbel, θ > zero(θ)); new(μ, θ))
Gumbel::Real) = new(μ, 1.0)
Gumbel() = new(0.0, 1.0)
end

Expand Down
11 changes: 5 additions & 6 deletions src/univariate/continuous/inversegamma.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ immutable InverseGamma <: ContinuousUnivariateDistribution
θ::Float64

function InverseGamma::Real, θ::Real)
> zero(α) && θ > zero(θ)) ||
throw(ArgumentError("InverseGamma: both α and θ must be positive."))
@compat new(Gamma(α, 1.0 / θ), Float64(θ))
@check_args(InverseGamma, α > zero(α) && θ > zero(θ))
new(Gamma(α, 1.0 / θ), θ)
end

InverseGamma::Real) = @compat InverseGamma(Float64(α), 1.0)
InverseGamma::Real) = InverseGamma(α, 1.0)
InverseGamma() = InverseGamma(1.0, 1.0)
end

Expand Down Expand Up @@ -72,12 +71,12 @@ invlogccdf(d::InverseGamma, p::Float64) = 1.0 / invlogcdf(d.invd, p)

function mgf(d::InverseGamma, t::Real)
(a, b) = params(d)
@compat t == zero(t) ? one(Float64(t)) : 2.0*(-b*t)^(0.5a) / gamma(a) * besselk(a, sqrt(-4.0*b*t))
t == zero(t) ? 1.0 : 2.0*(-b*t)^(0.5a) / gamma(a) * besselk(a, sqrt(-4.0*b*t))
end

function cf(d::InverseGamma, t::Real)
(a, b) = params(d)
@compat t == zero(t) ? complex(one(Float64(t))) : 2.0*(-im*b*t)^(0.5a) / gamma(a) * besselk(a, sqrt(-4.0*im*b*t))
t == zero(t) ? 1.0+0.0im : 2.0*(-im*b*t)^(0.5a) / gamma(a) * besselk(a, sqrt(-4.0*im*b*t))
end


Expand Down
8 changes: 3 additions & 5 deletions src/univariate/continuous/inversegaussian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ immutable InverseGaussian <: ContinuousUnivariateDistribution
λ::Float64

function InverseGaussian::Real, λ::Real)
> zero(μ) && λ > zero(λ)) ||
throw(ArgumentError("InverseGaussian: μ and λ must be positive."))
@compat new(Float64(μ), Float64(λ))
@check_args(InverseGaussian, μ > zero(μ) && λ > zero(λ))
new(μ, λ)
end

InverseGaussian::Real) = @compat InverseGaussian(Float64(μ), 1.0)
InverseGaussian::Real) = InverseGaussian(μ, 1.0)
InverseGaussian() = new(1.0, 1.0)
end

Expand Down
8 changes: 2 additions & 6 deletions src/univariate/continuous/laplace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ immutable Laplace <: ContinuousUnivariateDistribution
μ::Float64
θ::Float64

function Laplace::Real, θ::Real)
θ > zero(θ) || error("Laplace's scale must be positive")
@compat new(Float64(μ), Float64(θ))
end

@compat Laplace::Real) = new(Float64(μ), 1.0)
Laplace::Real, θ::Real) = (@check_args(Laplace, θ > zero(θ)); new(μ, θ))
Laplace::Real) = new(μ, 1.0)
Laplace() = new(0.0, 1.0)
end

Expand Down
Loading

0 comments on commit fe4cba0

Please sign in to comment.