-
Notifications
You must be signed in to change notification settings - Fork 17
/
sample.jl
594 lines (515 loc) · 20.6 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
@testset "sample.jl" begin
@testset "Basic sampling" begin
@testset "REPL" begin
empty!(LOGGERS)
Random.seed!(1234)
N = 1_000
chain = sample(MyModel(), MySampler(), N; sleepy=true, loggers=true)
@test length(LOGGERS) == 1
logger = first(LOGGERS)
@test logger isa TeeLogger
@test logger.loggers[1].logger isa
(Sys.iswindows() && VERSION < v"1.5.3" ? ProgressLogger : TerminalLogger)
@test logger.loggers[2].logger === CURRENT_LOGGER
@test Logging.current_logger() === CURRENT_LOGGER
# test output type and size
@test chain isa Vector{<:MySample}
@test length(chain) == N
# test some statistical properties
tail_chain = @view chain[2:end]
@test mean(x.a for x in tail_chain) ≈ 0.5 atol = 6e-2
@test var(x.a for x in tail_chain) ≈ 1 / 12 atol = 5e-3
@test mean(x.b for x in tail_chain) ≈ 0.0 atol = 5e-2
@test var(x.b for x in tail_chain) ≈ 1 atol = 6e-2
# initial parameters
chain = sample(
MyModel(), MySampler(), 3; progress=false, init_params=(b=3.2, a=-1.8)
)
@test chain[1].a == -1.8
@test chain[1].b == 3.2
end
@testset "Juno" begin
empty!(LOGGERS)
Random.seed!(1234)
N = 10
logger = JunoProgressLogger()
Logging.with_logger(logger) do
sample(MyModel(), MySampler(), N; sleepy=true, loggers=true)
end
@test length(LOGGERS) == 1
@test first(LOGGERS) === logger
@test Logging.current_logger() === CURRENT_LOGGER
end
@testset "IJulia" begin
# emulate running IJulia kernel
@eval IJulia begin
inited = true
end
empty!(LOGGERS)
Random.seed!(1234)
N = 10
sample(MyModel(), MySampler(), N; sleepy=true, loggers=true)
@test length(LOGGERS) == 1
logger = first(LOGGERS)
@test logger isa TeeLogger
@test logger.loggers[1].logger isa ProgressLogger
@test logger.loggers[2].logger === CURRENT_LOGGER
@test Logging.current_logger() === CURRENT_LOGGER
@eval IJulia begin
inited = false
end
end
@testset "Custom logger" begin
empty!(LOGGERS)
Random.seed!(1234)
N = 10
logger = Logging.ConsoleLogger(stderr, Logging.LogLevel(-1))
Logging.with_logger(logger) do
sample(MyModel(), MySampler(), N; sleepy=true, loggers=true)
end
@test length(LOGGERS) == 1
@test first(LOGGERS) === logger
@test Logging.current_logger() === CURRENT_LOGGER
end
@testset "Suppress output" begin
logs, _ = collect_test_logs(; min_level=Logging.LogLevel(-1)) do
sample(MyModel(), MySampler(), 100; progress=false, sleepy=true)
end
@test all(l.level > Logging.LogLevel(-1) for l in logs)
# disable progress logging globally
@test !(@test_logs (:info, "progress logging is disabled globally") AbstractMCMC.setprogress!(
false
))
@test !AbstractMCMC.PROGRESS[]
logs, _ = collect_test_logs(; min_level=Logging.LogLevel(-1)) do
sample(MyModel(), MySampler(), 100; sleepy=true)
end
@test all(l.level > Logging.LogLevel(-1) for l in logs)
# enable progress logging globally
@test (@test_logs (:info, "progress logging is enabled globally") AbstractMCMC.setprogress!(
true
))
@test AbstractMCMC.PROGRESS[]
end
end
@testset "Multithreaded sampling" begin
if Threads.nthreads() == 1
warnregex = r"^Only a single thread available"
@test_logs (:warn, warnregex) sample(
MyModel(), MySampler(), MCMCThreads(), 10, 10
)
end
# No dedicated chains type
N = 10_000
chains = sample(MyModel(), MySampler(), MCMCThreads(), N, 1000)
@test chains isa Vector{<:Vector{<:MySample}}
@test length(chains) == 1000
@test all(length(x) == N for x in chains)
Random.seed!(1234)
chains = sample(MyModel(), MySampler(), MCMCThreads(), N, 1000; chain_type=MyChain)
# test output type and size
@test chains isa Vector{<:MyChain}
@test length(chains) == 1000
@test all(x -> length(x.as) == length(x.bs) == N, chains)
# test some statistical properties
@test all(x -> isapprox(mean(@view x.as[2:end]), 0.5; atol=5e-2), chains)
@test all(x -> isapprox(var(@view x.as[2:end]), 1 / 12; atol=5e-3), chains)
@test all(x -> isapprox(mean(@view x.bs[2:end]), 0; atol=5e-2), chains)
@test all(x -> isapprox(var(@view x.bs[2:end]), 1; atol=1e-1), chains)
# test reproducibility
Random.seed!(1234)
chains2 = sample(MyModel(), MySampler(), MCMCThreads(), N, 1000; chain_type=MyChain)
@test all(c1.as[i] === c2.as[i] for (c1, c2) in zip(chains, chains2), i in 1:N)
@test all(c1.bs[i] === c2.bs[i] for (c1, c2) in zip(chains, chains2), i in 1:N)
# Unexpected order of arguments.
str = "Number of chains (10) is greater than number of samples per chain (5)"
@test_logs (:warn, str) match_mode = :any sample(
MyModel(), MySampler(), MCMCThreads(), 5, 10; chain_type=MyChain
)
# Suppress output.
logs, _ = collect_test_logs(; min_level=Logging.LogLevel(-1)) do
sample(
MyModel(),
MySampler(),
MCMCThreads(),
10_000,
1000;
progress=false,
chain_type=MyChain,
)
end
@test all(l.level > Logging.LogLevel(-1) for l in logs)
# Smoke test for nchains < nthreads
if Threads.nthreads() == 2
sample(MyModel(), MySampler(), MCMCThreads(), N, 1)
end
# initial parameters
init_params = [(b=randn(), a=rand()) for _ in 1:100]
chains = sample(
MyModel(),
MySampler(),
MCMCThreads(),
3,
100;
progress=false,
init_params=init_params,
)
@test length(chains) == 100
@test all(
chain[1].a == params.a && chain[1].b == params.b for
(chain, params) in zip(chains, init_params)
)
init_params = (a=randn(), b=rand())
chains = sample(
MyModel(),
MySampler(),
MCMCThreads(),
3,
100;
progress=false,
init_params=Iterators.repeated(init_params),
)
@test length(chains) == 100
@test all(
chain[1].a == init_params.a && chain[1].b == init_params.b for chain in chains
)
end
@testset "Multicore sampling" begin
if nworkers() == 1
warnregex = r"^Only a single process available"
@test_logs (:warn, warnregex) sample(
MyModel(), MySampler(), MCMCDistributed(), 10, 10; chain_type=MyChain
)
end
# Add worker processes.
# Memory requirements on Windows are ~4x larger than on Linux, hence number of processes is reduced
# See, e.g., https://github.com/JuliaLang/julia/issues/40766 and https://github.com/JuliaLang/Pkg.jl/pull/2366
addprocs(Sys.iswindows() ? div(Sys.CPU_THREADS::Int, 2) : Sys.CPU_THREADS::Int)
# Load all required packages (`interface.jl` needs Random).
@everywhere begin
using AbstractMCMC
using AbstractMCMC: sample
using Random
include("utils.jl")
end
# No dedicated chains type
N = 10_000
chains = sample(MyModel(), MySampler(), MCMCThreads(), N, 1000)
@test chains isa Vector{<:Vector{<:MySample}}
@test length(chains) == 1000
@test all(length(x) == N for x in chains)
Random.seed!(1234)
chains = sample(
MyModel(), MySampler(), MCMCDistributed(), N, 1000; chain_type=MyChain
)
# Test output type and size.
@test chains isa Vector{<:MyChain}
@test all(c.as[1] === missing for c in chains)
@test length(chains) == 1000
@test all(x -> length(x.as) == length(x.bs) == N, chains)
# Test some statistical properties.
@test all(x -> isapprox(mean(@view x.as[2:end]), 0.5; atol=5e-2), chains)
@test all(x -> isapprox(var(@view x.as[2:end]), 1 / 12; atol=5e-3), chains)
@test all(x -> isapprox(mean(@view x.bs[2:end]), 0; atol=5e-2), chains)
@test all(x -> isapprox(var(@view x.bs[2:end]), 1; atol=1e-1), chains)
# Test reproducibility.
Random.seed!(1234)
chains2 = sample(
MyModel(), MySampler(), MCMCDistributed(), N, 1000; chain_type=MyChain
)
@test all(c1.as[i] === c2.as[i] for (c1, c2) in zip(chains, chains2), i in 1:N)
@test all(c1.bs[i] === c2.bs[i] for (c1, c2) in zip(chains, chains2), i in 1:N)
# Unexpected order of arguments.
str = "Number of chains (10) is greater than number of samples per chain (5)"
@test_logs (:warn, str) match_mode = :any sample(
MyModel(), MySampler(), MCMCDistributed(), 5, 10; chain_type=MyChain
)
# Suppress output.
logs, _ = collect_test_logs(; min_level=Logging.LogLevel(-1)) do
sample(
MyModel(),
MySampler(),
MCMCDistributed(),
10_000,
100;
progress=false,
chain_type=MyChain,
)
end
@test all(l.level > Logging.LogLevel(-1) for l in logs)
# initial parameters
init_params = [(a=randn(), b=rand()) for _ in 1:100]
chains = sample(
MyModel(),
MySampler(),
MCMCDistributed(),
3,
100;
progress=false,
init_params=init_params,
)
@test length(chains) == 100
@test all(
chain[1].a == params.a && chain[1].b == params.b for
(chain, params) in zip(chains, init_params)
)
init_params = (b=randn(), a=rand())
chains = sample(
MyModel(),
MySampler(),
MCMCDistributed(),
3,
100;
progress=false,
init_params=Iterators.repeated(init_params),
)
@test length(chains) == 100
@test all(
chain[1].a == init_params.a && chain[1].b == init_params.b for chain in chains
)
end
@testset "Serial sampling" begin
# No dedicated chains type
N = 10_000
chains = sample(MyModel(), MySampler(), MCMCSerial(), N, 1000)
@test chains isa Vector{<:Vector{<:MySample}}
@test length(chains) == 1000
@test all(length(x) == N for x in chains)
Random.seed!(1234)
chains = sample(MyModel(), MySampler(), MCMCSerial(), N, 1000; chain_type=MyChain)
# Test output type and size.
@test chains isa Vector{<:MyChain}
@test all(c.as[1] === missing for c in chains)
@test length(chains) == 1000
@test all(x -> length(x.as) == length(x.bs) == N, chains)
# Test some statistical properties.
@test all(x -> isapprox(mean(@view x.as[2:end]), 0.5; atol=5e-2), chains)
@test all(x -> isapprox(var(@view x.as[2:end]), 1 / 12; atol=5e-3), chains)
@test all(x -> isapprox(mean(@view x.bs[2:end]), 0; atol=5e-2), chains)
@test all(x -> isapprox(var(@view x.bs[2:end]), 1; atol=1e-1), chains)
# Test reproducibility.
Random.seed!(1234)
chains2 = sample(MyModel(), MySampler(), MCMCSerial(), N, 1000; chain_type=MyChain)
@test all(c1.as[i] === c2.as[i] for (c1, c2) in zip(chains, chains2), i in 1:N)
@test all(c1.bs[i] === c2.bs[i] for (c1, c2) in zip(chains, chains2), i in 1:N)
# Unexpected order of arguments.
str = "Number of chains (10) is greater than number of samples per chain (5)"
@test_logs (:warn, str) match_mode = :any sample(
MyModel(), MySampler(), MCMCSerial(), 5, 10; chain_type=MyChain
)
# Suppress output.
logs, _ = collect_test_logs(; min_level=Logging.LogLevel(-1)) do
sample(
MyModel(),
MySampler(),
MCMCSerial(),
10_000,
100;
progress=false,
chain_type=MyChain,
)
end
@test all(l.level > Logging.LogLevel(-1) for l in logs)
# initial parameters
init_params = [(a=rand(), b=randn()) for _ in 1:100]
chains = sample(
MyModel(),
MySampler(),
MCMCSerial(),
3,
100;
progress=false,
init_params=init_params,
)
@test length(chains) == 100
@test all(
chain[1].a == params.a && chain[1].b == params.b for
(chain, params) in zip(chains, init_params)
)
init_params = (b=rand(), a=randn())
chains = sample(
MyModel(),
MySampler(),
MCMCSerial(),
3,
100;
progress=false,
init_params=Iterators.repeated(init_params),
)
@test length(chains) == 100
@test all(
chain[1].a == init_params.a && chain[1].b == init_params.b for chain in chains
)
end
@testset "Ensemble sampling: Reproducibility" begin
N = 1_000
nchains = 10
# Serial sampling
Random.seed!(1234)
chains_serial = sample(
MyModel(),
MySampler(),
MCMCSerial(),
N,
nchains;
progress=false,
chain_type=MyChain,
)
# Multi-threaded sampling
Random.seed!(1234)
chains_threads = sample(
MyModel(),
MySampler(),
MCMCThreads(),
N,
nchains;
progress=false,
chain_type=MyChain,
)
@test all(
c1.as[i] === c2.as[i] for (c1, c2) in zip(chains_serial, chains_threads),
i in 1:N
)
@test all(
c1.bs[i] === c2.bs[i] for (c1, c2) in zip(chains_serial, chains_threads),
i in 1:N
)
# Multi-core sampling
Random.seed!(1234)
chains_distributed = sample(
MyModel(),
MySampler(),
MCMCDistributed(),
N,
nchains;
progress=false,
chain_type=MyChain,
)
@test all(
c1.as[i] === c2.as[i] for (c1, c2) in zip(chains_serial, chains_distributed),
i in 1:N
)
@test all(
c1.bs[i] === c2.bs[i] for (c1, c2) in zip(chains_serial, chains_distributed),
i in 1:N
)
end
@testset "Chain constructors" begin
chain1 = sample(MyModel(), MySampler(), 100; sleepy=true)
chain2 = sample(MyModel(), MySampler(), 100; sleepy=true, chain_type=MyChain)
@test chain1 isa Vector{<:MySample}
@test chain2 isa MyChain
end
@testset "Sample stats" begin
chain = sample(MyModel(), MySampler(), 1000; chain_type=MyChain)
@test chain.stats.stop >= chain.stats.start
@test chain.stats.duration == chain.stats.stop - chain.stats.start
end
@testset "Discard initial samples" begin
# Create a chain and discard initial samples.
Random.seed!(1234)
N = 100
discard_initial = 50
chain = sample(MyModel(), MySampler(), N; discard_initial=discard_initial)
@test length(chain) == N
@test !ismissing(chain[1].a)
# Repeat sampling without discarding initial samples.
# On Julia < 1.6 progress logging changes the global RNG and hence is enabled here.
# https://github.com/TuringLang/AbstractMCMC.jl/pull/102#issuecomment-1142253258
Random.seed!(1234)
ref_chain = sample(
MyModel(), MySampler(), N + discard_initial; progress=VERSION < v"1.6"
)
@test all(chain[i].a === ref_chain[i + discard_initial].a for i in 1:N)
@test all(chain[i].b === ref_chain[i + discard_initial].b for i in 1:N)
end
@testset "Thin chain by a factor of `thinning`" begin
# Run a thinned chain with `N` samples thinned by factor of `thinning`.
Random.seed!(100)
N = 100
thinning = 3
chain = sample(MyModel(), MySampler(), N; thinning=thinning)
@test length(chain) == N
@test ismissing(chain[1].a)
# Repeat sampling without thinning.
# On Julia < 1.6 progress logging changes the global RNG and hence is enabled here.
# https://github.com/TuringLang/AbstractMCMC.jl/pull/102#issuecomment-1142253258
Random.seed!(100)
ref_chain = sample(MyModel(), MySampler(), N * thinning; progress=VERSION < v"1.6")
@test all(chain[i].a === ref_chain[(i - 1) * thinning + 1].a for i in 1:N)
@test all(chain[i].b === ref_chain[(i - 1) * thinning + 1].b for i in 1:N)
end
@testset "Sample without predetermined N" begin
Random.seed!(1234)
chain = sample(MyModel(), MySampler())
bmean = mean(x.b for x in chain)
@test ismissing(chain[1].a)
@test abs(bmean) <= 0.001 || length(chain) == 10_000
# Discard initial samples.
Random.seed!(1234)
discard_initial = 50
chain = sample(MyModel(), MySampler(); discard_initial=discard_initial)
bmean = mean(x.b for x in chain)
@test !ismissing(chain[1].a)
@test abs(bmean) <= 0.001 || length(chain) == 10_000
# On Julia < 1.6 progress logging changes the global RNG and hence is enabled here.
# https://github.com/TuringLang/AbstractMCMC.jl/pull/102#issuecomment-1142253258
Random.seed!(1234)
N = length(chain)
ref_chain = sample(
MyModel(),
MySampler(),
N;
discard_initial=discard_initial,
progress=VERSION < v"1.6",
)
@test all(chain[i].a === ref_chain[i].a for i in 1:N)
@test all(chain[i].b === ref_chain[i].b for i in 1:N)
# Thin chain by a factor of `thinning`.
Random.seed!(1234)
thinning = 3
chain = sample(MyModel(), MySampler(); thinning=thinning)
bmean = mean(x.b for x in chain)
@test ismissing(chain[1].a)
@test abs(bmean) <= 0.001 || length(chain) == 10_000
# On Julia < 1.6 progress logging changes the global RNG and hence is enabled here.
# https://github.com/TuringLang/AbstractMCMC.jl/pull/102#issuecomment-1142253258
Random.seed!(1234)
N = length(chain)
ref_chain = sample(
MyModel(), MySampler(), N; thinning=thinning, progress=VERSION < v"1.6"
)
@test all(chain[i].a === ref_chain[i].a for i in 1:N)
@test all(chain[i].b === ref_chain[i].b for i in 1:N)
end
@testset "Sample vector of `NamedTuple`s" begin
chain = sample(MyModel(), MySampler(), 1_000; chain_type=Vector{NamedTuple})
# Check output type
@test chain isa Vector{<:NamedTuple}
@test length(chain) == 1_000
@test all(keys(x) == (:a, :b) for x in chain)
# Check some statistical properties
@test ismissing(chain[1].a)
@test mean(x.a for x in view(chain, 2:1_000)) ≈ 0.5 atol = 6e-2
@test var(x.a for x in view(chain, 2:1_000)) ≈ 1 / 12 atol = 1e-2
@test mean(x.b for x in chain) ≈ 0 atol = 0.1
@test var(x.b for x in chain) ≈ 1 atol = 0.15
end
@testset "Testing callbacks" begin
function count_iterations(
rng, model, sampler, sample, state, i; iter_array, kwargs...
)
return push!(iter_array, i)
end
N = 100
it_array = Float64[]
sample(MyModel(), MySampler(), N; callback=count_iterations, iter_array=it_array)
@test it_array == collect(1:N)
# sampling without predetermined N
it_array = Float64[]
chain = sample(
MyModel(), MySampler(); callback=count_iterations, iter_array=it_array
)
@test it_array == collect(1:size(chain, 1))
end
end