Skip to content

Commit

Permalink
arg check with keyword (#976)
Browse files Browse the repository at this point in the history
* arg check with keyword

* fix multinomial

* more distributions

* fix ambig method

* fix laplace

* reversed arg check

* Update src/multivariate/multinomial.jl

Co-Authored-By: Milan Bouchet-Valat <nalimilan@club.fr>

* more distributions

* last distributions

* arg check doesn't sort

* formatting

* reverse words

* space around kw, checkargs

* last formatting and typo
  • Loading branch information
matbesancon committed Sep 26, 2019
1 parent 415b79b commit f0cf263
Show file tree
Hide file tree
Showing 54 changed files with 270 additions and 384 deletions.
11 changes: 7 additions & 4 deletions src/multivariate/multinomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,21 @@ struct Multinomial{T<:Real} <: DiscreteMultivariateDistribution
n::Int
p::Vector{T}

function Multinomial{T}(n::Integer, p::Vector{T}) where T
Multinomial{T}(n::Integer, p::Vector{T}) where {T} = new{T}(Int(n), p)
end

function Multinomial(n::Integer, p::Vector{T}; check_args=true) where {T<:Real}
if check_args
if n < 0
throw(ArgumentError("n must be a nonnegative integer."))
end
if !isprobvec(p)
throw(ArgumentError("p = $p is not a probability vector."))
end
new{T}(round(Int, n), p)
end
Multinomial{T}(n::Integer, p::Vector{T}, ::NoArgCheck) where {T} = new{T}(round(Int, n), p)
return Multinomial{T}(n, p)
end
Multinomial(n::Integer, p::Vector{T}) where {T<:Real} = Multinomial{T}(n, p)

Multinomial(n::Integer, k::Integer) = Multinomial{Float64}(round(Int, n), fill(1.0 / k, k))

# Parameters
Expand Down
8 changes: 3 additions & 5 deletions src/univariate/continuous/arcsine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,19 @@ External links
* [Arcsine distribution on Wikipedia](http://en.wikipedia.org/wiki/Arcsine_distribution)
Use `Arcsine(a, b, ::NoArgCheck)` to bypass argument checks.
Use `Arcsine(a, b, check_args=false)` to bypass argument checks.
"""
struct Arcsine{T<:Real} <: ContinuousUnivariateDistribution
a::T
b::T
Arcsine{T}(a::T, b::T) where {T<:Real} = new{T}(a, b)
end

function Arcsine(a::T, b::T) where {T <: Real}
@check_args(Arcsine, a < b)
function Arcsine(a::T, b::T; check_args=true) where {T <: Real}
check_args && @check_args(Arcsine, a < b)
return Arcsine{T}(a, b)
end

Arcsine(a::T, b::T, ::NoArgCheck) where {T <: Real} = Arcsine{T}(a, b)

Arcsine(a::Real, b::Real) = Arcsine(promote(a, b)...)
Arcsine(a::Integer, b::Integer) = Arcsine(float(a), float(b))
Arcsine(b::T) where {T <: Real} = Arcsine(zero(T), b)
Expand Down
23 changes: 8 additions & 15 deletions src/univariate/continuous/beta.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,15 @@ struct Beta{T<:Real} <: ContinuousUnivariateDistribution
Beta{T}::T, β::T) where {T} = new{T}(α, β)
end

function Beta::T, β::T) where {T<:Real}
@check_args(Beta, α > zero(α) && β > zero(β))
return Beta{T}(α, β)
end

function Beta::T, β::T, ::NoArgCheck) where {T<:Real}
function Beta::T, β::T; check_args=true) where {T<:Real}
check_args && @check_args(Beta, α > zero(α) && β > zero(β))
return Beta{T}(α, β)
end

Beta::Real, β::Real) = Beta(promote(α, β)...)
Beta::Integer, β::Integer) = Beta(float(α), float(β))
Beta::Real) = Beta(α, α)
Beta() = Beta(1.0, 1.0, NoArgCheck())
Beta() = Beta(1.0, 1.0, check_args=false)

@distr_support Beta 0.0 1.0

Expand All @@ -53,7 +49,7 @@ function convert(::Type{Beta{T}}, α::Real, β::Real) where T<:Real
Beta(T(α), T(β))
end
function convert(::Type{Beta{T}}, d::Beta{S}) where {T <: Real, S <: Real}
Beta(T(d.α), T(d.β), NoArgCheck())
Beta(T(d.α), T(d.β), check_args=false)
end

#### Parameters
Expand All @@ -66,14 +62,11 @@ params(d::Beta) = (d.α, d.β)

mean(d::Beta) = ((α, β) = params(d); α /+ β))

function mode(d::Beta)
(α, β) = params(d)
> 1 && β > 1) || error("mode is defined only when α > 1 and β > 1.")
return- 1) /+ β - 2)
end

function mode(d::Beta, ::NoArgCheck)
function mode(d::Beta; check_args=true)
(α, β) = params(d)
if check_args
> 1 && β > 1) || error("mode is defined only when α > 1 and β > 1.")
end
return- 1) /+ β - 2)
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 @@ -32,17 +32,15 @@ struct BetaPrime{T<:Real} <: ContinuousUnivariateDistribution
BetaPrime{T}::T, β::T) where {T} = new{T}(α, β)
end

function BetaPrime::T, β::T) where {T<:Real}
@check_args(BetaPrime, α > zero(α) && β > zero(β))
function BetaPrime::T, β::T; check_args=true) where {T<:Real}
check_args && @check_args(BetaPrime, α > zero(α) && β > zero(β))
return BetaPrime{T}(α, β)
end

BetaPrime::T, β::T, ::NoArgCheck) where {T<:Real} = BetaPrime{T}(α, β)

BetaPrime::Real, β::Real) = BetaPrime(promote(α, β)...)
BetaPrime::Integer, β::Integer) = BetaPrime(float(α), float(β))
BetaPrime::Real) = BetaPrime(α, α)
BetaPrime() = BetaPrime(1.0, 1.0, NoArgCheck())
BetaPrime() = BetaPrime(1.0, 1.0, check_args=false)

@distr_support BetaPrime 0.0 Inf

Expand All @@ -51,7 +49,7 @@ function convert(::Type{BetaPrime{T}}, α::Real, β::Real) where T<:Real
BetaPrime(T(α), T(β))
end
function convert(::Type{BetaPrime{T}}, d::BetaPrime{S}) where {T <: Real, S <: Real}
BetaPrime(T(d.α), T(d.β), NoArgCheck())
BetaPrime(T(d.α), T(d.β), check_args=false)
end

#### Parameters
Expand Down
8 changes: 3 additions & 5 deletions src/univariate/continuous/biweight.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@ struct Biweight{T<:Real} <: ContinuousUnivariateDistribution
Biweight{T}::T, σ::T) where {T <: Real} = new{T}(µ, σ)
end

function Biweight::T, σ::T) where {T<:Real}
@check_args(Biweight, σ > zero(σ))
function Biweight::T, σ::T; check_args=true) where {T<:Real}
check_args && @check_args(Biweight, σ > zero(σ))
return Biweight{T}(μ, σ)
end

Biweight::T, σ::T, ::NoArgCheck) where {T<:Real} = Biweight{T}(μ, σ)

Biweight::Real, σ::Real) = Biweight(promote(μ, σ)...)
Biweight::Integer, σ::Integer) = Biweight(float(μ), float(σ))
Biweight::T) where {T<:Real} = Biweight(μ, one(T))
Biweight() = Biweight(0.0, 1.0, NoArgCheck())
Biweight() = Biweight(0.0, 1.0, check_args=false)

@distr_support Biweight d.μ - d.σ d.μ + d.σ

Expand Down
12 changes: 4 additions & 8 deletions src/univariate/continuous/cauchy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,15 @@ struct Cauchy{T<:Real} <: ContinuousUnivariateDistribution
Cauchy{T}(µ, σ) where {T} = new{T}(µ, σ)
end

function Cauchy::T, σ::T) where {T<:Real}
@check_args(Cauchy, σ > zero(σ))
return Cauchy{T}(μ, σ)
end

function Cauchy::T, σ::T, ::NoArgCheck) where {T<:Real}
function Cauchy::T, σ::T; check_args=true) where {T<:Real}
check_args && @check_args(Cauchy, σ > zero(σ))
return Cauchy{T}(μ, σ)
end

Cauchy::Real, σ::Real) = Cauchy(promote(μ, σ)...)
Cauchy::Integer, σ::Integer) = Cauchy(float(μ), float(σ))
Cauchy::T) where {T<:Real} = Cauchy(μ, one(T))
Cauchy() = Cauchy(0.0, 1.0, NoArgCheck())
Cauchy() = Cauchy(0.0, 1.0, check_args=false)

@distr_support Cauchy -Inf Inf

Expand All @@ -49,7 +45,7 @@ function convert(::Type{Cauchy{T}}, μ::Real, σ::Real) where T<:Real
Cauchy(T(μ), T(σ))
end
function convert(::Type{Cauchy{T}}, d::Cauchy{S}) where {T <: Real, S <: Real}
Cauchy(T(d.μ), T(d.σ), NoArgCheck())
Cauchy(T(d.μ), T(d.σ), check_args=false)
end

#### Parameters
Expand Down
8 changes: 3 additions & 5 deletions src/univariate/continuous/chi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,18 @@ struct Chi{T<:Real} <: ContinuousUnivariateDistribution
Chi{T}::T) where {T} = new{T}(ν)
end

function Chi::T) where {T<:Real}
@check_args(Chi, ν > zero(ν))
function Chi::T; check_args=true) where {T<:Real}
check_args && @check_args(Chi, ν > zero(ν))
return Chi{T}(ν)
end

Chi::T, ::NoArgCheck) where {T<:Real} = Chi{T}(ν)

Chi::Integer) = Chi(float(ν))

@distr_support Chi 0.0 Inf

### Conversions
convert(::Type{Chi{T}}, ν::Real) where {T<:Real} = Chi(T(ν))
convert(::Type{Chi{T}}, d::Chi{S}) where {T <: Real, S <: Real} = Chi(T(d.ν), NoArgCheck())
convert(::Type{Chi{T}}, d::Chi{S}) where {T <: Real, S <: Real} = Chi(T(d.ν), check_args=false)

#### Parameters

Expand Down
5 changes: 2 additions & 3 deletions src/univariate/continuous/chisq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ struct Chisq{T<:Real} <: ContinuousUnivariateDistribution
Chisq{T}::T) where {T} = new{T}(ν)
end

function Chisq::T) where {T <: Real}
@check_args(Chisq, ν > zero(ν))
function Chisq::T; check_args=true) where {T <: Real}
check_args && @check_args(Chisq, ν > zero(ν))
return Chisq{T}(ν)
end

Chisq::T, ::NoArgCheck) where {T <: Real} = Chisq{T}(ν)
Chisq::Integer) = Chisq(float(ν))

@distr_support Chisq 0.0 Inf
Expand Down
9 changes: 4 additions & 5 deletions src/univariate/continuous/cosine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ struct Cosine{T<:Real} <: ContinuousUnivariateDistribution
Cosine{T}::T, σ::T) where {T} = new{T}(µ, σ)
end

function Cosine::T, σ::T) where {T <: Real}
@check_args(Cosine, σ > zero(σ))
function Cosine::T, σ::T; check_args=true) where {T <: Real}
check_args && @check_args(Cosine, σ > zero(σ))
return Cosine{T}(μ, σ)
end

Cosine::T, σ::T, ::NoArgCheck) where {T<:Real} = Cosine{T}(μ, σ)
Cosine::Real, σ::Real) = Cosine(promote(μ, σ)...)
Cosine::Integer, σ::Integer) = Cosine(float(μ), float(σ))
Cosine::T) where {T <: Real} = Cosine(μ, one(µ))
Cosine() = Cosine(0.0, 1.0, NoArgCheck())
Cosine() = Cosine(0.0, 1.0, check_args=false)

@distr_support Cosine d.μ - d.σ d.μ + d.σ

Expand All @@ -31,7 +30,7 @@ function convert(::Type{Cosine{T}}, μ::Real, σ::Real) where T<:Real
Cosine(T(μ), T(σ))
end
function convert(::Type{Cosine{T}}, d::Cosine{S}) where {T <: Real, S <: Real}
Cosine(T(d.μ), T(d.σ), NoArgCheck())
Cosine(T(d.μ), T(d.σ), check_args=false)
end

#### Parameters
Expand Down
12 changes: 5 additions & 7 deletions src/univariate/continuous/epanechnikov.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,24 @@ struct Epanechnikov{T<:Real} <: ContinuousUnivariateDistribution
Epanechnikov{T}::T, σ::T) where {T} = new{T}(µ, σ)
end

function Epanechnikov::T, σ::T) where {T<:Real}
@check_args(Epanechnikov, σ > zero(σ))
function Epanechnikov::T, σ::T; check_args=true) where {T<:Real}
check_args && @check_args(Epanechnikov, σ > zero(σ))
return Epanechnikov{T}(μ, σ)
end

Epanechnikov::T, σ::T, ::NoArgCheck) where {T<:Real} = Epanechnikov{T}(μ, σ)

Epanechnikov::Real, σ::Real) = Epanechnikov(promote(μ, σ)...)
Epanechnikov::Integer, σ::Integer) = Epanechnikov(float(μ), float(σ))
Epanechnikov::T) where {T <: Real} = Epanechnikov(μ, one(T))
Epanechnikov() = Epanechnikov(0.0, 1.0, NoArgCheck())
Epanechnikov() = Epanechnikov(0.0, 1.0, check_args=false)

@distr_support Epanechnikov d.μ - d.σ d.μ + d.σ

#### Conversions
function convert(::Type{Epanechnikov{T}}, μ::Real, σ::Real) where T<:Real
Epanechnikov(T(μ), T(σ), NoArgCheck())
Epanechnikov(T(μ), T(σ), check_args=false)
end
function convert(::Type{Epanechnikov{T}}, d::Epanechnikov{S}) where {T <: Real, S <: Real}
Epanechnikov(T(d.μ), T(d.σ), NoArgCheck())
Epanechnikov(T(d.μ), T(d.σ), check_args=false)
end

## Parameters
Expand Down
18 changes: 7 additions & 11 deletions src/univariate/continuous/erlang.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,13 @@ struct Erlang{T<:Real} <: ContinuousUnivariateDistribution
Erlang{T}::Int, θ::T) where {T} = new{T}(α, θ)
end

function Erlang::Real, θ::T) where {T <: Real}
@check_args(Erlang, isinteger(α) && α >= zero(α))
function Erlang::Real, θ::T; check_args=true) where {T <: Real}
check_args && @check_args(Erlang, isinteger(α) && α >= zero(α))
return Erlang{T}(α, θ)
end

function Erlang::Integer, θ::T) where {T <: Real}
@check_args(Erlang, α >= zero(α))
return Erlang{T}(α, θ)
end

function Erlang::Integer, θ::T, ::NoArgCheck) where {T <: Real}
function Erlang::Integer, θ::T; check_args=true) where {T <: Real}
check_args && @check_args(Erlang, α >= zero(α))
return Erlang{T}(α, θ)
end

Expand All @@ -40,16 +36,16 @@ function Erlang(α::Integer, θ::Integer)
end

Erlang::Integer) = Erlang(α, 1.0)
Erlang() = Erlang(1, 1.0, NoArgCheck())
Erlang() = Erlang(1, 1.0, check_args=false)

@distr_support Erlang 0.0 Inf

#### Conversions
function convert(::Type{Erlang{T}}, α::Integer, θ::S) where {T <: Real, S <: Real}
Erlang(α, T(θ), NoArgCheck())
Erlang(α, T(θ), check_args=false)
end
function convert(::Type{Erlang{T}}, d::Erlang{S}) where {T <: Real, S <: Real}
Erlang(d.α, T(d.θ), NoArgCheck())
Erlang(d.α, T(d.θ), check_args=false)
end

#### Parameters
Expand Down
20 changes: 8 additions & 12 deletions src/univariate/continuous/exponential.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,44 +26,40 @@ struct Exponential{T<:Real} <: ContinuousUnivariateDistribution
Exponential{T}::T) where {T} = new{T}(θ)
end

function Exponential::T) where {T <: Real}
@check_args(Exponential, θ > zero(θ))
function Exponential::T; check_args=true) where {T <: Real}
check_args && @check_args(Exponential, θ > zero(θ))
return Exponential{T}(θ)
end

Exponential::T, ::NoArgCheck) where {T <: Real} = Exponential{T}(θ)

Exponential::Integer) = Exponential(float(θ))
Exponential() = Exponential(1.0, NoArgCheck())
Exponential() = Exponential(1.0, check_args=false)

@distr_support Exponential 0.0 Inf

### Conversions
convert(::Type{Exponential{T}}, θ::S) where {T <: Real, S <: Real} = Exponential(T(θ))
convert(::Type{Exponential{T}}, d::Exponential{S}) where {T <: Real, S <: Real} = Exponential(T(d.θ), NoArgCheck())
convert(::Type{Exponential{T}}, d::Exponential{S}) where {T <: Real, S <: Real} = Exponential(T(d.θ), check_args=false)

#### Parameters

scale(d::Exponential) = d.θ
rate(d::Exponential) = inv(d.θ)

params(d::Exponential) = (d.θ,)
@inline partype(d::Exponential{T}) where {T<:Real} = T

partype(::Exponential{T}) where {T<:Real} = T

#### Statistics

mean(d::Exponential) = d.θ
median(d::Exponential) = logtwo * d.θ
mode(d::Exponential{T}) where {T<:Real} = zero(T)
mode(::Exponential{T}) where {T<:Real} = zero(T)

var(d::Exponential) = d.θ^2
skewness(d::Exponential{T}) where {T} = T(2)
kurtosis(d::Exponential{T}) where {T} = T(6)
skewness(::Exponential{T}) where {T} = T(2)
kurtosis(::Exponential{T}) where {T} = T(6)

entropy(d::Exponential{T}) where {T} = one(T) + log(d.θ)


#### Evaluation

zval(d::Exponential, x::Real) = x / d.θ
Expand Down
Loading

0 comments on commit f0cf263

Please sign in to comment.