-
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 #70 from mrc-ide/mrc-5765
Add basic examples
- Loading branch information
Showing
41 changed files
with
584 additions
and
89 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.4 | ||
Version: 0.2.5 | ||
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
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
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,99 @@ | ||
##' Load example models from monty. These models exist so that we can | ||
##' create (hopefully) interesting examples in the documentation | ||
##' without them becoming overwhelming. You should probably not use | ||
##' these for anything other than exploring the package. | ||
##' | ||
##' # Supported models | ||
##' | ||
##' ## `bananna` | ||
##' | ||
##' The banana model is a two-dimensional banana-shaped function, | ||
##' picked because it is quite annoying to sample from directly. The | ||
##' model has two parameters `alpha` and `beta` and is based on two | ||
##' successive draws, one conditional on the other. | ||
##' | ||
##' You can vary `sigma` for this model on creation, the default is 0.5 | ||
##' | ||
##' ## Gaussian | ||
##' | ||
##' A multivariate Gaussian centred at the origin. Takes a | ||
##' variance-covariance-matrix as its argument. Parameters are | ||
##' letters a, b, ... up to the number of dimensions. | ||
##' | ||
##' @title Example models | ||
##' | ||
##' @param name Name of the example, as a string. See Details for | ||
##' supported models. | ||
##' | ||
##' @param ... Optional parameters that are passed to create the | ||
##' model. All models can be created with no additional parameters, | ||
##' but you can tweak their behaviour by passing named parameters | ||
##' here. See Details. | ||
##' | ||
##' @return A [monty_model] object | ||
##' @export | ||
##' | ||
##' @examples | ||
##' monty_example("banana") | ||
##' monty_example("gaussian", diag(2)) | ||
monty_example <- function(name, ...) { | ||
examples <- list(banana = monty_example_banana, | ||
gaussian = monty_example_gaussian) | ||
examples[[match_value(name, names(examples))]](...) | ||
} | ||
|
||
|
||
monty_example_banana <- function(sigma = 0.5) { | ||
monty_model( | ||
list( | ||
parameters = c("alpha", "beta"), | ||
direct_sample = function(rng) { | ||
beta <- rng$random_normal(1) | ||
alpha <- rng$normal(1, beta^2, sigma) | ||
if (length(alpha) == 1) { | ||
c(alpha, beta) | ||
} else { | ||
rbind(alpha, beta) | ||
} | ||
}, | ||
density = function(x) { | ||
if (is.matrix(x)) { | ||
alpha <- x[1, ] | ||
beta <- x[2, ] | ||
} else { | ||
alpha <- x[[1]] | ||
beta <- x[[2]] | ||
} | ||
stats::dnorm(beta, log = TRUE) + | ||
stats::dnorm((alpha - beta^2) / sigma, log = TRUE) | ||
}, | ||
gradient = function(x) { | ||
if (is.matrix(x)) { | ||
alpha <- x[1, ] | ||
beta <- x[2, ] | ||
} else { | ||
alpha <- x[[1]] | ||
beta <- x[[2]] | ||
} | ||
da <- (beta^2 - alpha) / sigma^2 | ||
db <- -beta + 2 * beta * (alpha - beta^2) / sigma^2 | ||
if (is.matrix(x)) { | ||
rbind(da, db, deparse.level = 0) | ||
} else { | ||
c(da, db) | ||
} | ||
}, | ||
domain = rbind(c(-Inf, Inf), c(-Inf, Inf))), | ||
properties = monty_model_properties(allow_multiple_parameters = TRUE)) | ||
} | ||
|
||
|
||
monty_example_gaussian <- function(vcv) { | ||
n <- nrow(vcv) | ||
monty_model(list( | ||
parameters = letters[seq_len(n)], | ||
direct_sample = make_rmvnorm(vcv, centred = TRUE), | ||
density = make_ldmvnorm(vcv), | ||
gradient = make_deriv_ldmvnorm(vcv), | ||
domain = cbind(rep(-Inf, n), rep(Inf, n)))) | ||
} |
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
Oops, something went wrong.