Skip to content

Commit

Permalink
Merge pull request #65 from mrc-ide/mrc-5709
Browse files Browse the repository at this point in the history
Package tidyup
  • Loading branch information
richfitz authored Aug 22, 2024
2 parents 3405cf3 + 460c4bf commit a5d6414
Show file tree
Hide file tree
Showing 35 changed files with 294 additions and 198 deletions.
5 changes: 4 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
Package: monty
Title: Monte Carlo Models
Version: 0.2.0
Version: 0.2.1
Authors@R: c(person("Rich", "FitzJohn", role = c("aut", "cre"),
email = "[email protected]"),
person("Wes", "Hinsley", role = "aut"),
person("Ed", "Knock", role = "aut"),
person("Marc", "Baguelin", role = "aut"),
person("Imperial College of Science, Technology and Medicine",
role = "cph"))
Description: Experimental sources for the next generation of mcstate,
Expand Down
16 changes: 8 additions & 8 deletions R/dsl-differentiate-expr.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
##' Differentiate expressions in the monty dsl. This function is
##' Differentiate expressions in the monty DSL. This function is
##' exported for advanced use, and really so that we can use it from
##' odin. But it has the potential to be generally useful, so while
##' we'll tweak the interface quite a lot over the next while it is
##' fine to use if you can handle some disruption.
##'
##' R already has support for differentiating expressions using [D],
##' which is useful for creating derivatives of simple functions to
##' pass into nonlinear optimisation. We need something a bit more
##' flexible for differentiating models in the monty dsl
##' ([monty_dsl]) and also in the related odin dsl.
##' pass into non-linear optimisation. We need something a bit more
##' flexible for differentiating models in the monty DSL
##' ([monty_dsl]) and also in the related odin DSL.
##'
##' # Differences to [D()]
##'
Expand All @@ -33,7 +33,7 @@
##' * `differentiate`: A function that can differentiate an expression
##' with respect to a variable (as a string).
##'
##' * `maths`: Some mathmatical utilities for constructing
##' * `maths`: Some mathematical utilities for constructing
##' expressions. This will be documented later, but the most useful
##' bits on here are the function elements `times`, `plus` and
##' `plus_fold`.
Expand Down Expand Up @@ -147,7 +147,7 @@ derivative <- list(
maths$times(differentiate(a, name), call("exp", a))
},
log1p = function(expr, name) {
## log(1 + x)
## > log(1 + x)
a <- maths$rewrite(expr[[2]])
maths$divide(differentiate(a, name), maths$plus(1, a))
},
Expand Down Expand Up @@ -223,8 +223,8 @@ derivative <- list(
## 'k' the way that the density is defined in R.
##
## We can consider this as:
## lfactorial(n) - lfactorial(k) - lfactorial(n - k)
## ^--a ^--b ^--c
## > lfactorial(n) - lfactorial(k) - lfactorial(n - k)
## > ^--a ^--b ^--c
##
## and then apply the chain rule as:
n <- maths$rewrite(expr[[2]])
Expand Down
2 changes: 1 addition & 1 deletion R/dsl-distributions.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
##' Report information about supported distributions in the dsl. This
##' Report information about supported distributions in the DSL. This
##' is primarily intended for use in packages which use
##' [monty_dsl_parse_distribution], as this function reports
##' information about which distributions and arguments would succeed
Expand Down
2 changes: 1 addition & 1 deletion R/dsl-preprocess.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ preprocess_detect <- function(x, type, call = NULL) {
} else if (length(x) != 1L || grepl("([\n;=]|<-| ~ )", x)) {
## Our heuristic above is a little fragile; it will think that
##
## a~f(1)
## > a~f(1)
##
## is a filename, but we need the spaces there to avoid windows
## short paths getting considered to be code. Parentheses would
Expand Down
2 changes: 1 addition & 1 deletion R/dsl.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ monty_dsl_parse <- function(x, type = NULL, gradient = NULL) {

##' Parse an expression as if it were a call to one of monty's
##' distribution functions (e.g., `Normal`, `Poisson`). This will
##' fill in any defaults, disambiguate where mulitple
##' fill in any defaults, disambiguate where multiple
##' parameterisations of the distribution are available, and provide
##' links through to the C++ API. This function is designed for use
##' from other packages that use monty, and is unlikely to be
Expand Down
8 changes: 4 additions & 4 deletions R/model.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ monty_model_properties <- function(has_gradient = NULL,
##' could be anything really. Models can return `-Inf` if things
##' are impossible, and we'll try and cope gracefully with that
##' wherever possible. If the property `allow_multiple_parameters`
##' is `TRUE`, then this function must be able to handle the
##' argument parameter being a matrix, and return a vector
##' is `TRUE`, then this function must be able to handle the
##' argument parameter being a matrix, and return a vector
##' of densities.
##'
##' * `parameters`: A character vector of parameter names. This
Expand Down Expand Up @@ -138,7 +138,7 @@ monty_model_properties <- function(has_gradient = NULL,
##' (especially if we explore more complex nestings) but at present
##' parameters with group 0 affect everything (so are accepted or
##' rejected as a whole), while parameters in groups 1 to `n` are
##' indepenent (for example, changing the parameters in group 2 does
##' independent (for example, changing the parameters in group 2 does
##' not affect the density of parameters proposed in group 3).
##'
##' @title Create basic model
Expand Down Expand Up @@ -265,7 +265,7 @@ monty_model_gradient <- function(model, parameters, named = FALSE) {
##'
##' @param rng Random number state, created by [monty_rng]. Use of
##' an RNG with more than one stream may or may not work as
##' expected; this is something we need to tidy up (mrc-5292)
##' expected; this is something we need to tidy up (`mrc-5292`)
##'
##' @return A vector or matrix of sampled parameters
##'
Expand Down
4 changes: 2 additions & 2 deletions R/packer.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
##' sort of output. Assume you have a matrix `m` with 3 rows and 2
##' columns; this means that we have two sets of parameters or state
##' (one per column) and 3 states within each; this is the format that
##' mcmc parameters will be in for example.
##' MCMC parameters will be in for example.
##'
##' The first would to be return a list where the `i`th element is the
##' result of unpacking the `i`th parameter/state vector. You can do
Expand All @@ -112,7 +112,7 @@
##' matrices (one column per set) and so on.
##'
##' This approach generalises to higher dimensional input, though we
##' suspect you'll spend a bit of time headscratching if you use it.
##' suspect you'll spend a bit of time head-scratching if you use it.
##'
##' We do not currently offer the ability to pack this sort of output
##' back up, though it's not hard. Please let us know if you would
Expand Down
8 changes: 4 additions & 4 deletions R/runner-simultaneous.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ monty_runner_simultaneous <- function(progress = NULL) {
rng_state <- lapply(rng, function(r) r$state())
## TODO: get the rng state back into 'rng' here, or (better) look
## at if we should just be using seed instead here perhaps?
## rng_state <- matrix(res$internal$state$rng, ncol = n_chains)
## for (i in seq_len(n_chains)) {
## rng[[i]]$set_state(rng_state[, i]) # not supported!
## }
## > rng_state <- matrix(res$internal$state$rng, ncol = n_chains)
## > for (i in seq_len(n_chains)) {
## > rng[[i]]$set_state(rng_state[, i]) # not supported!
## > }
monty_run_chains_simultaneous(pars, model, sampler, observer,
n_steps, progress, rng_state)
}
Expand Down
2 changes: 1 addition & 1 deletion R/runner.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ monty_runner_serial <- function(progress = NULL) {
##' Run MCMC chains in parallel (at the same time). This runner uses
##' the `parallel` package to distribute your chains over a number of
##' worker processes on the same machine. Compared with the "worker"
##' support in mcstate version 1 this is very simple and we'll improve
##' support in `mcstate` version 1 this is very simple and we'll improve
##' it over time. In particular we do not report back any information
##' about progress while a chain is running on a worker or even across
##' chains. There's also no support to warn you if your number of
Expand Down
2 changes: 1 addition & 1 deletion R/sampler-adaptive.R
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ monty_sampler_adaptive <- function(initial_vcv,
calc_scaling_increment <- function(n_pars, acceptance_target,
log_scaling_update) {
if (log_scaling_update) {
A <- -stats::qnorm(acceptance_target / 2)
A <- -stats::qnorm(acceptance_target / 2) # nolint

scaling_increment <-
(1 - 1 / n_pars) * (sqrt(2 * pi) * exp(A^2 / 2)) / (2 * A) +
Expand Down
Loading

0 comments on commit a5d6414

Please sign in to comment.