Last data update: 2014.03.03
R: Multivariate normal distribution
Multivariate normal distribution
Description
Density and random generation for the multivariate normal distribution
with mean equal to mean
, precision matrix equal to Q
(or covariance
matrix equal to Sigma
).
Function rcMVN
samples from the multivariate normal
distribution with a canonical mean b , i.e., the mean is
mu = Q^{-1} * b.
Usage
dMVN(x, mean=0, Q=1, Sigma, log=FALSE)
rMVN(n, mean=0, Q=1, Sigma)
rcMVN(n, b=0, Q=1, Sigma)
Arguments
mean
vector of mean.
b
vector of a canonical mean.
Q
precision matrix of the multivariate normal
distribution. Ignored if Sigma
is given.
Sigma
covariance matrix of the multivariate normal
distribution. If Sigma
is supplied, precision
is computed from Sigma as
Q = Sigma^{-1} .
n
number of observations to be sampled.
x
vector or matrix of the points where the density should be
evaluated.
log
logical; if TRUE
, log-density is computed
Value
Some objects.
Value for dMVN
A vector with evaluated values of the (log-)density
Value for rMVN
A list with the components:
x vector or matrix with sampled values
log.dens vector with the values of the log-density evaluated
in the sampled values
Value for rcMVN
A list with the components:
x vector or matrix with sampled values
mean vector or the mean of the normal distribution
log.dens vector with the values of the log-density evaluated
in the sampled values
Author(s)
Arno<c3><85><c2><a1>t Kom<c3><83><c2><a1>rek arnost.komarek[AT]mff.cuni.cz
References
Rue, H. and Held, L. (2005).
Gaussian Markov Random Fields: Theory and Applications .
Boca Raton: Chapman and Hall/CRC.
See Also
dnorm
, Mvnorm
.
Examples
set.seed(1977)
### Univariate normal distribution
### ==============================
c(dMVN(0), dnorm(0))
c(dMVN(0, log=TRUE), dnorm(0, log=TRUE))
rbind(dMVN(c(-1, 0, 1)), dnorm(c(-1, 0, 1)))
rbind(dMVN(c(-1, 0, 1), log=TRUE), dnorm(c(-1, 0, 1), log=TRUE))
c(dMVN(1, mean=1.2, Q=0.5), dnorm(1, mean=1.2, sd=sqrt(2)))
c(dMVN(1, mean=1.2, Q=0.5, log=TRUE), dnorm(1, mean=1.2, sd=sqrt(2), log=TRUE))
rbind(dMVN(0:2, mean=1.2, Q=0.5), dnorm(0:2, mean=1.2, sd=sqrt(2)))
rbind(dMVN(0:2, mean=1.2, Q=0.5, log=TRUE), dnorm(0:2, mean=1.2, sd=sqrt(2), log=TRUE))
### Multivariate normal distribution
### ================================
mu <- c(0, 6, 8)
L <- matrix(1:9, nrow=3)
L[upper.tri(L, diag=FALSE)] <- 0
Sigma <- L %*% t(L)
Q <- chol2inv(chol(Sigma))
b <- solve(Sigma, mu)
dMVN(mu, mean=mu, Q=Q)
dMVN(mu, mean=mu, Sigma=Sigma)
dMVN(mu, mean=mu, Q=Q, log=TRUE)
dMVN(mu, mean=mu, Sigma=Sigma, log=TRUE)
xx <- matrix(c(0,6,8, 1,5,7, -0.5,5.5,8.5, 0.5,6.5,7.5), ncol=3, byrow=TRUE)
dMVN(xx, mean=mu, Q=Q)
dMVN(xx, mean=mu, Sigma=Sigma)
dMVN(xx, mean=mu, Q=Q, log=TRUE)
dMVN(xx, mean=mu, Sigma=Sigma, log=TRUE)
zz <- rMVN(1000, mean=mu, Sigma=Sigma)
rbind(apply(zz$x, 2, mean), mu)
var(zz$x)
Sigma
cbind(dMVN(zz$x, mean=mu, Sigma=Sigma, log=TRUE), zz$log.dens)[1:10,]
zz <- rcMVN(1000, b=b, Sigma=Sigma)
rbind(apply(zz$x, 2, mean), mu)
var(zz$x)
Sigma
cbind(dMVN(zz$x, mean=mu, Sigma=Sigma, log=TRUE), zz$log.dens)[1:10,]
zz <- rMVN(1000, mean=rep(0, 3), Sigma=Sigma)
rbind(apply(zz$x, 2, mean), rep(0, 3))
var(zz$x)
Sigma
cbind(dMVN(zz$x, mean=rep(0, 3), Sigma=Sigma, log=TRUE), zz$log.dens)[1:10,]
### The same using the package mvtnorm
### ==================================
# require(mvtnorm)
# c(dMVN(mu, mean=mu, Sigma=Sigma), dmvnorm(mu, mean=mu, sigma=Sigma))
# c(dMVN(mu, mean=mu, Sigma=Sigma, log=TRUE), dmvnorm(mu, mean=mu, sigma=Sigma, log=TRUE))
#
# rbind(dMVN(xx, mean=mu, Sigma=Sigma), dmvnorm(xx, mean=mu, sigma=Sigma))
# rbind(dMVN(xx, mean=mu, Sigma=Sigma, log=TRUE), dmvnorm(xx, mean=mu, sigma=Sigma, log=TRUE))
Results