247
247
248
248
"""
249
249
transitions_init(transition, model, sampler, N[; kwargs...])
250
+ transitions_init(transition, model, sampler[; kwargs...])
250
251
251
252
Generate a container for the `N` transitions of the MCMC `sampler` for the provided
252
- `model`, whose first transition is `transition`.
253
+ `model`, whose first transition is `transition`. Can be called with an without a predefined size `N`.
253
254
"""
254
255
function transitions_init (
255
256
transition,
@@ -261,11 +262,21 @@ function transitions_init(
261
262
return Vector {typeof(transition)} (undef, N)
262
263
end
263
264
265
+ function transitions_init (
266
+ transition,
267
+ :: AbstractModel ,
268
+ :: AbstractSampler ;
269
+ kwargs...
270
+ )
271
+ return [transition]
272
+ end
273
+
264
274
"""
265
275
transitions_save!(transitions, iteration, transition, model, sampler, N[; kwargs...])
276
+ transitions_save!(transitions, iteration, transition, model, sampler[; kwargs...])
266
277
267
278
Save the `transition` of the MCMC `sampler` at the current `iteration` in the container of
268
- `transitions`.
279
+ `transitions`. Can be called with an without a predefined size `N`.
269
280
"""
270
281
function transitions_save! (
271
282
transitions:: AbstractVector ,
@@ -280,6 +291,19 @@ function transitions_save!(
280
291
return
281
292
end
282
293
294
+
295
+ function transitions_save! (
296
+ transitions:: AbstractVector ,
297
+ iteration:: Integer ,
298
+ transition,
299
+ :: AbstractModel ,
300
+ :: AbstractSampler ;
301
+ kwargs...
302
+ )
303
+ push! (transitions, transition)
304
+ return
305
+ end
306
+
283
307
"""
284
308
psample([rng::AbstractRNG, ]model::AbstractModel, sampler::AbstractSampler, N::Integer,
285
309
nchains::Integer; kwargs...)
421
445
# Sample-until-convergence tools #
422
446
# #################################
423
447
424
-
425
448
"""
426
449
sample([rng::AbstractRNG, ]model::AbstractModel, s::AbstractSampler, is_done::Function; kwargs...)
427
450
@@ -450,19 +473,19 @@ function StatsBase.sample(
450
473
# Perform any necessary setup.
451
474
sample_init! (rng, model, sampler, 1 ; kwargs... )
452
475
453
- # Obtain the initial transition.
454
- transition = step! (rng, model, sampler, 1 ; iteration= 1 , kwargs... )
476
+ @ifwithprogresslogger progress name= progressname begin
477
+ # Obtain the initial transition.
478
+ transition = step! (rng, model, sampler, 1 ; iteration= 1 , kwargs... )
455
479
456
- # Run callback.
457
- callback (rng, model, sampler, 1 , 1 , transition; kwargs... )
480
+ # Run callback.
481
+ callback (rng, model, sampler, 1 , 1 , transition; kwargs... )
458
482
459
- # Save the transition.
460
- transitions = [ transition]
483
+ # Save the transition.
484
+ transitions = transitions_init ( transition, model, sampler; kwargs ... )
461
485
462
- # Step through the sampler until stopping.
463
- i = 2
486
+ # Step through the sampler until stopping.
487
+ i = 2
464
488
465
- @ifwithprogresslogger progress name= progressname begin
466
489
while ! is_done (rng, model, sampler, transitions, i; progress= progress, kwargs... )
467
490
# Obtain the next transition.
468
491
transition = step! (rng, model, sampler, 1 , transition; iteration= i, kwargs... )
@@ -471,7 +494,7 @@ function StatsBase.sample(
471
494
callback (rng, model, sampler, 1 , i, transition; kwargs... )
472
495
473
496
# Save the transition.
474
- push ! (transitions, transition)
497
+ transitions_save ! (transitions, i, transition, model, sampler; kwargs ... )
475
498
476
499
# Increment iteration counter.
477
500
i += 1
0 commit comments