forked from greta-dev/greta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsamplers.R
377 lines (330 loc) · 10.1 KB
/
samplers.R
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#' @name samplers
#'
#' @title MCMC samplers
#' @description Functions to set up MCMC samplers and change the starting values
#' of their parameters, for use in [mcmc()].
#'
#' @details During the warmup iterations of `mcmc`, some of these
#' sampler parameters will be tuned to improve the efficiency of the sampler,
#' so the values provided here are used as starting values.
#'
#' @return a `sampler` object that can be passed to [mcmc()].
NULL
# nolint start
#' @rdname samplers
#' @export
#'
#' @param Lmin minimum number of leapfrog steps (positive integer, Lmin > Lmax)
#' @param Lmax maximum number of leapfrog steps (positive integer, Lmax > Lmin)
#' @param epsilon leapfrog stepsize hyperparameter (positive, will be tuned)
#' @param diag_sd estimate of the posterior marginal standard deviations
#' (positive, will be tuned).
#'
#' @details For `hmc()`, the number of leapfrog steps at each iteration is
#' selected uniformly at random from between `Lmin` and `Lmax`.
#' `diag_sd` is used to rescale the parameter space to make it more
#' uniform, and make sampling more efficient.
hmc <- function(Lmin = 5, Lmax = 10, epsilon = 0.1, diag_sd = 1) {
# nolint end
obj <- list(
parameters = list(
Lmin = Lmin,
Lmax = Lmax,
epsilon = epsilon,
diag_sd = diag_sd
),
name = "hmc",
class = hmc_sampler
)
class(obj) <- c("hmc sampler", "sampler")
obj
}
#' @rdname samplers
#'
#' @details `rwmh()` creates a random walk Metropolis-Hastings sampler; a
#' a gradient-free sampling algorithm. The algorithm involves a proposal
#' generating step `proposal_state = current_state + perturb` by a random
#' perturbation, followed by Metropolis-Hastings accept/reject step. The class
#' is implemented for uniform and normal proposals.
#'
#' @param proposal the probability distribution used to generate proposal states
#'
#' @export
rwmh <- function(
proposal = c("normal", "uniform"),
epsilon = 0.1,
diag_sd = 1
) {
proposal <- match.arg(proposal)
obj <- list(
parameters = list(
proposal = proposal,
epsilon = epsilon,
diag_sd = diag_sd
),
name = "rwmh",
class = rwmh_sampler
)
class(obj) <- c("rwmh sampler", "sampler")
obj
}
#' @rdname samplers
#'
#' @details `slice()` implements a multivariate slice sampling algorithm.
#' The parameter `max_doublings` is not tuned during warmup.
#'
#' @param max_doublings the maximum number of iterations of the 'doubling'
#' algorithm used to adapt the size of the slice
#'
#' @export
slice <- function(max_doublings = 5) {
obj <- list(
parameters = list(
max_doublings = as.integer(max_doublings)[1]
),
name = "slice",
class = slice_sampler
)
class(obj) <- c("slice sampler", "sampler")
obj
}
#' @noRd
#' @export
print.sampler <- function(x, ...) {
values_text <- paste(
names(x$parameters),
prettyNum(x$parameters),
sep = " = ",
collapse = ", "
)
if (!nzchar(values_text)) values_text <- "None"
parameters_text <- glue::glue(
"
parameters:
{values_text}
"
)
msg <- glue::glue(
"{class(x)[1]} object with {parameters_text}"
)
cat(msg)
}
hmc_sampler <- R6Class(
"hmc_sampler",
inherit = sampler,
public = list(
parameters = list(
Lmin = 10,
Lmax = 20,
epsilon = 0.005,
diag_sd = 1
),
accept_target = 0.651,
define_tf_kernel = function(sampler_param_vec) {
dag <- self$model$dag
tfe <- dag$tf_environment
free_state_size <- length(sampler_param_vec) - 2
# TF1/2 check
# this will likely get replaced...
hmc_l <- sampler_param_vec[0]
hmc_epsilon <- sampler_param_vec[1]
hmc_diag_sd <- sampler_param_vec[2:(1 + free_state_size)]
hmc_step_sizes <- tf$cast(
x = tf$reshape(
hmc_epsilon * (hmc_diag_sd / tf$reduce_sum(hmc_diag_sd)),
shape = shape(free_state_size)
),
dtype = tf$float64
)
# TF1/2 check
# where is "free_state" pulled from, given that it is the
# argument to this function, "generate_log_prob_function" ?
# log probability function
# build the kernel
# nolint start
sampler_kernel <- tfp$mcmc$HamiltonianMonteCarlo(
target_log_prob_fn = dag$tf_log_prob_function_adjusted,
step_size = hmc_step_sizes,
num_leapfrog_steps = hmc_l
)
return(
sampler_kernel
)
},
sampler_parameter_values = function() {
# random number of integration steps
l_min <- self$parameters$Lmin
l_max <- self$parameters$Lmax
l <- sample(seq(l_min, l_max), 1)
epsilon <- self$parameters$epsilon
diag_sd <- matrix(self$parameters$diag_sd)
# return named list for replacing tensors
list(
hmc_l = l,
hmc_epsilon = epsilon,
hmc_diag_sd = diag_sd
)
}
)
)
rwmh_sampler <- R6Class(
"rwmh_sampler",
inherit = sampler,
public = list(
parameters = list(
proposal = "normal",
epsilon = 0.1,
diag_sd = 1
),
accept_target = 0.44,
define_tf_kernel = function(sampler_param_vec) {
# wrap this up into a function to extract these out
free_state_size <- length(sampler_param_vec) - 1 # get it from dag object
# e.g., length(dag$free_state)
rwmh_epsilon <- sampler_param_vec[0]
rwmh_diag_sd <- sampler_param_vec[1:(1 + free_state_size)]
dag <- self$model$dag
tfe <- dag$tf_environment
tfe$rwmh_proposal <- switch(
self$parameters$proposal,
normal = tfp$mcmc$random_walk_normal_fn,
uniform = tfp$mcmc$random_walk_uniform_fn
)
# TF1/2 check
# I think a good portion of this code could be abstracted away
# Perhaps from `rwmh_epsilon` to `new_state_fn`
# could be passed
# tfe$log_prob_fun <- dag$generate_log_prob_function()
# tensors for sampler parameters
# rwmh_epsilon <- tf$compat$v1$placeholder(dtype = tf_float())
# need to pass in the value for this placeholder as a matrix (shape(n, 1))
# rwmh_diag_sd <- tf$compat$v1$placeholder(
# dtype = tf_float(),
# # TF1/2 check
# again what do we with with `free_state`?
# shape = shape(dim(free_state)[[2]], 1)
# )
# but it step_sizes must be a vector (shape(n, )), so reshape it
rwmh_step_sizes <- tf$reshape(
rwmh_epsilon * (rwmh_diag_sd / tf$reduce_sum(rwmh_diag_sd)),
# TF1/2 check
# what are we to do about `free_state` here?
shape = shape(free_state_size)
)
new_state_fn <- tfe$rwmh_proposal(scale = rwmh_step_sizes)
# build the kernel
# nolint start
sampler_kernel <- tfp$mcmc$RandomWalkMetropolis(
target_log_prob_fn = dag$tf_log_prob_function_adjusted,
new_state_fn = new_state_fn
)
return(
sampler_kernel
)
},
sampler_parameter_values = function() {
epsilon <- self$parameters$epsilon
diag_sd <- matrix(self$parameters$diag_sd)
# return named list for replacing tensors
list(
rwmh_epsilon = epsilon,
rwmh_diag_sd = diag_sd
)
}
)
)
slice_sampler <- R6Class(
"slice_sampler",
inherit = sampler,
public = list(
parameters = list(
max_doublings = NA
),
tuning_interval = Inf,
uses_metropolis = FALSE,
define_tf_kernel = function(sampler_param_vec) {
slice_max_doublings <- tensorflow::as_tensor(
x = sampler_param_vec[0],
dtype = tf$int32
)
dag <- self$model$dag
tfe <- dag$tf_environment
# build the kernel
# nolint start
sampler_kernel <- tfp$mcmc$SliceSampler(
target_log_prob_fn = dag$tf_log_prob_function_adjusted,
step_size = fl(1),
max_doublings = slice_max_doublings
)
return(
sampler_kernel
)
},
sampler_parameter_values = function() {
max_doublings <- as.integer(self$parameters$max_doublings)
# return named list for replacing tensors
list(slice_max_doublings = max_doublings)
},
# no additional here tuning
tune = function(iterations_completed, total_iterations) {
}
)
)
adaptive_hmc_sampler <- R6Class(
"adaptive_hmc_sampler",
inherit = sampler,
public = list(
parameters = list(
# Lmin = 10,
# Lmax = 20,
max_leapfrog_steps = 1000,
# TODO clean up these parameter usage else where
# epsilon = 0.005,
# diag_sd = 1,
# TODO some kind of validity check of method? Currently this can only be
# "SNAPER".
method = "SNAPER"
),
accept_target = 0.651,
define_tf_kernel = function(sampler_param_vec) {
dag <- self$model$dag
tfe <- dag$tf_environment
free_state_size <- length(sampler_param_vec) - 2
adaptive_hmc_max_leapfrog_steps <- tf$cast(
x = sampler_param_vec[0],
dtype = tf$int32
)
# TODO pipe that in properly
n_warmup <- sampler_param_vec[1]
# adaptive_hmc_epsilon <- sampler_param_vec[1]
# adaptive_hmc_diag_sd <- sampler_param_vec[2:(1+free_state_size)]
kernel_base <- tfp$experimental$mcmc$SNAPERHamiltonianMonteCarlo(
target_log_prob_fn = dag$tf_log_prob_function_adjusted,
step_size = 1,
num_adaptation_steps = as.integer(self$warmup),
max_leapfrog_steps = adaptive_hmc_max_leapfrog_steps
)
sampler_kernel <- tfp$mcmc$DualAveragingStepSizeAdaptation(
inner_kernel = kernel_base,
num_adaptation_steps = as.integer(self$warmup)
)
return(
sampler_kernel
)
},
sampler_parameter_values = function() {
# random number of integration steps
max_leapfrog_steps <- self$parameters$max_leapfrog_steps
epsilon <- self$parameters$epsilon
diag_sd <- matrix(self$parameters$diag_sd)
method <- self$parameters$method
# return named list for replacing tensors
list(
adaptive_hmc_max_leapfrog_steps = max_leapfrog_steps,
# adaptive_hmc_epsilon = epsilon,
# adaptive_hmc_diag_sd = diag_sd,
method = method
)
}
)
)