Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DiscreteNonParametric distribution #634

Merged
merged 17 commits into from
Nov 4, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Generalize probability vector type for DiscreteNonParametric and Cate…
…gorical, fix #743
  • Loading branch information
Gord Stephen committed Oct 30, 2018
commit 3c2efbe6048322769846484dcb87a86bbbb30933
38 changes: 22 additions & 16 deletions src/univariate/discrete/categorical.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,39 @@ External links:

* [Categorical distribution on Wikipedia](http://en.wikipedia.org/wiki/Categorical_distribution)
"""
Categorical{T} = DiscreteNonParametric{Int,T,UnitRange{Int}}
Categorical{P,Ps} = DiscreteNonParametric{Int,P,UnitRange{Int},Ps}

Categorical{P}(p::Vector{P}, ::NoArgCheck) where P =
Categorical{P}(1:length(p), p, NoArgCheck())
Categorical(p::Vector{P}, ::NoArgCheck) where P = Categorical{P}(p, NoArgCheck())
Categorical{P,Ps}(p::Ps, ::NoArgCheck) where {P<:Real, Ps<:AbstractVector{P}} =
Categorical{P,Ps}(1:length(p), p, NoArgCheck())

function Categorical{P}(p::Vector{P}) where P
@check_args(DiscreteNonParametric, isprobvec(p))
Categorical{P}(1:length(p), p, NoArgCheck())
Categorical(p::Ps, ::NoArgCheck) where {P<:Real, Ps<:AbstractVector{P}} =
Categorical{P,Ps}(p, NoArgCheck())

function Categorical{P,Ps}(p::Ps) where {P<:Real, Ps<:AbstractVector{P}}
@check_args(Categorical, isprobvec(p))
Categorical{P,Ps}(1:length(p), p, NoArgCheck())
end
Categorical(p::Vector{P}) where P = Categorical{P}(p)

Categorical(p::Ps) where {P<:Real, Ps<:AbstractVector{P}} =
Categorical{P,Ps}(p)

function Categorical(k::Integer)
@check_args(DiscreteNonParametric, k >= 1)
Categorical{Float64}(1:k, fill(1/k, k), NoArgCheck())
@check_args(Categorical, k >= 1)
Categorical{Float64,Vector{Float64}}(1:k, fill(1/k, k), NoArgCheck())
end

@distr_support Categorical 1 support(d).stop

### Conversions

convert(::Type{Categorical{T}}, p::Vector{S}) where {T<:Real, S<:Real} = Categorical(Vector{T}(p))
convert(::Type{Categorical{T}}, d::Categorical{S}) where {T<:Real, S<:Real} = Categorical(Vector{T}(probs(d)))
convert(::Type{Categorical{P,Ps}}, x::AbstractVector{<:Real}) where {
P<:Real,Ps<:AbstractVector{P}} = Categorical{P,Ps}(Ps(x))

# convert(::Type{Categorical{P,Ps}}, d::Categorical) where {
# P<:Real,Ps<:AbstractVector{P}} = Categorical{P,Ps}(Ps(probs(d)))

### Parameters

ncategories(d::Categorical) = support(d).stop
params(d::Categorical) = (probs(d),)
params(d::Categorical{P,Ps}) where {P<:Real, Ps<:AbstractVector{P}} = (probs(d),)
@inline partype(d::Categorical{T}) where {T<:Real} = T

### Statistics
Expand Down Expand Up @@ -112,7 +117,8 @@ end

# sampling

sampler(d::Categorical) = AliasTable(d.p)
sampler(d::Categorical{P,Ps}) where {P<:Real,Ps<:AbstractVector{P}} =
AliasTable(probs(d))


### sufficient statistics
Expand Down
51 changes: 28 additions & 23 deletions src/univariate/discrete/discretenonparametric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,31 @@ External links

* [Probability mass function on Wikipedia](http://en.wikipedia.org/wiki/Probability_mass_function)
"""
struct DiscreteNonParametric{T<:Real,P<:Real,S<:AbstractVector{T}} <: DiscreteUnivariateDistribution
support::S
p::Vector{P}
struct DiscreteNonParametric{T<:Real,P<:Real,Ts<:AbstractVector{T},Ps<:AbstractVector{P}} <: DiscreteUnivariateDistribution
support::Ts
p::Ps

DiscreteNonParametric{T,P,S}(vs::S, ps::Vector{P}, ::NoArgCheck) where {T<:Real,P<:Real,S<:AbstractVector{T}} =
new{T,P,S}(vs, ps)
DiscreteNonParametric{T,P,Ts,Ps}(vs::Ts, ps::Ps, ::NoArgCheck) where {
T<:Real,P<:Real,Ts<:AbstractVector{T},Ps<:AbstractVector{P}} =
new{T,P,Ts,Ps}(vs, ps)

function DiscreteNonParametric{T,P,S}(vs::S, ps::Vector{P}) where {T<:Real,P<:Real,S<:AbstractVector{T}}
function DiscreteNonParametric{T,P,Ts,Ps}(vs::Ts, ps::Ps) where {
T<:Real,P<:Real,Ts<:AbstractVector{T},Ps<:AbstractVector{P}}
@check_args(DiscreteNonParametric, length(vs) == length(ps))
@check_args(DiscreteNonParametric, isprobvec(ps))
@check_args(DiscreteNonParametric, allunique(vs))
sort_order = sortperm(vs)
new{T,P,S}(vs[sort_order], ps[sort_order])
new{T,P,Ts,Ps}(vs[sort_order], ps[sort_order])
end
end

DiscreteNonParametric(vs::S, ps::Vector{P}) where {T<:Real,P<:Real,S<:AbstractVector{T}} =
DiscreteNonParametric{T,P,S}(vs, ps)
DiscreteNonParametric(vs::Ts, ps::Ps) where {
T<:Real,P<:Real,Ts<:AbstractVector{T},Ps<:AbstractVector{P}} =
DiscreteNonParametric{T,P,Ts,Ps}(vs, ps)

# Conversion
convert(::Type{DiscreteNonParametric{T,P,S}}, d::DiscreteNonParametric) where {T,P,S} =
DiscreteNonParametric{T,P,S}(S(support(d)), Vector{P}(probs(d)), NoArgCheck())
convert(::Type{DiscreteNonParametric{T,P,Ts,Ps}}, d::DiscreteNonParametric) where {T,P,Ts,Ps} =
DiscreteNonParametric{T,P,Ts,Ps}(Ts(support(d)), Ps(probs(d)), NoArgCheck())

# Accessors
params(d::DiscreteNonParametric) = (d.support, d.p)
Expand Down Expand Up @@ -110,9 +113,8 @@ function quantile(d::DiscreteNonParametric, q::Real)
x[i]
end


minimum(d::DiscreteNonParametric) = support(d)[1]
maximum(d::DiscreteNonParametric) = support(d)[end]
minimum(d::DiscreteNonParametric) = first(support(d))
maximum(d::DiscreteNonParametric) = last(support(d))
insupport(d::DiscreteNonParametric, x::Real) =
length(searchsorted(support(d), x)) > 0

Expand Down Expand Up @@ -205,9 +207,10 @@ end

# Sufficient statistics

struct DiscreteNonParametricStats{T<:Real,P<:AbstractFloat,S<:AbstractVector{T}} <: SufficientStats
support::S
freq::Vector{P}
struct DiscreteNonParametricStats{T<:Real,W<:Real,Ts<:AbstractVector{T},
Ws<:AbstractVector{W}} <: SufficientStats
support::Ts
freq::Ws
end

function suffstats(::Type{DiscreteNonParametric}, x::AbstractArray{T}) where {T<:Real}
Expand All @@ -217,7 +220,7 @@ function suffstats(::Type{DiscreteNonParametric}, x::AbstractArray{T}) where {T<

n = 1
vs = Vector{T}(undef,N)
ps = zeros(N)
ps = zeros(Float64, N)
x = sort(vec(x))

vs[1] = x[1]
Expand All @@ -240,16 +243,17 @@ function suffstats(::Type{DiscreteNonParametric}, x::AbstractArray{T}) where {T<

end

function suffstats(::Type{DiscreteNonParametric}, x::AbstractArray{T}, w::AbstractArray{P}) where {T<:Real,P<:AbstractFloat}
function suffstats(::Type{DiscreteNonParametric}, x::AbstractArray{T},
w::AbstractArray{W}) where {T<:Real,W<:Real}

@check_args(DiscreteNonParametric, length(x) == length(w))

N = length(x)
N == 0 && return DiscreteNonParametricStats(T[], P[])
N == 0 && return DiscreteNonParametricStats(T[], W[])

n = 1
vs = Vector{T}(undef, N)
ps = zeros(P, N)
ps = zeros(W, N)

xorder = sortperm(vec(x))
x = vec(x)[xorder]
Expand Down Expand Up @@ -278,5 +282,6 @@ end

# # Model fitting

fit_mle(::Type{DiscreteNonParametric}, ss::DiscreteNonParametricStats{T,P,S}) where {T,P,S} =
DiscreteNonParametric{T,P,S}(ss.support, pnormalize!(copy(ss.freq)), NoArgCheck())
fit_mle(::Type{DiscreteNonParametric},
ss::DiscreteNonParametricStats{T,W,Ts,Ws}) where {T,W,Ts,Ws} =
DiscreteNonParametric{T,W,Ts,Ws}(ss.support, pnormalize!(copy(ss.freq)), NoArgCheck())
12 changes: 1 addition & 11 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,7 @@ end

isprobvec(p::Vector{T}) where {T<:Real} = allnonneg(p) && isapprox(sum(p), one(T))

function pnormalize!(v::AbstractVector{T}) where T<:AbstractFloat
s = 0.
n = length(v)
for i = 1:n
@inbounds s += v[i]
end
for i = 1:n
@inbounds v[i] /= s
end
v
end
pnormalize!(v::AbstractVector{<:Real}) = (v ./= sum(v); v)

add!(x::AbstractArray, y::AbstractVector) = broadcast!(+, x, x, y)
add!(x::AbstractVecOrMat, y::ZeroVector) = x
Expand Down
4 changes: 2 additions & 2 deletions test/categorical.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ println(" testing $d as Categorical")
p = ones(10^6) * 1.0e-6
@test Distributions.isprobvec(p)

@test typeof(convert(Categorical{Float32}, d)) == Categorical{Float32}
@test typeof(convert(Categorical{Float32}, d.p)) == Categorical{Float32}
@test typeof(convert(Categorical{Float32,Vector{Float32}}, d)) == Categorical{Float32,Vector{Float32}}
@test typeof(convert(Categorical{Float32,Vector{Float32}}, d.p)) == Categorical{Float32,Vector{Float32}}