rmnorm: Random number generator for the (conditional) multivariate...

View source: R/RcppExports.R

rmnormR Documentation

Random number generator for the (conditional) multivariate normal distribution

Description

This function generates random numbers (i.e. variates) from the (conditional) multivariate normal distribution.

Usage

rmnorm(
  n,
  mean,
  sigma,
  given_ind = numeric(),
  given_x = numeric(),
  dependent_ind = numeric(),
  is_validation = TRUE,
  n_cores = 1L
)

Arguments

n

positive integer representing the number of random variates to be generated from the (conditional) multivariate normal distribution. If given_ind is not an empty vector, then n should be equal to nrow(given_x).

mean

numeric vector representing an expectation of the multivariate normal vector (distribution).

sigma

positively defined numeric matrix representing the covariance matrix of the multivariate normal vector (distribution).

given_ind

numeric vector representing indexes of a multivariate normal vector which are conditioned on the values given by the given_x argument.

given_x

numeric vector whose i-th element corresponds to the given value of the given_ind[i]-th element (component) of a multivariate normal vector. If given_x is a numeric matrix then its rows are such vectors of given values.

dependent_ind

numeric vector representing indexes of the unconditioned elements (components) of a multivariate normal vector.

is_validation

logical value indicating whether input arguments should be validated. Set it to FALSE to get a performance boost (default value is TRUE).

n_cores

positive integer representing the number of CPU cores used for parallel computing. Currently it is not recommended to set n_cores > 1 if vectorized arguments include less than 100000 elements.

Details

This function uses Cholesky decomposition to generate multivariate normal variates from independent standard normal variates.

Value

This function returns a numeric matrix whose rows are random variates from the (conditional) multivariate normal distribution with the mean equal to mean and the covariance equal to sigma. If given_x and given_ind are also provided, then random variates will be from the conditional multivariate normal distribution. Please, see the details section of cmnorm to get additional information on the conditioning procedure.

Examples

# Consider a multivariate normal vector:
# X = (X1, X2, X3, X4, X5) ~ N(mean, sigma)

# Prepare the multivariate normal vector parameters
  # the expected value
mean <- c(-2, -1, 0, 1, 2)
n_dim <- length(mean)
  # the correlation matrix
cor <- c(   1,  0.1,  0.2,   0.3,  0.4,
          0.1,    1, -0.1,  -0.2, -0.3,
          0.2, -0.1,    1,   0.3,  0.2,
          0.3, -0.2,  0.3,     1, -0.05,
          0.4, -0.3,  0.2, -0.05,     1)
cor <- matrix(cor, ncol = n_dim, nrow = n_dim, byrow = TRUE)
  # the covariance matrix
sd_mat <- diag(c(1, 1.5, 2, 2.5, 3))
sigma <- sd_mat %*% cor %*% t(sd_mat)

# Simulate random variates from this distribution
rmnorm(n = 3, mean = mean, sigma = sigma)

# Simulate random variate from (X1, X3, X5 | X1 = -1, X4 = 1)
given_x <- c(-1, 1)
given_ind <- c(1, 4)
rmnorm(n = 1, mean = mean, sigma = sigma, 
       given_x = given_x, given_ind = given_ind)

# Simulate random variate from (X1, X3, X5 | X1 = -1, X4 = 1)
# and (X1, X3, X5 | X1 = 2, X4 = 3)
given_x = rbind(c(-1, 1), c(2, 3))
rmnorm(n = nrow(given_x), mean = mean, sigma = sigma, 
       given_x = given_x, given_ind = given_ind)

mnorm documentation built on April 14, 2026, 5:07 p.m.

Related to rmnorm in mnorm...