Arithmetic on random variables and scalars
Source:R/random_variable.R
random_variable_arithmetic.RdRandom variables combine with each other and with single numbers under +,
-, * and /, pointwise: Z <- X + Y is Z(x) = X(x) + Y(x), and
2 * X + 3 rescales X. Other operators are not supported.
Arguments
- e1, e2
A random_variable or a single number, at least one of them a random variable.
Details
Two random variables must share a sample space; a mismatch is an error when the expression is built, not when it is evaluated.
Examples
fam <- gaussian_family(d = 1)
X <- random_variable(\(x) dnorm(x, 1), sample_space = fam@sample_space)
Y <- 2 * X + 3
Y(as.matrix(0:2))
#> [1] 3.483941 3.797885 3.483941
Z <- X / X
Z(as.matrix(0:2))
#> [1] 1 1 1