The function dmvnorm()
computes the density of a multivariate normal
distribution.
The function pmvnorm()
computes the cumulative distribution function 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)
pmvnorm_cpp(x, mean, Sigma, abseps = 0.001)
rmvnorm_cpp(mean, Sigma, log = FALSE)
dmvnorm(x, mean, Sigma, log = FALSE)
pmvnorm(x, mean, Sigma, abseps = 0.001)
rmvnorm(n = 1, mean, Sigma, log = FALSE)
Arguments
- x
[
numeric()
]
A quantile vector of lengthp
.- mean
[
numeric()
]
The mean vector of lengthp
.For
dmvnorm()
andrmvnorm()
, it can also be of length1
for convenience, thenrep(mean, p)
is considered.- Sigma
[
matrix()
]
The covariance matrix of dimensionp
.For
rmvnorm()
, arbitrary dimensions (i.e., full rows and corresponding columns) ofSigma
can be0
.For
dmvnorm()
andrmvnorm()
and ifp = 1
, it can also be a singlenumeric
for convenience. Note thatSigma
is this case is a variance, which is a different format than instats::dnorm()
orstats::rnorm
, which require a standard deviation.- log
[
logical(1)
]
Fordmvnorm()
: Return the logarithm of the density value?For
rmvnorm()
: Draw from a log-normal distribution?- abseps
[
numeric(1)
]
The absolute error tolerance.- n
[
integer(1)
]
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.
Details
pmvnorm()
just calls mvtnorm::pmvnorm
with the randomized
Quasi-Monte-Carlo procedure by Genz and Bretz. The argument abseps
controls
the accuracy of the Gaussian integral approximation.
See also
Other simulation helpers:
Simulator
,
correlated_regressors()
,
ddirichlet_cpp()
,
dtnorm_cpp()
,
dwishart_cpp()
,
simulate_markov_chain()
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
# compute CDF
pmvnorm(x = x, mean = mean, Sigma = Sigma)
#> [1] 0.25
# sample
rmvnorm(n = 3, mean = mean, Sigma = Sigma)
#> [,1] [,2]
#> [1,] 0.201326 0.1113072
#> [2,] 1.290432 -0.1789433
#> [3,] 0.689619 1.3125645
rmvnorm(mean = mean, Sigma = Sigma, log = TRUE)
#> [1] 0.8438016 0.5036300