Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/univariate/continuous/arcsine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,24 @@ External links

* [Arcsine distribution on Wikipedia](http://en.wikipedia.org/wiki/Arcsine_distribution)

Use `Arcsine(a, b, ::NoArgCheck)` 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

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

Arcsine(a::T, b::T) where {T<:Real} = Arcsine{T}(a, b)
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(Float64(a), Float64(b))
Arcsine(b::Real) = Arcsine(0.0, b)
Arcsine(a::Integer, b::Integer) = Arcsine(float(a), float(b))
Arcsine(b::T) where {T <: Real} = Arcsine(zero(T), b)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure this is an OK definition, as later we e.g. define: kurtosis(d::Arcsine{T}) where {T<:Real} = -T(3/2), which will fail if we write Arcsine(1).
A general question is if for continuous distributions, in cases like this one, we should not add restriction for parameters to be AbstractFloat?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or add a conversion if parameters are <:Integer to float?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arcsine(1) will dispatch to Arcsine(a::Integer, b::Integer):

julia> kurtosis(Arcsine(1))
-1.5

Seems fine?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or add a conversion if parameters are <:Integer to float?

there is one on line 38

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I have not noticed it, sorry. It should probably use float instead of Float64 as noted below.

Arcsine() = Arcsine(0.0, 1.0)

@distr_support Arcsine d.a d.b
Expand Down
39 changes: 26 additions & 13 deletions src/univariate/continuous/beta.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,22 @@ External links
struct Beta{T<:Real} <: ContinuousUnivariateDistribution
α::T
β::T
Beta{T}(α::T, β::T) where {T} = new{T}(α, β)
end

function Beta{T}(α::T, β::T) where T
@check_args(Beta, α > zero(α) && β > zero(β))
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}
return Beta{T}(α, β)
end

Beta(α::T, β::T) where {T<:Real} = Beta{T}(α, β)
Beta(α::Real, β::Real) = Beta(promote(α, β)...)
Beta(α::Integer, β::Integer) = Beta(Float64(α), Float64(β))
Beta(α::Integer, β::Integer) = Beta(float(α), float(β))
Beta(α::Real) = Beta(α, α)
Beta() = Beta(1, 1)
Beta() = Beta(1.0, 1.0, NoArgCheck())

@distr_support Beta 0.0 1.0

Expand All @@ -49,7 +53,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.β))
Beta(T(d.α), T(d.β), NoArgCheck())
end

#### Parameters
Expand All @@ -68,6 +72,11 @@ function mode(d::Beta)
return (α - 1) / (α + β - 2)
end

function mode(d::Beta, ::NoArgCheck)
(α, β) = params(d)
return (α - 1) / (α + β - 2)
end

modes(d::Beta) = [mode(d)]

function var(d::Beta)
Expand Down Expand Up @@ -200,12 +209,16 @@ end

# TODO: add MLE method (should be similar to Dirichlet)

# This is a moment-matching method (not MLE)
#
"""
fit(::Type{<:Beta}, x::AbstractArray{T})

fit a `Beta` distribution
"""
function fit(::Type{<:Beta}, x::AbstractArray{T}) where T<:Real
x_bar = mean(x)
v_bar = varm(x, x_bar)
α = x_bar * (((x_bar * (1 - x_bar)) / v_bar) - 1)
β = (1 - x_bar) * (((x_bar * (1 - x_bar)) / v_bar) - 1)
Beta(α, β)
temp = ((x_bar * (one(T) - x_bar)) / v_bar) - one(T)
α = x_bar * temp
β = (one(T) - x_bar) * temp
return Beta(α, β)
end
18 changes: 10 additions & 8 deletions src/univariate/continuous/betaprime.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,20 @@ External links
struct BetaPrime{T<:Real} <: ContinuousUnivariateDistribution
α::T
β::T
BetaPrime{T}(α::T, β::T) where {T} = new{T}(α, β)
end

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

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

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

@distr_support BetaPrime 0.0 Inf

Expand All @@ -49,7 +51,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.β))
BetaPrime(T(d.α), T(d.β), NoArgCheck())
end

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

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

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

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

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

Expand Down Expand Up @@ -46,7 +51,7 @@ function ccdf(d::Biweight{T}, x::Real) where T<:Real
u = (d.μ - x) / d.σ
u <= -1 ? one(T) :
u >= 1 ? zero(T) :
(u + 1)^3/16 * @horner(u,8,-9,3)
(u + 1)^3/16 * @horner(u, 8, -9, 3)
end

@quantile_newton Biweight
Expand Down
22 changes: 13 additions & 9 deletions src/univariate/continuous/cauchy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,22 @@ External links
struct Cauchy{T<:Real} <: ContinuousUnivariateDistribution
μ::T
σ::T
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, σ::T) where T
@check_args(Cauchy, σ > zero(σ))
new{T}(μ, σ)
end
function Cauchy(μ::T, σ::T, ::NoArgCheck) where {T<:Real}
return Cauchy{T}(μ, σ)
end

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

@distr_support Cauchy -Inf Inf

Expand All @@ -45,7 +49,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.σ))
Cauchy(T(d.μ), T(d.σ), NoArgCheck())
end

#### Parameters
Expand Down
13 changes: 9 additions & 4 deletions src/univariate/continuous/chi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,23 @@ External links
"""
struct Chi{T<:Real} <: ContinuousUnivariateDistribution
ν::T
Chi{T}(ν::T) where {T} = new{T}(ν)
end

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

Chi(ν::T) where {T<:Real} = Chi{T}(ν)
Chi(ν::Integer) = Chi(Float64(ν))
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.ν))
convert(::Type{Chi{T}}, d::Chi{S}) where {T <: Real, S <: Real} = Chi(T(d.ν), NoArgCheck())

#### Parameters

Expand Down
10 changes: 7 additions & 3 deletions src/univariate/continuous/chisq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ External links
"""
struct Chisq{T<:Real} <: ContinuousUnivariateDistribution
ν::T
Chisq{T}(ν::T) where {T} = new{T}(ν)
end

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

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

@distr_support Chisq 0.0 Inf

Expand Down
16 changes: 10 additions & 6 deletions src/univariate/continuous/cosine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ External link:
struct Cosine{T<:Real} <: ContinuousUnivariateDistribution
μ::T
σ::T
Cosine{T}(μ::T, σ::T) where {T} = new{T}(µ, σ)
end

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

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

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

Expand All @@ -27,7 +31,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.σ))
Cosine(T(d.μ), T(d.σ), NoArgCheck())
end

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

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

Epanechnikov(μ::T, σ::T) where {T<:Real} = Epanechnikov{T}(μ, σ)
Epanechnikov(μ::Real, σ::Real) = Epanechnikov(promote(μ, σ)...)
Epanechnikov(μ::Integer, σ::Integer) = Epanechnikov(Float64(μ), Float64(σ))
Epanechnikov(μ::Real) = Epanechnikov(μ, 1.0)
Epanechnikov() = Epanechnikov(0.0, 1.0)
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())

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

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

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

function Erlang(α::Real, θ::T) where {T <: Real}
@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}
return Erlang{T}(α, θ)
end

function Erlang{T}(α::Real, θ::T) where T
@check_args(Erlang, isinteger(α) && α >= zero(α))
new{T}(α, θ)
end
function Erlang(α::Integer, θ::Integer)
θf = float(θ)
return Erlang{typeof(θf)}(α, θf)
end

Erlang(α::Int, θ::T) where {T<:Real} = Erlang{T}(α, θ)
Erlang(α::Int, θ::Integer) = Erlang{Float64}(α, Float64(θ))
Erlang(α::Int) = Erlang(α, 1.0)
Erlang() = Erlang(1, 1.0)
Erlang(α::Integer) = Erlang(α, 1.0)
Erlang() = Erlang(1, 1.0, NoArgCheck())

@distr_support Erlang 0.0 Inf

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

#### Parameters
Expand Down
Loading