itertracer: Trace Iterations

Description Usage Arguments Examples

View source: R/itertracer.R

Description

Run an iterative process and collect a trace of the iterations.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
itertracer(
  param_init,
  step_funs,
  fixed = list(),
  shared = new.env(parent = emptyenv()),
  callbacks = list(),
  n_iter = 200L,
  n_chains = 1L,
  n_parallel = 1L,
  n_burn = 0L,
  n_skip = 0L,
  seeds = NULL,
  read_cache = TRUE,
  clean_cache = FALSE
)

Arguments

param_init

[list]

  • a named list of arrays for where to start sampling for each parameter

  • there must be an element in step_funs for each element here (matching by names)

  • all elements must be one of

    • numeric array

    • list of arrays, with one array for each chain — hence the length must be equal to n_chains

step_funs

list“

  • a list of functions for sampling each random variables' step (oneitertracer iteration = one step for each variable)

  • matched with param_init based on names

  • the order of calling each step fun is determined by callback param_order

fixed

[list]

  • passed to step_funs and callbacks in arg_list[["fixed"]]

  • fixed is a list, so if you modify any of it's elements in step_funs or callbacks, you actually create a new list and you cannot pass changes in fixed to other functions

shared

[environment, list]

  • list: a list of environment objects, one for each chain

  • environment:

    • shared is passed to all step_funs and callbacks in arg_list[["shared"]]

    • since it's an environment, you can save any results computed in one function to be used by others (NOTE: in parallel computation you of course cannot share results between different threads)

callbacks

[list]

  • named list of functions called at various points of the process (pre-iter, post-iter, etc.)

  • supplied to itertracer_callbacks

n_iter

number of iterations

n_chains

number of separate chains to sample

n_parallel

number of parallel processes to run

n_burn

number of iterations out of n_iter to run before samples will be saved and included in output

n_skip

keep every (n_skip + 1)'th sample

seeds

vector of numbers or NULL; if not NULL, each of these is passed to each separate chain; if NULL these are picked automatically (and each chain has a different seed)

read_cache

if TRUE, will read the trace at the end of iterating into R; else the samples will be left in the trace folder only — but see clean_cache

clean_cache

if TRUE, will delete any folder created to store samples

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
## beta-binomial Gibbs sampling, multi-core

g <- itertracer(
  fixed = list(n = 20L),
  param_init = lapply(list(x = 10, theta = 0.5), as.array),
  step_funs = list(
    x = function(arg_list) {
      rbinom(
        n = 1,
        size = arg_list[["fixed"]][["n"]],
        prob = arg_list[["param_list"]][["theta"]]
      )
    },
    theta = function(arg_list) {
      rbeta(
        n = 1,
        shape1 = 2 + arg_list[["param_list"]][["x"]],
        shape2 = 4 + arg_list[["fixed"]][["n"]] - arg_list[["param_list"]][["x"]]
      )
    }
  ),
  n_chains = 1L,
  n_iter = 300,
  n_burn = 50L,
  n_skip = 4L,
  n_parallel = 1L
)

WetRobot/tracer documentation built on Aug. 7, 2020, 10:53 p.m.