Learn R Programming

posterior (version 1.6.0)

rvar-matmult: Matrix multiplication of random variables

Description

Matrix multiplication of random variables.

Usage

x %**% y

# S3 method for rvar matrixOps(x, y)

Value

An rvar representing the matrix product of x and y.

Arguments

x

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

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

y

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

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).

In R >= 4.3, you can also use %*% in place of %**% for matrix multiplication of rvars. In R < 4.3, S3 classes cannot properly override %*%, so you must use %**% for matrix multiplication of rvars.

Examples

Run this code

# 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

Run the code above in your browser using DataLab