-
Notifications
You must be signed in to change notification settings - Fork 0
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 #66 from mrc-ide/mrc-5640
Add simple function interface
- Loading branch information
Showing
8 changed files
with
181 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
Package: monty | ||
Title: Monte Carlo Models | ||
Version: 0.2.1 | ||
Version: 0.2.2 | ||
Authors@R: c(person("Rich", "FitzJohn", role = c("aut", "cre"), | ||
email = "[email protected]"), | ||
person("Wes", "Hinsley", role = "aut"), | ||
|
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,56 @@ | ||
##' Create a [monty_model] from a function that computes density. | ||
##' This allows use of any R function as a simple monty model. If you | ||
##' need advanced model features, then this interface may not suit you | ||
##' and you may prefer to use [monty_model] directly. | ||
##' | ||
##' This interface will expand in future versions of monty to support | ||
##' gradients, stochastic models, parameter groups and simultaneous | ||
##' calculation of density. | ||
##' | ||
##' @title Create `monty_model` from a function computing density | ||
##' | ||
##' @param density A function to compute log density. It can take any | ||
##' number of parameters | ||
##' | ||
##' @param packer Optionally, a [monty_packer] object to control how | ||
##' your function parameters are packed into a numeric vector. You | ||
##' can typically omit this if all the arguments to your functions | ||
##' are present in your numeric vector and if they are all scalars. | ||
##' | ||
##' @param fixed Optionally, a named list of fixed values to | ||
##' substitute into the call to `density`. This cannot be used in | ||
##' conjunction with `packer` (you should use the `fixed` argument | ||
##' to `monty_packer` instead). | ||
##' | ||
##' @return A [monty_model] object that computes log density with the | ||
##' provided `density` function, given a numeric vector argument | ||
##' representing all parameters. | ||
##' | ||
##' @export | ||
monty_model_function <- function(density, packer = NULL, fixed = NULL) { | ||
if (!is.function(density)) { | ||
cli::cli_abort("Expected 'density' to be a function", arg = "density") | ||
} | ||
|
||
if (!is.null(fixed)) { | ||
assert_named(fixed, unique = TRUE) | ||
assert_list(fixed, call = call) | ||
} | ||
|
||
if (is.null(packer)) { | ||
packer <- monty_packer( | ||
setdiff(names(formals(density)), names(fixed)), | ||
fixed = fixed) | ||
} else { | ||
assert_is(packer, "monty_packer") | ||
if (!is.null(fixed)) { | ||
cli::cli_abort("Can't provide both 'packer' and 'fixed'", arg = "fixed") | ||
} | ||
} | ||
|
||
monty_model( | ||
list(parameters = packer$parameters, | ||
density = function(x) { | ||
rlang::inject(density(!!!packer$unpack(x))) | ||
})) | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
test_that("can create model from function", { | ||
fn <- function(a, b) { | ||
dnorm(0, a, b) | ||
} | ||
m <- monty_model_function(fn) | ||
expect_s3_class(m, "monty_model") | ||
expect_equal(m$parameters, c("a", "b")) | ||
expect_equal(monty_model_density(m, c(1, 2)), | ||
dnorm(0, 1, 2)) | ||
}) | ||
|
||
|
||
test_that("density must be a function", { | ||
expect_error(monty_model_function(NULL), | ||
"Expected 'density' to be a function") | ||
}) | ||
|
||
|
||
test_that("can provide a custom packer", { | ||
p <- monty_packer(c("a", "b"), fixed = list(x = 10)) | ||
fn <- function(a, b, x) { | ||
dnorm(x, b, a) | ||
} | ||
m <- monty_model_function(fn, p) | ||
expect_equal(m$parameters, c("a", "b")) | ||
expect_equal(monty_model_density(m, c(1, 2)), | ||
dnorm(10, 2, 1)) | ||
}) | ||
|
||
|
||
test_that("packer must be a monty_packer if provided", { | ||
fn <- function(a, b) { | ||
dnorm(0, a, b) | ||
} | ||
expect_no_error(monty_model_function(fn, NULL)) | ||
expect_error( | ||
monty_model_function(fn, TRUE), | ||
"Expected 'packer' to be a 'monty_packer' object") | ||
}) | ||
|
||
|
||
test_that("can fix some data", { | ||
p <- monty_packer(c("a", "b")) | ||
fn <- function(a, b, x) { | ||
dnorm(x, b, a) | ||
} | ||
m <- monty_model_function(fn, fixed = list(x = 10)) | ||
expect_equal(m$parameters, c("a", "b")) | ||
expect_equal(monty_model_density(m, c(1, 2)), | ||
dnorm(10, 2, 1)) | ||
expect_error( | ||
monty_model_function(fn, p, fixed = list(x = 10)), | ||
"Can't provide both 'packer' and 'fixed'") | ||
}) |
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