-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #295 from mrc-ide/mrc-4318
Support for differentiation of parameters in DSL
- Loading branch information
Showing
4 changed files
with
124 additions
and
17 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
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
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
test_that("Can parse with differentiable parameters", { | ||
ir <- odin_parse({ | ||
initial(x) <- 1 | ||
update(x) <- rnorm(0, 0.1) | ||
d <- data() | ||
compare(d) ~ normal(0, scale) | ||
scale <- user(differentiate = TRUE) | ||
}) | ||
|
||
d <- ir_deserialise(ir) | ||
expect_true(d$features$has_derivative) | ||
}) | ||
|
||
|
||
test_that("can't differentiate integer parameters", { | ||
expect_error(odin_parse({ | ||
initial(x) <- 1 | ||
update(x) <- rnorm(0, 0.1) | ||
d <- data() | ||
compare(d) ~ normal(x, scale) | ||
scale <- user(differentiate = TRUE, integer = TRUE) | ||
}), | ||
"Can't differentiate integer parameters\\s+scale <-") | ||
}) | ||
|
||
|
||
test_that("can't differentiate without compare", { | ||
expect_error( | ||
odin_parse({ | ||
initial(x) <- 1 | ||
update(x) <- rnorm(x, scale) | ||
scale <- user(differentiate = TRUE) | ||
}), | ||
"You need a compare expression to differentiate!\\s+scale <-") | ||
}) | ||
|
||
|
||
test_that("can't differentiate continuous time models", { | ||
expect_error( | ||
odin_parse({ | ||
initial(x) <- 1 | ||
deriv(x) <- 1 | ||
d <- data() | ||
compare(d) ~ normal(x, scale) | ||
scale <- user(differentiate = TRUE) | ||
}), | ||
"Can't use differentiate with continuous time models\\s+scale <-") | ||
}) | ||
|
||
|
||
test_that("can't differentiate models with arrays", { | ||
err <- expect_error( | ||
odin_parse({ | ||
initial(x[]) <- 1 | ||
update(x[]) <- rnorm(x, 1) | ||
dim(x) <- 5 | ||
d <- data() | ||
compare(d) ~ normal(sum(x), scale) | ||
scale <- user(differentiate = TRUE) | ||
}), | ||
"Can't use differentiate with models that use arrays") | ||
expect_match(err$message, "dim(x) <-", fixed = TRUE) | ||
expect_match(err$message, "scale <-", fixed = TRUE) | ||
}) |