Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop legacy RNG interface #129

Merged
merged 8 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: monty
Title: Monte Carlo Models
Version: 0.3.18
Version: 0.3.19
Authors@R: c(person("Rich", "FitzJohn", role = c("aut", "cre"),
email = "[email protected]"),
person("Wes", "Hinsley", role = "aut"),
Expand Down
4 changes: 0 additions & 4 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,9 @@ export(monty_random_real)
export(monty_random_truncated_normal)
export(monty_random_uniform)
export(monty_random_weibull)
export(monty_rng)
export(monty_rng_create)
export(monty_rng_distributed_pointer)
export(monty_rng_distributed_state)
export(monty_rng_jump)
export(monty_rng_long_jump)
export(monty_rng_pointer)
export(monty_rng_set_state)
export(monty_rng_state)
export(monty_runner_callr)
Expand Down
116 changes: 6 additions & 110 deletions R/cpp11.R

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion R/dsl.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ monty_dsl_parse <- function(x, type = NULL, gradient = NULL, fixed = NULL) {
##' provided!
##'
##' * `sample`: A function to sample from the distribution, given (as
##' a first argument) a rng object (see [monty_rng])
##' a first argument) a `monty_rng` object (see [monty_rng_create])
##'
##' @export
##' @examples
Expand Down
28 changes: 14 additions & 14 deletions R/model.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ monty_model_properties <- function(has_gradient = NULL,
##' `(-Inf, Inf)`.
##'
##' * `direct_sample`: A function to sample directly from the
##' parameter space, given a [monty_rng] object to sample from.
##' In the case where a model returns a posterior (e.g., in Bayesian
##' inference), this is assumed to be sampling from the prior.
##' We'll use this for generating initial conditions for MCMC where
##' those are not given, and possibly other uses. If not given then
##' when using [monty_sample()] the user will have to provide a
##' vector of initial states.
##' parameter space, given a `monty_rng` object to sample from (see
##' [monty_rng_create]). In the case where a model returns a
##' posterior (e.g., in Bayesian inference), this is assumed to be
##' sampling from the prior. We'll use this for generating initial
##' conditions for MCMC where those are not given, and possibly
##' other uses. If not given then when using [monty_sample()] the
##' user will have to provide a vector of initial states.
##'
##' * `gradient`: A function to compute the gradient of `density` with
##' respect to the parameter vector; takes a parameter vector and
Expand All @@ -141,11 +141,11 @@ monty_model_properties <- function(has_gradient = NULL,
##' look after their own stream, and that they may need many
##' streams). Models that provide this method are assumed to be
##' stochastic; however, you can use the `is_stochastic` property
##' (via [monty_model_properties()]) to override this (e.g., to
##' run a stochastic model with its deterministic expectation).
##' This function takes a raw vector of random number state from
##' [monty_rng] and uses it to set the random number state for
##' your model; this is derived from the random number stream for a
##' (via [monty_model_properties()]) to override this (e.g., to run
##' a stochastic model with its deterministic expectation). This
##' function takes a raw vector of random number state from a
##' `monty_rng` and uses it to set the random number state for your
##' model; this is derived from the random number stream for a
##' particular chain, jumped ahead.
##'
##' * `get_rng_state`: A function to get the RNG state; must be
Expand Down Expand Up @@ -304,8 +304,8 @@ monty_model_gradient <- function(model, parameters, named = FALSE) {
##'
##' @inheritParams monty_model_gradient
##'
##' @param rng Random number state, created by [monty_rng]. Use of
##' an RNG with more than one stream may or may not work as
##' @param rng Random number state, created by [monty_rng_create].
##' 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`)
##'
##' @return A vector or matrix of sampled parameters
Expand Down
14 changes: 14 additions & 0 deletions R/random.R
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,20 @@ monty_rng_long_jump <- function(state, n = 1) {
}


## This was used previously, we have a variant of this in dust2, as
## well.
monty_rng_distributed_state <- function(seed = NULL, n_nodes = 1L) {
rng <- monty_rng_create(seed = seed)
ret <- vector("list", n_nodes)
for (i in seq_len(n_nodes)) {
ret[[i]] <- monty_rng_state(rng)
if (i < n_nodes) {
monty_rng_long_jump(rng)
}
}
ret
}


##' @export
print.monty_rng_state <- function(x, ...) {
Expand Down
Loading
Loading