tensor_predictors/tensorPredictors/R/random.R

31 lines
884 B
R
Raw Normal View History

2020-06-10 14:35:27 +00:00
#' Multivariate Normal Distribution.
#'
#' Random generation for the multivariate normal distribution.
#' \deqn{X \sim N_p(\mu, \Sigma)}{X ~ N_p(\mu, \Sigma)}
#'
#' @param n number of samples.
#' @param mu mean
#' @param sigma covariance matrix.
#'
#' @return a \eqn{n\times p}{n x p} matrix with samples in its rows.
#'
2021-11-04 12:05:15 +00:00
#' @examples \dontrun{
2020-06-10 14:35:27 +00:00
#' rmvnorm(20, sigma = matrix(c(2, 1, 1, 2), 2))
#' rmvnorm(20, mu = c(3, -1, 2))
#' }
2021-11-04 12:05:15 +00:00
#'
2020-06-10 14:35:27 +00:00
#' @keywords internal
rmvnorm <- function(n = 1, mu = rep(0, p), sigma = diag(p)) {
if (!missing(sigma)) {
p <- nrow(sigma)
} else if (!missing(mu)) {
mu <- matrix(mu, ncol = 1)
p <- nrow(mu)
} else {
stop("At least one of 'mu' or 'sigma' must be supplied.")
}
# See: https://en.wikipedia.org/wiki/Multivariate_normal_distribution
2021-12-09 12:21:38 +00:00
rep(mu, each = n) + matrix(rnorm(n * p), n) %*% chol(sigma)
2020-06-10 14:35:27 +00:00
}