the MCMC.
Our implementation of an adaptive MCMC algorithm is based on an
adaptation of the "accelerated shaping" algorithm in Spencer (2021).
-The algorithm is based on a random-walk Metropolis-Hasting algorithm where
-the proposal is a multi-variate Normal distribution centered on the current
+The algorithm is based on a random-walk Metropolis-Hastings algorithm where
+the proposal is a multi-variate Normal distribution centred on the current
point.
Spencer SEF (2021) Accelerating adaptation in the adaptive
Metropolis–Hastings random walk algorithm. Australian & New Zealand Journal
diff --git a/search.json b/search.json
index 10a280c1..15b41eda 100644
--- a/search.json
+++ b/search.json
@@ -1 +1 @@
-[{"path":"https://mrc-ide.github.io/orderly2/articles/mcstate2.html","id":"the-basic-idea","dir":"Articles","previous_headings":"","what":"The basic idea","title":"Introduction to mcstate2","text":"Draw samples model using Markov Chain Monte Carlo methods. need: model: mcstate_model() object minimally knows names parameter vector (unstructured real-valued vector) can compute log probability density. may also able compute gradient log density, sample directly parameter space (e.g., represents prior distribution). sampler: method drawing samples model’s distribution sequence. define several different sampler types, simplest one mcstate_sampler_random_walk(), implements simple Metropolis algorithm random walk. runner: controls chains run (e.g., one another parallel). system designed composable; can work Bayesian way defining model representing likelihood another model representing prior pick sampler based capabilities model, pick runner based capabilities computer. mcstate_model() interface designed flexible user-friendly. expect write higher-level interface help work , describe write wrappers models implemented packages (might write model dust odin adaptor make easy work tools provided mcstate2 start making inferences model).","code":""},{"path":"https://mrc-ide.github.io/orderly2/articles/mcstate2.html","id":"an-example","dir":"Articles","previous_headings":"","what":"An example","title":"Introduction to mcstate2","text":"starting example, ’s worth noting far better tools model sort thing (stan, bugs, jags, R - really anything). aim section derive simple model may feel familiar. strength package performing inference custom models can’t expressed high level interfaces. simple likelihood, following model formulation “Statistical Rethinking” chapter 3; height modelled normally distributed departures linear relationship weight. prior ’ll make much nicer work future, construct density hand sum normally distributed priors b, weak uniform prior sigma. provide direct_sample function can draw samples prior distribution directly. posterior distribution combination two models (indicated + ’re adding log-scale, using prior posterior; can use mcstate_model_combine() prefer). Constructing sensible initial variance-covariance matrix bit trick, using adaptive sampler reduces pain . values chosen reasonable starting points. Now run sampler. ’ve started good starting point make simple sampler converge quickly: don’t yet tools working samples objects, can see density time easily enough: plots estimated parameters:","code":"head(data) #> height weight #> 1 162.5401 45.92805 #> 2 159.9566 51.19368 #> 3 156.1808 44.56841 #> 4 168.4164 60.36933 #> 5 158.6978 52.14180 #> 6 154.7666 44.66696 plot(height ~ weight, data) likelihood <- mcstate_model( list( parameters = c(\"a\", \"b\", \"sigma\"), density = function(x) { a <- x[[1]] b <- x[[2]] sigma <- x[[3]] mu <- a + b * data$weight sum(dnorm(data$height, mu, sigma, log = TRUE)) })) prior <- local({ a_mu <- 178 a_sd <- 100 b_mu <- 0 b_sd <- 10 sigma_min <- 0 sigma_max <- 50 mcstate_model( list( parameters = c(\"a\", \"b\", \"sigma\"), density = function(x) { a <- x[[1]] b <- x[[2]] sigma <- x[[3]] dnorm(a, a_mu, a_sd, log = TRUE) + dnorm(b, b_mu, b_sd, log = TRUE) + dunif(sigma, sigma_min, sigma_max, log = TRUE) }, direct_sample = function(rng) { c(rng$normal(1, a_mu, a_sd), rng$normal(1, b_mu, b_sd), rng$uniform(1, sigma_min, sigma_max)) }, domain = rbind(c(-Inf, Inf), c(-Inf, Inf), c(0, Inf)) )) }) posterior <- likelihood + prior vcv <- rbind(c(4.5, -0.088, 0.076), c(-0.088, 0.0018, -0.0015), c(0.076, -0.0015, 0.0640)) sampler <- mcstate_sampler_random_walk(vcv = vcv) samples <- mcstate_sample(posterior, sampler, 5000, initial = c(114, 0.9, 3), n_chains = 4) matplot(t(samples$density), type = \"l\", lty = 1, xlab = \"log posterior density\", ylab = \"sample\", col = \"#00000055\") par(mfrow = c(1, 3)) plot(density(samples$pars[\"a\", , ]), main = \"a\") abline(v = 114, col = \"red\") plot(density(samples$pars[\"b\", , ]), main = \"b\") abline(v = 0.9, col = \"red\") plot(density(samples$pars[\"sigma\", , ]), main = \"sigma\") abline(v = 3, col = \"red\")"},{"path":"https://mrc-ide.github.io/orderly2/authors.html","id":null,"dir":"","previous_headings":"","what":"Authors","title":"Authors and Citation","text":"Rich FitzJohn. Author, maintainer. Imperial College Science, Technology Medicine. Copyright holder.","code":""},{"path":"https://mrc-ide.github.io/orderly2/authors.html","id":"citation","dir":"","previous_headings":"","what":"Citation","title":"Authors and Citation","text":"FitzJohn R (2024). mcstate2: Next Generation mcstate. R package version 0.1.0, https://github.com/mrc-ide/mcstate2, https://mrc-ide.github.io/mcstate2.","code":"@Manual{, title = {mcstate2: Next Generation mcstate}, author = {Rich FitzJohn}, year = {2024}, note = {R package version 0.1.0, https://github.com/mrc-ide/mcstate2}, url = {https://mrc-ide.github.io/mcstate2}, }"},{"path":[]},{"path":"https://mrc-ide.github.io/orderly2/index.html","id":"installation","dir":"","previous_headings":"","what":"Installation","title":"Next Generation mcstate","text":"install mcstate2:","code":"remotes::install_github(\"mrc-ide/mcstate2\", upgrade = FALSE)"},{"path":"https://mrc-ide.github.io/orderly2/index.html","id":"license","dir":"","previous_headings":"","what":"License","title":"Next Generation mcstate","text":"MIT © Imperial College Science, Technology Medicine","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_model.html","id":null,"dir":"Reference","previous_headings":"","what":"Create basic model — mcstate_model","title":"Create basic model — mcstate_model","text":"Create basic mcstate model. takes user-supplied object minimally can compute probability density (via density function) information parameters; can sample model using MCMC using mcstate_sample. imagine many users call function directly, glue used packages.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_model.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Create basic model — mcstate_model","text":"","code":"mcstate_model(model, properties = NULL)"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_model.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Create basic model — mcstate_model","text":"model list environment elements described Details. properties Optionally, mcstate_model_properties object, used enforce clarify properties model.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_model.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Create basic model — mcstate_model","text":"object class mcstate_model. elements: model: model provided parameters: parameter name vector domain: parameter domain matrix, named parameters direct_sample: direct_sample function, provided model gradient: gradient function, provided model properties: list properties model; see mcstate_model_properties(). Currently contains: has_gradient: model can compute gradient has_direct_sample: model can sample parameters space is_stochastic: model behave stochastically","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_model.html","id":"details","dir":"Reference","previous_headings":"","what":"Details","title":"Create basic model — mcstate_model","text":"model argument can list environment (something indexable $) elements: density: function compute probability density. must take argument representing parameter vector (numeric vector) return single value. posterior probability density Bayesian inference, anything really. Models can return -Inf things impossible, try cope gracefully wherever possible. parameters: character vector parameter names. vector source truth length parameter vector. domain: Information parameter domain. two column matrix length(parameters) rows representing parameter. parameter minimum maximum bounds given first second column. Infinite values (-Inf Inf) used parameter infinite domain . Currently used translate bounded unbounded space HMC, might also use reflecting proposals MCMC . present assume model valid everywhere (.e., parameters valid -Inf Inf. direct_sample: function sample directly parameter space, given mcstate_rng object sample . case model returns posterior (e.g., Bayesian inference), assumed sampling prior. use generating initial conditions MCMC given, possibly uses. given using mcstate_sample() user provide vector initial states. gradient: function compute gradient density respect parameter vector; takes parameter vector returns vector length. efficiency, model may want stateful gradients can efficiently calculated density calculation, density gradient, called parameters. function optional (may well defined possible define). set_rng_state: function set state (contrast rng passed direct_sample sampler's rng stream, assume models look stream, may need many streams). Models provide method assumed stochastic; however, can use is_stochastic property (via mcstate_model_properties()) override (e.g., run stochastic model deterministic expectation). function takes mcstate_rng object uses seed random number state model. two options (1) hold copy provided object draw samples needed (effect sharing random number stream sampler) create new rng stream jump stream (provide utility present mcstate_rng$new(rng$state(), n_streams)$jump()$state() ). main reason need multiple (perhaps parallel) streams random numbers model. $jump() important, otherwise end correlated draws sampler.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_model_combine.html","id":null,"dir":"Reference","previous_headings":"","what":"Combine two models — mcstate_model_combine","title":"Combine two models — mcstate_model_combine","text":"Combine two models multiplication. need better name . Bayesian inference want create model represents multiplication likelihood prior (log space) convenient think models separately. Multiplying probabilities (adding log scale) common enough may situations want .","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_model_combine.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Combine two models — mcstate_model_combine","text":"","code":"mcstate_model_combine(a, b, properties = NULL, name_a = \"a\", name_b = \"b\")"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_model_combine.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Combine two models — mcstate_model_combine","text":"first model b second model properties mcstate_model_properties object, used control (enforce) properties combined model. name_a Name first model (defaulting ''); can use make error messages nicer read, practical effect. name_b Name first model (defaulting 'b'); can use make error messages nicer read, practical effect.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_model_combine.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Combine two models — mcstate_model_combine","text":"mcstate_model object","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_model_combine.html","id":"details","dir":"Reference","previous_headings":"","what":"Details","title":"Combine two models — mcstate_model_combine","text":"describe impact combining pair models density: sum log densities model parameters: union parameters model taken domain: restrictive domain taken parameter. Parameters appear one model assumed infinite domain . gradient: models define gradient, sum gradients. either define gradient, resulting model gradient support. Set has_gradient = TRUE within `properties want enforce combination differentiable. models disagree parameters, parameters missing model assumed (reasonably) zero gradient. direct_sample: one hard right thing . neither model can directly sampled fine, directly sample. one model can sampled can sample union parameters take function (case prior model combined likelihood). cases errors, can avoided setting has_direct_gradient = FALSE properties. properties model combined , reflecting properties joint model. model field ordered, unnamed, list containing two elements corresponding first second model (mcstate_model, underlying model, perhaps?). part makes distinction two models ; components equivalent.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_model_properties.html","id":null,"dir":"Reference","previous_headings":"","what":"Describe model properties — mcstate_model_properties","title":"Describe model properties — mcstate_model_properties","text":"Describe properties model. Use function optional, can pass return value properties argument mcstate_model enforce model actually properties.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_model_properties.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Describe model properties — mcstate_model_properties","text":"","code":"mcstate_model_properties( has_gradient = NULL, has_direct_sample = NULL, is_stochastic = NULL )"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_model_properties.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Describe model properties — mcstate_model_properties","text":"has_gradient Logical, indicating model gradient method. Use NULL (default) detect model. has_direct_sample Logical, indicating model direct_sample method. Use NULL (default) detect model. is_stochastic Logical, indicating model stochastic. Stochastic models must supply set_rng_state method might support get_rng_state method later.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_model_properties.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Describe model properties — mcstate_model_properties","text":"list class mcstate_model_properties modified.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":null,"dir":"Reference","previous_headings":"","what":"Mcstate Random Number Generator — mcstate_rng","title":"Mcstate Random Number Generator — mcstate_rng","text":"Create object can used generate random numbers RNG mcstate uses internally. primarily meant debugging testing underlying C++ rather source random numbers R.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Mcstate Random Number Generator — mcstate_rng","text":"mcstate_rng object, can used drawn random numbers mcstate's distributions.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"running-multiple-streams-perhaps-in-parallel","dir":"Reference","previous_headings":"","what":"Running multiple streams, perhaps in parallel","title":"Mcstate Random Number Generator — mcstate_rng","text":"underlying random number generators designed work parallel, random access parameters (see vignette(\"rng\") details). However, usually done within context running model particle sees stream numbers. provide support running random number generators parallel, speed gains parallelisation likely somewhat eroded overhead copying around large number random numbers. random distribution functions support argument n_threads controls number threads used. argument silently effect installation support OpenMP. Parallelisation performed level stream, draw n numbers stream total n * n_streams random numbers using n_threads threads . Setting n_threads higher n_streams therefore effect. running somebody else's system (e.g., HPC, CRAN) must respect various environment variables control maximum allowable number threads. exception random_real, random number distribution accepts parameters; interpretations depend n, n_streams rank. scalar use parameter value every draw every stream vector length n draw n random numbers per stream, every stream use parameter value every stream draw (different, shared, parameter value subsequent draws). matrix provided one row n_streams columns use different parameters stream, parameter draw. matrix provided n rows n_streams columns use parameter value [, j] ith draw jth stream. rules slightly different prob argument multinomial prob vector values. shift dimensions one: vector use prob every draw every stream length(prob) possible outcomes. matrix n columns vary draw (ith draw using vector prob[, ] shared across streams. nrow(prob) possible outcomes. 3d array provided 1 column n_streams \"layers\" (third dimension) use use different parameters stream, parameter draw. 3d array provided n columns n_streams \"layers\" vary draws streams use vector prob[, , j] ith draw jth stream. output differ based number threads used, number streams.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"public-fields","dir":"Reference","previous_headings":"","what":"Public fields","title":"Mcstate Random Number Generator — mcstate_rng","text":"info Information generator (read-)","code":""},{"path":[]},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"public-methods","dir":"Reference","previous_headings":"","what":"Public methods","title":"Mcstate Random Number Generator — mcstate_rng","text":"mcstate_rng$new() mcstate_rng$size() mcstate_rng$jump() mcstate_rng$long_jump() mcstate_rng$random_real() mcstate_rng$random_normal() mcstate_rng$uniform() mcstate_rng$normal() mcstate_rng$binomial() mcstate_rng$nbinomial() mcstate_rng$hypergeometric() mcstate_rng$gamma() mcstate_rng$poisson() mcstate_rng$exponential() mcstate_rng$cauchy() mcstate_rng$multinomial() mcstate_rng$state()","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"method-new-","dir":"Reference","previous_headings":"","what":"Method new()","title":"Mcstate Random Number Generator — mcstate_rng","text":"Create mcstate_rng object","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"mcstate_rng$new( seed = NULL, n_streams = 1L, real_type = \"double\", deterministic = FALSE )"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Mcstate Random Number Generator — mcstate_rng","text":"seed seed, integer, raw vector NULL. integer create suitable seed via \"splitmix64\" algorithm, raw vector must correct length (multiple either 32 16 float = FALSE float = TRUE respectively). NULL create seed using R's random number generator. n_streams number streams use (see Details) real_type type floating point number use. Currently float double supported (double default). (negligible) impact speed, exists test low-precision generators. deterministic Logical, indicating use \"deterministic\" mode distributions return expectations state never changed.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"method-size-","dir":"Reference","previous_headings":"","what":"Method size()","title":"Mcstate Random Number Generator — mcstate_rng","text":"Number streams available","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"usage-1","dir":"Reference","previous_headings":"","what":"Usage","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"mcstate_rng$size()"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"method-jump-","dir":"Reference","previous_headings":"","what":"Method jump()","title":"Mcstate Random Number Generator — mcstate_rng","text":"jump function updates random number state stream advancing state equivalent 2^128 numbers drawn stream.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"usage-2","dir":"Reference","previous_headings":"","what":"Usage","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"mcstate_rng$jump()"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"method-long-jump-","dir":"Reference","previous_headings":"","what":"Method long_jump()","title":"Mcstate Random Number Generator — mcstate_rng","text":"Longer $jump, $long_jump method equivalent 2^192 numbers drawn stream.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"usage-3","dir":"Reference","previous_headings":"","what":"Usage","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"mcstate_rng$long_jump()"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"method-random-real-","dir":"Reference","previous_headings":"","what":"Method random_real()","title":"Mcstate Random Number Generator — mcstate_rng","text":"Generate n numbers standard uniform distribution","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"usage-4","dir":"Reference","previous_headings":"","what":"Usage","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"mcstate_rng$random_real(n, n_threads = 1L)"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"arguments-1","dir":"Reference","previous_headings":"","what":"Arguments","title":"Mcstate Random Number Generator — mcstate_rng","text":"n Number samples draw (per stream) n_threads Number threads use; see Details","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"method-random-normal-","dir":"Reference","previous_headings":"","what":"Method random_normal()","title":"Mcstate Random Number Generator — mcstate_rng","text":"Generate n numbers standard normal distribution","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"usage-5","dir":"Reference","previous_headings":"","what":"Usage","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"mcstate_rng$random_normal(n, n_threads = 1L, algorithm = \"box_muller\")"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"arguments-2","dir":"Reference","previous_headings":"","what":"Arguments","title":"Mcstate Random Number Generator — mcstate_rng","text":"n Number samples draw (per stream) n_threads Number threads use; see Details algorithm Name algorithm use; currently box_muller ziggurat supported, latter considerably faster.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"method-uniform-","dir":"Reference","previous_headings":"","what":"Method uniform()","title":"Mcstate Random Number Generator — mcstate_rng","text":"Generate n numbers uniform distribution","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"usage-6","dir":"Reference","previous_headings":"","what":"Usage","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"mcstate_rng$uniform(n, min, max, n_threads = 1L)"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"arguments-3","dir":"Reference","previous_headings":"","what":"Arguments","title":"Mcstate Random Number Generator — mcstate_rng","text":"n Number samples draw (per stream) min minimum distribution (length 1 n) max maximum distribution (length 1 n) n_threads Number threads use; see Details","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"method-normal-","dir":"Reference","previous_headings":"","what":"Method normal()","title":"Mcstate Random Number Generator — mcstate_rng","text":"Generate n numbers normal distribution","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"usage-7","dir":"Reference","previous_headings":"","what":"Usage","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"mcstate_rng$normal(n, mean, sd, n_threads = 1L, algorithm = \"box_muller\")"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"arguments-4","dir":"Reference","previous_headings":"","what":"Arguments","title":"Mcstate Random Number Generator — mcstate_rng","text":"n Number samples draw (per stream) mean mean distribution (length 1 n) sd standard deviation distribution (length 1 n) n_threads Number threads use; see Details algorithm Name algorithm use; currently box_muller ziggurat supported, latter considerably faster.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"method-binomial-","dir":"Reference","previous_headings":"","what":"Method binomial()","title":"Mcstate Random Number Generator — mcstate_rng","text":"Generate n numbers binomial distribution","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"usage-8","dir":"Reference","previous_headings":"","what":"Usage","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"mcstate_rng$binomial(n, size, prob, n_threads = 1L)"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"arguments-5","dir":"Reference","previous_headings":"","what":"Arguments","title":"Mcstate Random Number Generator — mcstate_rng","text":"n Number samples draw (per stream) size number trials (zero , length 1 n) prob probability success trial (0 1, length 1 n) n_threads Number threads use; see Details","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"method-nbinomial-","dir":"Reference","previous_headings":"","what":"Method nbinomial()","title":"Mcstate Random Number Generator — mcstate_rng","text":"Generate n numbers negative binomial distribution","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"usage-9","dir":"Reference","previous_headings":"","what":"Usage","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"mcstate_rng$nbinomial(n, size, prob, n_threads = 1L)"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"arguments-6","dir":"Reference","previous_headings":"","what":"Arguments","title":"Mcstate Random Number Generator — mcstate_rng","text":"n Number samples draw (per stream) size target number successful trials (zero , length 1 n) prob probability success trial (0 1, length 1 n) n_threads Number threads use; see Details","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"method-hypergeometric-","dir":"Reference","previous_headings":"","what":"Method hypergeometric()","title":"Mcstate Random Number Generator — mcstate_rng","text":"Generate n numbers hypergeometric distribution","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"usage-10","dir":"Reference","previous_headings":"","what":"Usage","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"mcstate_rng$hypergeometric(n, n1, n2, k, n_threads = 1L)"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"arguments-7","dir":"Reference","previous_headings":"","what":"Arguments","title":"Mcstate Random Number Generator — mcstate_rng","text":"n Number samples draw (per stream) n1 number white balls urn (called n R's rhyper) n2 number black balls urn (called m R's rhyper) k number balls draw n_threads Number threads use; see Details","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"method-gamma-","dir":"Reference","previous_headings":"","what":"Method gamma()","title":"Mcstate Random Number Generator — mcstate_rng","text":"Generate n numbers gamma distribution","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"usage-11","dir":"Reference","previous_headings":"","what":"Usage","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"mcstate_rng$gamma(n, shape, scale, n_threads = 1L)"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"arguments-8","dir":"Reference","previous_headings":"","what":"Arguments","title":"Mcstate Random Number Generator — mcstate_rng","text":"n Number samples draw (per stream) shape Shape scale Scale ' n_threads Number threads use; see Details","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"method-poisson-","dir":"Reference","previous_headings":"","what":"Method poisson()","title":"Mcstate Random Number Generator — mcstate_rng","text":"Generate n numbers Poisson distribution","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"usage-12","dir":"Reference","previous_headings":"","what":"Usage","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"mcstate_rng$poisson(n, lambda, n_threads = 1L)"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"arguments-9","dir":"Reference","previous_headings":"","what":"Arguments","title":"Mcstate Random Number Generator — mcstate_rng","text":"n Number samples draw (per stream) lambda mean (zero , length 1 n). valid lambda <= 10^7 n_threads Number threads use; see Details","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"method-exponential-","dir":"Reference","previous_headings":"","what":"Method exponential()","title":"Mcstate Random Number Generator — mcstate_rng","text":"Generate n numbers exponential distribution","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"usage-13","dir":"Reference","previous_headings":"","what":"Usage","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"mcstate_rng$exponential(n, rate, n_threads = 1L)"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"arguments-10","dir":"Reference","previous_headings":"","what":"Arguments","title":"Mcstate Random Number Generator — mcstate_rng","text":"n Number samples draw (per stream) rate rate exponential n_threads Number threads use; see Details","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"method-cauchy-","dir":"Reference","previous_headings":"","what":"Method cauchy()","title":"Mcstate Random Number Generator — mcstate_rng","text":"Generate n draws Cauchy distribution.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"usage-14","dir":"Reference","previous_headings":"","what":"Usage","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"mcstate_rng$cauchy(n, location, scale, n_threads = 1L)"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"arguments-11","dir":"Reference","previous_headings":"","what":"Arguments","title":"Mcstate Random Number Generator — mcstate_rng","text":"n Number samples draw (per stream) location location peak distribution (also median) scale scale parameter, specifies distribution's \"half-width half-maximum\" n_threads Number threads use; see Details","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"method-multinomial-","dir":"Reference","previous_headings":"","what":"Method multinomial()","title":"Mcstate Random Number Generator — mcstate_rng","text":"Generate n draws multinomial distribution. contrast distributions , draw vector length prob.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"usage-15","dir":"Reference","previous_headings":"","what":"Usage","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"mcstate_rng$multinomial(n, size, prob, n_threads = 1L)"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"arguments-12","dir":"Reference","previous_headings":"","what":"Arguments","title":"Mcstate Random Number Generator — mcstate_rng","text":"n number samples draw (per stream) size number trials (zero , length 1 n) prob vector probabilities success trial. need sum 1 (though elements must non-negative), case interpret prob weights normalise equal 1 sampling. n_threads Number threads use; see Details","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"method-state-","dir":"Reference","previous_headings":"","what":"Method state()","title":"Mcstate Random Number Generator — mcstate_rng","text":"Returns state random number stream. returns raw vector length 32 * n_streams. primarily intended debugging one (yet) initialise mcstate_rng object state.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"usage-16","dir":"Reference","previous_headings":"","what":"Usage","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"mcstate_rng$state()"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"rng <- mcstate2::mcstate_rng$new(42) # Shorthand for Uniform(0, 1) rng$random_real(5) #> [1] 0.4969537 0.6896184 0.3669609 0.3620384 0.9299854 # Shorthand for Normal(0, 1) rng$random_normal(5) #> [1] -1.53271190 -1.41913805 0.02345789 -0.73297710 -1.23072552 # Uniform random numbers between min and max rng$uniform(5, -2, 6) #> [1] -0.07182473 4.28030151 -1.86883075 1.79198057 2.40249777 # Normally distributed random numbers with mean and sd rng$normal(5, 4, 2) #> [1] 5.100104 3.489436 4.315190 2.868816 4.235165 # Binomially distributed random numbers with size and prob rng$binomial(5, 10, 0.3) #> [1] 5 3 2 5 2 # Negative binomially distributed random numbers with size and prob rng$nbinomial(5, 10, 0.3) #> [1] 18 12 10 28 41 # Hypergeometric distributed random numbers with parameters n1, n2 and k rng$hypergeometric(5, 6, 10, 4) #> [1] 2 2 2 0 2 # Gamma distributed random numbers with parameters a and b rng$gamma(5, 0.5, 2) #> [1] 0.1440913 0.5461841 0.8933152 2.0532643 0.2496346 # Poisson distributed random numbers with mean lambda rng$poisson(5, 2) #> [1] 6 2 2 2 1 # Exponentially distributed random numbers with rate rng$exponential(5, 2) #> [1] 0.19990637 0.02665489 0.14149562 0.60697382 0.31936759 # Multinomial distributed random numbers with size and vector of # probabiltiies prob rng$multinomial(5, 10, c(0.1, 0.3, 0.5, 0.1)) #> [,1] [,2] [,3] [,4] [,5] #> [1,] 0 2 0 0 0 #> [2,] 3 2 3 3 4 #> [3,] 7 6 7 6 6 #> [4,] 0 0 0 1 0"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_distributed.html","id":null,"dir":"Reference","previous_headings":"","what":"Create a set of distributed seeds — mcstate_rng_distributed_state","title":"Create a set of distributed seeds — mcstate_rng_distributed_state","text":"Create set initial random number seeds suitable using within distributed context (multiple processes nodes) level higher single group synchronised threads.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_distributed.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Create a set of distributed seeds — mcstate_rng_distributed_state","text":"","code":"mcstate_rng_distributed_state( seed = NULL, n_streams = 1L, n_nodes = 1L, algorithm = \"xoshiro256plus\" ) mcstate_rng_distributed_pointer( seed = NULL, n_streams = 1L, n_nodes = 1L, algorithm = \"xoshiro256plus\" )"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_distributed.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Create a set of distributed seeds — mcstate_rng_distributed_state","text":"seed Initial seed use. mcstate_rng, can NULL (create seed using R's generators), integer raw vector appropriate length. n_streams number streams create per node. n_nodes number separate seeds create. separated \"long jump\" generator. algorithm name algorithm use.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_distributed.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Create a set of distributed seeds — mcstate_rng_distributed_state","text":"list either raw vectors (mcstate_rng_distributed_state) mcstate_rng_pointer objects (mcstate_rng_distributed_pointer)","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_distributed.html","id":"details","dir":"Reference","previous_headings":"","what":"Details","title":"Create a set of distributed seeds — mcstate_rng_distributed_state","text":"See vignette(\"rng_distributed\") proper introduction functions.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_distributed.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Create a set of distributed seeds — mcstate_rng_distributed_state","text":"","code":"mcstate2::mcstate_rng_distributed_state(n_nodes = 2) #> [[1]] #> [1] 22 a8 ac 52 20 b1 43 d8 2b e5 c3 26 17 1b b0 7a 81 b0 17 3d 6b 2b cf 75 ed #> [26] 16 7d d4 1d 47 ae 07 #> #> [[2]] #> [1] 55 69 d3 7d c7 28 ae e9 29 d4 12 8a 04 da 6a 57 fb 09 d9 3f d9 65 9d 31 4e #> [26] db 4b c8 88 28 e2 28 #> mcstate2::mcstate_rng_distributed_pointer(n_nodes = 2) #> [[1]] #> #> Public: #> algorithm: xoshiro256plus #> initialize: function (seed = NULL, n_streams = 1L, long_jump = 0L, algorithm = \"xoshiro256plus\") #> is_current: function () #> n_streams: 1 #> state: function () #> sync: function () #> Private: #> is_current_: TRUE #> ptr_: externalptr #> state_: 83 6f a7 81 a1 dc 69 f1 90 00 81 d1 88 22 c9 fb 5c 8b 95 ... #> #> [[2]] #> #> Public: #> algorithm: xoshiro256plus #> initialize: function (seed = NULL, n_streams = 1L, long_jump = 0L, algorithm = \"xoshiro256plus\") #> is_current: function () #> n_streams: 1 #> state: function () #> sync: function () #> Private: #> is_current_: TRUE #> ptr_: externalptr #> state_: 58 c2 bd fe cb 52 7c ae 16 f9 bc 6a ea 67 89 e6 cf ce 7e ... #>"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_pointer.html","id":null,"dir":"Reference","previous_headings":"","what":"Create pointer to random number generator stream — mcstate_rng_pointer","title":"Create pointer to random number generator stream — mcstate_rng_pointer","text":"function exists support use packages wish use mcstate's random number support, creates opaque pointer set random number streams.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_pointer.html","id":"public-fields","dir":"Reference","previous_headings":"","what":"Public fields","title":"Create pointer to random number generator stream — mcstate_rng_pointer","text":"algorithm name generator algorithm used (read-) n_streams number streams random numbers provided (read-)","code":""},{"path":[]},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_pointer.html","id":"public-methods","dir":"Reference","previous_headings":"","what":"Public methods","title":"Create pointer to random number generator stream — mcstate_rng_pointer","text":"mcstate_rng_pointer$new() mcstate_rng_pointer$sync() mcstate_rng_pointer$state() mcstate_rng_pointer$is_current()","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_pointer.html","id":"method-new-","dir":"Reference","previous_headings":"","what":"Method new()","title":"Create pointer to random number generator stream — mcstate_rng_pointer","text":"Create new mcstate_rng_pointer object","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_pointer.html","id":"usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Create pointer to random number generator stream — mcstate_rng_pointer","text":"","code":"mcstate_rng_pointer$new( seed = NULL, n_streams = 1L, long_jump = 0L, algorithm = \"xoshiro256plus\" )"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_pointer.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Create pointer to random number generator stream — mcstate_rng_pointer","text":"seed random number seed use (see mcstate_rng details) n_streams number independent random number streams create long_jump Optionally integer indicating many \"long jumps\" carried immediately creation. can used create distributed parallel random number generator (see mcstate_rng_distributed_state) algorithm random number algorithm use. default xoshiro256plus good general choice","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_pointer.html","id":"method-sync-","dir":"Reference","previous_headings":"","what":"Method sync()","title":"Create pointer to random number generator stream — mcstate_rng_pointer","text":"Synchronise R copy random number state. Typically needed serialisation ever used object.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_pointer.html","id":"usage-1","dir":"Reference","previous_headings":"","what":"Usage","title":"Create pointer to random number generator stream — mcstate_rng_pointer","text":"","code":"mcstate_rng_pointer$sync()"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_pointer.html","id":"method-state-","dir":"Reference","previous_headings":"","what":"Method state()","title":"Create pointer to random number generator stream — mcstate_rng_pointer","text":"Return raw vector state. can used create generators state.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_pointer.html","id":"usage-2","dir":"Reference","previous_headings":"","what":"Usage","title":"Create pointer to random number generator stream — mcstate_rng_pointer","text":"","code":"mcstate_rng_pointer$state()"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_pointer.html","id":"method-is-current-","dir":"Reference","previous_headings":"","what":"Method is_current()","title":"Create pointer to random number generator stream — mcstate_rng_pointer","text":"Return logical, indicating random number state returned state() \"current\" (.e., copy held pointer) . TRUE creation immediately calling $sync() $state() FALSE use pointer.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_pointer.html","id":"usage-3","dir":"Reference","previous_headings":"","what":"Usage","title":"Create pointer to random number generator stream — mcstate_rng_pointer","text":"","code":"mcstate_rng_pointer$is_current()"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_pointer.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Create pointer to random number generator stream — mcstate_rng_pointer","text":"","code":"mcstate2::mcstate_rng_pointer$new() #> #> Public: #> algorithm: xoshiro256plus #> initialize: function (seed = NULL, n_streams = 1L, long_jump = 0L, algorithm = \"xoshiro256plus\") #> is_current: function () #> n_streams: 1 #> state: function () #> sync: function () #> Private: #> is_current_: TRUE #> ptr_: externalptr #> state_: a1 04 ad dd ba 69 6a b2 e7 ca a8 40 19 ec 68 f4 3c 4b 08 ..."},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_runner_parallel.html","id":null,"dir":"Reference","previous_headings":"","what":"Run MCMC chain in parallel — mcstate_runner_parallel","title":"Run MCMC chain in parallel — mcstate_runner_parallel","text":"Run MCMC chains parallel (time). runner uses parallel package distribute chains number worker processes machine. Compared \"worker\" support mcstate version 1 simple improve time. particular report back information progress chain running worker even across chains. also support warn number chains neatly divide number workers. Mostly exists proof concept us think different interfaces. Unless chains quite slow, parallel runner slower serial runner (mcstate_runner_serial) due overhead cost starting cluster.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_runner_parallel.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Run MCMC chain in parallel — mcstate_runner_parallel","text":"","code":"mcstate_runner_parallel(n_workers)"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_runner_parallel.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Run MCMC chain in parallel — mcstate_runner_parallel","text":"n_workers Number workers create cluster . multi-user setting careful set cores allowed use. can use parallel::detectCores() get estimate number cores single user system (often overestimate returns number logical cores, including \"hyperthreading\"). Fewer cores used run fewer chains workers.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_runner_parallel.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Run MCMC chain in parallel — mcstate_runner_parallel","text":"runner class mcstate_runner can passed mcstate_sample()","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_runner_serial.html","id":null,"dir":"Reference","previous_headings":"","what":"Run MCMC chain in series — mcstate_runner_serial","title":"Run MCMC chain in series — mcstate_runner_serial","text":"Run MCMC chains series (one another). simplest chain runner, default used mcstate_sample(). nothing can configured (yet).","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_runner_serial.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Run MCMC chain in series — mcstate_runner_serial","text":"","code":"mcstate_runner_serial()"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_runner_serial.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Run MCMC chain in series — mcstate_runner_serial","text":"runner class mcstate_runner can passed mcstate_sample()","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sample.html","id":null,"dir":"Reference","previous_headings":"","what":"Sample from a model — mcstate_sample","title":"Sample from a model — mcstate_sample","text":"Sample model. Uses Monte Carlo method (possibly something else future) generate samples distribution. going change lot future, add support distributing workers, things like parallel reproducible streams random numbers. now just runs single chain proof concept.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sample.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Sample from a model — mcstate_sample","text":"","code":"mcstate_sample( model, sampler, n_steps, initial = NULL, n_chains = 1L, runner = NULL, restartable = FALSE )"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sample.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Sample from a model — mcstate_sample","text":"model model sample ; mcstate_model now, might change future test see things match interface rather particular class attribute. sampler sampler use. described later, hope make reasonably easy implement can try different sampling ideas. now, sampler implemented mcstate_sampler_random_walk(). n_steps number steps run sampler . initial Optionally, initial parameter values sampling. given, sample model (prior). n_chains Number chains run. default run single chain, likely want run . runner runner chains. default option run chains series (via mcstate_runner_serial). current option mcstate_runner_parallel uses parallel package run chains parallel. run one chain argument best left alone. restartable Logical, indicating chains restartable. add additional data chains object.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sample.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Sample from a model — mcstate_sample","text":"list parameters densities; write tools dealing later. Elements include: pars: matrix many columns parameters, many rows total number samples taken across chains (n_steps * n_chains) density: vector model log densities, one per step (length n_steps * n_chains) initial: record initial conditions, matrix many rows parameters n_chains columns (format matrix form initial input parameter) details: Additional details reported sampler; list length n_chains (NULL) details depend sampler. one subject change. chain: integer vector indicating chain samples came (1, 2, ..., n_chains)","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sample_continue.html","id":null,"dir":"Reference","previous_headings":"","what":"Continue sampling — mcstate_sample_continue","title":"Continue sampling — mcstate_sample_continue","text":"Continue (restart) chains started mcstate_sample. Requires original chains run restartable = TRUE. Running chains way result final state exactly running total (original + continued) number steps single push.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sample_continue.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Continue sampling — mcstate_sample_continue","text":"","code":"mcstate_sample_continue(samples, n_steps, restartable = FALSE)"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sample_continue.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Continue sampling — mcstate_sample_continue","text":"samples mcstate_samples object created mcstate_sample() n_steps number new steps run restartable Logical, indicating chains restartable. add additional data chains object.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sample_continue.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Continue sampling — mcstate_sample_continue","text":"list parameters densities","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sampler_adaptive.html","id":null,"dir":"Reference","previous_headings":"","what":"Adaptive Metropolis-Hastings Sampler — mcstate_sampler_adaptive","title":"Adaptive Metropolis-Hastings Sampler — mcstate_sampler_adaptive","text":"Create adaptive Metropolis-Hastings sampler, tune variance covariance matrix (vs simple random walk sampler mcstate_sampler_random_walk).","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sampler_adaptive.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Adaptive Metropolis-Hastings Sampler — mcstate_sampler_adaptive","text":"","code":"mcstate_sampler_adaptive( initial_vcv, initial_vcv_weight = 1000, initial_scaling = 1, initial_scaling_weight = NULL, min_scaling = 0, scaling_increment = NULL, log_scaling_update = TRUE, acceptance_target = 0.234, forget_rate = 0.2, forget_end = Inf, adapt_end = Inf, pre_diminish = 0 )"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sampler_adaptive.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Adaptive Metropolis-Hastings Sampler — mcstate_sampler_adaptive","text":"initial_vcv initial variance covariance matrix; start using proposal, gradually become weighted towards empirical covariance matrix calculated chain. initial_vcv_weight Weight initial variance-covariance matrix used build proposal random-walk. Higher values translate higher confidence initial variance-covariance matrix means update additional samples slower. initial_scaling initial scaling variance covariance matrix used generate multivariate normal proposal random-walk Metropolis-Hastings algorithm. generate proposal matrix, weighted variance covariance matrix multiplied scaling parameter squared times 2.38^2 / n_pars (n_pars number fitted parameters). Thus, Gaussian target parameter space, optimal scaling around 1. initial_scaling_weight initial weight used scaling update. scaling weight increase first pre_diminish iterations, scaling weight increases adaptation scaling diminishes. NULL (default) value 5 / (acceptance_target * (1 - acceptance_target)). min_scaling minimum scaling variance covariance matrix used generate multivariate normal proposal random-walk Metropolis-Hastings algorithm. scaling_increment scaling increment added subtracted scaling factor variance-covariance adaptive step. NULL (default) optimal value calculated. log_scaling_update Logical, whether changes scaling parameter made log-scale. acceptance_target target fraction proposals accepted (optimally) adaptive part mixture model. forget_rate rate forgetting early parameter sets empirical variance-covariance matrix MCMC chains. example, forget_rate = 0.2 (default) means every 5th iterations remove earliest parameter set included, remove 1st parameter set 5th update, 2nd 10th update, . Setting forget_rate = 0 means early parameter sets never forgotten. forget_end final iteration early parameter sets can forgotten. Setting forget_rate = Inf (default) means forgetting mechanism continues throughout chains. Forgetting early parameter sets becomes less useful chains settled posterior mode, parameter might set estimate long take. adapt_end final iteration can adapt multivariate normal proposal. Thereafter empirical variance-covariance matrix, scaling weight remain fixed. allows adaptation switched certain point help ensure convergence chain. pre_diminish number updates adaptation scaling parameter starts diminish. Setting pre_diminish = 0 means diminishing adaptation scaling parameter offset, pre_diminish = Inf mean never diminishing adaptation. Diminishing adaptation help scaling parameter converge better, chains find location scale posterior mode might useful explore switched .","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sampler_adaptive.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Adaptive Metropolis-Hastings Sampler — mcstate_sampler_adaptive","text":"mcstate_sampler object, can used mcstate_sample","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sampler_adaptive.html","id":"details","dir":"Reference","previous_headings":"","what":"Details","title":"Adaptive Metropolis-Hastings Sampler — mcstate_sampler_adaptive","text":"Efficient exploration parameter space MCMC might difficult target distribution high dimensionality, especially target probability distribution present high degree correlation. Adaptive schemes used \"learn\" fly correlation structure updating proposal distribution recalculating empirical variance-covariance matrix rescale adaptive step MCMC. implementation adaptive MCMC algorithm based adaptation \"accelerated shaping\" algorithm Spencer (2021). algorithm based random-walk Metropolis-Hasting algorithm proposal multi-variate Normal distribution centered current point. Spencer SEF (2021) Accelerating adaptation adaptive Metropolis–Hastings random walk algorithm. Australian & New Zealand Journal Statistics 63:468-484.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sampler_hmc.html","id":null,"dir":"Reference","previous_headings":"","what":"Create HMC — mcstate_sampler_hmc","title":"Create HMC — mcstate_sampler_hmc","text":"Create Hamiltonian Monte Carlo sampler, implemented using leapfrog algorithm.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sampler_hmc.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Create HMC — mcstate_sampler_hmc","text":"","code":"mcstate_sampler_hmc( epsilon = 0.015, n_integration_steps = 10, vcv = NULL, debug = FALSE )"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sampler_hmc.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Create HMC — mcstate_sampler_hmc","text":"epsilon step size HMC steps n_integration_steps number HMC steps per step vcv variance-covariance matrix momentum vector. default uses identity matrix. debug Logical, indicating save intermediate points gradients. add vector \"history\" details integration. slow things though accumulate history inefficiently.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sampler_hmc.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Create HMC — mcstate_sampler_hmc","text":"mcstate_sampler object, can used mcstate_sample","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sampler_random_walk.html","id":null,"dir":"Reference","previous_headings":"","what":"Random Walk Sampler — mcstate_sampler_random_walk","title":"Random Walk Sampler — mcstate_sampler_random_walk","text":"Create simple random walk sampler, uses symmetric proposal move around parameter space. sampler supports sampling models likelihood computable randomly (e.g., pmcmc).","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sampler_random_walk.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Random Walk Sampler — mcstate_sampler_random_walk","text":"","code":"mcstate_sampler_random_walk(proposal = NULL, vcv = NULL)"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sampler_random_walk.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Random Walk Sampler — mcstate_sampler_random_walk","text":"proposal proposal function; must take vector parameters random number generator object (mcstate_rng) produce new vector proposed parameters. vcv variance covariance matrix generate proposal function . want multivariate Gaussian proposal, likely simpler supplying proposal, generally efficient .","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sampler_random_walk.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Random Walk Sampler — mcstate_sampler_random_walk","text":"mcstate_sampler object, can used mcstate_sample","code":""}]
+[{"path":"https://mrc-ide.github.io/orderly2/articles/mcstate2.html","id":"the-basic-idea","dir":"Articles","previous_headings":"","what":"The basic idea","title":"Introduction to mcstate2","text":"Draw samples model using Markov Chain Monte Carlo methods. need: model: mcstate_model() object minimally knows names parameter vector (unstructured real-valued vector) can compute log probability density. may also able compute gradient log density, sample directly parameter space (e.g., represents prior distribution). sampler: method drawing samples model’s distribution sequence. define several different sampler types, simplest one mcstate_sampler_random_walk(), implements simple Metropolis algorithm random walk. runner: controls chains run (e.g., one another parallel). system designed composable; can work Bayesian way defining model representing likelihood another model representing prior pick sampler based capabilities model, pick runner based capabilities computer. mcstate_model() interface designed flexible user-friendly. expect write higher-level interface help work , describe write wrappers models implemented packages (might write model dust odin adaptor make easy work tools provided mcstate2 start making inferences model).","code":""},{"path":"https://mrc-ide.github.io/orderly2/articles/mcstate2.html","id":"an-example","dir":"Articles","previous_headings":"","what":"An example","title":"Introduction to mcstate2","text":"starting example, ’s worth noting far better tools model sort thing (stan, bugs, jags, R - really anything). aim section derive simple model may feel familiar. strength package performing inference custom models can’t expressed high level interfaces. simple likelihood, following model formulation “Statistical Rethinking” chapter 3; height modelled normally distributed departures linear relationship weight. prior ’ll make much nicer work future, construct density hand sum normally distributed priors b, weak uniform prior sigma. provide direct_sample function can draw samples prior distribution directly. posterior distribution combination two models (indicated + ’re adding log-scale, using prior posterior; can use mcstate_model_combine() prefer). Constructing sensible initial variance-covariance matrix bit trick, using adaptive sampler reduces pain . values chosen reasonable starting points. Now run sampler. ’ve started good starting point make simple sampler converge quickly: don’t yet tools working samples objects, can see density time easily enough: plots estimated parameters:","code":"head(data) #> height weight #> 1 162.5401 45.92805 #> 2 159.9566 51.19368 #> 3 156.1808 44.56841 #> 4 168.4164 60.36933 #> 5 158.6978 52.14180 #> 6 154.7666 44.66696 plot(height ~ weight, data) likelihood <- mcstate_model( list( parameters = c(\"a\", \"b\", \"sigma\"), density = function(x) { a <- x[[1]] b <- x[[2]] sigma <- x[[3]] mu <- a + b * data$weight sum(dnorm(data$height, mu, sigma, log = TRUE)) })) prior <- local({ a_mu <- 178 a_sd <- 100 b_mu <- 0 b_sd <- 10 sigma_min <- 0 sigma_max <- 50 mcstate_model( list( parameters = c(\"a\", \"b\", \"sigma\"), density = function(x) { a <- x[[1]] b <- x[[2]] sigma <- x[[3]] dnorm(a, a_mu, a_sd, log = TRUE) + dnorm(b, b_mu, b_sd, log = TRUE) + dunif(sigma, sigma_min, sigma_max, log = TRUE) }, direct_sample = function(rng) { c(rng$normal(1, a_mu, a_sd), rng$normal(1, b_mu, b_sd), rng$uniform(1, sigma_min, sigma_max)) }, domain = rbind(c(-Inf, Inf), c(-Inf, Inf), c(0, Inf)) )) }) posterior <- likelihood + prior vcv <- rbind(c(4.5, -0.088, 0.076), c(-0.088, 0.0018, -0.0015), c(0.076, -0.0015, 0.0640)) sampler <- mcstate_sampler_random_walk(vcv = vcv) samples <- mcstate_sample(posterior, sampler, 5000, initial = c(114, 0.9, 3), n_chains = 4) matplot(t(samples$density), type = \"l\", lty = 1, xlab = \"log posterior density\", ylab = \"sample\", col = \"#00000055\") par(mfrow = c(1, 3)) plot(density(samples$pars[\"a\", , ]), main = \"a\") abline(v = 114, col = \"red\") plot(density(samples$pars[\"b\", , ]), main = \"b\") abline(v = 0.9, col = \"red\") plot(density(samples$pars[\"sigma\", , ]), main = \"sigma\") abline(v = 3, col = \"red\")"},{"path":[]},{"path":"https://mrc-ide.github.io/orderly2/articles/samplers.html","id":"the-bendy-banana","dir":"Articles","previous_headings":"Comparisons","what":"The bendy banana","title":"Samplers","text":"example shows HMC outperforming random walk two dimensional banana-shaped function. model takes two parameters alpha beta, based two successive simple draws, one conditional :  \\[ \\beta \\sim Normal(1,0) \\\\ \\alpha \\sim Normal(\\beta^2, \\sigma) \\]  \\(\\sigma\\) standard deviation conditional draw. ’ll use R’s dnorm likelihood calculations differentiate density hand (using formula gaussian density) derive gradient hand. skip details automated upcoming version package.  Let’s create model \\(\\sigma = 0.5\\) can plot greyscale visualisation density computing density grid. Normally possible course: particular case can also easily generate samples, know good sampler produce: also possible compute 95% confidence interval distribution using relationship standard bivariate normal distribution banana shaped distribution defined . can check roughly 10 samples (200) 95% CI contour.","code":"banana_model <- function(sd = 0.5) { mcstate_model(list( parameters = c(\"alpha\", \"beta\"), direct_sample = function(rng) { beta <- rng$random_normal(1) alpha <- rng$normal(1, beta^2, sd) c(alpha, beta) }, density = function(x) { alpha <- x[[1]] beta <- x[[2]] dnorm(beta, log = TRUE) + dnorm((alpha - beta^2) / sd, log = TRUE) }, gradient = function(x) { alpha <- x[[1]] beta <- x[[2]] c((beta^2 - alpha) / sd^2, -beta + 2 * beta * (alpha - beta^2) / sd^2) }, domain = rbind(c(-Inf, Inf), c(-Inf, Inf)))) } m <- banana_model(0.5) a <- seq(-2, 6, length.out = 1000) b <- seq(-2, 2, length.out = 1000) z <- outer(a, b, function(a, b) dnorm(b) * dnorm((a - b^2) / 0.5)) image(a, b, z, xlab = \"alpha\", ylab = \"beta\") rng <- mcstate_rng$new() s <- vapply(seq(200), function(x) m$direct_sample(rng), numeric(2)) image(a, b, z, xlab = \"alpha\", ylab = \"beta\") points(s[1, ], s[2, ], pch = 19, col = \"#00000055\") theta <- seq(0, 2 * pi, length.out = 10000) z95 <- local({ sd <- 0.5 r <- sqrt(qchisq(.95, df = 2)) x <- r * cos(theta) y <- r * sin(theta) cbind(x^2 + y * sd, x) }) image(a, b, z, xlab = \"alpha\", ylab = \"beta\") lines(z95[, 1], z95[, 2]) points(s[1, ], s[2, ], pch = 19, col = \"#00000055\")"},{"path":"https://mrc-ide.github.io/orderly2/articles/samplers.html","id":"sampling-with-other-samplers","dir":"Articles","previous_headings":"Comparisons","what":"Sampling with other samplers","title":"Samplers","text":"generally possible directly sample density (otherwise MCMC similar methods exist!). cases need use sampler based density available possibly gradient density. can start basic random-walk sampler can see great. can probably improve samples finding better variance covariance matrix (VCV), single VCV hold well whole surface similar multivariate normal (, appropriate VCV change depending position parameter space) Let’s try Hamiltonian Monte Carlo (HMC) sampler, uses gradient move efficiently parameter space: Clearly better!","code":"sampler_rw <- mcstate_sampler_random_walk(vcv = diag(2) * 0.01) res_rw <- mcstate_sample(m, sampler_rw, 2000) plot(res_rw$pars, pch = 19, col = \"#ff222277\") lines(z95[, 1], z95[, 2]) sampler_hmc <- mcstate_sampler_hmc(epsilon = 0.1, n_integration_steps = 10) res_hmc <- mcstate_sample(m, sampler_hmc, 2000) plot(res_hmc$pars, pch = 19, col = \"#ff222277\") lines(z95[, 1], z95[, 2])"},{"path":"https://mrc-ide.github.io/orderly2/authors.html","id":null,"dir":"","previous_headings":"","what":"Authors","title":"Authors and Citation","text":"Rich FitzJohn. Author, maintainer. Imperial College Science, Technology Medicine. Copyright holder.","code":""},{"path":"https://mrc-ide.github.io/orderly2/authors.html","id":"citation","dir":"","previous_headings":"","what":"Citation","title":"Authors and Citation","text":"FitzJohn R (2024). mcstate2: Next Generation mcstate. R package version 0.1.0, https://github.com/mrc-ide/mcstate2, https://mrc-ide.github.io/mcstate2.","code":"@Manual{, title = {mcstate2: Next Generation mcstate}, author = {Rich FitzJohn}, year = {2024}, note = {R package version 0.1.0, https://github.com/mrc-ide/mcstate2}, url = {https://mrc-ide.github.io/mcstate2}, }"},{"path":[]},{"path":"https://mrc-ide.github.io/orderly2/index.html","id":"installation","dir":"","previous_headings":"","what":"Installation","title":"Next Generation mcstate","text":"install mcstate2:","code":"remotes::install_github(\"mrc-ide/mcstate2\", upgrade = FALSE)"},{"path":"https://mrc-ide.github.io/orderly2/index.html","id":"license","dir":"","previous_headings":"","what":"License","title":"Next Generation mcstate","text":"MIT © Imperial College Science, Technology Medicine","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_model.html","id":null,"dir":"Reference","previous_headings":"","what":"Create basic model — mcstate_model","title":"Create basic model — mcstate_model","text":"Create basic mcstate model. takes user-supplied object minimally can compute probability density (via density function) information parameters; can sample model using MCMC using mcstate_sample. imagine many users call function directly, glue used packages.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_model.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Create basic model — mcstate_model","text":"","code":"mcstate_model(model, properties = NULL)"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_model.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Create basic model — mcstate_model","text":"model list environment elements described Details. properties Optionally, mcstate_model_properties object, used enforce clarify properties model.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_model.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Create basic model — mcstate_model","text":"object class mcstate_model. elements: model: model provided parameters: parameter name vector domain: parameter domain matrix, named parameters direct_sample: direct_sample function, provided model gradient: gradient function, provided model properties: list properties model; see mcstate_model_properties(). Currently contains: has_gradient: model can compute gradient has_direct_sample: model can sample parameters space is_stochastic: model behave stochastically","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_model.html","id":"details","dir":"Reference","previous_headings":"","what":"Details","title":"Create basic model — mcstate_model","text":"model argument can list environment (something indexable $) elements: density: function compute probability density. must take argument representing parameter vector (numeric vector) return single value. posterior probability density Bayesian inference, anything really. Models can return -Inf things impossible, try cope gracefully wherever possible. parameters: character vector parameter names. vector source truth length parameter vector. domain: Information parameter domain. two column matrix length(parameters) rows representing parameter. parameter minimum maximum bounds given first second column. Infinite values (-Inf Inf) used parameter infinite domain . Currently used translate bounded unbounded space HMC, might also use reflecting proposals MCMC . present assume model valid everywhere (.e., parameters valid -Inf Inf. direct_sample: function sample directly parameter space, given mcstate_rng object sample . case model returns posterior (e.g., Bayesian inference), assumed sampling prior. use generating initial conditions MCMC given, possibly uses. given using mcstate_sample() user provide vector initial states. gradient: function compute gradient density respect parameter vector; takes parameter vector returns vector length. efficiency, model may want stateful gradients can efficiently calculated density calculation, density gradient, called parameters. function optional (may well defined possible define). set_rng_state: function set state (contrast rng passed direct_sample sampler's rng stream, assume models look stream, may need many streams). Models provide method assumed stochastic; however, can use is_stochastic property (via mcstate_model_properties()) override (e.g., run stochastic model deterministic expectation). function takes mcstate_rng object uses seed random number state model. two options (1) hold copy provided object draw samples needed (effect sharing random number stream sampler) create new rng stream jump stream (provide utility present mcstate_rng$new(rng$state(), n_streams)$jump()$state() ). main reason need multiple (perhaps parallel) streams random numbers model. $jump() important, otherwise end correlated draws sampler.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_model_combine.html","id":null,"dir":"Reference","previous_headings":"","what":"Combine two models — mcstate_model_combine","title":"Combine two models — mcstate_model_combine","text":"Combine two models multiplication. need better name . Bayesian inference want create model represents multiplication likelihood prior (log space) convenient think models separately. Multiplying probabilities (adding log scale) common enough may situations want .","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_model_combine.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Combine two models — mcstate_model_combine","text":"","code":"mcstate_model_combine(a, b, properties = NULL, name_a = \"a\", name_b = \"b\")"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_model_combine.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Combine two models — mcstate_model_combine","text":"first model b second model properties mcstate_model_properties object, used control (enforce) properties combined model. name_a Name first model (defaulting ''); can use make error messages nicer read, practical effect. name_b Name first model (defaulting 'b'); can use make error messages nicer read, practical effect.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_model_combine.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Combine two models — mcstate_model_combine","text":"mcstate_model object","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_model_combine.html","id":"details","dir":"Reference","previous_headings":"","what":"Details","title":"Combine two models — mcstate_model_combine","text":"describe impact combining pair models density: sum log densities model parameters: union parameters model taken domain: restrictive domain taken parameter. Parameters appear one model assumed infinite domain . gradient: models define gradient, sum gradients. either define gradient, resulting model gradient support. Set has_gradient = TRUE within `properties want enforce combination differentiable. models disagree parameters, parameters missing model assumed (reasonably) zero gradient. direct_sample: one hard right thing . neither model can directly sampled fine, directly sample. one model can sampled can sample union parameters take function (case prior model combined likelihood). cases errors, can avoided setting has_direct_gradient = FALSE properties. properties model combined , reflecting properties joint model. model field ordered, unnamed, list containing two elements corresponding first second model (mcstate_model, underlying model, perhaps?). part makes distinction two models ; components equivalent.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_model_properties.html","id":null,"dir":"Reference","previous_headings":"","what":"Describe model properties — mcstate_model_properties","title":"Describe model properties — mcstate_model_properties","text":"Describe properties model. Use function optional, can pass return value properties argument mcstate_model enforce model actually properties.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_model_properties.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Describe model properties — mcstate_model_properties","text":"","code":"mcstate_model_properties( has_gradient = NULL, has_direct_sample = NULL, is_stochastic = NULL )"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_model_properties.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Describe model properties — mcstate_model_properties","text":"has_gradient Logical, indicating model gradient method. Use NULL (default) detect model. has_direct_sample Logical, indicating model direct_sample method. Use NULL (default) detect model. is_stochastic Logical, indicating model stochastic. Stochastic models must supply set_rng_state method might support get_rng_state method later.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_model_properties.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Describe model properties — mcstate_model_properties","text":"list class mcstate_model_properties modified.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":null,"dir":"Reference","previous_headings":"","what":"Mcstate Random Number Generator — mcstate_rng","title":"Mcstate Random Number Generator — mcstate_rng","text":"Create object can used generate random numbers RNG mcstate uses internally. primarily meant debugging testing underlying C++ rather source random numbers R.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Mcstate Random Number Generator — mcstate_rng","text":"mcstate_rng object, can used drawn random numbers mcstate's distributions.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"running-multiple-streams-perhaps-in-parallel","dir":"Reference","previous_headings":"","what":"Running multiple streams, perhaps in parallel","title":"Mcstate Random Number Generator — mcstate_rng","text":"underlying random number generators designed work parallel, random access parameters (see vignette(\"rng\") details). However, usually done within context running model particle sees stream numbers. provide support running random number generators parallel, speed gains parallelisation likely somewhat eroded overhead copying around large number random numbers. random distribution functions support argument n_threads controls number threads used. argument silently effect installation support OpenMP. Parallelisation performed level stream, draw n numbers stream total n * n_streams random numbers using n_threads threads . Setting n_threads higher n_streams therefore effect. running somebody else's system (e.g., HPC, CRAN) must respect various environment variables control maximum allowable number threads. exception random_real, random number distribution accepts parameters; interpretations depend n, n_streams rank. scalar use parameter value every draw every stream vector length n draw n random numbers per stream, every stream use parameter value every stream draw (different, shared, parameter value subsequent draws). matrix provided one row n_streams columns use different parameters stream, parameter draw. matrix provided n rows n_streams columns use parameter value [, j] ith draw jth stream. rules slightly different prob argument multinomial prob vector values. shift dimensions one: vector use prob every draw every stream length(prob) possible outcomes. matrix n columns vary draw (ith draw using vector prob[, ] shared across streams. nrow(prob) possible outcomes. 3d array provided 1 column n_streams \"layers\" (third dimension) use use different parameters stream, parameter draw. 3d array provided n columns n_streams \"layers\" vary draws streams use vector prob[, , j] ith draw jth stream. output differ based number threads used, number streams.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"public-fields","dir":"Reference","previous_headings":"","what":"Public fields","title":"Mcstate Random Number Generator — mcstate_rng","text":"info Information generator (read-)","code":""},{"path":[]},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"public-methods","dir":"Reference","previous_headings":"","what":"Public methods","title":"Mcstate Random Number Generator — mcstate_rng","text":"mcstate_rng$new() mcstate_rng$size() mcstate_rng$jump() mcstate_rng$long_jump() mcstate_rng$random_real() mcstate_rng$random_normal() mcstate_rng$uniform() mcstate_rng$normal() mcstate_rng$binomial() mcstate_rng$nbinomial() mcstate_rng$hypergeometric() mcstate_rng$gamma() mcstate_rng$poisson() mcstate_rng$exponential() mcstate_rng$cauchy() mcstate_rng$multinomial() mcstate_rng$state()","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"method-new-","dir":"Reference","previous_headings":"","what":"Method new()","title":"Mcstate Random Number Generator — mcstate_rng","text":"Create mcstate_rng object","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"mcstate_rng$new( seed = NULL, n_streams = 1L, real_type = \"double\", deterministic = FALSE )"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Mcstate Random Number Generator — mcstate_rng","text":"seed seed, integer, raw vector NULL. integer create suitable seed via \"splitmix64\" algorithm, raw vector must correct length (multiple either 32 16 float = FALSE float = TRUE respectively). NULL create seed using R's random number generator. n_streams number streams use (see Details) real_type type floating point number use. Currently float double supported (double default). (negligible) impact speed, exists test low-precision generators. deterministic Logical, indicating use \"deterministic\" mode distributions return expectations state never changed.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"method-size-","dir":"Reference","previous_headings":"","what":"Method size()","title":"Mcstate Random Number Generator — mcstate_rng","text":"Number streams available","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"usage-1","dir":"Reference","previous_headings":"","what":"Usage","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"mcstate_rng$size()"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"method-jump-","dir":"Reference","previous_headings":"","what":"Method jump()","title":"Mcstate Random Number Generator — mcstate_rng","text":"jump function updates random number state stream advancing state equivalent 2^128 numbers drawn stream.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"usage-2","dir":"Reference","previous_headings":"","what":"Usage","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"mcstate_rng$jump()"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"method-long-jump-","dir":"Reference","previous_headings":"","what":"Method long_jump()","title":"Mcstate Random Number Generator — mcstate_rng","text":"Longer $jump, $long_jump method equivalent 2^192 numbers drawn stream.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"usage-3","dir":"Reference","previous_headings":"","what":"Usage","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"mcstate_rng$long_jump()"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"method-random-real-","dir":"Reference","previous_headings":"","what":"Method random_real()","title":"Mcstate Random Number Generator — mcstate_rng","text":"Generate n numbers standard uniform distribution","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"usage-4","dir":"Reference","previous_headings":"","what":"Usage","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"mcstate_rng$random_real(n, n_threads = 1L)"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"arguments-1","dir":"Reference","previous_headings":"","what":"Arguments","title":"Mcstate Random Number Generator — mcstate_rng","text":"n Number samples draw (per stream) n_threads Number threads use; see Details","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"method-random-normal-","dir":"Reference","previous_headings":"","what":"Method random_normal()","title":"Mcstate Random Number Generator — mcstate_rng","text":"Generate n numbers standard normal distribution","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"usage-5","dir":"Reference","previous_headings":"","what":"Usage","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"mcstate_rng$random_normal(n, n_threads = 1L, algorithm = \"box_muller\")"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"arguments-2","dir":"Reference","previous_headings":"","what":"Arguments","title":"Mcstate Random Number Generator — mcstate_rng","text":"n Number samples draw (per stream) n_threads Number threads use; see Details algorithm Name algorithm use; currently box_muller ziggurat supported, latter considerably faster.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"method-uniform-","dir":"Reference","previous_headings":"","what":"Method uniform()","title":"Mcstate Random Number Generator — mcstate_rng","text":"Generate n numbers uniform distribution","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"usage-6","dir":"Reference","previous_headings":"","what":"Usage","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"mcstate_rng$uniform(n, min, max, n_threads = 1L)"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"arguments-3","dir":"Reference","previous_headings":"","what":"Arguments","title":"Mcstate Random Number Generator — mcstate_rng","text":"n Number samples draw (per stream) min minimum distribution (length 1 n) max maximum distribution (length 1 n) n_threads Number threads use; see Details","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"method-normal-","dir":"Reference","previous_headings":"","what":"Method normal()","title":"Mcstate Random Number Generator — mcstate_rng","text":"Generate n numbers normal distribution","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"usage-7","dir":"Reference","previous_headings":"","what":"Usage","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"mcstate_rng$normal(n, mean, sd, n_threads = 1L, algorithm = \"box_muller\")"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"arguments-4","dir":"Reference","previous_headings":"","what":"Arguments","title":"Mcstate Random Number Generator — mcstate_rng","text":"n Number samples draw (per stream) mean mean distribution (length 1 n) sd standard deviation distribution (length 1 n) n_threads Number threads use; see Details algorithm Name algorithm use; currently box_muller ziggurat supported, latter considerably faster.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"method-binomial-","dir":"Reference","previous_headings":"","what":"Method binomial()","title":"Mcstate Random Number Generator — mcstate_rng","text":"Generate n numbers binomial distribution","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"usage-8","dir":"Reference","previous_headings":"","what":"Usage","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"mcstate_rng$binomial(n, size, prob, n_threads = 1L)"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"arguments-5","dir":"Reference","previous_headings":"","what":"Arguments","title":"Mcstate Random Number Generator — mcstate_rng","text":"n Number samples draw (per stream) size number trials (zero , length 1 n) prob probability success trial (0 1, length 1 n) n_threads Number threads use; see Details","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"method-nbinomial-","dir":"Reference","previous_headings":"","what":"Method nbinomial()","title":"Mcstate Random Number Generator — mcstate_rng","text":"Generate n numbers negative binomial distribution","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"usage-9","dir":"Reference","previous_headings":"","what":"Usage","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"mcstate_rng$nbinomial(n, size, prob, n_threads = 1L)"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"arguments-6","dir":"Reference","previous_headings":"","what":"Arguments","title":"Mcstate Random Number Generator — mcstate_rng","text":"n Number samples draw (per stream) size target number successful trials (zero , length 1 n) prob probability success trial (0 1, length 1 n) n_threads Number threads use; see Details","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"method-hypergeometric-","dir":"Reference","previous_headings":"","what":"Method hypergeometric()","title":"Mcstate Random Number Generator — mcstate_rng","text":"Generate n numbers hypergeometric distribution","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"usage-10","dir":"Reference","previous_headings":"","what":"Usage","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"mcstate_rng$hypergeometric(n, n1, n2, k, n_threads = 1L)"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"arguments-7","dir":"Reference","previous_headings":"","what":"Arguments","title":"Mcstate Random Number Generator — mcstate_rng","text":"n Number samples draw (per stream) n1 number white balls urn (called n R's rhyper) n2 number black balls urn (called m R's rhyper) k number balls draw n_threads Number threads use; see Details","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"method-gamma-","dir":"Reference","previous_headings":"","what":"Method gamma()","title":"Mcstate Random Number Generator — mcstate_rng","text":"Generate n numbers gamma distribution","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"usage-11","dir":"Reference","previous_headings":"","what":"Usage","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"mcstate_rng$gamma(n, shape, scale, n_threads = 1L)"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"arguments-8","dir":"Reference","previous_headings":"","what":"Arguments","title":"Mcstate Random Number Generator — mcstate_rng","text":"n Number samples draw (per stream) shape Shape scale Scale ' n_threads Number threads use; see Details","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"method-poisson-","dir":"Reference","previous_headings":"","what":"Method poisson()","title":"Mcstate Random Number Generator — mcstate_rng","text":"Generate n numbers Poisson distribution","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"usage-12","dir":"Reference","previous_headings":"","what":"Usage","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"mcstate_rng$poisson(n, lambda, n_threads = 1L)"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"arguments-9","dir":"Reference","previous_headings":"","what":"Arguments","title":"Mcstate Random Number Generator — mcstate_rng","text":"n Number samples draw (per stream) lambda mean (zero , length 1 n). valid lambda <= 10^7 n_threads Number threads use; see Details","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"method-exponential-","dir":"Reference","previous_headings":"","what":"Method exponential()","title":"Mcstate Random Number Generator — mcstate_rng","text":"Generate n numbers exponential distribution","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"usage-13","dir":"Reference","previous_headings":"","what":"Usage","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"mcstate_rng$exponential(n, rate, n_threads = 1L)"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"arguments-10","dir":"Reference","previous_headings":"","what":"Arguments","title":"Mcstate Random Number Generator — mcstate_rng","text":"n Number samples draw (per stream) rate rate exponential n_threads Number threads use; see Details","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"method-cauchy-","dir":"Reference","previous_headings":"","what":"Method cauchy()","title":"Mcstate Random Number Generator — mcstate_rng","text":"Generate n draws Cauchy distribution.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"usage-14","dir":"Reference","previous_headings":"","what":"Usage","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"mcstate_rng$cauchy(n, location, scale, n_threads = 1L)"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"arguments-11","dir":"Reference","previous_headings":"","what":"Arguments","title":"Mcstate Random Number Generator — mcstate_rng","text":"n Number samples draw (per stream) location location peak distribution (also median) scale scale parameter, specifies distribution's \"half-width half-maximum\" n_threads Number threads use; see Details","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"method-multinomial-","dir":"Reference","previous_headings":"","what":"Method multinomial()","title":"Mcstate Random Number Generator — mcstate_rng","text":"Generate n draws multinomial distribution. contrast distributions , draw vector length prob.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"usage-15","dir":"Reference","previous_headings":"","what":"Usage","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"mcstate_rng$multinomial(n, size, prob, n_threads = 1L)"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"arguments-12","dir":"Reference","previous_headings":"","what":"Arguments","title":"Mcstate Random Number Generator — mcstate_rng","text":"n number samples draw (per stream) size number trials (zero , length 1 n) prob vector probabilities success trial. need sum 1 (though elements must non-negative), case interpret prob weights normalise equal 1 sampling. n_threads Number threads use; see Details","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"method-state-","dir":"Reference","previous_headings":"","what":"Method state()","title":"Mcstate Random Number Generator — mcstate_rng","text":"Returns state random number stream. returns raw vector length 32 * n_streams. primarily intended debugging one (yet) initialise mcstate_rng object state.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"usage-16","dir":"Reference","previous_headings":"","what":"Usage","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"mcstate_rng$state()"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Mcstate Random Number Generator — mcstate_rng","text":"","code":"rng <- mcstate2::mcstate_rng$new(42) # Shorthand for Uniform(0, 1) rng$random_real(5) #> [1] 0.4969537 0.6896184 0.3669609 0.3620384 0.9299854 # Shorthand for Normal(0, 1) rng$random_normal(5) #> [1] -1.53271190 -1.41913805 0.02345789 -0.73297710 -1.23072552 # Uniform random numbers between min and max rng$uniform(5, -2, 6) #> [1] -0.07182473 4.28030151 -1.86883075 1.79198057 2.40249777 # Normally distributed random numbers with mean and sd rng$normal(5, 4, 2) #> [1] 5.100104 3.489436 4.315190 2.868816 4.235165 # Binomially distributed random numbers with size and prob rng$binomial(5, 10, 0.3) #> [1] 5 3 2 5 2 # Negative binomially distributed random numbers with size and prob rng$nbinomial(5, 10, 0.3) #> [1] 18 12 10 28 41 # Hypergeometric distributed random numbers with parameters n1, n2 and k rng$hypergeometric(5, 6, 10, 4) #> [1] 2 2 2 0 2 # Gamma distributed random numbers with parameters a and b rng$gamma(5, 0.5, 2) #> [1] 0.1440913 0.5461841 0.8933152 2.0532643 0.2496346 # Poisson distributed random numbers with mean lambda rng$poisson(5, 2) #> [1] 6 2 2 2 1 # Exponentially distributed random numbers with rate rng$exponential(5, 2) #> [1] 0.19990637 0.02665489 0.14149562 0.60697382 0.31936759 # Multinomial distributed random numbers with size and vector of # probabiltiies prob rng$multinomial(5, 10, c(0.1, 0.3, 0.5, 0.1)) #> [,1] [,2] [,3] [,4] [,5] #> [1,] 0 2 0 0 0 #> [2,] 3 2 3 3 4 #> [3,] 7 6 7 6 6 #> [4,] 0 0 0 1 0"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_distributed.html","id":null,"dir":"Reference","previous_headings":"","what":"Create a set of distributed seeds — mcstate_rng_distributed_state","title":"Create a set of distributed seeds — mcstate_rng_distributed_state","text":"Create set initial random number seeds suitable using within distributed context (multiple processes nodes) level higher single group synchronised threads.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_distributed.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Create a set of distributed seeds — mcstate_rng_distributed_state","text":"","code":"mcstate_rng_distributed_state( seed = NULL, n_streams = 1L, n_nodes = 1L, algorithm = \"xoshiro256plus\" ) mcstate_rng_distributed_pointer( seed = NULL, n_streams = 1L, n_nodes = 1L, algorithm = \"xoshiro256plus\" )"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_distributed.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Create a set of distributed seeds — mcstate_rng_distributed_state","text":"seed Initial seed use. mcstate_rng, can NULL (create seed using R's generators), integer raw vector appropriate length. n_streams number streams create per node. n_nodes number separate seeds create. separated \"long jump\" generator. algorithm name algorithm use.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_distributed.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Create a set of distributed seeds — mcstate_rng_distributed_state","text":"list either raw vectors (mcstate_rng_distributed_state) mcstate_rng_pointer objects (mcstate_rng_distributed_pointer)","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_distributed.html","id":"details","dir":"Reference","previous_headings":"","what":"Details","title":"Create a set of distributed seeds — mcstate_rng_distributed_state","text":"See vignette(\"rng_distributed\") proper introduction functions.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_distributed.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Create a set of distributed seeds — mcstate_rng_distributed_state","text":"","code":"mcstate2::mcstate_rng_distributed_state(n_nodes = 2) #> [[1]] #> [1] 22 a8 ac 52 20 b1 43 d8 2b e5 c3 26 17 1b b0 7a 81 b0 17 3d 6b 2b cf 75 ed #> [26] 16 7d d4 1d 47 ae 07 #> #> [[2]] #> [1] 55 69 d3 7d c7 28 ae e9 29 d4 12 8a 04 da 6a 57 fb 09 d9 3f d9 65 9d 31 4e #> [26] db 4b c8 88 28 e2 28 #> mcstate2::mcstate_rng_distributed_pointer(n_nodes = 2) #> [[1]] #> #> Public: #> algorithm: xoshiro256plus #> initialize: function (seed = NULL, n_streams = 1L, long_jump = 0L, algorithm = \"xoshiro256plus\") #> is_current: function () #> n_streams: 1 #> state: function () #> sync: function () #> Private: #> is_current_: TRUE #> ptr_: externalptr #> state_: 83 6f a7 81 a1 dc 69 f1 90 00 81 d1 88 22 c9 fb 5c 8b 95 ... #> #> [[2]] #> #> Public: #> algorithm: xoshiro256plus #> initialize: function (seed = NULL, n_streams = 1L, long_jump = 0L, algorithm = \"xoshiro256plus\") #> is_current: function () #> n_streams: 1 #> state: function () #> sync: function () #> Private: #> is_current_: TRUE #> ptr_: externalptr #> state_: 58 c2 bd fe cb 52 7c ae 16 f9 bc 6a ea 67 89 e6 cf ce 7e ... #>"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_pointer.html","id":null,"dir":"Reference","previous_headings":"","what":"Create pointer to random number generator stream — mcstate_rng_pointer","title":"Create pointer to random number generator stream — mcstate_rng_pointer","text":"function exists support use packages wish use mcstate's random number support, creates opaque pointer set random number streams.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_pointer.html","id":"public-fields","dir":"Reference","previous_headings":"","what":"Public fields","title":"Create pointer to random number generator stream — mcstate_rng_pointer","text":"algorithm name generator algorithm used (read-) n_streams number streams random numbers provided (read-)","code":""},{"path":[]},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_pointer.html","id":"public-methods","dir":"Reference","previous_headings":"","what":"Public methods","title":"Create pointer to random number generator stream — mcstate_rng_pointer","text":"mcstate_rng_pointer$new() mcstate_rng_pointer$sync() mcstate_rng_pointer$state() mcstate_rng_pointer$is_current()","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_pointer.html","id":"method-new-","dir":"Reference","previous_headings":"","what":"Method new()","title":"Create pointer to random number generator stream — mcstate_rng_pointer","text":"Create new mcstate_rng_pointer object","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_pointer.html","id":"usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Create pointer to random number generator stream — mcstate_rng_pointer","text":"","code":"mcstate_rng_pointer$new( seed = NULL, n_streams = 1L, long_jump = 0L, algorithm = \"xoshiro256plus\" )"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_pointer.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Create pointer to random number generator stream — mcstate_rng_pointer","text":"seed random number seed use (see mcstate_rng details) n_streams number independent random number streams create long_jump Optionally integer indicating many \"long jumps\" carried immediately creation. can used create distributed parallel random number generator (see mcstate_rng_distributed_state) algorithm random number algorithm use. default xoshiro256plus good general choice","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_pointer.html","id":"method-sync-","dir":"Reference","previous_headings":"","what":"Method sync()","title":"Create pointer to random number generator stream — mcstate_rng_pointer","text":"Synchronise R copy random number state. Typically needed serialisation ever used object.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_pointer.html","id":"usage-1","dir":"Reference","previous_headings":"","what":"Usage","title":"Create pointer to random number generator stream — mcstate_rng_pointer","text":"","code":"mcstate_rng_pointer$sync()"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_pointer.html","id":"method-state-","dir":"Reference","previous_headings":"","what":"Method state()","title":"Create pointer to random number generator stream — mcstate_rng_pointer","text":"Return raw vector state. can used create generators state.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_pointer.html","id":"usage-2","dir":"Reference","previous_headings":"","what":"Usage","title":"Create pointer to random number generator stream — mcstate_rng_pointer","text":"","code":"mcstate_rng_pointer$state()"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_pointer.html","id":"method-is-current-","dir":"Reference","previous_headings":"","what":"Method is_current()","title":"Create pointer to random number generator stream — mcstate_rng_pointer","text":"Return logical, indicating random number state returned state() \"current\" (.e., copy held pointer) . TRUE creation immediately calling $sync() $state() FALSE use pointer.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_pointer.html","id":"usage-3","dir":"Reference","previous_headings":"","what":"Usage","title":"Create pointer to random number generator stream — mcstate_rng_pointer","text":"","code":"mcstate_rng_pointer$is_current()"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_rng_pointer.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Create pointer to random number generator stream — mcstate_rng_pointer","text":"","code":"mcstate2::mcstate_rng_pointer$new() #> #> Public: #> algorithm: xoshiro256plus #> initialize: function (seed = NULL, n_streams = 1L, long_jump = 0L, algorithm = \"xoshiro256plus\") #> is_current: function () #> n_streams: 1 #> state: function () #> sync: function () #> Private: #> is_current_: TRUE #> ptr_: externalptr #> state_: a1 04 ad dd ba 69 6a b2 e7 ca a8 40 19 ec 68 f4 3c 4b 08 ..."},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_runner_parallel.html","id":null,"dir":"Reference","previous_headings":"","what":"Run MCMC chain in parallel — mcstate_runner_parallel","title":"Run MCMC chain in parallel — mcstate_runner_parallel","text":"Run MCMC chains parallel (time). runner uses parallel package distribute chains number worker processes machine. Compared \"worker\" support mcstate version 1 simple improve time. particular report back information progress chain running worker even across chains. also support warn number chains neatly divide number workers. Mostly exists proof concept us think different interfaces. Unless chains quite slow, parallel runner slower serial runner (mcstate_runner_serial) due overhead cost starting cluster.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_runner_parallel.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Run MCMC chain in parallel — mcstate_runner_parallel","text":"","code":"mcstate_runner_parallel(n_workers)"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_runner_parallel.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Run MCMC chain in parallel — mcstate_runner_parallel","text":"n_workers Number workers create cluster . multi-user setting careful set cores allowed use. can use parallel::detectCores() get estimate number cores single user system (often overestimate returns number logical cores, including \"hyperthreading\"). Fewer cores used run fewer chains workers.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_runner_parallel.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Run MCMC chain in parallel — mcstate_runner_parallel","text":"runner class mcstate_runner can passed mcstate_sample()","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_runner_serial.html","id":null,"dir":"Reference","previous_headings":"","what":"Run MCMC chain in series — mcstate_runner_serial","title":"Run MCMC chain in series — mcstate_runner_serial","text":"Run MCMC chains series (one another). simplest chain runner, default used mcstate_sample(). nothing can configured (yet).","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_runner_serial.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Run MCMC chain in series — mcstate_runner_serial","text":"","code":"mcstate_runner_serial()"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_runner_serial.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Run MCMC chain in series — mcstate_runner_serial","text":"runner class mcstate_runner can passed mcstate_sample()","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sample.html","id":null,"dir":"Reference","previous_headings":"","what":"Sample from a model — mcstate_sample","title":"Sample from a model — mcstate_sample","text":"Sample model. Uses Monte Carlo method (possibly something else future) generate samples distribution. going change lot future, add support distributing workers, things like parallel reproducible streams random numbers. now just runs single chain proof concept.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sample.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Sample from a model — mcstate_sample","text":"","code":"mcstate_sample( model, sampler, n_steps, initial = NULL, n_chains = 1L, runner = NULL, restartable = FALSE )"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sample.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Sample from a model — mcstate_sample","text":"model model sample ; mcstate_model now, might change future test see things match interface rather particular class attribute. sampler sampler use. described later, hope make reasonably easy implement can try different sampling ideas. now, sampler implemented mcstate_sampler_random_walk(). n_steps number steps run sampler . initial Optionally, initial parameter values sampling. given, sample model (prior). n_chains Number chains run. default run single chain, likely want run . runner runner chains. default option run chains series (via mcstate_runner_serial). current option mcstate_runner_parallel uses parallel package run chains parallel. run one chain argument best left alone. restartable Logical, indicating chains restartable. add additional data chains object.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sample.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Sample from a model — mcstate_sample","text":"list parameters densities; write tools dealing later. Elements include: pars: matrix many columns parameters, many rows total number samples taken across chains (n_steps * n_chains) density: vector model log densities, one per step (length n_steps * n_chains) initial: record initial conditions, matrix many rows parameters n_chains columns (format matrix form initial input parameter) details: Additional details reported sampler; list length n_chains (NULL) details depend sampler. one subject change. chain: integer vector indicating chain samples came (1, 2, ..., n_chains)","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sample_continue.html","id":null,"dir":"Reference","previous_headings":"","what":"Continue sampling — mcstate_sample_continue","title":"Continue sampling — mcstate_sample_continue","text":"Continue (restart) chains started mcstate_sample. Requires original chains run restartable = TRUE. Running chains way result final state exactly running total (original + continued) number steps single push.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sample_continue.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Continue sampling — mcstate_sample_continue","text":"","code":"mcstate_sample_continue(samples, n_steps, restartable = FALSE)"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sample_continue.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Continue sampling — mcstate_sample_continue","text":"samples mcstate_samples object created mcstate_sample() n_steps number new steps run restartable Logical, indicating chains restartable. add additional data chains object.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sample_continue.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Continue sampling — mcstate_sample_continue","text":"list parameters densities","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sampler_adaptive.html","id":null,"dir":"Reference","previous_headings":"","what":"Adaptive Metropolis-Hastings Sampler — mcstate_sampler_adaptive","title":"Adaptive Metropolis-Hastings Sampler — mcstate_sampler_adaptive","text":"Create adaptive Metropolis-Hastings sampler, tune variance covariance matrix (vs simple random walk sampler mcstate_sampler_random_walk).","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sampler_adaptive.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Adaptive Metropolis-Hastings Sampler — mcstate_sampler_adaptive","text":"","code":"mcstate_sampler_adaptive( initial_vcv, initial_vcv_weight = 1000, initial_scaling = 1, initial_scaling_weight = NULL, min_scaling = 0, scaling_increment = NULL, log_scaling_update = TRUE, acceptance_target = 0.234, forget_rate = 0.2, forget_end = Inf, adapt_end = Inf, pre_diminish = 0 )"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sampler_adaptive.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Adaptive Metropolis-Hastings Sampler — mcstate_sampler_adaptive","text":"initial_vcv initial variance covariance matrix; start using proposal, gradually become weighted towards empirical covariance matrix calculated chain. initial_vcv_weight Weight initial variance-covariance matrix used build proposal random-walk. Higher values translate higher confidence initial variance-covariance matrix means update additional samples slower. initial_scaling initial scaling variance covariance matrix used generate multivariate normal proposal random-walk Metropolis-Hastings algorithm. generate proposal matrix, weighted variance covariance matrix multiplied scaling parameter squared times 2.38^2 / n_pars (n_pars number fitted parameters). Thus, Gaussian target parameter space, optimal scaling around 1. initial_scaling_weight initial weight used scaling update. scaling weight increase first pre_diminish iterations, scaling weight increases adaptation scaling diminishes. NULL (default) value 5 / (acceptance_target * (1 - acceptance_target)). min_scaling minimum scaling variance covariance matrix used generate multivariate normal proposal random-walk Metropolis-Hastings algorithm. scaling_increment scaling increment added subtracted scaling factor variance-covariance adaptive step. NULL (default) optimal value calculated. log_scaling_update Logical, whether changes scaling parameter made log-scale. acceptance_target target fraction proposals accepted (optimally) adaptive part mixture model. forget_rate rate forgetting early parameter sets empirical variance-covariance matrix MCMC chains. example, forget_rate = 0.2 (default) means every 5th iterations remove earliest parameter set included, remove 1st parameter set 5th update, 2nd 10th update, . Setting forget_rate = 0 means early parameter sets never forgotten. forget_end final iteration early parameter sets can forgotten. Setting forget_rate = Inf (default) means forgetting mechanism continues throughout chains. Forgetting early parameter sets becomes less useful chains settled posterior mode, parameter might set estimate long take. adapt_end final iteration can adapt multivariate normal proposal. Thereafter empirical variance-covariance matrix, scaling weight remain fixed. allows adaptation switched certain point help ensure convergence chain. pre_diminish number updates adaptation scaling parameter starts diminish. Setting pre_diminish = 0 means diminishing adaptation scaling parameter offset, pre_diminish = Inf mean never diminishing adaptation. Diminishing adaptation help scaling parameter converge better, chains find location scale posterior mode might useful explore switched .","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sampler_adaptive.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Adaptive Metropolis-Hastings Sampler — mcstate_sampler_adaptive","text":"mcstate_sampler object, can used mcstate_sample","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sampler_adaptive.html","id":"details","dir":"Reference","previous_headings":"","what":"Details","title":"Adaptive Metropolis-Hastings Sampler — mcstate_sampler_adaptive","text":"Efficient exploration parameter space MCMC might difficult target distribution high dimensionality, especially target probability distribution present high degree correlation. Adaptive schemes used \"learn\" fly correlation structure updating proposal distribution recalculating empirical variance-covariance matrix rescale adaptive step MCMC. implementation adaptive MCMC algorithm based adaptation \"accelerated shaping\" algorithm Spencer (2021). algorithm based random-walk Metropolis-Hastings algorithm proposal multi-variate Normal distribution centred current point. Spencer SEF (2021) Accelerating adaptation adaptive Metropolis–Hastings random walk algorithm. Australian & New Zealand Journal Statistics 63:468-484.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sampler_hmc.html","id":null,"dir":"Reference","previous_headings":"","what":"Create HMC — mcstate_sampler_hmc","title":"Create HMC — mcstate_sampler_hmc","text":"Create Hamiltonian Monte Carlo sampler, implemented using leapfrog algorithm.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sampler_hmc.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Create HMC — mcstate_sampler_hmc","text":"","code":"mcstate_sampler_hmc( epsilon = 0.015, n_integration_steps = 10, vcv = NULL, debug = FALSE )"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sampler_hmc.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Create HMC — mcstate_sampler_hmc","text":"epsilon step size HMC steps n_integration_steps number HMC steps per step vcv variance-covariance matrix momentum vector. default uses identity matrix. debug Logical, indicating save intermediate points gradients. add vector \"history\" details integration. slow things though accumulate history inefficiently.","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sampler_hmc.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Create HMC — mcstate_sampler_hmc","text":"mcstate_sampler object, can used mcstate_sample","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sampler_random_walk.html","id":null,"dir":"Reference","previous_headings":"","what":"Random Walk Sampler — mcstate_sampler_random_walk","title":"Random Walk Sampler — mcstate_sampler_random_walk","text":"Create simple random walk sampler, uses symmetric proposal move around parameter space. sampler supports sampling models likelihood computable randomly (e.g., pmcmc).","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sampler_random_walk.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Random Walk Sampler — mcstate_sampler_random_walk","text":"","code":"mcstate_sampler_random_walk(proposal = NULL, vcv = NULL)"},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sampler_random_walk.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Random Walk Sampler — mcstate_sampler_random_walk","text":"proposal proposal function; must take vector parameters random number generator object (mcstate_rng) produce new vector proposed parameters. vcv variance covariance matrix generate proposal function . want multivariate Gaussian proposal, likely simpler supplying proposal, generally efficient .","code":""},{"path":"https://mrc-ide.github.io/orderly2/reference/mcstate_sampler_random_walk.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Random Walk Sampler — mcstate_sampler_random_walk","text":"mcstate_sampler object, can used mcstate_sample","code":""}]