An internal utility to simulate from a MVN distribution. By using a pivoted Cholesky decomposition, the function can handle semi-definite covariance matrices by effectively reducing the dimensionality to the numerical rank of the matrix.
rmvnorm(n, mu, Sig)A matrix of dimension n x p where each row is a
random draw from the specified MVN distribution.
Integer; the number of samples to generate.
Numeric vector; the mean vector of length p.
Numeric matrix; the p x p covariance matrix.
The function extracts the pivoting index and the numerical rank from
the result of chol(Sig, pivot = TRUE). It then generates
independent standard normal draws of dimension r (the rank)
and transforms them using the Cholesky factor to match the target
covariance and mean.