Learn R Programming

lrstat (version 0.2.15)

rmvnorm: Random Multivariate Normal Generation

Description

Generates random samples from a multivariate normal distribution with a specified mean vector and covariance matrix.

Usage

rmvnorm(n, mean, sigma)

Value

A numeric matrix where each row represents a sample from the multivariate normal distribution.

Arguments

n

The number of samples to generate.

mean

A numeric vector representing the mean of the distribution.

sigma

A numeric matrix representing the covariance matrix.

Author

Kaifeng Lu, kaifenglu@gmail.com

Details

This function generates samples from a multivariate normal distribution using the Cholesky decomposition method. It first computes the Cholesky factorization of the covariance matrix, then generates standard normal random variables, and finally transforms them to the desired multivariate normal distribution.

Examples

Run this code
# Generate 5 samples from a bivariate normal distribution with mean (0,0)
# and covariance matrix [[1, 0.5], [0.5, 1]]

set.seed(314159)
rmvnorm(5, c(0, 0), matrix(c(1, 0.5, 0.5, 1), nrow=2))

Run the code above in your browser using DataLab