Ce serveur Gitlab sera éteint le 30 juin 2020, pensez à migrer vos projets vers les serveurs gitlab-research.centralesupelec.fr et gitlab-student.centralesupelec.fr !

Commit f5a6d817 authored by Dahua Lin's avatar Dahua Lin

add randi + tweaked AliasTable

The sampling speed of AliasTable is doubled with this commit.
parent c05af2af
......@@ -162,6 +162,7 @@ include("constants.jl")
include("fallbacks.jl")
include("rmath.jl")
include("tvpack.jl")
include("utils.jl")
include(joinpath("samplers", "categorical_samplers.jl"))
......
immutable AliasTable <: AbstractCategoricalSampler
Ku::Uint
U::Uint
accept::Vector{Float64}
alias::Vector{Int}
end
......@@ -37,11 +39,13 @@ function AliasTable(probs)
accept[s] = 1.0
end
AliasTable(accept,alias)
Ku = uint(length(accept))
U = div(typemax(Uint), Ku) * Ku
AliasTable(Ku, U, accept, alias)
end
function rand(a::AliasTable)
i = rand(1:length(a.accept))
i = _randi(a.Ku, a.U)
u = rand()
u < a.accept[i] ? i : a.alias[i]
end
......
......@@ -66,7 +66,7 @@ function quantile(d::DiscreteUniform, p::Real)
return d.a + ifloor(p * n)
end
rand(d::DiscreteUniform) = d.a + rand(0:(d.b - d.a))
rand(d::DiscreteUniform) = d.a + randi(d.b - d.a)
skewness(d::DiscreteUniform) = 0.0
......
# Utility functions
function _randi(Ku::Uint, U::Uint)
x = rand(Uint)
while x > U
x = rand(Uint)
end
int(rem(x, Ku)) + 1
end
function randi(K::Int) # Fast method to draw a random integer from 1:K
Ku = uint(K)
U = div(typemax(Uint), Ku) * Ku
_randi(Ku, U)
end
......@@ -37,8 +37,8 @@ for K in Ks
@bench_sampler DiscreteUniform(1, K) # samples generated by rand(1:K)
@bench_sampler Categorical(p)
@bench_sampler Distributions.DiscreteDistributionTable(p)
@bench_sampler Distributions.huffman(1:K, pu)
# @bench_sampler Distributions.DiscreteDistributionTable(p)
# @bench_sampler Distributions.huffman(1:K, pu)
@bench_sampler Distributions.AliasTable(p)
println()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment