-
Notifications
You must be signed in to change notification settings - Fork 419
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a faster implementation of AliasTables (#1848)
* switch to AliasTables.jl * retune heuristic * add test for #832 * add more tests * move alias table import and tighten from using to import * Back out multinomial heuristic adjustment at @adienes's request * Update test/univariate/discrete/categorical.jl (style) Co-authored-by: David Widmann <[email protected]> --------- Co-authored-by: David Widmann <[email protected]>
- Loading branch information
1 parent
f33af97
commit b670fee
Showing
5 changed files
with
25 additions
and
21 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,22 +1,7 @@ | ||
struct AliasTable <: Sampleable{Univariate,Discrete} | ||
accept::Vector{Float64} | ||
alias::Vector{Int} | ||
at::AliasTables.AliasTable{UInt64, Int} | ||
AliasTable(probs::AbstractVector{<:Real}) = new(AliasTables.AliasTable(probs)) | ||
end | ||
ncategories(s::AliasTable) = length(s.alias) | ||
|
||
function AliasTable(probs::AbstractVector) | ||
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) | ||
end | ||
|
||
function rand(rng::AbstractRNG, s::AliasTable) | ||
i = rand(rng, 1:length(s.alias)) % Int | ||
# using `ifelse` improves performance here: github.com/JuliaStats/Distributions.jl/pull/1831/ | ||
ifelse(rand(rng) < s.accept[i], i, s.alias[i]) | ||
end | ||
|
||
ncategories(s::AliasTable) = length(s.at) | ||
rand(rng::AbstractRNG, s::AliasTable) = rand(rng, s.at) | ||
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