forked from greta-dev/greta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-adaptive-hmc.R
61 lines (55 loc) · 1.29 KB
/
test-adaptive-hmc.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
set.seed(2025 - 02 - 13)
test_that("bad mcmc proposals are rejected", {
skip_if_not(check_tf_version())
# set up for numerical rejection of initial location
x <- rnorm(10000, 1e60, 1)
z <- normal(-1e60, 1e-60)
distribution(x) <- normal(z, 1e-60)
m <- model(z, precision = "single")
# # catch badness in the progress bar
out <- get_output(
mcmc(m, n_samples = 10, warmup = 0, pb_update = 10)
)
expect_match(out, "100% bad")
expect_snapshot(
error = TRUE,
draws <- mcmc(
m,
chains = 1,
n_samples = 2,
warmup = 0,
verbose = FALSE,
sampler = adaptive_hmc(),
initial_values = initials(z = 1e120)
)
)
# really bad proposals
x <- rnorm(100000, 1e120, 1)
z <- normal(-1e120, 1e-120)
distribution(x) <- normal(z, 1e-120)
m <- model(z, precision = "single")
expect_snapshot(
error = TRUE,
mcmc(m,
chains = 1,
n_samples = 1,
warmup = 0,
sampler = adaptive_hmc(),
verbose = FALSE)
)
# proposals that are fine, but rejected anyway
z <- normal(0, 1)
m <- model(z, precision = "single")
expect_ok(mcmc(
m,
adaptive_hmc(
epsilon = 100,
# Lmin = 1,
# Lmax = 1
),
chains = 1,
n_samples = 5,
warmup = 0,
verbose = FALSE
))
})