-
Notifications
You must be signed in to change notification settings - Fork 2
/
slice.jl
220 lines (170 loc) · 6.23 KB
/
slice.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#################### Slice Sampler ####################
#################### Types and Constructors ####################
const SliceForm = Union{Univariate,Multivariate}
mutable struct SliceTune{F<:SliceForm} <: SamplerTune
logf::Union{Function,Missing}
width::Union{Float64,Vector{Float64}}
SliceTune{F}() where {F<:SliceForm} = new{F}()
SliceTune{F}(x::Vector, width) where {F<:SliceForm} = SliceTune{F}(x, width, missing)
SliceTune{F}(
x::Vector,
width::Real,
logf::Union{Function,Missing},
) where {F<:SliceForm} = new{F}(logf, Float64(width))
SliceTune{F}(
x::Vector,
width::Vector,
logf::Union{Function,Missing},
) where {F<:SliceForm} = new{F}(logf, convert(Vector{Float64}, width))
SliceTune{F}(width, logf::Union{Function,Missing}) where {F<:SliceForm} =
new{F}(logf, width)
end
const SliceUnivariate = Sampler{SliceTune{Univariate},R} where {R}
const SliceMultivariate = Sampler{SliceTune{Multivariate},R} where {R}
validate(v::Sampler{SliceTune{F}}) where {F<:SliceForm} = validate(v, v.tune.width)
validate(v::Sampler{SliceTune{F}}, width::Float64) where {F<:SliceForm} = v
function validate(v::Sampler{SliceTune{F}}, width::Vector) where {F<:SliceForm}
n = length(v)
length(width) == n ||
throw(ArgumentError("length(width) differs from variate length $n"))
v
end
#################### Sampler Constructor ####################
"""
Slice(params::ElementOrVector{Symbol},
width::ElementOrVector{T},
::Type{F}=Multivariate;
transform::Bool=false) where {T<:Real, F<:SliceForm}
Construct a `Sampler` object for Slice sampling. Parameters are assumed to be
continuous, but may be constrained or unconstrained.
Returns a `Sampler{SliceTune{Univariate}}` or `Sampler{SliceTune{Multivariate}}`
type object if sampling univariately or multivariately, respectively.
* `params`: stochastic node(s) to be updated with the sampler.
* `width`: scaling value or vector of the same length as the combined elements of nodes `params`, defining initial widths of a hyperrectangle from which to simulate values.
* `F` : sampler type. Options are
* `:Univariate` : sequential univariate sampling of parameters.
* `:Multivariate` : joint multivariate sampling.
* `transform`: whether to sample parameters on the link-transformed scale (unconstrained parameter space). If `true`, then constrained parameters are mapped to unconstrained space according to transformations defined by the Stochastic `unlist()` function, and `width` is interpreted as being relative to the unconstrained parameter space. Otherwise, sampling is relative to the untransformed space.
"""
function Slice(
params::ElementOrVector{Symbol},
width::ElementOrVector{T},
::Type{F} = Multivariate;
transform::Bool = false,
) where {T<:Real,F<:SliceForm}
tune = SliceTune{F}(width, logpdf!)
Sampler(params, tune, Symbol[], transform)
end
#################### Sampling Functions ####################
#sample!(v::Union{SliceUnivariate,SliceMultivariate}, logf::Function; args...) = sample!(v, logf)
"""
sample!(v::Union{SliceUnivariate, SliceMultivariate}, logf::Function)
Draw one sample from a target distribution using the Slice univariate or
multivariate sampler. Parameters are assumed to be continuous, but may be
constrained or unconstrained.
Returns `v` updated with simulated values and associated tuning parameters.
"""
function sample!(
v::SliceUnivariate{Vector{T}},
logf::Function;
kwargs...,
) where {T<:GeneralNode}
tree = v.value[1]
logf0 = logf(v.value)
blv = get_branchlength_vector(tree)
n = length(blv)
lower = blv - v.tune.width .* rand(n)
lower[lower.<0.0] .= 0.0
upper = lower .+ v.tune.width
@inbounds for i = 1:n
p0 = logf0 + log(rand())
x = blv[i]
blv[i] = rand(Uniform(lower[i], upper[i]))
while true
set_branchlength_vector!(tree, blv)
logf0 = logf([tree])
logf0 < p0 || break
value = blv[i]
if value < x
lower[i] = value
else
upper[i] = value
end
blv[i] = rand(Uniform(lower[i], upper[i]))
end
end
v
end
function sample!(
v::SliceMultivariate{Vector{T}},
logf::Function;
kwargs...,
) where {T<:GeneralNode}
tree = v.value[1]
p0 = logf(v.value) + log(rand())
blv = get_branchlength_vector(tree)
org = deepcopy(blv)
n = length(blv)
lower = blv - v.tune.width .* rand(n)
lower[lower.<0.0] .= 0.0
upper = lower .+ v.tune.width
blv = v.tune.width .* rand(n) + lower
set_branchlength_vector!(tree, blv)
@inbounds while logf([tree]) < p0
for i = 1:n
value = blv[i]
if value < org[i]
lower[i] = value
else
upper[i] = value
end
blv[i] = rand(Uniform(lower[i], upper[i]))
end
set_branchlength_vector!(tree, blv)
end
v.value[1] = tree
v
end
function sample!(v::SliceUnivariate{Vector{Float64}}, logf::Function; kwargs...)
logf0 = logf(v.value)
n = length(v)
lower = v - v.tune.width .* rand(n)
upper = lower .+ v.tune.width
@inbounds for i = 1:n
p0 = logf0 + log(rand())
x = v[i]
v[i] = rand(Uniform(lower[i], upper[i]))
while true
logf0 = logf(v.value)
logf0 < p0 || break
value = v[i]
if value < x
lower[i] = value
else
upper[i] = value
end
v[i] = rand(Uniform(lower[i], upper[i]))
end
end
v
end
function sample!(v::SliceMultivariate{Vector{Float64}}, logf::Function; kwargs...)
p0 = logf(v.value) + log(rand())
n = length(v)
lower = v - v.tune.width .* rand(n)
upper = lower .+ v.tune.width
x = v.tune.width .* rand(n) + lower
while logf(x) < p0
@inbounds for i = 1:n
value = x[i]
if value < v.value[i]
lower[i] = value
else
upper[i] = value
end
x[i] = rand(Uniform(lower[i], upper[i]))
end
end
v[:] = x
v
end