Skip to content

Commit

Permalink
Rename and remove dust dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
richfitz committed Oct 15, 2024
1 parent d7adc1f commit be19bbd
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 24 deletions.
3 changes: 0 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ Suggests:
coda,
decor,
knitr,
dust,
mockery,
mvtnorm,
numDeriv,
Expand All @@ -43,5 +42,3 @@ Suggests:
Config/testthat/edition: 3
Language: en-GB
VignetteBuilder: knitr
Remotes:
mrc-ide/dust
11 changes: 5 additions & 6 deletions tests/testthat/helper-monty.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,21 @@ random_array <- function(dim, named = FALSE) {
}


ex_dust_sir_likelihood <- function(n_particles = 100,
deterministic = FALSE,
save_trajectories = FALSE) {
ex_sir_filter_likelihood <- function(n_particles = 100,
deterministic = FALSE,
save_trajectories = FALSE) {
data <- data.frame(time = c( 4, 8, 12, 16, 20, 24, 28, 32, 36),
incidence = c( 1, 0, 3, 5, 2, 4, 3, 7, 2))
sir_filter_monty(data, n_particles, deterministic, save_trajectories)
}


ex_dust_sir <- function(...) {
testthat::skip_if_not_installed("dust")
ex_sir_filter_posterior <- function(...) {
prior <- monty_dsl({
beta ~ Gamma(shape = 1, rate = 1 / 0.5)
gamma ~ Gamma(shape = 1, rate = 1 / 0.5)
})
ex_dust_sir_likelihood(...) + prior
ex_sir_filter_likelihood(...) + prior
}


Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-combine.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ test_that("can combine gradients where parameters do not agree", {


test_that("can combine a stochastic and deterministic model", {
ll <- ex_dust_sir_likelihood()
ll <- ex_sir_filter_likelihood()
prior <- monty_dsl({
beta ~ Gamma(shape = 1, rate = 1 / 0.5)
gamma ~ Gamma(shape = 1, rate = 1 / 0.5)
Expand All @@ -199,7 +199,7 @@ test_that("can combine a stochastic and deterministic model", {


test_that("can't disable stochastic model on combination", {
ll <- ex_dust_sir_likelihood()
ll <- ex_sir_filter_likelihood()
prior <- monty_dsl({
beta ~ Gamma(shape = 1, rate = 1 / 0.5)
gamma ~ Gamma(shape = 1, rate = 1 / 0.5)
Expand All @@ -212,7 +212,7 @@ test_that("can't disable stochastic model on combination", {


test_that("can't create model out of two stochastic halves", {
ll <- ex_dust_sir_likelihood()
ll <- ex_sir_filter_likelihood()
expect_error(
ll + ll,
"Can't combine two stochastic models")
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-sample.R
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ test_that("error if initial conditions do not have finite density", {

test_that("can continue a stochastic model identically", {
set.seed(1)
model <- ex_dust_sir()
model <- ex_sir_filter_posterior()
vcv <- matrix(c(0.0006405, 0.0005628, 0.0005628, 0.0006641), 2, 2)
sampler <- monty_sampler_random_walk(vcv = vcv)
initial <- c(0.2, 0.1)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-sampler-adaptive.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ test_that("can continue adaptive sampler", {

test_that("can't use adaptive sampler with stochastic models", {
set.seed(1)
m <- ex_dust_sir()
m <- ex_sir_filter_posterior()
sampler <- monty_sampler_adaptive(initial_vcv = diag(c(0.01, 0.01)))
expect_error(
monty_sample(m, sampler, 30, n_chains = 3),
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-sampler-hmc.R
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ test_that("can't use hmc with models that lack gradients", {

test_that("can't use hmc with stochastic models", {
set.seed(1)
m <- ex_dust_sir()
m <- ex_sir_filter_posterior()
sampler <- monty_sampler_hmc(epsilon = 0.1, n_integration_steps = 10)
expect_error(
monty_sample(m, sampler, 30, n_chains = 3),
Expand Down
14 changes: 5 additions & 9 deletions tests/testthat/test-sampler-random-walk.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test_that("validate sampler against model on initialisation", {

test_that("can draw samples from a random model", {
set.seed(1)
m <- ex_dust_sir()
m <- ex_sir_filter_posterior()
vcv <- matrix(c(0.0006405, 0.0005628, 0.0005628, 0.0006641), 2, 2)
sampler <- monty_sampler_random_walk(vcv = vcv)
res <- monty_sample(m, sampler, 20)
Expand All @@ -36,7 +36,7 @@ test_that("can draw samples from a random model", {


test_that("can observe a model", {
m <- ex_dust_sir(save_trajectories = TRUE)
m <- ex_sir_filter_posterior(save_trajectories = TRUE)
vcv <- matrix(c(0.0006405, 0.0005628, 0.0005628, 0.0006641), 2, 2)
sampler <- monty_sampler_random_walk(vcv = vcv)

Expand All @@ -46,18 +46,14 @@ test_that("can observe a model", {
expect_setequal(names(res),
c("pars", "density", "initial", "details", "observations"))
expect_equal(names(res$observations),
c("trajectories", "state"))
"trajectories")
expect_equal(dim(res$observations$trajectories),
c(2, 151, 20, 3)) # states, time steps, samples, chains
expect_equal(dim(res$observations$state),
c(5, 20, 3)) # states, samples, chains
expect_equal(res$observations$state[c(2, 4), , ],
res$observations$trajectories[, 151, , ])
c(2, 9, 20, 3)) # states, time steps, samples, chains
})


test_that("can continue observed models", {
m <- ex_dust_sir(save_trajectories = TRUE)
m <- ex_sir_filter_posterior(save_trajectories = TRUE)
vcv <- matrix(c(0.0006405, 0.0005628, 0.0005628, 0.0006641), 2, 2)
sampler <- monty_sampler_random_walk(vcv = vcv)

Expand Down

0 comments on commit be19bbd

Please sign in to comment.