rmvnorm: Generate Random Samples from Multivariate Normal Distribution

Description Usage Arguments Value Examples

View source: R/utility_rmvnorm.R

Description

In \mathbf{R}^p, random samples are drawn

X_1,X_2,…,X_n~ \sim ~ \mathcal{N}(μ, Σ)

where μ \in \mathbf{R}^p is a mean vector and Σ \in \textrm{SPD}(p) is a positive definite covariance matrix.

Usage

1
rmvnorm(n = 1, mu, sigma)

Arguments

n

the number of samples to be generated.

mu

mean vector.

sigma

covariance matrix.

Value

either (1) a length-p vector (n=1) or (2) an (n\times p) matrix where rows are random samples.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#-------------------------------------------------------------------
#   Generate Random Data and Compare with Empirical Covariances
#
# In R^5 with zero mean and diagonal covariance, 
# generate 100 and 200 observations and compute MLE covariance.
#-------------------------------------------------------------------
## GENERATE DATA
mymu  = rep(0,5)
mysig = diag(5)

## MLE FOR COVARIANCE
smat1 = stats::cov(rmvnorm(n=100, mymu, mysig))
smat2 = stats::cov(rmvnorm(n=200, mymu, mysig))

## VISUALIZE
opar <- par(no.readonly=TRUE)
par(mfrow=c(1,3), pty="s")
image(mysig[,5:1], axes=FALSE, main="true covariance")
image(smat1[,5:1], axes=FALSE, main="empirical cov with n=100")
image(smat2[,5:1], axes=FALSE, main="empirical cov with n=200")
par(opar)

Riemann documentation built on June 20, 2021, 5:07 p.m.