|
| 1 | +# definition of some distribution types |
| 2 | + |
| 3 | + |
| 4 | +## Distribution & Combine |
| 5 | + |
| 6 | +abstract type Distribution{T} end |
| 7 | + |
| 8 | +Base.eltype(::Type{Distribution{T}}) where {T} = T |
| 9 | + |
| 10 | +abstract type Combine{T} <: Distribution{T} end |
| 11 | + |
| 12 | +struct Combine0{T} <: Combine{T} end |
| 13 | + |
| 14 | +Combine(::Type{T}) where {T} = Combine0{T}() |
| 15 | + |
| 16 | +struct Combine1{T,X} <: Combine{T} |
| 17 | + x::X |
| 18 | +end |
| 19 | + |
| 20 | +Combine(::Type{T}, x::X) where {T,X} = Combine1{T,X}(x) |
| 21 | +Combine(::Type{T}, ::Type{X}) where {T,X} = Combine1{T,Type{X}}(X) |
| 22 | + |
| 23 | +struct Combine2{T,X,Y} <: Combine{T} |
| 24 | + x::X |
| 25 | + y::Y |
| 26 | +end |
| 27 | + |
| 28 | +Combine(::Type{T}, x::X, y::Y) where {T,X,Y} = Combine2{deduce_type(T,X,Y),X,Y}(x, y) |
| 29 | +Combine(::Type{T}, ::Type{X}, y::Y) where {T,X,Y} = Combine2{deduce_type(T,X,Y),Type{X},Y}(X, y) |
| 30 | +Combine(::Type{T}, x::X, ::Type{Y}) where {T,X,Y} = Combine2{deduce_type(T,X,Y),X,Type{Y}}(x, Y) |
| 31 | +Combine(::Type{T}, ::Type{X}, ::Type{Y}) where {T,X,Y} = Combine2{deduce_type(T,X,Y),Type{X},Type{Y}}(X, Y) |
| 32 | + |
| 33 | +deduce_type(::Type{T}, ::Type{X}, ::Type{Y}) where {T,X,Y} = _deduce_type(T, Val(isconcretetype(T)), eltype(X), eltype(Y)) |
| 34 | +deduce_type(::Type{T}, ::Type{X}) where {T,X} = _deduce_type(T, Val(isconcretetype(T)), eltype(X)) |
| 35 | + |
| 36 | +_deduce_type(::Type{T}, ::Val{true}, ::Type{X}, ::Type{Y}) where {T,X,Y} = T |
| 37 | +_deduce_type(::Type{T}, ::Val{false}, ::Type{X}, ::Type{Y}) where {T,X,Y} = deduce_type(T{X}, Y) |
| 38 | + |
| 39 | +_deduce_type(::Type{T}, ::Val{true}, ::Type{X}) where {T,X} = T |
| 40 | +_deduce_type(::Type{T}, ::Val{false}, ::Type{X}) where {T,X} = T{X} |
| 41 | + |
| 42 | + |
| 43 | +## Uniform |
| 44 | + |
| 45 | +abstract type Uniform{T} <: Distribution{T} end |
| 46 | + |
| 47 | + |
| 48 | +struct UniformType{T} <: Uniform{T} end |
| 49 | + |
| 50 | +Uniform(::Type{T}) where {T} = UniformType{T}() |
| 51 | + |
| 52 | +Base.getindex(::UniformType{T}) where {T} = T |
| 53 | + |
| 54 | +struct UniformWrap{T,E} <: Uniform{E} |
| 55 | + val::T |
| 56 | +end |
| 57 | + |
| 58 | +Uniform(x::T) where {T} = UniformWrap{T,eltype(T)}(x) |
| 59 | + |
| 60 | +Base.getindex(x::UniformWrap) = x.val |
| 61 | + |
| 62 | + |
| 63 | +## Normal & Exponential |
| 64 | + |
| 65 | +abstract type Normal{T} <: Distribution{T} end |
| 66 | + |
| 67 | +struct Normal01{T} <: Normal{T} end |
| 68 | + |
| 69 | +struct Normalμσ{T} <: Normal{T} |
| 70 | + μ::T |
| 71 | + σ::T |
| 72 | +end |
| 73 | + |
| 74 | +Normal(::Type{T}=Float64) where {T} = Normal01{T}() |
| 75 | +Normal(μ::T, σ::T) where {T} = Normalμσ(μ, σ) |
| 76 | + |
| 77 | +abstract type Exponential{T} <: Distribution{T} end |
| 78 | + |
| 79 | +struct Exponential1{T} <: Exponential{T} end |
| 80 | + |
| 81 | +struct Exponentialθ{T} <: Exponential{T} |
| 82 | + θ::T |
| 83 | +end |
| 84 | + |
| 85 | +Exponential(::Type{T}=Float64) where {T<:AbstractFloat} = Exponential1{T}() |
| 86 | +Exponential(θ::T) where {T<:AbstractFloat} = Exponentialθ(θ) |
| 87 | + |
| 88 | + |
| 89 | +## floats |
| 90 | + |
| 91 | +abstract type FloatInterval{T<:AbstractFloat} <: Uniform{T} end |
| 92 | +abstract type CloseOpen{T<:AbstractFloat} <: FloatInterval{T} end |
| 93 | + |
| 94 | +struct CloseOpen01{T<:AbstractFloat} <: CloseOpen{T} end # interval [0,1) |
| 95 | +struct CloseOpen12{T<:AbstractFloat} <: CloseOpen{T} end # interval [1,2) |
| 96 | + |
| 97 | +struct CloseOpenAB{T<:AbstractFloat} <: CloseOpen{T} # interval [a,b) |
| 98 | + a::T |
| 99 | + b::T |
| 100 | +end |
| 101 | + |
| 102 | +const FloatInterval_64 = FloatInterval{Float64} |
| 103 | +const CloseOpen01_64 = CloseOpen01{Float64} |
| 104 | +const CloseOpen12_64 = CloseOpen12{Float64} |
| 105 | + |
| 106 | +CloseOpen01(::Type{T}=Float64) where {T<:AbstractFloat} = CloseOpen01{T}() |
| 107 | +CloseOpen12(::Type{T}=Float64) where {T<:AbstractFloat} = CloseOpen12{T}() |
| 108 | + |
| 109 | +CloseOpen(::Type{T}=Float64) where {T<:AbstractFloat} = CloseOpen01{T}() |
| 110 | +CloseOpen(a::T, b::T) where {T<:AbstractFloat} = CloseOpenAB{T}(a, b) |
| 111 | + |
| 112 | + |
| 113 | +Base.eltype(::Type{<:FloatInterval{T}}) where {T<:AbstractFloat} = T |
| 114 | + |
| 115 | + |
| 116 | +## a dummy container type to take advangage of SamplerTag constructor |
| 117 | + |
| 118 | +struct Cont{T} end |
| 119 | + |
| 120 | +Base.eltype(::Type{Cont{T}}) where {T} = T |
0 commit comments