mvrnormArma: Sample from multivariate normal distribution with C++

View source: R/RcppExports.R

mvrnormArmaR Documentation

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)

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.

Value

A matrix of size d x n containing the samples.

Examples

# 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)]))
## Not run: 
plot(density(realizations[2,]))

## End(Not run)

anMC documentation built on Aug. 22, 2023, 5:08 p.m.