rvar-matmult: Matrix multiplication of random variables

rvar-matmultR Documentation

Matrix multiplication of random variables

Description

Matrix multiplication of random variables.

Usage

x %**% y

Arguments

x

(multiple options) The object to be postmultiplied by y:

  • An rvar

  • A numeric vector or matrix

  • A logical vector or matrix

If a vector is used, it is treated as a row vector.

y

(multiple options) The object to be premultiplied by x:

  • An rvar

  • A numeric vector or matrix

  • A logical vector or matrix

If a vector is used, it is treated as a column vector.

Details

If x or y are vectors, they are converted into matrices prior to multiplication, with x converted to a row vector and y to a column vector. Numerics and logicals can be multiplied by rvars and are broadcasted across all draws of the rvar argument. Tensor multiplication is used to efficiently multiply matrices across draws, so if either x or y is an rvar, x %**% y will be much faster than rdo(x %*% y).

Because rvar is an S3 class and S3 classes cannot properly override %*%, rvars use ⁠%**%⁠ for matrix multiplication.

Value

An rvar representing the matrix product of x and y.

Examples


# d has mu (mean vector of length 3) and Sigma (3x3 covariance matrix)
d <- as_draws_rvars(example_draws("multi_normal"))
d$Sigma

# trivial example: multiplication by a non-random matrix
d$Sigma %**% diag(1:3)

# Decompose Sigma into R s.t. R'R = Sigma ...
R <- chol(d$Sigma)
# ... and recreate Sigma using matrix multiplication
t(R) %**% R


posterior documentation built on Nov. 2, 2023, 5:56 p.m.