-
Notifications
You must be signed in to change notification settings - Fork 414
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DiscreteNonParametric distribution (#634)
* Add Generic discrete distribution * Convert modes to explicit loop * Add uniqueness check for values in Generic constructor * Convert Generic's support to an AbstractVector * Convert Categorical to a type alias of Generic * Add Generic mgf and cf * Add Generic fit and sufficient statistic methods * Easy 0.7 fixes * Updates for Random in 0.7 and tests passing * Fix 0.7 constructor dispatch * Fix weighted suffstats and minor changes * Rename Generic to DiscreteNonParametric * Add documentation for DiscreteNonParametric * Generalize probability vector type for DiscreteNonParametric and Categorical, fix #743 * Generalize DiscreteNonParametric/Categorical samplers * Further specialize support of Categorical * Partially support test_distr with DiscreteNonParametric
- Loading branch information
1 parent
ad271d2
commit cd0ae4e
Showing
16 changed files
with
536 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
# delegation of samplers | ||
|
||
for fname in ["categorical.jl", | ||
for fname in ["aliastable.jl", | ||
"binomial.jl", | ||
"poissonbinomial.jl", | ||
"poisson.jl", | ||
"exponential.jl", | ||
"gamma.jl", | ||
"multinomial.jl", | ||
"vonmises.jl", | ||
"vonmisesfisher.jl"] | ||
"vonmisesfisher.jl", | ||
"discretenonparametric.jl", | ||
"categorical.jl"] | ||
|
||
include(joinpath("samplers", fname)) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
struct AliasTable{S} <: Sampleable{Univariate,Discrete} | ||
accept::Vector{Float64} | ||
alias::Vector{Int} | ||
isampler::S | ||
end | ||
ncategories(s::AliasTable) = length(s.accept) | ||
|
||
function AliasTable(probs::AbstractVector{T}) where T<:Real | ||
n = length(probs) | ||
n > 0 || throw(ArgumentError("The input probability vector is empty.")) | ||
accp = Vector{Float64}(undef, n) | ||
alias = Vector{Int}(undef, n) | ||
StatsBase.make_alias_table!(probs, 1.0, accp, alias) | ||
AliasTable(accp, alias, Random.RangeGenerator(1:n)) | ||
end | ||
|
||
function rand(rng::AbstractRNG, s::AliasTable) | ||
i = rand(GLOBAL_RNG, s.isampler) % Int | ||
u = rand() | ||
@inbounds r = u < s.accept[i] ? i : s.alias[i] | ||
r | ||
end | ||
rand(s::AliasTable) = rand(Random.GLOBAL_RNG, s) | ||
|
||
show(io::IO, s::AliasTable) = @printf(io, "AliasTable with %d entries", ncategories(s)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
""" | ||
DiscreteNonParametricSampler(xs, ps) | ||
Data structure for efficiently sampling from an arbitrary probability mass | ||
function defined by support `xs` and probabilities `ps`. | ||
""" | ||
struct DiscreteNonParametricSampler{T<:Real, S<:AbstractVector{T}} <: Sampleable{Univariate,Discrete} | ||
support::S | ||
aliastable::AliasTable | ||
|
||
DiscreteNonParametricSampler{T,S}(support::S, probs::AbstractVector{<:Real}) where {T<:Real,S<:AbstractVector{T}} = | ||
new(support, AliasTable(probs)) | ||
end | ||
|
||
DiscreteNonParametricSampler(support::S, probs::AbstractVector{<:Real}) where {T<:Real,S<:AbstractVector{T}} = | ||
DiscreteNonParametricSampler{T,S}(support, probs) | ||
|
||
rand(s::DiscreteNonParametricSampler) = | ||
(@inbounds v = s.support[rand(s.aliastable)]; v) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.