Density and random sampling for the Multivariate Normal distribution.
dmNorm(x, mu, Sigma, log = FALSE)
rmNorm(n, mu, Sigma)
Argument to the density function. A vector of length q
or an n x q
matrix.
Mean vector(s). Either a vector of length q
or an n x q
matrix. If missing defaults to a vector of zeros.
Covariance matrix or matrices. Either a q x q
matrix or a q x q x n
array. If missing defaults to the identity matrix.
Logical; whether or not to compute the log-density.
Integer number of random samples to generate.
A vector for densities, or a n x q
matrix for random sampling.
# Parameter specification
q <- 4 # number of dimensions
mu <- 1:q # mean vector
V <- toeplitz(exp(-seq(1:q))) # variance matrix
# Random sample
n <- 100
X <- rmNorm(n, mu, V)
# Calculate log density for each sampled vector
dmNorm(X, mu, V, log = TRUE)
#> [1] -2.948558 -3.069395 -4.146631 -4.332509 -1.883980 -2.844227 -3.854589
#> [8] -4.945445 -5.709061 -3.525747 -3.342214 -1.713391 -2.071143 -3.342749
#> [15] -1.853075 -3.260743 -2.801797 -2.820458 -5.152313 -3.271066 -2.932790
#> [22] -3.494317 -3.648105 -2.155481 -5.042101 -2.503222 -3.621914 -4.996129
#> [29] -2.037270 -1.828994 -5.649358 -2.021501 -1.823357 -2.819121 -2.035177
#> [36] -4.244290 -4.852941 -5.410440 -3.741171 -2.794339 -2.571688 -4.093160
#> [43] -2.810749 -3.052392 -2.735390 -6.413913 -2.586082 -3.088821 -2.961534
#> [50] -3.696361 -2.065049 -2.806888 -2.861491 -2.563219 -2.156760 -5.040861
#> [57] -6.765012 -2.982039 -3.680326 -4.422260 -1.863519 -2.874615 -2.186002
#> [64] -2.798948 -2.478181 -2.620646 -3.868594 -4.483668 -2.441481 -3.246864
#> [71] -5.005250 -3.148048 -6.291859 -1.975585 -2.270725 -5.517700 -1.683778
#> [78] -4.139806 -4.183790 -2.817942 -3.361158 -5.355549 -3.932483 -3.689638
#> [85] -2.294551 -1.718489 -2.255261 -3.330209 -3.826162 -3.653079 -3.579192
#> [92] -2.464002 -2.538575 -2.320500 -1.945910 -2.822934 -3.087502 -3.120234
#> [99] -2.064597 -5.631859