-
Notifications
You must be signed in to change notification settings - Fork 2
/
empirical.jl
58 lines (44 loc) · 1.44 KB
/
empirical.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#################### Empirical Move ####################
#################### Types and Constructors ####################
mutable struct EmpiricalTune{F<:Function,F2<:Function} <: SamplerTune
logf::F
samplerfun::F2
width::Int64
replacement::Bool
end
EmpiricalTune(
x::Vector,
logf::F,
samplerfun::F2,
width::Int64,
replacement::Bool = false,
) where {F,F2} = EmpiricalTune{F,F2}(logf, samplerfun, width, replacement)
const EmpiricalVariate = Sampler{EmpiricalTune,T} where {T}
#################### Sampler Constructor ####################
"""
function Empirical(params::Symbol, width::Int64; replace::Bool=false)
This Sampler generates Samples from a distribution by selecting a random value
from this distribution. The move will always be accepted. Sampling can be done with or without replacement.
"""
function Empirical(params::Symbol, width::Int64; replace::Bool = false)
tune = EmpiricalTune(Float64[], logpdf!, rand, width, replace)
Sampler(Float64[], asvec(params), tune, Symbol[], false)
end
function sample!(
v::Sampler{EmpiricalTune{F,F2},T},
lpdf::Function;
model::Model = model,
kwargs...,
) where {T,F,F2}
width = v.tune.width
samfun = v.tune.samplerfun
params = v.params
sam = samfun(model, params, 1)
if width > 1 && v.tune.replacement
while length(Set(sam)) != width
sam = samfun(model, params, 1)
end
end
v[:] = sam
v
end