Skip to contents

The function dmvnorm() computes the density of a multivariate normal distribution.

The function rmvnorm() samples from a multivariate normal distribution.

The functions with suffix _cpp perform no input checks, hence are faster.

The univariate normal distribution is available as the special case p = 1.

Usage

dmvnorm_cpp(x, mean, Sigma, log = FALSE)

rmvnorm_cpp(mean, Sigma, log = FALSE)

dmvnorm(x, mean, Sigma, log = FALSE)

rmvnorm(n = 1, mean, Sigma, log = FALSE)

Arguments

x

[numeric()]
A quantile vector of length p.

mean

[numeric()]
The mean vector of length p.

For dmvnorm() and rmvnorm(), it can also be of length 1 for convenience, then rep(mean, p) is considered.

Sigma

[matrix()]
The covariance matrix of dimension p.

It can also be a zero matrix.

For rmvnorm(), arbitrary dimensions (i.e., full rows and corresponding columns) of Sigma can be 0.

For dmvnorm() and rmvnorm() and if p = 1, it can also be a single numeric for convenience. Note that Sigma is this case is a variance, which is a different format than in stats::dnorm() or stats::rnorm, which require a standard deviation.

log

[logical(1)]
For dmvnorm(): Return the logarithm of the density value?

For rmvnorm(): Draw from a log-normal distribution?

n

[integer(1)]
An integer, the number of requested samples.

Value

For dmvnorm(): The density value.

For rmvnorm(): If n = 1 a vector of length p (note that it is a column vector for rmvnorm_cpp()), else a matrix of dimension n times p with samples as rows.

See also

Examples

x <- c(0, 0)
mean <- c(0, 0)
Sigma <- diag(2)

# compute density
dmvnorm(x = x, mean = mean, Sigma = Sigma)
#> [1] 0.1591549
dmvnorm(x = x, mean = mean, Sigma = Sigma, log = TRUE)
#> [1] -1.837877

# sample
rmvnorm(n = 3, mean = mean, Sigma = Sigma)
#>            [,1]       [,2]
#> [1,]  0.4307952 -0.8469921
#> [2,] -1.1102431  0.7742219
#> [3,] -0.5109398 -1.0941590
rmvnorm(mean = mean, Sigma = Sigma, log = TRUE)
#> [1] 0.4125872 1.1312663