-
Notifications
You must be signed in to change notification settings - Fork 17
/
sample.jl
633 lines (546 loc) · 19.3 KB
/
sample.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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
# Default implementations of `sample`.
const PROGRESS = Ref(true)
"""
setprogress!(progress::Bool)
Enable progress logging globally if `progress` is `true`, and disable it otherwise.
"""
function setprogress!(progress::Bool)
@info "progress logging is $(progress ? "enabled" : "disabled") globally"
PROGRESS[] = progress
return progress
end
function StatsBase.sample(
model_or_logdensity, sampler::AbstractSampler, N_or_isdone; kwargs...
)
return StatsBase.sample(
Random.default_rng(), model_or_logdensity, sampler, N_or_isdone; kwargs...
)
end
"""
sample(
rng::Random.AbatractRNG=Random.default_rng(),
model::AbstractModel,
sampler::AbstractSampler,
N::Integer;
kwargs...,
)
Return `N` samples from the `model` with the Markov chain Monte Carlo `sampler`.
---
sample(
rng::Random.AbatractRNG,
model::AbstractModel,
sampler::AbstractSampler,
isdone;
kwargs...,
)
Sample from the `model` with the Markov chain Monte Carlo `sampler` until a
convergence criterion `isdone` returns `true`, and return the samples.
The function `isdone` has the signature
```julia
isdone(rng, model, sampler, samples, state, iteration; kwargs...)
```
where `state` and `iteration` are the current state and iteration of the sampler, respectively.
It should return `true` when sampling should end, and `false` otherwise.
"""
function StatsBase.sample(
rng::Random.AbstractRNG,
model::AbstractModel,
sampler::AbstractSampler,
N_or_isdone;
kwargs...,
)
return mcmcsample(rng, model, sampler, N_or_isdone; kwargs...)
end
# Fallback: Wrap log density function in a model
"""
sample(
rng::Random.AbstractRNG=Random.default_rng(),
logdensity,
sampler::AbstractSampler,
N_or_isdone;
kwargs...,
)
Wrap the `logdensity` function in a [`LogDensityModel`](@ref), and call `sample` with the resulting model instead of `logdensity`.
The `logdensity` function has to support the [LogDensityProblems.jl](https://github.com/tpapp/LogDensityProblems.jl) interface.
"""
function StatsBase.sample(
rng::Random.AbstractRNG, logdensity, sampler::AbstractSampler, N_or_isdone; kwargs...
)
return StatsBase.sample(rng, _model(logdensity), sampler, N_or_isdone; kwargs...)
end
function StatsBase.sample(
model_or_logdensity,
sampler::AbstractSampler,
parallel::AbstractMCMCEnsemble,
N::Integer,
nchains::Integer;
kwargs...,
)
return StatsBase.sample(
Random.default_rng(), model_or_logdensity, sampler, parallel, N, nchains; kwargs...
)
end
"""
sample(
rng::Random.AbstractRNG=Random.default_rng(),
model::AbstractModel,
sampler::AbstractSampler,
parallel::AbstractMCMCEnsemble,
N::Integer,
nchains::Integer;
kwargs...,
)
Sample `nchains` Monte Carlo Markov chains from the `model` with the `sampler` in parallel
using the `parallel` algorithm, and combine them into a single chain.
"""
function StatsBase.sample(
rng::Random.AbstractRNG,
model::AbstractModel,
sampler::AbstractSampler,
parallel::AbstractMCMCEnsemble,
N::Integer,
nchains::Integer;
kwargs...,
)
return mcmcsample(rng, model, sampler, parallel, N, nchains; kwargs...)
end
# Fallback: Wrap log density function in a model
"""
sample(
rng::Random.AbstractRNG=Random.default_rng(),,
logdensity,
sampler::AbstractSampler,
parallel::AbstractMCMCEnsemble,
N::Integer,
nchains::Integer;
kwargs...,
)
Wrap the `logdensity` function in a [`LogDensityModel`](@ref), and call `sample` with the resulting model instead of `logdensity`.
The `logdensity` function has to support the [LogDensityProblems.jl](https://github.com/tpapp/LogDensityProblems.jl) interface.
"""
function StatsBase.sample(
rng::Random.AbstractRNG,
logdensity,
sampler::AbstractSampler,
parallel::AbstractMCMCEnsemble,
N::Integer,
nchains::Integer;
kwargs...,
)
return StatsBase.sample(
rng, _model(logdensity), sampler, parallel, N, nchains; kwargs...
)
end
# Default implementations of regular and parallel sampling.
function mcmcsample(
rng::Random.AbstractRNG,
model::AbstractModel,
sampler::AbstractSampler,
N::Integer;
progress=PROGRESS[],
progressname="Sampling",
callback=nothing,
discard_initial=0,
thinning=1,
chain_type::Type=Any,
kwargs...,
)
# Check the number of requested samples.
N > 0 || error("the number of samples must be ≥ 1")
Ntotal = thinning * (N - 1) + discard_initial + 1
# Start the timer
start = time()
local state
@ifwithprogresslogger progress name = progressname begin
# Determine threshold values for progress logging
# (one update per 0.5% of progress)
if progress
threshold = Ntotal ÷ 200
next_update = threshold
end
# Obtain the initial sample and state.
sample, state = step(rng, model, sampler; kwargs...)
# Discard initial samples.
for i in 1:discard_initial
# Update the progress bar.
if progress && i >= next_update
ProgressLogging.@logprogress i / Ntotal
next_update = i + threshold
end
# Obtain the next sample and state.
sample, state = step(rng, model, sampler, state; kwargs...)
end
# Run callback.
callback === nothing || callback(rng, model, sampler, sample, state, 1; kwargs...)
# Save the sample.
samples = AbstractMCMC.samples(sample, model, sampler, N; kwargs...)
samples = save!!(samples, sample, 1, model, sampler, N; kwargs...)
# Update the progress bar.
itotal = 1 + discard_initial
if progress && itotal >= next_update
ProgressLogging.@logprogress itotal / Ntotal
next_update = itotal + threshold
end
# Step through the sampler.
for i in 2:N
# Discard thinned samples.
for _ in 1:(thinning - 1)
# Obtain the next sample and state.
sample, state = step(rng, model, sampler, state; kwargs...)
# Update progress bar.
if progress && (itotal += 1) >= next_update
ProgressLogging.@logprogress itotal / Ntotal
next_update = itotal + threshold
end
end
# Obtain the next sample and state.
sample, state = step(rng, model, sampler, state; kwargs...)
# Run callback.
callback === nothing ||
callback(rng, model, sampler, sample, state, i; kwargs...)
# Save the sample.
samples = save!!(samples, sample, i, model, sampler, N; kwargs...)
# Update the progress bar.
if progress && (itotal += 1) >= next_update
ProgressLogging.@logprogress itotal / Ntotal
next_update = itotal + threshold
end
end
end
# Get the sample stop time.
stop = time()
duration = stop - start
stats = SamplingStats(start, stop, duration)
return bundle_samples(
samples,
model,
sampler,
state,
chain_type;
stats=stats,
discard_initial=discard_initial,
thinning=thinning,
kwargs...,
)
end
function mcmcsample(
rng::Random.AbstractRNG,
model::AbstractModel,
sampler::AbstractSampler,
isdone;
chain_type::Type=Any,
progress=PROGRESS[],
progressname="Convergence sampling",
callback=nothing,
discard_initial=0,
thinning=1,
kwargs...,
)
# Start the timer
start = time()
local state
@ifwithprogresslogger progress name = progressname begin
# Obtain the initial sample and state.
sample, state = step(rng, model, sampler; kwargs...)
# Discard initial samples.
for _ in 1:discard_initial
# Obtain the next sample and state.
sample, state = step(rng, model, sampler, state; kwargs...)
end
# Run callback.
callback === nothing || callback(rng, model, sampler, sample, state, 1; kwargs...)
# Save the sample.
samples = AbstractMCMC.samples(sample, model, sampler; kwargs...)
samples = save!!(samples, sample, 1, model, sampler; kwargs...)
# Step through the sampler until stopping.
i = 2
while !isdone(rng, model, sampler, samples, state, i; progress=progress, kwargs...)
# Discard thinned samples.
for _ in 1:(thinning - 1)
# Obtain the next sample and state.
sample, state = step(rng, model, sampler, state; kwargs...)
end
# Obtain the next sample and state.
sample, state = step(rng, model, sampler, state; kwargs...)
# Run callback.
callback === nothing ||
callback(rng, model, sampler, sample, state, i; kwargs...)
# Save the sample.
samples = save!!(samples, sample, i, model, sampler; kwargs...)
# Increment iteration counter.
i += 1
end
end
# Get the sample stop time.
stop = time()
duration = stop - start
stats = SamplingStats(start, stop, duration)
# Wrap the samples up.
return bundle_samples(
samples,
model,
sampler,
state,
chain_type;
stats=stats,
discard_initial=discard_initial,
thinning=thinning,
kwargs...,
)
end
function mcmcsample(
rng::Random.AbstractRNG,
model::AbstractModel,
sampler::AbstractSampler,
::MCMCThreads,
N::Integer,
nchains::Integer;
progress=PROGRESS[],
progressname="Sampling ($(min(nchains, Threads.nthreads())) threads)",
init_params=nothing,
kwargs...,
)
# Check if actually multiple threads are used.
if Threads.nthreads() == 1
@warn "Only a single thread available: MCMC chains are not sampled in parallel"
end
# Check if the number of chains is larger than the number of samples
if nchains > N
@warn "Number of chains ($nchains) is greater than number of samples per chain ($N)"
end
# Copy the random number generator, model, and sample for each thread
nchunks = min(nchains, Threads.nthreads())
chunksize = cld(nchains, nchunks)
interval = 1:nchunks
rngs = [deepcopy(rng) for _ in interval]
models = [deepcopy(model) for _ in interval]
samplers = [deepcopy(sampler) for _ in interval]
# Create a seed for each chain using the provided random number generator.
seeds = rand(rng, UInt, nchains)
# Ensure that initial parameters are `nothing` or indexable
_init_params = _first_or_nothing(init_params, nchains)
# Set up a chains vector.
chains = Vector{Any}(undef, nchains)
@ifwithprogresslogger progress name = progressname begin
# Create a channel for progress logging.
if progress
channel = Channel{Bool}(length(interval))
end
Distributed.@sync begin
if progress
# Update the progress bar.
Distributed.@async begin
# Determine threshold values for progress logging
# (one update per 0.5% of progress)
threshold = nchains ÷ 200
nextprogresschains = threshold
progresschains = 0
while take!(channel)
progresschains += 1
if progresschains >= nextprogresschains
ProgressLogging.@logprogress progresschains / nchains
nextprogresschains = progresschains + threshold
end
end
end
end
Distributed.@async begin
try
Distributed.@sync for (i, _rng, _model, _sampler) in
zip(1:nchunks, rngs, models, samplers)
chainidxs = if i == nchunks
((i - 1) * chunksize + 1):nchains
else
((i - 1) * chunksize + 1):(i * chunksize)
end
Threads.@spawn for chainidx in chainidxs
# Seed the chunk-specific random number generator with the pre-made seed.
Random.seed!(_rng, seeds[chainidx])
# Sample a chain and save it to the vector.
chains[chainidx] = StatsBase.sample(
_rng,
_model,
_sampler,
N;
progress=false,
init_params=if _init_params === nothing
nothing
else
_init_params[chainidx]
end,
kwargs...,
)
# Update the progress bar.
progress && put!(channel, true)
end
end
finally
# Stop updating the progress bar.
progress && put!(channel, false)
end
end
end
end
# Concatenate the chains together.
return chainsstack(tighten_eltype(chains))
end
function mcmcsample(
rng::Random.AbstractRNG,
model::AbstractModel,
sampler::AbstractSampler,
::MCMCDistributed,
N::Integer,
nchains::Integer;
progress=PROGRESS[],
progressname="Sampling ($(Distributed.nworkers()) processes)",
init_params=nothing,
kwargs...,
)
# Check if actually multiple processes are used.
if Distributed.nworkers() == 1
@warn "Only a single process available: MCMC chains are not sampled in parallel"
end
# Check if the number of chains is larger than the number of samples
if nchains > N
@warn "Number of chains ($nchains) is greater than number of samples per chain ($N)"
end
# Create a seed for each chain using the provided random number generator.
seeds = rand(rng, UInt, nchains)
# Set up worker pool.
pool = Distributed.CachingPool(Distributed.workers())
local chains
@ifwithprogresslogger progress name = progressname begin
# Create a channel for progress logging.
if progress
channel = Distributed.RemoteChannel(() -> Channel{Bool}(Distributed.nworkers()))
end
Distributed.@sync begin
if progress
# Update the progress bar.
Distributed.@async begin
# Determine threshold values for progress logging
# (one update per 0.5% of progress)
threshold = nchains ÷ 200
nextprogresschains = threshold
progresschains = 0
while take!(channel)
progresschains += 1
if progresschains >= nextprogresschains
ProgressLogging.@logprogress progresschains / nchains
nextprogresschains = progresschains + threshold
end
end
end
end
Distributed.@async begin
try
function sample_chain(seed, init_params=nothing)
# Seed a new random number generator with the pre-made seed.
Random.seed!(rng, seed)
# Sample a chain.
chain = StatsBase.sample(
rng,
model,
sampler,
N;
progress=false,
init_params=init_params,
kwargs...,
)
# Update the progress bar.
progress && put!(channel, true)
# Return the new chain.
return chain
end
chains = if init_params === nothing
Distributed.pmap(sample_chain, pool, seeds)
else
Distributed.pmap(sample_chain, pool, seeds, init_params)
end
finally
# Stop updating the progress bar.
progress && put!(channel, false)
end
end
end
end
# Concatenate the chains together.
return chainsstack(tighten_eltype(chains))
end
function mcmcsample(
rng::Random.AbstractRNG,
model::AbstractModel,
sampler::AbstractSampler,
::MCMCSerial,
N::Integer,
nchains::Integer;
progressname="Sampling",
init_params=nothing,
kwargs...,
)
# Check if the number of chains is larger than the number of samples
if nchains > N
@warn "Number of chains ($nchains) is greater than number of samples per chain ($N)"
end
# Create a seed for each chain using the provided random number generator.
seeds = rand(rng, UInt, nchains)
# Sample the chains.
function sample_chain(i, seed, init_params=nothing)
# Seed a new random number generator with the pre-made seed.
Random.seed!(rng, seed)
# Sample a chain.
return StatsBase.sample(
rng,
model,
sampler,
N;
progressname=string(progressname, " (Chain ", i, " of ", nchains, ")"),
init_params=init_params,
kwargs...,
)
end
chains = if init_params === nothing
map(sample_chain, 1:nchains, seeds)
else
map(sample_chain, 1:nchains, seeds, init_params)
end
# Concatenate the chains together.
return chainsstack(tighten_eltype(chains))
end
tighten_eltype(x) = x
tighten_eltype(x::Vector{Any}) = map(identity, x)
function _model(logdensity)
if LogDensityProblems.capabilities(logdensity) === nothing
throw(
ArgumentError(
"the log density function does not support the LogDensityProblems.jl interface. Please implement the interface or provide a model of type `AbstractMCMC.AbstractModel`",
),
)
end
return LogDensityModel(logdensity)
end
"""
_first_or_nothing(x, n::Int)
Return the first `n` elements of collection `x`, or `nothing` if `x === nothing`.
If `x !== nothing`, then `x` has to contain at least `n` elements.
"""
function _first_or_nothing(x, n::Int)
y = _first(x, n)
length(y) == n || throw(
ArgumentError("not enough initial parameters (expected $n, received $(length(y))"),
)
return y
end
_first_or_nothing(::Nothing, ::Int) = nothing
# `first(x, n::Int)` requires Julia 1.6
function _first(x, n::Int)
@static if VERSION >= v"1.6.0-DEV.431"
first(x, n)
else
if x isa AbstractVector
@inbounds x[firstindex(x):min(firstindex(x) + n - 1, lastindex(x))]
else
collect(Iterators.take(x, n))
end
end
end