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.

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.

Sigma

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

log

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

n

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

Value

For dmvnorm(): The density value.

For rmvnorm(): If n = 1 a vector of length p, 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.3201427  1.0799257
#> [2,] 0.8303475 -1.2876009
#> [3,] 1.0528909  0.9508922
rmvnorm(mean = mean, Sigma = Sigma, log = TRUE)
#> [1] 5.531714 1.122541