Multivariate normal distribution
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.
dMVN(x, mean=0, Q=1, Sigma, log=FALSE) rMVN(n, mean=0, Q=1, Sigma) rcMVN(n, b=0, Q=1, Sigma)
mean |
vector of mean. |
b |
vector of a canonical mean. |
Q |
precision matrix of the multivariate normal
distribution. Ignored if |
Sigma |
covariance matrix of the multivariate normal
distribution. If |
n |
number of observations to be sampled. |
x |
vector or matrix of the points where the density should be evaluated. |
log |
logical; if |
Some objects.
A vector with evaluated values of the (log-)density
A list with the components:
vector or matrix with sampled values
vector with the values of the log-density evaluated in the sampled values
A list with the components:
vector or matrix with sampled values
vector or the mean of the normal distribution
vector with the values of the log-density evaluated in the sampled values
Arnošt Komárek arnost.komarek[AT]mff.cuni.cz
Rue, H. and Held, L. (2005). Gaussian Markov Random Fields: Theory and Applications. Boca Raton: Chapman and Hall/CRC.
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))
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.