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 d61dfed8 authored by Dahua Lin's avatar Dahua Lin

Move discrete sampler source to samplers/

parent 2459733d
......@@ -161,12 +161,10 @@ include("constants.jl")
include("fallbacks.jl")
include("rmath.jl")
include("drawtable.jl")
include("huffman.jl")
include("alias.jl")
include("tvpack.jl")
include(joinpath("samplers", "categorical_samplers.jl"))
# Univariate distributions
include(joinpath("univariate", "arcsine.jl"))
include(joinpath("univariate", "bernoulli.jl"))
......
immutable AliasTable
immutable AliasTable <: AbstractCategoricalSampler
accept::Vector{Float64}
alias::Vector{Int}
end
......
# Store an alias table
immutable DiscreteDistributionTable
immutable DiscreteDistributionTable <: AbstractCategoricalSampler
table::Vector{Vector{Int64}}
bounds::Vector{Int64}
end
......
......@@ -6,7 +6,7 @@
import Base.isless, Base.show, Base.getindex, Base.rand
abstract HuffmanNode{T}
abstract HuffmanNode{T} <: AbstractCategoricalSampler
immutable HuffmanLeaf{T} <: HuffmanNode{T}
value::T
......@@ -40,7 +40,7 @@ end
# could be slightly more efficient using a Deque.
function huffman{T}(values::AbstractVector{T},weights::AbstractVector{Uint64})
leafs = [HuffmanLeaf{T}(values[i],weights[i]) for i = 1:length(weights)]
sort!(leafs,Base.Sort.Reverse)
sort!(leafs; rev=true)
branches = Array(HuffmanBranch{T},0)
......
using Distributions
n = 1_000_000
p = fill(1/n,n)
pu = fill(uint(1),n)
println("Set-up condensed table")
d = Distributions.DiscreteDistributionTable(p)
gc()
@time d = Distributions.DiscreteDistributionTable(p)
println("Set-up Huffman tree")
h = Distributions.huffman(1:length(pu),pu)
gc()
@time h = Distributions.huffman(1:length(pu),pu)
println("Set-up alias table")
a = Distributions.AliasTable(p)
gc()
@time a = Distributions.AliasTable(p)
function runsample(d,N)
x = Array(Int,N)
for i = 1:N
x[i] = rand(d)
end
x
end
println("Sample condensed table")
x = runsample(d,10)
gc()
@time x = runsample(d,1_000_000)
println("Sample Huffman tree")
x = runsample(h,10)
gc()
@time x = runsample(h,1_000_000)
println("Sample alias table")
x = runsample(a,10)
gc()
@time x = runsample(a,1_000_000)
using Distributions
# config
Ks = [3, 10, 30, 100, 1000, 10000]
n = 10^5
function run_sample(sampler, n::Int)
for i = 1:n
rand(sampler)
end
end
macro bench_sampler(construct_expr)
quote
s = $(esc(construct_expr)) # construct sampler
tname = typeof(s)
# warm up
run_sample(s, 100)
et = @elapsed run_sample(s, n)
mps = n * 1.0e-6 / et
@printf(" %-28s : elapsed = %12.6f sec | %8.4f MPS\n",
tname, et, mps)
end
end
for K in Ks
println("K = $K")
println("-----------------------")
p = fill(1/K, K)
pu = fill(uint64(1), K)
@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.AliasTable(p)
println()
end
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