Density and random sampling for the Matrix-Normal distribution.
dMNorm(X, Lambda, SigmaR, SigmaC, log = FALSE)
rMNorm(n, Lambda, SigmaR, SigmaC)
Argument to the density function. Either a p x q
matrix or a p x q x n
array.
Mean parameter. Either a p x q
matrix or a p x q x n
array.
Between-row covariance matrix. Either a p x p
matrix or a p x p x n
array.
Between-column covariance matrix Either a q x q
matrix or a q x q x n
array.
Logical; whether or not to compute the log-density.
Integer number of random samples to generate.
A vector length n
for density evaluation, or an array of size p x q x n
for random sampling.
The Matrix-Normal distribution \(\boldsymbol{X} \sim \textrm{Matrix-Normal}(\boldsymbol{\Lambda}, \boldsymbol{\Sigma}_R, \boldsymbol{\Sigma}_C)\) on the random matrix \(\boldsymbol{X}_{p \times q}\) is defined as $$ \textrm{vec}(\boldsymbol{X}) \sim \mathcal{N}(\textrm{vec}(\boldsymbol{\Lambda}), \boldsymbol{\Sigma}_C \otimes \boldsymbol{\Sigma}_R), $$ where \(\textrm{vec}(\boldsymbol{X})\) is a vector stacking the columns of \(\boldsymbol{X}\), and \(\boldsymbol{\Sigma}_C \otimes \boldsymbol{\Sigma}_R\) denotes the Kronecker product.
# problem dimensions
p <- 4
q <- 2
n <- 10 # number of observations
# parameter values
Lambda <- matrix(rnorm(p*q),p,q) # mean matrix
# row-wise variance matrix (positive definite)
SigmaR <- crossprod(matrix(rnorm(p*p), p, p))
SigmaC <- rwish(n, Psi = diag(q), nu = q + 1) # column-wise variance (vectorized)
# random sample
X <- rMNorm(n, Lambda = Lambda, SigmaR = SigmaR, SigmaC = SigmaC)
# log-density at each sampled value
dMNorm(X, Lambda = Lambda, SigmaR = SigmaR, SigmaC = SigmaC, log = TRUE)
#> [1] -17.68324 -12.47524 -14.69565 -20.49812 -12.41197 -13.78214 -19.75739
#> [8] -12.51010 -15.69047 -19.59618