R/matrix_normal.R

Defines functions matrix_normal

Documented in matrix_normal

#########################################
# Function to sample from Matrix Normal #
#########################################

# M = mean of matrix
# U = covariance matix of the columns
# V = covariance matrix of the rows

matrix_normal = function(M, U, V){
  a <- dim(M)[1]
  b <- dim(M)[2]
  
  # Draw Z from MN(O, I, I)
  Z <- matrix(stats::rnorm(a*b,0,1), a, b)
  
  # Cholesky decomposition of U and V
  L1 <- chol(U)
  L2 <- chol(V)
  
  # Return draw from MN(M,U,V)
  return(M + crossprod(L1,Z) %*% L2)
}

Try the MBSP package in your browser

Any scripts or data that you put into this service are public.

MBSP documentation built on May 31, 2023, 9:20 p.m.