Skip to contents

A callable random_variable maps a length-d outcome to a number, or an (n, d) matrix of outcomes to n numbers, after checking they belong to its sample space. Inf is a legitimate value, e.g. a likelihood ratio whose denominator is zero.

format() gives the expression alone, without the class banner print() adds.

Usage

random_variable(
  f,
  sample_space,
  label = "<rv>",
  op = NA_character_,
  operands = list(),
  log_f = NULL
)

Arguments

f

The mapping that defines the random variable, accepting an (n, d) matrix and returning n numbers.

sample_space

The space this variable is defined on.

label

How to name this variable when printing. Ignored when op is given, since the expression is then built from the operands.

op

The operator that produced this variable, or NA for a leaf. Set by random_variable_arithmetic; unnecessary externally.

operands

The operands op combined, or an empty list for a leaf. Used only for printing.

log_f

Optional: the same mapping in log space, log_f(x) == log(f(x)), for a non-negative variable. NULL otherwise.

Value

A callable random_variable.

Examples

X <- random_variable(\(x) dnorm(x, 1), sample_space = real_region(1))
X(as.matrix(0:2))
#> [1] 0.2419707 0.3989423 0.2419707