rvar {posterior} | R Documentation |
Random variables of arbitrary dimension
Description
Random variables backed by arrays of arbitrary dimension
Usage
rvar(
x = double(),
dim = NULL,
dimnames = NULL,
nchains = NULL,
with_chains = FALSE
)
Arguments
x |
(multiple options) The object to convert to an
|
dim |
(integer vector) One or more integers giving the maximal indices
in each dimension to override the dimensions of the |
dimnames |
(list) Character vectors giving the names in each dimension
to override the names of the dimensions of the |
nchains |
(positive integer) The number of chains. The if |
with_chains |
(logical) Does |
Details
The "rvar"
class internally represents random variables as arrays of arbitrary
dimension, where the first dimension is used to index draws from the distribution.
Most mathematical operators and functions are supported, including efficient matrix
multiplication and vector and array-style indexing. The intent is that an rvar
works as closely as possible to how a base vector/matrix/array does, with a few
differences:
The default behavior when subsetting is not to drop extra dimensions (i.e. the default
drop
argument for[
isFALSE
, notTRUE
).Rather than base R-style recycling,
rvar
s use a limited form of broadcasting: if an operation is being performed on two vectors with different size of the same dimension, the smaller vector will be recycled up to the size of the larger one along that dimension so long as it has size 1.
For functions that expect base numeric arrays and for which rvar
s cannot be
used directly as arguments, you can use rfun()
or rdo()
to translate your
code into code that executes across draws from one or more random variables
and returns a random variable as output. Typically rdo()
offers the most
straightforward translation.
As rfun()
and rdo()
incur some performance cost, you can also operate directly
on the underlying array using the draws_of()
function. To re-use existing
random number generator functions to efficiently create rvar
s, use rvar_rng()
.
Value
An object of class "rvar"
representing a random variable.
See Also
as_rvar()
to convert objects to rvar
s. See rdo()
, rfun()
, and
rvar_rng()
for higher-level interfaces for creating rvar
s.
Examples
set.seed(1234)
# To create a "scalar" `rvar`, pass a one-dimensional array or a vector
# whose length (here `4000`) is the desired number of draws:
x <- rvar(rnorm(4000, mean = 1, sd = 1))
x
# Create random vectors by adding an additional dimension:
n <- 4 # length of output vector
x <- rvar(array(rnorm(4000 * n, mean = rep(1:n, each = 4000), sd = 1), dim = c(4000, n)))
x
# Create a random matrix:
rows <- 4
cols <- 3
x <- rvar(array(rnorm(4000 * rows * cols, mean = 1, sd = 1), dim = c(4000, rows, cols)))
x
# If the input sample comes from multiple chains, we can indicate that using the
# nchains argument (here, 1000 draws each from 4 chains):
x <- rvar(rnorm(4000, mean = 1, sd = 1), nchains = 4)
x
# Or if the input sample has chain information as its second dimension, we can
# use with_chains to create the rvar
x <- rvar(array(rnorm(4000, mean = 1, sd = 1), dim = c(1000, 4)), with_chains = TRUE)
x