Learn R Programming

anMC (version 0.2.5)

mvrnormArma: Sample from multivariate normal distribution with C++

Description

Simulates realizations from a multivariate normal with mean mu and covariance matrix sigma.

Usage

mvrnormArma(n, mu, sigma, chol)

Value

A matrix of size \(d x n\) containing the samples.

Arguments

n

number of simulations.

mu

mean vector.

sigma

covariance matrix or Cholesky decomposition of the matrix (see chol).

chol

integer, if 0 sigma is a covariance matrix, otherwise it is the Cholesky decomposition of the matrix.

Examples

Run this code
# Simulate 1000 realizations from a multivariate normal vector
mu <- rep(0,200)
Sigma <- diag(rep(1,200))
realizations<-mvrnormArma(n=1000,mu = mu,sigma=Sigma, chol=0)
empMean<-rowMeans(realizations)
empCov<-cov(t(realizations))
# check if the sample mean is close to the actual mean
maxErrorOnMean<-max(abs(mu-empMean))
# check if we can estimate correctly the covariance matrix
maxErrorOnVar<-max(abs(rep(1,200)-diag(empCov)))
maxErrorOnCov<-max(abs(empCov[lower.tri(empCov)]))
if (FALSE) {
plot(density(realizations[2,]))
}

Run the code above in your browser using DataLab