-
Notifications
You must be signed in to change notification settings - Fork 2
/
sampler.jl
219 lines (173 loc) · 6.07 KB
/
sampler.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
#################### Sampler ####################
const samplerfxargs = [(:model, MCPhylo.Model), (:block, Integer)]
const chunksize = 40
#################### Types and Constructors ####################
struct NullFunction end
Sampler(param::Symbol, args...) = Sampler([param], args...)
"""
Sampler(params::Vector{Symbol}, f::Function, tune::Any=Dict())
Construct a `Sampler` object that defines a sampling function for a block of
stochastic nodes.
Returns a `Sampler{typeof(tune)}` type object.
* `params`: node(s) being block-updated by the sampler.
* `f`: function for the `eval` field of the constructed sampler and whose arguments are the other model nodes upon which the sampler depends, typed argument `model::Model` that contains all model nodes, and/or typed argument `block::Integer` that is an index identifying the corresponding sampling function in a vector of all samplers for the associated model. Through the arguments, all model nodes and fields can be accessed in the body of the function. The function may return an updated sample for the nodes identified in its `params` field. Such a return value can be a structure of the same type as the node if the block consists of only one node, or a dictionary of node structures with keys equal to the block node symbols if one or more. Alternatively, a value of `nothing` may be returned. Return values that are not `nothing` will be used to automatically update the node values and propagate them to dependent nodes. No automatic updating will be done if `nothing` is returned.
* `tune`: tuning parameters needed by the sampling function.
"""
function Sampler(params::Vector{Symbol}, f::Function, tune::Any = Dict())
Sampler(params, modelfx(samplerfxargs, f), tune, Symbol[])
end
function Sampler(
params::Vector{Symbol},
tune::SamplerTune,
targets::Vector{Symbol},
transform::Bool = false,
)
Sampler(Float64[], params, tune, targets, transform)
end
function Sampler(v::T, s::Sampler{R,X})::Sampler{R,T} where {R<:SamplerTune,X,T}
Sampler(v, s.params, s.tune, s.targets, s.transform)
end
#################### Base Methods ####################
function Base.show(io::IO, s::Sampler)
print(io, "An object of type \"$(summary(s))\"\n")
print(io, "Sampling Nodes:\n")
show(io, s.params)
println(io)
end
function showall(io::IO, s::Sampler)
show(io, s)
print(io, "\nTuning Parameters:\n")
show(io, s.tune)
print(io, "\n\nTarget Nodes:\n")
show(io, s.targets)
end
#################### Variate Validators ####################
validate(v::Sampler) = v
function validatebinary(v::Sampler)
all(insupport(Bernoulli, v)) || throw(ArgumentError("variate is not a binary vector"))
v
end
function validatesimplex(v::Sampler)
isprobvec(v) || throw(ArgumentError("variate is not a probability vector $v"))
v
end
#################### Simulation Methods ####################
function _gradlogpdf!(m::Model, x::AbstractArray, block::Integer, dtype::Symbol = :provided)
targets = keys(m, :target, block)
node = m[targets[1]]
update!(node, m)
return gradlogpdf(node)
end
function logpdfgrad!(
m::Model,
x::R,
t::Sampler{GS,X},
) where {R,GS<:GradSampler{G},X} where {G}
logpdfgrad!(G, m, x, t.params, t.targets, t.transform)
end
function logpdfgrad!(
::Type{fwd},
m::Model,
x::AbstractVector{T},
params::ElementOrVector{Symbol},
target::ElementOrVector{Symbol},
transform::Bool,
) where {T<:Real}
ll::Float64 = 0.0
function lf(y)
let m1 = m, para = params, tar = target, tr = transform
lp = logpdf!(m1, y, para, tar, tr)
ll = ForwardDiff.value(lp)
lp
end
end
chunk = ForwardDiff.Chunk(min(length(x), chunksize))
config = ForwardDiff.GradientConfig(lf, x, chunk)
grad = ForwardDiff.gradient!(similar(x), lf, x, config)
ll, grad
end
function logpdfgrad!(
::Type{fin},
m::Model,
x::AbstractVector{T},
params::ElementOrVector{Symbol},
target::ElementOrVector{Symbol},
transform::Bool,
) where {T<:Real}
function lf(y)
let m1 = m, para = params, tar = target, tr = transform
logpdf!(m1, y, para, tar, tr)
end
end
f(x), FiniteDiff.finite_difference_gradient(f, x)
end
function logpdfgrad!(
::Type{rev},
m::Model,
x::AbstractVector{T},
params::ElementOrVector{Symbol},
target::ElementOrVector{Symbol},
transform::Bool,
) where {T<:Real}
function lf(y)
let m1 = m, para = params, tar = target, tr = transform
logpdf!(m1, y, para, tar, tr)
end
end
lf(x), ReverseDiff.gradient(lf, x)
end
function logpdfgrad!(
::Type{zyg},
m::Model,
x::AbstractVector{T},
params::ElementOrVector{Symbol},
target::ElementOrVector{Symbol},
transform::Bool,
) where {T<:Real}
function lf(y)
let m1 = m, para = params, tar = target, tr = transform
logpdf!(m1, y, para, tar, tr)
end
end
ll, grad = withgradient(lf, x)
ll, grad[1]
end
function logpdfgrad!(
::Type{provided},
m::Model,
x::T,
params::ElementOrVector{Symbol},
target::ElementOrVector{Symbol},
transform::Bool,
) where {T<:GeneralNode}
m[params] = relist(m, x, params, transform)
# likelihood
v, grad = gradlogpdf(m, target)
# prior
t_node::Stochastic{T} = m[params[1]]
vp, gradp = gradlogpdf(t_node, x)
vp + v, gradp .+ grad
end
function logpdf!(m::Model, x::A, t::T) where {A,T<:Sampler}
logpdf!(m, x, t.params, t.targets, t.transform)
end
function logpdf!(
m::Model,
x::T,
params::ElementOrVector{Symbol},
target::ElementOrVector{Symbol},
transform::Bool,
) where {T<:GeneralNode}
m[params] = relist(m, x, params, transform)
lp = logpdf(m, setdiff(params, target), transform)
for key in target
isfinite(lp) || break
#node = m[key]
m[key] = update!(m[key], m)
lp += key in params ? logpdf(m[key], transform) : logpdf(m[key])
end
lp
end
#################### Auxiliary Functions ####################
asvec(x::Union{Number,Symbol}) = [x]
asvec(x::Vector) = x