-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow passing only position component for initial state (#39)
* Allow passing only position component for initial state * Make sample_chain robust to zero iterations * Add additional sample_chains test cases
- Loading branch information
1 parent
930dbd6
commit 330c39b
Showing
4 changed files
with
100 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,70 @@ | ||
for (n_warm_up_iteration in c(1, 100)) { | ||
for (n_main_iteration in c(1, 100)) { | ||
for (trace_warm_up in c(TRUE, FALSE)) { | ||
test_that( | ||
sprintf( | ||
paste0( | ||
"Sampling chain with %i warm-up iterations, %i main iterations, ", | ||
"and trace_warm_up = %i works" | ||
), | ||
n_warm_up_iteration, n_main_iteration, trace_warm_up | ||
), | ||
{ | ||
dimension <- 3 | ||
target_distribution <- standard_normal_target_distribution() | ||
barker_proposal(target_distribution) | ||
proposal <- barker_proposal(target_distribution) | ||
adapters <- list( | ||
scale_adapter(proposal, initial_scale = 1.) | ||
) | ||
withr::with_seed(default_seed(), { | ||
position <- rnorm(dimension) | ||
}) | ||
initial_state <- chain_state(position) | ||
results <- sample_chain( | ||
target_distribution = target_distribution, | ||
proposal = proposal, | ||
initial_state = initial_state, | ||
n_warm_up_iteration = n_warm_up_iteration, | ||
n_main_iteration = n_main_iteration, | ||
adapters = adapters, | ||
trace_warm_up = trace_warm_up | ||
) | ||
expected_results_names <- c("final_state", "traces", "statistics") | ||
if (trace_warm_up) { | ||
expected_results_names <- c( | ||
expected_results_names, "warm_up_traces", "warm_up_statistics" | ||
) | ||
} | ||
expect_named( | ||
results, | ||
expected_results_names, | ||
ignore.order = TRUE, | ||
for (n_warm_up_iteration in c(0, 1, 10)) { | ||
for (n_main_iteration in c(0, 1, 10)) { | ||
for (dimension in c(1, 2)) { | ||
for (trace_warm_up in c(TRUE, FALSE)) { | ||
for (wrapped_initial_state in c(TRUE, FALSE)) { | ||
test_that( | ||
sprintf( | ||
paste0( | ||
"Sampling chain with %i warm-up iterations, %i main iterations", | ||
" dimension %i, wrapped_initial_state = %i ", | ||
"and trace_warm_up = %i works" | ||
), | ||
n_warm_up_iteration, | ||
n_main_iteration, | ||
dimension, | ||
wrapped_initial_state, | ||
trace_warm_up | ||
), | ||
{ | ||
target_distribution <- standard_normal_target_distribution() | ||
barker_proposal(target_distribution) | ||
proposal <- barker_proposal(target_distribution) | ||
adapters <- list( | ||
scale_adapter(proposal, initial_scale = 1.) | ||
) | ||
withr::with_seed(default_seed(), { | ||
position <- rnorm(dimension) | ||
}) | ||
if (wrapped_initial_state) { | ||
initial_state <- chain_state(position) | ||
} else { | ||
initial_state <- position | ||
} | ||
results <- sample_chain( | ||
target_distribution = target_distribution, | ||
proposal = proposal, | ||
initial_state = initial_state, | ||
n_warm_up_iteration = n_warm_up_iteration, | ||
n_main_iteration = n_main_iteration, | ||
adapters = adapters, | ||
trace_warm_up = trace_warm_up | ||
) | ||
expected_results_names <- c("final_state", "traces", "statistics") | ||
if (trace_warm_up) { | ||
expected_results_names <- c( | ||
expected_results_names, "warm_up_traces", "warm_up_statistics" | ||
) | ||
} | ||
expect_named( | ||
results, | ||
expected_results_names, | ||
ignore.order = TRUE, | ||
) | ||
expect_nrow(results$traces, n_main_iteration) | ||
expect_ncol(results$traces, dimension + 1) | ||
expect_nrow(results$statistics, n_main_iteration) | ||
expect_ncol(results$statistics, 1) | ||
if (trace_warm_up) { | ||
expect_nrow(results$warm_up_traces, n_warm_up_iteration) | ||
expect_ncol(results$warm_up_traces, dimension + 1) | ||
expect_nrow(results$warm_up_statistics, n_warm_up_iteration) | ||
expect_ncol(results$warm_up_statistics, 2) | ||
} | ||
} | ||
) | ||
} | ||
) | ||
} | ||
} | ||
} | ||
} |