multinormal: The Vectorized Multivariate Normal Distribution

multinormalR Documentation

The Vectorized Multivariate Normal Distribution

Description

Vectorized versions of rmvnorm and dmvnorm from the mvtnorm package, providing random deviates and density for the multivariate normal distribution with varying vectors of means and varying covariance matrices.

Usage

rmultinormal(n, mean, sigma, method = c("eigen", "svd", "chol"))

dmultinormal(x, mean, sigma, log = FALSE)

Arguments

n

number of observations. If ‘⁠length(n) > 1⁠’, the length is taken to be the number required.

mean

vector or matrix of means. If a matrix, each row is taken to be a mean vector. Default is a vector of 0 of convenient length.

sigma

covariance vector corresponding to the coercion of the covariance matrix into a vector (if unique for all ‘⁠n⁠’ or ‘⁠x⁠’) or array of covariance vectors (if varying). Default is a diagonal matrix of convenient size.

method

matrix decomposition used to determine the matrix root of sigma. Possible methods are eigenvalue decomposition (‘⁠"eigen"⁠’, default), singular value decomposition (‘⁠"svd"⁠’), and Cholesky decomposition (‘⁠"chol"⁠’).

x

vector or matrix of quantiles. If x is a matrix, each row is taken to be a quantile.

log

logical; if ‘⁠TRUE⁠’, densities d are given as ‘⁠log(d)⁠’.

Details

‘⁠rmvnorm(n, m, s)⁠’ is equivalent to ‘⁠rmultinormal(n, m, as.vector(s))⁠’. ‘⁠dmvnorm(x, m, s)⁠’ is equivalent to ‘⁠dmultinormal(x, m, as.vector(s))⁠’.

If ‘⁠mean⁠’ and/or ‘⁠sigma⁠’ is a matrix, the first random deviate will use the first row of ‘⁠mean⁠’ and/or ‘⁠sigma⁠’, the second will use the second row, ... recycling being permitted by row.

If ‘⁠mean⁠’ is a vector of length ‘⁠l⁠’ or is a matrix with ‘⁠l⁠’ columns, ‘⁠sigma⁠’ should be a vector of length ‘⁠l x l⁠’ or a matrix with ‘⁠l^2⁠’ columns.

Note

The use of a varying sigma may be very time consuming.

Examples

## mean and sigma as vectors
(mean <- c(10, 0))
(sigma <- matrix(c(1, 2, 2, 10), ncol = 2))
sigma <- as.vector(sigma)
(x <- matrix(c(9, 8, 1, -1), ncol = 2))
round(rmultinormal(10, mean, sigma))
dmultinormal(x, mean, sigma)

## mean as matrix
(mean <- matrix(c(10, 0, 0, 10), ncol = 2))
round(rmultinormal(10, mean, sigma))

mc2d documentation built on Oct. 23, 2026, 1:06 a.m.