Skip to content

Commit

Permalink
Categorical distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
gdalle committed Aug 23, 2021
1 parent 419d2f2 commit 28b5539
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/parameterized/categorical.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Categorical distribution

# REFERENCES
# https://juliastats.org/Distributions.jl/stable/univariate/#Distributions.Categorical
# https://juliastats.org/Distributions.jl/stable/univariate/#Distributions.DiscreteNonParametric

export Categorical

@parameterized Categorical(p) CountingMeasure(ℤ[0:∞])

ncategories(d::Categorical) = length(d.p)

(d::Categorical ::CountingMeasure{IntegerRange{a,b}}) where {a,b} = a 1 && b ncategories(d)

(::CountingMeasure{IntegerRange{a,b}} ::Categorical) where {a,b} = a 1 && b ncategories(d)

###############################################################################
@kwstruct Categorical(p)

logdensity(d::Categorical{(:p)}, y) = log(d.p[y])

# Very inefficient because of the heavy implementation of Dists.DiscreteNonParametric
distproxy(d::Categorical{(:p)}) = Dists.Categorical(d.p)

Base.rand(rng::AbstractRNG, T::Type, d::Categorical{(:p)}) = rand(rng, distproxy(d))

asparams(::Type{<:Categorical}, ::Val{:p}) = as𝕀

0 comments on commit 28b5539

Please sign in to comment.